0

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()
  • I suspect that its due to the processing being done live as it were. You would probably get a better audience (and answer) if you post this on the [Raspberry Pi Stack](https://raspberrypi.stackexchange.com/) – DrBwts Jul 12 '21 at 22:14
  • as I remeber `cv2` has module which runs VideoCapture in separated thread and it may get more FPS. But for big calculation it may not help because it may need faster computer with better CPU - so you would have to send Video stream to server with much better CPU. – furas Jul 12 '21 at 22:41
  • 1
    add threada and queues so thar capturing and writing are decoupled. You will still loose frames but only if your RAM (or nax queue size) is full. If you only want to capture smooth for a small time (fixed number of frames thst fit into your RAM), just capture to RAM and start video writing in a post-processinh. – Micka Jul 13 '21 at 05:15
  • ah and tey to find hardware accelerated video encoding for your device. – Micka Jul 13 '21 at 05:15
  • You don't mention which camera you are using. You don't appear to set the dimensions of the frames you read, if they are larger than 640x480 you should set the capture frame dimensions. You should benchmark what your code can achieve without any processing before expecting it to work with processing. – Mark Setchell Jul 14 '21 at 08:07

0 Answers0