【问题标题】:Dataset creator with OpenCV and Python error带有 OpenCV 和 Python 错误的数据集创建者
【发布时间】: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


【解决方案1】:

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

你可以添加

cv2.imshow('frame', img)

之前

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

看看捕获的图像是什么样子的。

其余代码看起来不错。

【讨论】:

  • 除此之外,ret 如果帧被正确读取,也会给出真/假 - 从here"cap.read() 返回一个布尔值(真/假)。如果帧是读取正确,它将为 True。因此您可以通过检查此返回值来检查视频的结尾。"
猜你喜欢
  • 2019-08-22
  • 1970-01-01
  • 2023-04-03
  • 2015-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
相关资源
最近更新 更多