简体   繁体   English

Python:TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”

[英]Python: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

I tried executing the following code for facial recognition in python and got the error as mentioned, what could be the possible cause and what is a solution?我尝试在 python 中执行以下代码进行面部识别并得到如上所述的错误,可能的原因是什么,解决方案是什么?

code:代码:

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

data_path ='/Users/aksheenmalhotra/Desktop/facerecogdata/'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path,f))]

Training_Data, Labels = [], []

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)

Labels = np.asarray(Labels, dtype=np.int32)

model = cv2.face.LBPHFaceRecognizer_create()

model.train(np.asarray(Training_Data), np.asarray(Labels))

print("Model Training Complete!!!!!")

face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

def face_detector(img, size = 0.5):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)

    if faces is():
        return img,[]

    for(x,y,w,h) in faces:
        cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,255),2)
        roi = img[y:y+h, x:x+w]
        roi = cv2.resize(roi, (200,200))

    return img,roi

cap = cv2.VideoCapture(0)
while True:

    ret, frame = cap.read()

    image, face = face_detector(frame)

    try:
        face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
        result = model.predict(face)

        if result[1] < 500:
            confidence = int(100*(1-(result[1])/300))
            display_string = str(confidence)+'% Confidence it is user'
        cv2.putText(image,display_string,(100,120), cv2.FONT_HERSHEY_COMPLEX,1,(250,120,255),2)


        if confidence > 75:
            cv2.putText(image, "Unlocked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
            cv2.imshow('Face Cropper', image)

        else:
            cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
            cv2.imshow('Face Cropper', image)


    except:
        cv2.putText(image, "Face Not Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
        cv2.imshow('Face Cropper', image)
        pass

    if cv2.waitKey(1)==13:
        break


cap.release()
cv2.destroyAllWindows()

I get the following error traceback.我收到以下错误回溯。 Error:错误:

Traceback (most recent call last):
  File "/Users/aksheenmalhotra/Desktop/python files/18-1-2020/facerecogtrial.py", line 14, in <module>
    Training_Data.append(np.asarray(images,dtype=np.uint8))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numpy/core/_asarray.py", line 85, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

I can reproduce the error message with:我可以通过以下方式重现错误消息:

In [210]: np.asarray(None, dtype='uint8')                                                        
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-210-ab1b7ff4c0ab> in <module>
----> 1 np.asarray(None, dtype='uint8')

/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Thus, in因此,在

images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
Training_Data.append(np.asarray(images,dtype=np.uint8))

images is, most likely, None . images很可能是None cv2.imread returns None when it can't read the file, most likely because the file path is wrong. cv2.imread无法读取文件时返回None ,很可能是因为文件路径错误。

So a robust use of cv2 should be prepared for this, and test for None , and skip or raise an error.因此,应该为此做好充分使用cv2准备,并测试None ,并跳过或引发错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误测试python代码:TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ NoneType” - Error testing python code: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 python3 中的“NoneType”错误 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' error in python3 TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”(HTML 将输入发送到 Python) - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' (HTML send input to Python) TypeError: int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是使用 Python 3.7 时的“NoneType” - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' while using Python 3.7 int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - int() argument must be a string, a bytes-like object or a number, not 'NoneType' TypeError: int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是“NoneType”。 但我写了 int() - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. But I wrote the int() TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“ NoneType” - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' 使用 openpyxl 时,会发生此执行“TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'” - While using openpyxl this execption occurs “ TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' ” TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' Deep Learning - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' Deep Learning TensorFlow:TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - TensorFlow: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM