Convert the mov file to a numpy array

Asked 1 years ago, Updated 1 years ago, 93 views

I would like to read the quick time movie format video file and convert it to a 3D numpy array.
[Frame, X, Y] is the image.
You can save up to the serial number jpg below (all you have to do is read this jpg and turn it into a numpy array), but with this method, you have to play the video in real time every time.(1 hour video takes 1 hour)

If possible, I would appreciate it if you could tell me how to convert directly to mov→numpy array and how to speed it up to some extent.

import cv2
importos

path='***.mov'
cd=os.path.dirname(path)
os.chdir(cd)
vidcap=cv2.VideoCapture(path)
success, image=vidcap.read()
count = 0
image_array=[ ]
success=True
while success:
  success, image=vidcap.read()
  cv2.imwrite("frame%d.jpg"%count, image)
  count+=1

python opencv ffmpeg numpy mov

2022-09-30 18:59

1 Answers

The image type is already <type 'numpy.ndarray'>.So you don't have to save it to a file and read it again.

Also, it's not playing in real time, it just takes about real time to save jpg to disk.Comment out the cv2.imwrite line and you'll see that it's taking about 10 times faster to save.SSD, not HDD, might be a little faster.

If you don't need to process the frames of the video sequentially, we recommend that you divide the video into several parts and process them in a separate process to speed up.cv2.CAP_PROP_POS_MSEC or cv2.CAP_PROP_POS_FRAMES properties can be set in vidcap.

Another idea is to use threads and multiprocessing modules, but Python threads are still not performing well, and it seems too much of an overhead to send numpy objects in the C language world over process-to-process communication even with the multiprocessing module.


2022-09-30 18:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.