0

I am attempting to use opencv_python to break an mp4 file down into it's frames so I can later open them with pillow, or at least be able to use the images to run my own methods on them.

I understand that the following snippet of code gets a frame from a live video or a recorded video.

    import cv2
    cap = cv2.VideoCapture("myfile.mp4")
    boolean, frame = cap.read()

What exactly does the read function return and how can I create an array of images which I can modify.

3

2 Answers 2

1

adapted from How to process images of a video, frame by frame, in video streaming using OpenCV and Python. Untested. However, the frames are read into a numpy array and and append to a list that is converted to a numpy array when the all the frames are read in.

import cv2
import numpy as np


images = []

cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
    cap = cv2.VideoCapture("./out.mp4")
    cv2.waitKey(1000)
    print "Wait for the header"

pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
    frame_ready, frame = cap.read() # get the frame
    if frame_ready:
        # The frame is ready and already captured
        # cv2.imshow('video', frame)

        # store the current frame in as a numpy array
        np_frame = cv2.imread('video', frame)
        images.append(np_frame)
        
        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
    else:
        # The next frame is not ready, so we try to read it again
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
        print "frame is not ready"
        # It is better to wait for a while for the next frame to be ready
        cv2.waitKey(1000)

    if cv2.waitKey(10) == 27:
        break
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        # If the number of captured frames is equal to the total number of frames,
        # we stop
        break

all_frames = np.array(images)
Sign up to request clarification or add additional context in comments.

7 Comments

thank you very much. Thank you for commenting out the code. I love to have a good understanding of it
no problem, glad I could help. If it worked consider marking it as the answer.
hey, I have a small issue.
np_frame = cv2.imread('video', frame) returns this error and I have no idea what to do:
cv2.imread('video', frame) throws TypeError: 'flags' is required to be an integer
|
0

Simply use this code to get an array of frames from your video:

import cv2
import numpy as np
frames = []
video = cv2.VideoCapture("spiderino_turning.mp4")
while True:
    read, frame= video.read()
    if not read:
        break
    frames.append(frame)
frames = np.array(frames)

but regarding your question, video.read() returns two values. The first one (read in the example code) indicates if the frame is successfully read or not (i.e., True on succeeding and False on any error). The second returning value is the frame that can be empty if the read attempt is unsuccessful or a 3D array (i.e., color image) otherwise. But why can a read attempt be unsuccessful?

  1. If you are reading from a camera, any problem with the camera (e.g., the cable is disconnected or the camera's battery is dead) can cause an error.
  2. If you are reading from a video, the read attempt will fail when all the frames are read, and there are no more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.