gpt4 book ai didi

opencv - 我的脸部识别程序出现错误。 (-215:声明失败)函数 'cv::CascadeClassifier::detectMultiScale'中的!empty()

转载 作者:行者123 更新时间:2023-12-02 17:01:02 27 4
gpt4 key购买 nike

我正在使用LBPH算法进行人脸检测。收集数据和训练的部分工作正常,但是在测试部分,有一个错误

这是测试代码

import cv2
import numpy as np
import webbrowser


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

def face_detector(img, size=0.5):

faces = ()
# Convert image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('Printe1r', gray )
if np.count_nonzero(gray) >= 0:
print("in face detector")
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



# Open Webcam
cap = cv2.VideoCapture(0)
print("WebCam opened")
while True:

ret, frame = cap.read()
cv2.imshow('Printe1rwer', frame )
image, face = face_detector(frame)
cv2.imshow('Printe1aas r', image )
print(face)

try:
print("hell1o")
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
print("hello")
# Pass face to prediction model
# "results" comprises of a tuple containing the label and the confidence value
results = model.predict(face)
print("Helo",results)

if results[1] < 500:
print("in results < 500")
confidence = int( 100 * (1 - (results[1])/400) )
display_string = str(confidence) + '% Confident it is User'

cv2.putText(face, display_string, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2)

if confidence > 75:
print("in confidence < 75")
cv2.putText(face, "Hey Vimal", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Recognition', face )
webbrowser.open('')
break
else:
print("in else")
cv2.putText(face, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', face )

except Exception as e:
print(e)
cv2.putText(frame, "No Face Found", (220, 120) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.putText(frame, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
cv2.imshow('Face Recognition', frame )
pass

if cv2.waitKey(1) == 13: #13 is the Enter Key
break

cap.release()
cv2.destroyAllWindows()

我得到的错误是:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-1-3a076399e5b1> in <module>
34 ret, frame = cap.read()
35 cv2.imshow('Printe1rwer', frame )
---> 36 image, face = face_detector(frame)
37 cv2.imshow('Printe1aas r', image )
38 print(face)

<ipython-input-1-3a076399e5b1> in face_detector(img, size)
14 if np.count_nonzero(gray) >= 0:
15 print("in face detector")
---> 16 faces = face_classifier.detectMultiScale(gray, 1.3, 5)
17
18 if faces is ():

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689:
error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'`

有人可以帮我吗?
我正在使用OpenCV版本4.2.0
一件事是面部检测器功能中的灰色变量值始终是一个numpy数组,所有值均为零。我已经检查过了,但总是零。

最佳答案

我已经测试了代码的第一部分。似乎正在工作,打印gray我得到:

TEMP.py:20: SyntaxWarning: "is" with a literal. Did you mean "=="?
if faces is ():
WebCam opened
[[162 162 162 ... 97 97 97]
[161 161 160 ... 95 95 95]
[159 159 161 ... 95 95 95]
...
[252 252 252 ... 164 164 164]
[236 233 229 ... 165 164 164]
[164 158 153 ... 164 164 164]]

除了按照解释器的建议用 ()纠正 ==之外,我还要检查:
  • 人脸检测器中的img非零(只需打印出来,我的就可以了)。
  • 您实际上是在cap = cv2.VideoCapture(0)中加载正确的输入源(您是否连接了多个网络摄像头?)
  • 尝试在if frame:之后插入一个ret, frame = cap.read()块。 可能只有第一帧为“无”,这给您带来了所有问题。

  • 如果以上都可以,那么唯一的怀疑者仍然是 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ...

    旁注: ret, frame = cap.read()从相机连续读取所有输入,但是PC在处理每一帧时可能比您的算法慢。我通常避免使用技巧来创建长缓冲区。解决上述问题后,看看 this

    关于opencv - 我的脸部识别程序出现错误。 (-215:声明失败)函数 'cv::CascadeClassifier::detectMultiScale'中的!empty(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039140/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com