【发布时间】:2020-05-14 17:39:23
【问题描述】:
在使用 openCv 进行人脸检测时,我在函数 'cv::cvtColor' 中得到一个 (-215:Assertion failed) !_src.empty()。这是我的代码:
import cv2
import numpy as np
# Load HAAR face classifier
face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')
# Load functions
def face_extractor(img):
# Function detects faces and returns the cropped face
# If no face detected, it returns the input image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
return None
# Crop all faces found
for (x,y,w,h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
# Initialize Webcam
cap = cv2.VideoCapture(0)
count = 0
# Collect 100 samples of your face from webcam input
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)
# Save file in specified directory with unique name
file_name_path = './faces/user/' + str(count) + '.jpg'
cv2.imwrite(file_name_path, face)
# Put count on images and display live count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Face Cropper', face)
else:
print("Face not found")
pass
if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()
print("Collecting Samples Complete")
这是一个错误的输出
error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
我尝试将 / 转换为 \,但在扫描字符串文字时导致另一个 SyntaxError: EOL。 请帮我解决这个问题,我也有 tr
【问题讨论】:
-
这意味着您没有抓取到有效的帧,或者您没有正确设置您的摄像机或 cv2.VideoCapture。所以没有图像可以转换为灰色。或者这可能是不正确的
face = cv2.resize(face_extractor(frame), (200, 200))。我建议您检查所有这些或在每个步骤中使用 cv2.imshow() 来查看失败的地方。 -
好的,我会试着让你知道输出
-
我在初始化网络摄像头后尝试使用 cv2.imshow() 但我收到另一个错误
TypeError: imshow() missing required argument 'mat' (pos 2)。代码是# Initialize Webcam cap = cv2.VideoCapture(0) count = 0 cv2.imshow(cap) -
你需要添加一个标题和一个waitKey()。所以试试
cv2.imshow('frame', cap)cv2.waitKey(0)
标签: opencv machine-learning data-science