TypeError:int()参数必须是字符串,类似字节的对象或一个数字,而不是非电视'使用CV2时

发布于 2025-01-28 01:33:20 字数 1303 浏览 1 评论 0原文

import cv2
import numpy as np
from os import listdir
from os.path import isfile, join

# Get the training data we previously made
data_path = 'C:\\Users\\hp\\Unlock-Application\\frames'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]

# Create arrays for training data and labels
Training_Data, Labels = [], []

# Open training images in our datapath
# Create a numpy array for training data
for i, files in enumerate(onlyfiles):
    image_path = data_path + onlyfiles[i]
    images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    Training_Data.append(np.asarray(images, dtype=np.uint8))
    Labels.append(i)

# Create a numpy array for both training data and labels
Labels = np.asarray(Labels, dtype=np.int32)

# Initialize facial recognizer
model = cv2.face.LBPHFaceRecognizer_create()

# NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer()

# Let's train our model 
model.train(np.asarray(Training_Data), np.asarray(Labels))
print(cv2.__version__)
print("Model trained sucessefully")

当我运行此代码时,我会得到此错误

File "c:\Users\hp\Unlock-Application\model.py", line 18, in <module>
Training_Data.append(np.asarray(images, dtype=np.uint8))

typeError:int()参数必须是字符串,类似字节的对象或一个数字,而不是“ nontype” 有什么猜测如何解决这个问题?

import cv2
import numpy as np
from os import listdir
from os.path import isfile, join

# Get the training data we previously made
data_path = 'C:\\Users\\hp\\Unlock-Application\\frames'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]

# Create arrays for training data and labels
Training_Data, Labels = [], []

# Open training images in our datapath
# Create a numpy array for training data
for i, files in enumerate(onlyfiles):
    image_path = data_path + onlyfiles[i]
    images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    Training_Data.append(np.asarray(images, dtype=np.uint8))
    Labels.append(i)

# Create a numpy array for both training data and labels
Labels = np.asarray(Labels, dtype=np.int32)

# Initialize facial recognizer
model = cv2.face.LBPHFaceRecognizer_create()

# NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer()

# Let's train our model 
model.train(np.asarray(Training_Data), np.asarray(Labels))
print(cv2.__version__)
print("Model trained sucessefully")

When I run this code I get this error

File "c:\Users\hp\Unlock-Application\model.py", line 18, in <module>
Training_Data.append(np.asarray(images, dtype=np.uint8))

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
any guess how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘寄 2025-02-04 01:33:20

我怀疑该错误源自此行:

bextfiles = [f in listDir(data_path)在isfile(join(join_path,f))中]

您正在检查iSfile>但是,相反,您应该检查是否是图像IE,如果其.png.jpg,因为任何东西都可以是文件。字符串方法endSwith()可以用于此。

仅在listDir(data_path)中f in for f insfile(join(join_path,data_path,f))]

for f in listdir(data_path):
    image_path = os.path.join(data_path, f) 
    if image_path.endswith(('.png', '.jpg', '.jpeg')): # you can add other formats
       images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

I suspect the error originates from this line:

onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]

You are checking if isfile but instead you should be checking if it's an image i.e if its .png, .jpg, because anything can be a file. The string method endswith() can be used for this.

onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]

for f in listdir(data_path):
    image_path = os.path.join(data_path, f) 
    if image_path.endswith(('.png', '.jpg', '.jpeg')): # you can add other formats
       images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文