【发布时间】:2017-12-22 10:15:25
【问题描述】:
操作系统:Ubuntu 17.10
我正在尝试使用此代码使用 Python2.7 和 Open CV(使用 pip 安装)创建人脸检测数据集
import cv2
import numpy as np
cam = cv2.VideoCapture(0)
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Id = raw_input('enter your id')
sampleNum = 0
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
#incrementing sample number
sampleNum = sampleNum+1
#saving the captured face in the dataset folder
cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
cv2.imshow('frame', img)
#wait for 100 miliseconds
if cv2.waitKey(100) & 0xFF == ord('q'):break
# break if the sample number is morethan 20
elif sampleNum > 20: break
cam.release()
cv2.destroyAllWindows()
但我收到以下错误
Traceback (most recent call last):
File "/home/anushi/face/datasetCreator.py", line 10, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
error: /io/opencv/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor
【问题讨论】:
-
很可能您的框架是空的,在尝试处理之前从凸轮读取后添加检查。由于相机初始化等原因,有时第一帧是空的。
-
你能具体说明一下怎么做吗,我是openCV的新手
-
在
ret, img = cam.read()之后你可以检查它为if img is None: continue
标签: python-2.7 opencv face-detection