【发布时间】:2021-05-07 05:57:23
【问题描述】:
我正在尝试运行此 OpenCV 代码以使用我的摄像机检测人脸。每当我运行我的代码时,它都会给我这个错误。我的摄像机上的灯闪烁,但随后关闭,控制台框中出现此错误以及这个 cv2.error: OpenCV(4.5.1) error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
这是代码
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
#THIS IS THE ERROR AREA
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# Release the VideoCapture object
cap.release()
【问题讨论】:
标签: python python-3.x opencv opencv-python