Python 通过这段代码,我想收集人脸样本,但它给出了错误

Python 通过这段代码,我想收集人脸样本,但它给出了错误,python,opencv,python-3.6,opencv-python,Python,Opencv,Python 3.6,Opencv Python,下面是错误 import cv2 import numpy face_classifier = cv2.CascadeClassifier('C:/Users/my pc/AppData/Local/Programs/Python/Python38/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') def face_extractor(img): # to extract face feature gra

下面是错误

import cv2
import numpy 
face_classifier = cv2.CascadeClassifier('C:/Users/my pc/AppData/Local/Programs/Python/Python38/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')

def face_extractor(img): # to extract face feature
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces= face_classifier.detectMultiScale(gray,1,3,5)

    if faces is():     
        #if there is no face on screen
        return None

    for(x,y,w,h) in faces:  # if face present, crop face,return it
        cropped_face = img[y:y+h,x:x+w] 

    return cropped_face    

cap= cv2.VideoCapture(0,cv2.CAP_DSHOW)  # to open camera
count = 0 


while True:
    ret ,frame = cap.read()
    if face_extractor(frame) is not None:
        count += 1
        face = cv2.resize(face_extractor(frame),(200,200))
        face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
        file_name_path = 'C:/Users/my pc/faces/user' + str(count)+'.jpg'
        cv2.imwrite(file_name_path,face)  

        cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)  #count how mnay pics clicked
        cv2.imshow('face cropper',face)

    else:
        print('face not found')
        pass

    if cv2.waitKey1 == 13 or count == 100:
        break

cap.release()
cv2.destroyAllWindows()

print('collecting samples complete')
ace.py:9:SyntaxWarning:“is”带有文本。你是说“==”?
如果面为()
回溯(最近一次呼叫最后一次):
文件“face.py”,第24行,在
如果面_提取器(框架)不是无:
文件“face.py”,第7行,在face_提取器中
面=面\分类器。检测多尺度(灰色,1,3,5)
cv2.error:OpenCV(4.4.0)C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm\OpenCV\modules\objdetect\src\cascadedetect.cpp:1689:错误:(-215:断言失败)!函数“cv::CascadeClassifier::detectMultiScale”中的空()

错误消息显示了
如果faces is():
的问题,因为Python中没有
is()

detectMultiScale()
给出了包含对象的列表,您可以检查此列表是否为空:

ace.py:9: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if faces is():
Traceback (most recent call last):
  File "face.py", line 24, in <module>
    if face_extractor(frame) is not None:
  File "face.py", line 7, in face_extractor
    faces= face_classifier.detectMultiScale(gray,1,3,5)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm_\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
或者更容易阅读

if len(faces) == 0:
    return None

只要读一下错误。就我所知,在python中没有比is()更好的了。如果使用
is
关键字检查
face
是否存在,则这是一个语法错误。尝试使用错误中所述的
==
。同样,你也不清楚比较起来是什么。似乎是从某个地方复制粘贴的代码,您弄错了。如果faces是some _func()可能是
如果faces是None:
?或者,如果您在两种情况下都获得了list
if len(faces)==0:
,则可以使用更短的
if not faces:
if not faces:
    return None