0

I have a code that gets a video from a folder and does some calculations using contours and background subtraction. After that I am going to save that edited video into the folder. The code is shown below:

import numpy as np
import cv2
import time

# Capture video from file
cap = cv2.VideoCapture('test_video.mp4')
time.sleep(1)
fgbg = cv2.createBackgroundSubtractorMOG2()
j = 0

fourcc = cv2.VideoWriter_fourcc(*'MPEG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while (cap.isOpened()):
    ret, frame = cap.read()

    if ret == True:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        fgmask = fgbg.apply(gray)
        _, contours, _ = cv2.findContours(fgmask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        if contours:
            areas = []
            for contour in contours:
                ar = cv2.contourArea(contour)
                areas.append(ar)

            max_area = max(areas or [0])
            max_area_index = areas.index(max_area)
            cnt = contours[max_area_index]
            M = cv2.moments(cnt)
            x, y, w, h = cv2.boundingRect(cnt)
            cv2.drawContours(fgmask, [cnt], 0, (255,255,255), 3, maxLevel = 0)

            if h < w:
                j += 1

            if j>10:
                cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)

            if h > w:
                j = 0
                cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)

            cv2.imshow('video',frame)
            out.write(frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

This opens up a window and plays a video, then output.avi is created, but it doesn't contain any content. cmd produces nothing. I just can't able to save a file in a proper way.

Please recommend a solution to this issue

bit_scientist
  • 1,496
  • 2
  • 15
  • 34
  • Your modification is wrong. You should not loop forever, only while the video capture is "open". Also, you are breaking once your video capture fails to read in a frame. This is why your error disappeared. In reality the problem probably still exists. Refer to [this](https://stackoverflow.com/questions/41441150/how-to-read-video-files-using-python-opencv) and [this](https://stackoverflow.com/questions/18954889/how-to-process-images-of-a-video-frame-by-frame-in-video-streaming-using-opencv/19082750#19082750) on how to read in from video. – lightalchemist Oct 19 '18 at 06:52
  • in the documentation [here](https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html) they used the `while (cap.isOpened()):`. When I use that too, there is still no expected result. – bit_scientist Oct 19 '18 at 07:13

1 Answers1

0

The error is telling you that frame does not have 3 or 4 channels.

  1. Can you check that your camera is initialized properly

    if not cap.isOpened():
        print("Camera not initialized")
        return
    
  2. It is returning you a valid frame

    if not ret:
        print("Problem reading frame")
        return
    else:
        # Convert your frame to gray and find contours etc.
    
lightalchemist
  • 10,031
  • 4
  • 47
  • 55
  • @voo_doo 1) Are you sure the video file is in the same directory as your script? 2) Are you sure you have the codecs to read the mp4 file? – lightalchemist Oct 19 '18 at 06:08
  • yes, because the video is playing in window, that means it can capture the file. the saved file is just __6 KB__. I can read any *.mp4 file on my PC so I am considering I have a codec to read. Besides, I have ffmeg too. Let me update the question a bit – bit_scientist Oct 19 '18 at 06:43
  • Now the error is gone, but still can't save video. Please refer to the issue again – bit_scientist Oct 19 '18 at 06:48