OpenCV VideoWriter is missing frames on Raspberry pi 4

376 Views Asked by At

I'm new to OpenCV and Raspberry. I'm trying to capture a video with cv.VideoWriter_fourcc on a raspberry pi 4. I converted the image to gray, and blurred with cv.bilateralFilter, and searched for circles with cv.HoughCircles. The resulting video is freezing and missing frames. I set the FPS to 24.0 first, but it seemed like its fast-forwarding. I tried some values and it seems like 6.0 is the best value for matching the length of the video.

My code contains more irrelevant steps, I'll provide the relevant parts below.

How can I get a smooth 30 or 60 FPS video from this? Thanks in advance.

#Open cam and initialize video codecs
cap = cv.VideoCapture(0)
fourcc = cv.VideoWriter_fourcc(*"MJPG")
out = cv.VideoWriter('myvideo.avi', fourcc, 22.0, (640,480))
.
.
#For 80 Sec while loop:
start_time = int(time.time())
duration = int(80)
.
.
while ((int(time.time())-start_time)<duration):
    istrue, frame = cap.read()
    #Gray and  blur
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    blur = cv.bilateralFilter(gray, 13,75,75) #Blur density is 13. 75 is irrelevant.
    #Find Circles
    circles = cv.HoughCircles(blur, cv.HOUGH_GRADIENT, 1, 1000, param1 = 50, param2=45, minRadius = 1, maxRadius = 0)
    #Put time as text
    cv.putText(frame, str(datetime.time(datetime.now())), (15,30), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 1, cv.LINE_AA)
    
    #put  text at every 20 secs
    cv.putText(frame, every_20_sec(start_time), (15,50), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 1, cv.LINE_AA)
    #cv.putText(frame, str(datetime.time(datetime.now())), (15,50), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 1, cv.LINE_AA)
    if circles is not None:
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
            cv.circle(frame, (i[0],i[1]), i[2], (0,255,0),2)
            cv.circle(frame, (i[0],i[1]), 1, (0,0,255), 2)
            #pixel coordinates as text
            pos_str = str(int(i[0])) + ' ' + str(int(i[1]))
            cv.putText(frame, pos_str, (i[0]+100,i[1]+100), cv.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2, cv.LINE_AA)
            
            xlist.append(i[0])
            ylist.append(i[1])
    out.write(frame)

cap.release()
out.release()
0

There are 0 best solutions below