TypeError:int()参数必须是字符串,类似字节的对象或一个数字,而不是非电视'使用CV2时
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑该错误源自此行:
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))]
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 methodendswith()
can be used for this.onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]