Python 3.x 矩阵.cpp:310:错误:(-215)s>;=函数cv::setSize中的0

Python 3.x 矩阵.cpp:310:错误:(-215)s>;=函数cv::setSize中的0,python-3.x,opencv,cv2,lbph-algorithm,Python 3.x,Opencv,Cv2,Lbph Algorithm,我对python非常陌生。我正在使用 face\u recognizer=cv2.face.LBPHFaceRecognizer\u create() 并将我的预测函数定义为 #this function recognizes the person in image passed #and draws a rectangle around detected face with name of the #subject def predict(test_img): #make a copy of

我对python非常陌生。我正在使用
face\u recognizer=cv2.face.LBPHFaceRecognizer\u create()
并将我的预测函数定义为

#this function recognizes the person in image passed
#and draws a rectangle around detected face with name of the 
#subject
def predict(test_img):
#make a copy of the image as we don't want to chang original image
img = test_img.copy()
#detect face from the image
face, rect = detect_face(img)

#predict the image using our face recognizer 
label= face_recognizer.predict(face)
#get name of respective label returned by face recognizer
label_text = subjects[label]

#draw a rectangle around face detected
draw_rectangle(img, rect)
#draw name of predicted person
draw_text(img, label_text, rect[0], rect[1]-5)

return img`
在使用预测函数预测人脸时,我得到了以下误差

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-13-d6517b4e38bd> in <module>()
  6 
  7 #perform a prediction
----> 8 predicted_img1 = predict(test_img1)
  9 #predicted_img2 = predict(test_img2)
 10 print("Prediction complete")

<ipython-input-12-b46266ecb9d5> in predict(test_img)
  9 
 10     #predict the image using our face recognizer
---> 11     label= face_recognizer.predict(face)
 12     #get name of respective label returned by face recognizer
 13     label_text = subjects[label]

error: C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:310: 
error: (-215) s >= 0 in function cv::setSize
---------------------------------------------------------------------------
错误回溯(最近一次呼叫上次)
在()
6.
7#进行预测
---->8预测\u img1=预测(测试\u img1)
9#预测_img2=预测(测试_img2)
10打印(“预测完成”)
预测中(测试中)
9
10#使用我们的人脸识别器预测图像
--->11标签=人脸识别器。预测(人脸)
12#获取人脸识别器返回的各个标签的名称
13标签\文本=受试者[标签]
错误:C:\projects\opencv python\opencv\modules\core\src\matrix.cpp:310:
错误:(-215)s>=函数cv::setSize中的0

非常感谢您

我在测试同一个github项目时,在添加新的培训映像和测试映像时遇到了相同的错误。当使用训练数据中的一个图像作为测试图像时,问题就消失了。因此,问题是当@DaveW.Smith建议找不到匹配项时,将无法处理。

是否有可能
detect_face()
会给您一些指示(例如,
face is None
),表明它没有找到一张脸,并且您正在使用无效的
rect
?@DaveW.Smith,我没有听到你提到的任何错误。上面提到的是我唯一得到的错误。有时,如果你通过无效输入,OpenCV C++代码下面的代码会产生一些奇怪的错误。在调用
face\u recognizer.predict()
show?@DaveW.Smith之前,
print(repr(face))
做了什么工作?@DaveW.Smith,我使用了来自并且只更改了
face\u recognizer=cv2.face.LBPHFaceRecognizer\u create()
的代码。请注意,如果找不到面,它将返回
None,None
。您复制的调用该方法的示例方法没有检查
None
。我怀疑这就是问题的根源。