Ask Your Question
1

(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

asked 2020-10-09 01:20:27 -0600

Avik_21 gravatar image

updated 2020-10-09 04:21:17 -0600

Hi,

The error encountered is :

Traceback (most recent call last):
File ".\faces-test.py", line 26, in <module> gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

The code is below:

import numpy as numpy
import cv2 as cv
import pickle
from PIL import Image

face_cascade = cv.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
eye_cascade = cv.CascadeClassifier('cascades/data/haarcascade_eye.xml')
recognizer = cv.face.LBPHFaceRecognizer_create()
recognizer.read("trainner.yml")

labels = {"person_name": 1}
with open("labels.pickle", 'rb') as f:
    og_labels = pickle.load(f)
    labels = {v: k for k, v in og_labels.items()}

cap = cv.VideoCapture('mygeneratedvideo.avi')

if(cap.isOpened()):
    print("Camera opened")

while(cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    if(ret == True):
        print("It's working")
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)  # gray was there instead of frame
    for (x, y, w, h) in faces:
        # print(x, y, w, h)
        roi_gray = gray[y:y + h, x:x + w]  # roi(region of interest) # gray was there instead of frame
        roi_color = frame[y:y + h, x:x + w]
        id_, conf = recognizer.predict(roi_gray)
        if conf >= 45 and conf <= 85:
            # print(id_)
            # print(labels[id_])
            font = cv.FONT_HERSHEY_SIMPLEX
            name = labels[id_]
            color = (255, 255, 255)
            stroke = 2
            cv.putText(frame, name, (x, y), font, 1, color, stroke, cv.LINE_AA)
            print(name)
        img_item = "my-image.png"
        cv.imwrite(img_item, roi_gray)
        color = (255, 0, 0)
        stroke = 2
        end_cord_x = x + w
        end_cord_y = y + h
        cv.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for(ex, ey, ew, eh) in eyes:
            cv.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

            # Display the resulting frame
    cv.imshow('myframe', frame)
    if cv.waitKey(20) & 0xFF == ord('q'):
        break


# When everything is done, release the capture

cap.release()
cv.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-10-09 02:40:56 -0600

berak gravatar image

if you play a video file, it will come eventually to an end, and cv2.Videoapture will deliver empty frames.

you're already checking the ret value, but you need something like:

if(ret == False):
    break; # end of movie
edit flag offensive delete link more

Comments

I so much appreciate your answer. It resolved the error. But now the rest of the code is not identifying & displaying my name on the frames window opened.Whereas if instead of the video file(mygeneratedvideo.avi) if I pass 0 as the parameter for VideoCapture(0), then my face is detected properly and displayed on the frames window opened up..

So what I mean is as below:

cap = cv.VideoCapture('mygeneratedvideo.avi') #currently & identification does not work

cap = cv.VideoCapture(0) #Identification works properly

Avik_21 gravatar imageAvik_21 ( 2020-10-09 07:22:21 -0600 )edit

try to decrease the minNeighbours param in the detection

berak gravatar imageberak ( 2020-10-09 08:00:28 -0600 )edit

also, just saying, if the problem is authentication (is that me ?), then opencv's FaceRecognizer classes are the wrong tool, they're for identification (find closest match from a known db)

for better results have a look at dnn / facenet

berak gravatar imageberak ( 2020-10-09 11:07:11 -0600 )edit

Hello berak, I am very thankful to you, your suggestion worked. Thank you for your time and valuable input. It is for identification as of now.

Avik_21 gravatar imageAvik_21 ( 2020-10-10 00:59:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-10-09 01:20:27 -0600

Seen: 7,682 times

Last updated: Oct 09 '20