python读取视频并输出图片帧信息

代码如下:

from __future__ import print_function
import cv2 as cv
import argparse
import os
#output
parser = argparse.ArgumentParser(description='This program shows how to use background subtraction methods provided by \
                                              OpenCV. You can process both videos and images.')
#parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='vtest.avi')
parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='20210528154957812.h264')
parser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='MOG2')
args = parser.parse_args()
if args.algo == 'MOG2':
    backSub = cv.createBackgroundSubtractorMOG2()
else:
    backSub = cv.createBackgroundSubtractorKNN()

capture = cv.VideoCapture(cv.samples.findFileOrKeep(args.input))

if not capture.isOpened():
    print('Unable to open: ' + args.input)
    exit(0)

WIDTH = capture.get(cv.CAP_PROP_FRAME_WIDTH)
HEIGHT = capture.get(cv.CAP_PROP_FRAME_HEIGHT)

while True:
    ret, frame = capture.read()
    if frame is None:
        break
    
    fgMask = backSub.apply(frame)
    index = str(int(capture.get(cv.CAP_PROP_POS_FRAMES)))
    print('当前图片序号', index)
    '''
    cv.rectangle(frame, (10, 2), (300, 120), (255,255,255), -1)
    cv.putText(frame, index, (15, 100),
               cv.FONT_HERSHEY_COMPLEX, 2, (0, 0, 255))
    '''

    cv.namedWindow('Frame',0)
    cv.resizeWindow('Frame', int(WIDTH/2), int(HEIGHT/2))
    cv.imshow('Frame', frame)

    #cv.imwrite(os.path.join(path, index+'.jpg'), frame)
    cv.imwrite(index+'.jpg', frame)

    '''
    cv.namedWindow('FG Mask',0)
    cv.resizeWindow('FG Mask', 640, 480)
    cv.imshow('FG Mask', fgMask)
    '''
    keyboard = cv.waitKey(30)
    if keyboard == 'q' or keyboard == 27:
        break

在下面这句话的default处修改文件名称,并保证源视频和程序代码在一个文件夹之内即可

parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='20210528154957812.h264')
```![在这里插入图片描述](https://img-blog.csdnimg.cn/7adef51db37d4de38244641659f50a2d.png)

你可能感兴趣的:(python,python,opencv,视频处理)