restatement
Internship project to do helmet target detection, got some video data given by the company, use Opencv to read the video and store an image every 1 s 1s 1s 1s, here are some video data
Implementation Steps Adding Dependency Libraries
import cv2 import os
Defining video paths and image storage paths
video_path = '. /No helmet video 01/'. image_path = './images/'
Reading video files
video_files = [i for i in (video_path) if ('.')[-1] in ['mp4']] len(video_files)
Get video frame
# video_file:'. /unhelmeted video 01/cbc-east-enclosure-5_001_2021-03-22-18-04-28_2021-03-22-18-04-33.mp4', # pic_dir:'CBSA-East Enclosure 5_001_2021-03-22-18-04-28_2021-03-22-18-04-33' def get_image(video_file, pic_dir): if not (pic_dir): (pic_dir) # cv2 reading video files vc = (video_file) index = 0 # Determine if a loaded video can be opened rval = () while rval: # Loop through the video frames index = index + 1 rval, frame = () # Save a picture every ten frames if index * 10 % 1 == 0: if rval: # ("capture", frame) save_file = pic_dir + str(index).zfill(5) + '.png' (save_file, frame) # Store as image, save as folder name (1) else: break () print("Saved %d" %(index - 1) + "A picture.") # video_file = '. /unhelmeted video 01/01.mp4' # pic_path = '01/' # get_image(video_file, image_path + pic_path)
Iterating through video files
for file in video_files: video_file = video_path + file pic_path = image_path + ('.mp4', '/') get_image(video_file, pic_path)
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
1 image saved
Full Code
import cv2 import os def save_img(): video_path = r'F:\test\3.10' videos = (video_path) for video_name in videos: file_name = video_name.split('.')[0] folder_name = video_path +'_'+ file_name (folder_name, exist_ok=True) print(video_path + '/' + video_name) vc = (video_path + '/' + video_name) # Read the video file c = 0 rval = () while rval: # Loop through the video frames c = c + 1 rval, frame = () if c%10 ==0: pic_path = folder_name + '/' if rval: (pic_path + str(c) + '.png', frame) # Store as image, save as folder name (1) else: break () print('save_success') print(folder_name) save_img()
Problems
Read Path Problems
Problem: The result of reading the video shows that the video is not opened, and the check finds that the video path is wrong, resulting in not opening it correctly.
Solution: You can check the path before reading, i.e., determine whether the folder to be saved exists or not, and create the folder if it does not exist. The code is as follows:
if not (path): (path)
Chinese Path Problems
Problem: () Save image path can not exist Chinese characters, otherwise it can not be saved, and there is no prompt!!!!
Solution: Just change the path to English.
final result
to this article on opencv read video and save the image of the method of the article is introduced to this, more related opencv read video content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!