Issue
I have a python script in the same folder as a video I want to convert to a numpy array. My video is called 'test.mp4'.
Within my script, I want to call someFunction('test.mp4')
and get back a numpy array. The resulting numpy array should be a numpy array of images, where each image is a 3-d numpy array.
Does that make sense?
Thanks!
Solution
The script below does what you want. You may separate part of it into the function.
Code below doesn't check for errors, in particular, production code will check that every frame*
variable is greater than zero.
import cv2
import numpy as np
cap = cv2.VideoCapture('test.mp4')
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
buf = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))
fc = 0
ret = True
while (fc < frameCount and ret):
ret, buf[fc] = cap.read()
fc += 1
cap.release()
cv2.namedWindow('frame 10')
cv2.imshow('frame 10', buf[9])
cv2.waitKey(0)
Answered By - ivan_onys
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.