Capture every frame of the video

Requirements: Extract video frames and process the extracted video frames, such as resizing or turning them into grayscale images"""Function: convert video to picture (extract every frame picture of video) 1. Can set how many frames to extract a frame picture 2. You can set the size and gray scale of the output picture. Figure 3. Manually set the naming format of the output picture.""
Copy the code
Def ExtractVideoFrame(video_input,output_path): # If output folder does not exist, create output folderif not os.path.exists(output_path):
        os.mkdir(output_path)

    times = 0Frame_frequency =10The frequency of video extraction, one image per frameFrequency frame, complete video frame extraction set as1
    count = 0Cap = cv2.VideoCapture(video_input) # print('Start extracting', video_input, 'Picture of video')
    while True:
        times += 1Res, image = cap.read() # Res indicates whether an image has been read, and image indicates each imageif not res:
            print('End of image extraction')
            break
        if times % frame_frequency == 0: # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray) # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray)368.640Img_name = STR (count).zfill(6) +'.jpg'
            cv2.imwrite(output_path + os.sep + img_name, image)
            count += 1Print (output_path + os.sep + img_name)Copy the code

Captures the specified frame in a video

Need: Cut the specified frame of the video. Like cutting video from100The frame to the first200The picture of the frame"""Function: Cut the specified frame of the video. For example, cut video from frame 100 to frame 200 of the picture 1. Can set how many frames to extract a frame of picture 2. You can set the size and gray scale of the output picture. Figure 3. Manually set the naming format of the output picture.""
Copy the code
def ExtractVideoBySpecialFrame(video_input,output_path,start_frame_index,end_frame_index = -1If the output folder does not exist, create an output folderifnot os.path.exists(output_path): Op.mkdir (output_path) cap = v2.videocapture (video_input) Float (start_frame_index)) # Read file from specified frame times =0Frame_frequency =10The frequency of video extraction, one image per frameFrequency frame, complete video frame extraction set as1
    count = 0Cut from start_frame_index to the last frame without a given end frameif end_frame_index == -1:
        print('Start extracting', video_input, 'Video from the first',start_frame_index,'Frame to last frame of picture!! ')
        while True:
            times += 1Res, image = cap.read() #if not res:
                print('Image extraction finished!! ')
                break
            if times % frame_frequency == 0: # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray) # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray)368.640Img_name = STR (count).zfill(6) + '.jpg'
                cv2.imwrite(output_path + os.sep + img_name, image)
                count += 1Print (output_path + os.sep + img_name) #else:
        print('Start extracting', video_input, 'Video from the first', start_frame_index, 'Frame to first',end_frame_index,'Frame the picture!! ')
        k = end_frame_index - start_frame_index + 1
        while(k >= 0):
            times += 1
            k -= 1Res, image = cap.read() #if not res:
                print('Image extraction finished!! ')
                break
            if times % frame_frequency == 0: # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray) # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray)368.640Img_name = STR (count).zfill(6) + '.jpg'
                cv2.imwrite(output_path + os.sep + img_name, image)
                count += 1Print (output_path + os.sep + img_name)'Image extraction finished!! ')
    cap.release()
Copy the code

Displays a frame of a video

Requirement: Get the specified frame of the video and display it"""Function: Get the specified frame of the video and display it"""
Copy the code
def ShowSpecialFrame(file_path,frame_index): Cap = cv2.videocapture (file_path) # Cap.set (cv2.cap_prop_pos_frames, float(frame_index))ifCap.isopened (): # if rval isOpened properly, frame = cap.read() cv2.imshow()"image:"+frame_index,frame)
        cv2.waitKey()
    cap.release()
Copy the code

Code integration

Put the appeal code together

import os
import cv2

"""Function: convert video to picture (extract every frame picture of video) 1. Can set how many frames to extract a frame picture 2. You can set the size and gray scale of the output picture. Figure 3. Manually set the naming format of the output picture.""Def ExtractVideoFrame(video_input,output_path): # If output folder does not exist, create output folderif not os.path.exists(output_path):
        os.mkdir(output_path)

    times = 0Frame_frequency =10The frequency of video extraction, one image per frameFrequency frame, complete video frame extraction set as1
    count = 0Cap = cv2.VideoCapture(video_input) # print('Start extracting', video_input, 'Picture of video')
    while True:
        times += 1Res, image = cap.read() # Res indicates whether an image has been read, and image indicates each imageif not res:
            print('End of image extraction')
            break
        if times % frame_frequency == 0: # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray) # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray)368.640Img_name = STR (count).zfill(6) +'.jpg'
            cv2.imwrite(output_path + os.sep + img_name, image)
            count += 1Print (output_path + os.sep + img_name)"""Function: Get the specified frame of the video and display it"""def ShowSpecialFrame(file_path,frame_index): Cap = cv2.videocapture (file_path) # Cap.set (cv2.cap_prop_pos_frames, float(frame_index))ifCap.isopened (): # if rval isOpened properly, frame = cap.read() cv2.imshow()"image:"+frame_index,frame)
        cv2.waitKey()
    cap.release()

"""Function: Cut the specified frame of the video. For example, cut video from frame 100 to frame 200 of the picture 1. Can set how many frames to extract a frame of picture 2. You can set the size and gray scale of the output picture. Figure 3. Manually set the naming format of the output picture.""
def ExtractVideoBySpecialFrame(video_input,output_path,start_frame_index,end_frame_index = -1If the output folder does not exist, create an output folderifnot os.path.exists(output_path): Op.mkdir (output_path) cap = v2.videocapture (video_input) Float (start_frame_index)) # Read file from specified frame times =0Frame_frequency =10The frequency of video extraction, one image per frameFrequency frame, complete video frame extraction set as1
    count = 0Cut from start_frame_index to the last frame without a given end frameif end_frame_index == -1:
        print('Start extracting', video_input, 'Video from the first',start_frame_index,'Frame to last frame of picture!! ')
        while True:
            times += 1Res, image = cap.read() #if not res:
                print('Image extraction finished!! ')
                break
            if times % frame_frequency == 0: # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray) # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray)368.640Img_name = STR (count).zfill(6) + '.jpg'
                cv2.imwrite(output_path + os.sep + img_name, image)
                count += 1Print (output_path + os.sep + img_name) #else:
        print('Start extracting', video_input, 'Video from the first', start_frame_index, 'Frame to first',end_frame_index,'Frame the picture!! ')
        k = end_frame_index - start_frame_index + 1
        while(k >= 0):
            times += 1
            k -= 1Res, image = cap.read() #if not res:
                print('Image extraction finished!! ')
                break
            if times % frame_frequency == 0: # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray) # picture_gray = cv2.cvtcolor (image, cv2.color_bgr2gray)368.640Img_name = STR (count).zfill(6) + '.jpg'
                cv2.imwrite(output_path + os.sep + img_name, image)
                count += 1Print (output_path + os.sep + img_name)'Image extraction finished!! ')
    cap.release()


if __name__ == "__main__": # Video_input = r'G:\zzzz2\001.mp4'Output_path = r'G:\zzzz2\output'# ExtractVideoFrame(video_input, output_path) # show video first100Frame image # ShowSpecialFrame(video_input,1500) # get the video first100The frame to the first200The picture of the frame # ExtractVideoBySpecialFrame (video_input output_path,100.200)
Copy the code