0

I am trying to stack a individual frame to a video file using Opencv. I want to combine two different code together to make the individual frame. Following code help me extract the individual frame,

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('file_data.mp4',fourcc,20 (1920,1080),False)
while True:
    ret, frame=cap.read()
    mask = object_detector.apply(frame)
    _, mask  = cv2.threshold(mask,254,255,cv2.THRESH_BINARY)       
    contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    res = cv2.bitwise_and(frame,frame,mask=mask)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>1000:   
            #print("Area of contour:", area)
            cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
            cv2.imwrite("file%d.jpg"%count, frame)
            out.write(frame)
    if cv2.waitKey(1) and 0xFF == ord('q'):
        break

I tried storing the individual frame in array, but it didn't work. It doesn't show any error, but pc crash.

fps = 20, ,width = 1920 ,height = 1080

Rohit
  • 87
  • 1
  • 7
  • **1.** Why are you saving JPEG images `file%d.jpg` and passing PNG images `frame%03d.png` to FFmpeg? **2.** Please explain why are you using FFmpeg instead of using `cv2.VideoWriter`. **3.** You may take a look at my [following answer](https://stackoverflow.com/a/61281547/4926757) - example of how to use FFmpeg CLI within Python for writing the video frame by frame (no need to store the individual frame in an array). – Rotem Aug 16 '22 at 16:45
  • I was able to use`fourcc = cv2.VideoWriter_fourcc(*'XVID')` `out = cv2.VideoWriter('file_data.mp4',fourcc,20,(320,180),False)` But, I don't know the issue, it takes a lot of time to render even while testing with 4MB video file. Please let me know if there is any error. – Rohit Aug 16 '22 at 18:08
  • I don't understand... Do you want to use `cv2.VideoWriter`? In case you do want to use `cv2.VideoWriter` (and don't mind about using `XVID` codec), please update your question with the relevant code. We need to know the value of `frame.shape` for answering if `cv2.VideoWriter('file_data.mp4',fourcc,20,(320,180),False)` is correct. (add the resolution of `frame` to your question). – Rotem Aug 16 '22 at 19:16
  • I made the recommended changes, it produces the video file but the program freezes after like 44KB video file is produced. – Rohit Aug 17 '22 at 00:15
  • you are not doing this res = cv2.bitwise_and(frame,frame,mask=mask). – toyota Supra Aug 17 '22 at 01:36
  • I assume that the last argument should be `True`: `cv2.VideoWriter('file_data.mp4',fourcc,20 (1920,1080), True)` because the frame is BGR (not Grayscale). Make sure to execute `out.release()` at the end. Check the value of `ret`, and if it is `False`, break the loop - after `ret, frame=cap.read()` add the code: `if not ret:` and `break`. – Rotem Aug 17 '22 at 07:12

1 Answers1

0

Thanks to Rotem. The issue has been solved using the VideoWriter from Opencv. The working code is given below.

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('file_data.mp4',fourcc,20,(1920,1080), True)
while True:
ret, frame=cap.read()
if ret == True:
    # frame[frame<=thresholds]=0
    mask = object_detector.apply(frame)
    _, mask  = cv2.threshold(mask,254,255,cv2.THRESH_BINARY)       
    contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    res = cv2.bitwise_and(frame,frame,mask=mask)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>1000:    
            cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
            out.write(frame)
    break
if cv2.waitKey(20) == ord('q'):
    break
Rohit
  • 87
  • 1
  • 7