Ashish Bula
Ashish Bula

Reputation: 1

opencv in python showing a error not read video frame

read videocapture frame using cv2 but any time showing a error

error is :

Traceback (most recent call last): File "d:\pythonprojects\gym\demo.py", line 33, in <module> gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

code is

<import cv2
import numpy as np
import os
from PIL import Image
from Attendance import attendance
from datetime import datetime
from database import\*

def getProfile(Id):
query="SELECT \* FROM users WHERE id="+str(Id)
cursor=mycursor.execute(query)
profile = mycursor.fetchone()
\# profile=None
\# for row in cursor:
\#     profile=row
\# con.close()
return profile

\# os.chdir(os.getcwd())

detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("face-trainner.yml")

cap = cv2.VideoCapture(0) #Get vidoe feed from the Camera
cap.set(3, 640)
cap.set(4, 480)
font = cv2.FONT_HERSHEY_COMPLEX
while(True):
ret, img = cap.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), (0,255,0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        
        nbr_predicted, conf = recognizer.predict(gray[y:y+h, x:x+w])
        print(nbr_predicted, conf)
        if(conf < 80):  
            profile=getProfile(nbr_predicted)
            if profile != None:
                time_now=datetime.now()
                newdate=time_now.strftime('%Y-%m-%d') 
                newtime=time_now.strftime('%H:%M:%S')
                attendance(nbr_predicted,newtime,newdate)
                cv2.putText(img, "Name: "+str(profile[4]), (x, y+h+30), font, 0.4, (0, 0, 255), 1)
                cv2.putText(img, "Gender: " + str(profile[7]), (x, y + h + 50), font, 0.4, (0, 0, 255), 1)
        else:
            cv2.putText(img, "Name: Unknown", (x, y + h + 30), font, 0.4, (0, 0, 255), 1)
    
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
    
    cv2.imshow('Preview',img) #Display the Video
    cv2.waitKey(1)
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

Upvotes: 0

Views: 1589

Answers (2)

the image is null because it only takes the name string instead of the directory of the image, which is actually needed. So, try to concatenate the directory with the image name and try.

Upvotes: 0

Ashyam
Ashyam

Reputation: 706

This error seems to be due to img variable being None or empty. Not sure why it is None in your case. It could be due to various reasons like if the camera is not connected properly, or the camera driver is not installed, or there are permission issues.

You can add a condition after cap.read() call:

...

while True:
    ret, img = cap.read()

    if img is None:
        print("Could not read frame from the source.")
        break

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

...

Upvotes: 0

Related Questions