具有OpenCV和Python错误的数据集创建者

问题描述 投票:0回答:1

操作系统: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
python-2.7 opencv face-detection
1个回答
1
投票

正如评论中正确提到的,一个可能的原因是图像为空(未正确捕获)。另一种可能性是图像不是彩色图像。

你可以加

cv2.imshow('frame', img)

之前

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

并查看捕获的图像是什么样的。

其余的代码看起来很好。

© www.soinside.com 2019 - 2024. All rights reserved.