【问题标题】:Getting a (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'在函数 'cv::cvtColor' 中获取 (-215:Assertion failed) !_src.empty()
【发布时间】:2020-05-14 17:39:23
【问题描述】:

在使用 openCv 进行人脸检测时,我在函数 'cv::cvtColor' 中得到一个 (-215:Assertion failed) !_src.empty()。这是我的代码:

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 = './faces/user/' + 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")

这是一个错误的输出

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

我尝试将 / 转换为 \,但在扫描字符串文字时导致另一个 SyntaxError: EOL。 请帮我解决这个问题,我也有 tr

【问题讨论】:

  • 这意味着您没有抓取到有效的帧,或者您没有正确设置您的摄像机或 cv2.VideoCapture。所以没有图像可以转换为灰色。或者这可能是不正确的face = cv2.resize(face_extractor(frame), (200, 200))。我建议您检查所有这些或在每个步骤中使用 cv2.imshow() 来查看失败的地方。
  • 好的,我会试着让你知道输出
  • 我在初始化网络摄像头后尝试使用 cv2.imshow() 但我收到另一个错误TypeError: imshow() missing required argument 'mat' (pos 2)。代码是# Initialize Webcam cap = cv2.VideoCapture(0) count = 0 cv2.imshow(cap)
  • 你需要添加一个标题和一个waitKey()。所以试试cv2.imshow('frame', cap)cv2.waitKey(0)

标签: opencv machine-learning data-science


【解决方案1】:

您需要指定 haarcascade_frontalface_default.xml 文件的路径。 像我一样在你的路径中放双 \ 我修改了你的代码,希望对你有帮助

`#importing library 
import cv2
import numpy as np

# Load HAAR face classifier
face_classifier = 

 cv2.CascadeClassifier
('D:\\OpenCV\\opencv\\build\\etc\\Haarcascades\\haarcascade_frontalface_default.xml')
# crop the image into 20 by 20 px and change it to B&W
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
#Asks for the total number of users
num=int(input("Enter no of user you want to add "))
user=1
# Initialize Webcam 
cap = cv2.VideoCapture(0)
#count the total number of faces
count = 0
print("Start Capturing the input Data Set  ")
d=input('Enter Face name')
# Collect 100 samples of your face from webcam input
 while True:
ret, frame = cap.read()
print(type(frame))
#condition to check if face is in the frame or not
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 = './faces/user/' +str(d)+'-' +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

#checks if ech user 100 images is captured
if(count%100)==0 and count!= num*100 and count!=0:
    print("Place the new user Signature")
    cv2.waitKey()
    
#checks if total images are captured
if cv2.waitKey(1) == 13 or count == num*100: #13 is the Enter Key
    break

#closes all windows
cap.release()
cv2.destroyAllWindows()      
print("Collecting Samples Complete")`

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2019-08-30
    • 2019-06-26
    • 2021-04-19
    • 2021-07-30
    • 2023-04-01
    • 2019-11-12
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多