错误:C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp:11111:错误:(-215)scn == 3 || scn == 4在函数cv :: cvtColor中

时间:2019-01-20 12:50:34

标签: python opencv

我遵循了有关创建训练集以识别人脸的教程 这是代码

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 = 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")
        pass

    if cv2.waitKey(1) == 13 or count == 100: #13 is the Enter Key
        break

cap.release()
cv2.destroyAllWindows()      
print("Collecting Samples Complete")

每当我尝试在jupyter笔记本中运行此代码时,都会出现此错误

error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:11111: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

并试图通过在stackoverflow和所有其他站点上进行搜索来找出问题,即使在搜索后我也无法理解此问题的原因 这是错误的完整回溯 错误

    Traceback (most recent call last)
<ipython-input-13-6aa561124bc3> in <module>()
     30 
     31     ret, frame = cap.read()
---> 32     if face_extractor(frame) is not None:
     33         count += 1
     34         face = cv2.resize(face_extractor(frame), (200, 200))

<ipython-input-13-6aa561124bc3> in face_extractor(img)
     11 
     12     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
---> 13     faces = face_classifier.detectMultiScale(gray, 1.3, 5)
     14 
     15     if faces is ():

并且我尝试在python根目录中添加2.4.13 v dll文件,并将它们添加到环境变量中,但仍然没有用处

0 个答案:

没有答案