程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of Pythons method of quickly extracting video frames from video

編輯:Python

Catalog

1、 Extract video frames

2、 Multithreading approach

3、 The overall code

Add

Python Quickly extract video frames ( Multithreading )

Today we introduce a method of extracting video frames from video , Because the speed of extracting video frames by single thread is slow , So here we add the method of multithreading .

1、 Extract video frames

Extracting video frames mainly uses Opencv modular .

among :

camera = cv2.Videocapture( ) , The function mainly reads video frames by calling the built-in camera of the notebook ;

res, image = camera.read( ) The function mainly reads video by frame , Return value “res” It's Boolean , Successful reading returns True, Reading failed and returned False;

Last use cv2.imwrite( ) Function to store the read video frame .

Refer to this article for video frame extraction methods

import cv2import osdef video_to_frames(video_path, outPutDirName): times = 0 # Extract the frequency of the video , Every time 1 Extract a frame frame_frequency = 1# If the file directory does not exist, create a directory if not os.path.exists(outPutDirName): os.makedirs(outPutDirName) # Read video frames camera = cv2.VideoCapture(video_path) while True: times = times + 1 res, image = camera.read() if not res: print('not res , not image') break # Store video frames at set intervals if times % frame_frequency == 0: cv2.imwrite(outPutDirName + '\\' + str(times)+'.jpg', image) print(' Picture extraction is over ') # Release the camera device camera.release()2、 Multithreading approach

Multithreaded applications mainly use threading library .

among :

threading.Thread( ) Function is mainly used to call multithreading , The parameter “target” Is the function used above , Parameters “args” Is the input parameter of the above function .

Among them, there is a detailed introduction to multithreading , As well as the speed improvement effect, please refer to this article

import threadingthreading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start()

verified , The speed improvement is still very fast !

3、 The overall code import cv2import osimport threadingdef video_to_frames(video_path, outPutDirName): times = 0 # Extract the frequency of the video , Every time 1 Extract a frame frame_frequency = 1# If the file directory does not exist, create a directory if not os.path.exists(outPutDirName): os.makedirs(outPutDirName) # Read video frames camera = cv2.VideoCapture(video_path) while True: times = times + 1 res, image = camera.read() if not res: print('not res , not image') break if times % frame_frequency == 0: cv2.imwrite(outPutDirName + '\\' + str(times)+'.jpg', image) print(' Picture extraction is over ') camera.release()if __name__ == "__main__": input_dir = r'D:\datasets\cow_dataset' # Input video Folder location save_dir = r'E:\relate_code\dataset' # Output pictures to the current directory video Under the folder count = 0 # Number of videos for video_name in os.listdir(input_dir): video_path = os.path.join(input_dir, video_name) outPutDirName = os.path.join(save_dir, video_name[:-4]) threading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start() count = count + 1 print("%s th video has been finished!" % count) Add

It can also be used Python Implement the video frame extraction and elimination tool

Code

Here's how to use opencv The method of extracting the middle frames of video .

The main idea is to read frame When , By the way, write down the frame .

At the same time, if it is not necessary to extract the deleted frames , direct continue To the next cycle .

The sample code is as follows , Mainly according to MP4 Format for processing .

#!/user/bin/env python# coding=utf-8"""@project : [email protected] : Swordsman a Liang [email protected] : [email protected] : [email protected] : 2022-06-30 17:55:48"""import cv2# Video frame extraction def extract_frame(video_path: str, result_path: str, fps, weight, height, start, end): fourcc = cv2.VideoWriter_fourcc(*'mp4v') videoWriter = cv2.VideoWriter(result_path, fourcc, fps, (weight, height)) vc = cv2.VideoCapture(video_path) if vc.isOpened(): ret, frame = vc.read() else: ret = False count = 0 # count the number of pictures while ret: ret, frame = vc.read() if start <= count <= end: count += 1 continue else: videoWriter.write(frame) count += 1 print(count) videoWriter.release() vc.release()if __name__ == '__main__': extract_frame('C:\\Users\\xxx\\Desktop\\123.mp4', 'C:\\Users\\xxx\\Desktop\\114.mp4', 25, 640, 368, 119, 125)

Be careful

1、extract_frame The input parameters of methods are : Enter the video address 、 Output video address 、 video fps、 Wide video resolution 、 High video resolution 、 The starting frame of the video that needs to be extracted 、 The end frame of the video that needs to be removed .

This is about Python This is the article on how to quickly extract video frames from video , More about Python To extract the video frame content, please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved