【发布时间】:2019-06-14 22:07:59
【问题描述】:
我按照一些教程创建了一个 OpenCV 函数,该函数从我的网络摄像头捕获 100 帧并将其存储在我提到的路径中,但是当我尝试检查网络摄像头是否与 OpenCV 正确集成时 我已经运行了这段代码
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
这一切都很好,我可以在灰色的框架中看到自己
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, 3)
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)
# ret, frame = cap.read()
count = 0
# Collect 100 samples of your face from webcam input
while(True):
ret, frame = cap.read()
print(type(frame))
print(frame.shape)
# if ret==True:
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 = r'C:\Users\madhumani\path\\' + 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")
# else:
# print("no camera")
if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()
print("Collecting Samples Complete")
根据该教程,网络摄像头应捕获该特定人的 100 帧并将其存储在路径中,但我只是将 face not found 打印为输出
这是输出:
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
<type 'numpy.ndarray'>
(480L, 640L, 3L)
Face not found
【问题讨论】:
-
我运行了你的代码,它可以正常工作,没有错误。不知何故,方法
face_extractor在您的情况下返回None。你能分享print(len(faces))在if faces is ()上方调用的结果吗?如果为 0,则分类器有问题。 -
@Bhoke 0 是输出
-
那么分类器有什么问题??
-
我怀疑您的 xml 文件可能已损坏。您也可以尝试将行
cv2.imshow('window',gray)和cv2.waitKey()添加到上面的同一位置,以查看如何获取帧。