我正在做一个开放式cv项目,使用网络摄像头进行人脸检测。我收到了这个错误,下面的错误,看起来灰度变量是非类型的,但是idk如何修复它:
注意:xml文件已与源代码位于同一文件夹中
face_coordinates = trained_face_data.detectMultiScale(grayscale)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
以下是源代码:
import cv2
from random import randrange
# load some pre-trained data on face frontal from opencv
trained_face_data = cv2.CascadeClassifier('haarcasde_frontalface_default.xml')
# Capture video from webcam:
webcam = cv2.VideoCapture(0) # 0 is the default cam, u can put a video file in it too
while True:
# Read the current frame
successful_frame_read, frame = webcam.read()
"""
The first argument will return us a boolean, True or False, telling us if it can read the frame,
and the second argument is the frame that is getting read
"""
if frame is not None:
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
face_coordinates = trained_face_data.detectMultiScale(grayscale)
# Reminder:
# trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
print(face_coordinates)
for (x, y, w, h) in face_coordinates:
cv2.rectangle(frame, (x, y), (x + w, y + h),
(randrange(128, 256), randrange(256), randrange(256)), 2)
cv2.imshow('face detected', frame)
cv2.waitKey(1)
"""
1 represents how many milliseconds, before it automatically press any key and go to the next frame to
scan ur face
"""
print("Code Completed")
elif frame is None:
print('fail')
break
目前没有回答
相关问题 更多 >
编程相关推荐