Src.empty() in function 'cvtColor'

Hi,
I’ve installed OpenCV(v4.5.3) wich uses cuda and cudnn for a faster inference. I’m new in openCV so I follow this tutorial: everything was fine following it.
My problem comes when I try to execute my test.py, wich is a vehicle counter program.

This is the code:

import cv2
import numpy as np
from time import sleep

largura_min=80 #Largura minima do retangulo
altura_min=80 #Altura minima do retangulo

offset=6 #Erro permitido entre pixel  

pos_linha=550 #Posição da linha de contagem 

delay= 60 #FPS do vídeo

detec = []
carros= 0

	
def pega_centro(x, y, w, h):
    x1 = int(w / 2)
    y1 = int(h / 2)
    cx = x + x1
    cy = y + y1
    return cx,cy

cap = cv2.VideoCapture('video.mp4')
subtracao = cv2.bgsegm.createBackgroundSubtractorMOG()

while True:
    ret , frame1 = cap.read()
    tempo = float(1/delay)
    sleep(tempo) 
    grey = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(grey,(3,3),5)
    img_sub = subtracao.apply(blur)
    dilat = cv2.dilate(img_sub,np.ones((5,5)))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    dilatada = cv2.morphologyEx (dilat, cv2. MORPH_CLOSE , kernel)
    dilatada = cv2.morphologyEx (dilatada, cv2. MORPH_CLOSE , kernel)
    contorno,h=cv2.findContours(dilatada,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    cv2.line(frame1, (25, pos_linha), (1200, pos_linha), (255,127,0), 3) 
    for(i,c) in enumerate(contorno):
        (x,y,w,h) = cv2.boundingRect(c)
        validar_contorno = (w >= largura_min) and (h >= altura_min)
        if not validar_contorno:
            continue

        cv2.rectangle(frame1,(x,y),(x+w,y+h),(0,255,0),2)        
        centro = pega_centro(x, y, w, h)
        detec.append(centro)
        cv2.circle(frame1, centro, 4, (0, 0,255), -1)

        for (x,y) in detec:
            if y<(pos_linha+offset) and y>(pos_linha-offset):
                carros+=1
                cv2.line(frame1, (25, pos_linha), (1200, pos_linha), (0,127,255), 3)  
                detec.remove((x,y))
                print("car is detected : "+str(carros))        
       
    cv2.putText(frame1, "VEHICLE COUNT : "+str(carros), (450, 70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255),5)
    cv2.imshow("Video Original" , frame1)
    cv2.imshow("Detectar",dilatada)

    if cv2.waitKey(1) == 27:
        break
    
cv2.destroyAllWindows()
cap.release()

When I execute it, i get this error:

File "main.py", line 32, in <module>
grey = cv2.cvtColor(frame1,cv2.COLOR:BRG2GRAY)
cv2.error: OpenCV(4.5.3-pre) /home/abraham/workspace/opencv/opencv/modules/imgproc/src/color.cpp:182 error: (-215: Assertion failed) !:src.empty() in function 'cvtColor'

I thought that it would still be a code problem, but if a execute main.py without cuda and dnn support it works perfectly.

Anyone can help me?

I emphasize that I am new in this world, please don’t be too hard on me :joy:

Thanks all and good forum!

python users NEVER check their IO, and that’s your problem, too :wink:

frame1 is empty, it has nothing to do with cuda or not…

you have to check for

cap.isOpened()

before the loop, and

ret == True

inside. if you play a movie, it wll eventually come to an end, and VideoCapture will deliver empty, invalid frames.

btw, you’re #7364 with this question, i wonder why it’s so hard to gggl it …

1 Like

4 posts were split to a new topic: Unspecified issue when using dnn module