把视频裁剪成图片Python

把视频裁剪成图片Python

在做数据集时往往需要将视频裁剪成图片,该代码可以完成此功能。


```python
import cv2
input_movie = cv2.VideoCapture("./model1/Rec 0008.mp4") #导入要裁剪的视频
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
 
#pre_file = '12_28_62_'
#f_index = 0
n = 3697 
while True:
    # Grab a single frame of video
    ret, frame = input_movie.read()
 
    # Quit when the input video file ends
    if not ret:
        break
    cv2.imshow('mouth',frame)
    n=n+1
    if n%2==0:    #可以设置间隔几帧保存图片
        cv2.imwrite('./model1/images'+str(n)+'.jpg',frame)
    key = cv2.waitKey(1)
    
 #   frame_resize = cv2.resize(frame, (640, 360), interpolation=cv2.INTER_AREA)
# All done!
input_movie.release()
cv2.destroyAllWindows()


猜你喜欢

转载自blog.csdn.net/gaoqing_dream163/article/details/109490597
今日推荐