To be honest, I think it's better to look for free tools. In an environment where you can't put free tools in your work or detour I make something similar every time, so I wrote it as a memorandum.
Video source as the first argument. → When accessing the webcam, specify the number 0-9. When accessing a video file, specify the video file with a relative or absolute path. The frame rate is the second argument. → Frame rate when displaying. Omission processing that only changes the waiting time of cv2.waitKey (). The reduction ratio is the third argument. → Specify when changing the size of the jpg file to be spit out. If you specify 2, the image will be 1/2 the width and height.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
video2jpg.py.
Usage:
video2jpg.py [<video source>] [<fps>] [<resize_rate>]
'''
import numpy as np
import cv2
import sys
print(__doc__)
try:
fn = sys.argv[1]
if fn.isdigit() == True:
fn = int(fn)
except:
fn = 0
print fn
try:
fps = sys.argv[2]
fps = int(fps)
except:
fps = 30
print fps
try:
resize_rate = sys.argv[3]
resize_rate = int(resize_rate)
except:
resize_rate = 1
print resize_rate
video_input = cv2.VideoCapture(fn)
if (video_input.isOpened() == False):
exit()
count = 0
while(True):
count += 1
count_padded = '%05d' % count
ret, frame = video_input.read()
height, width = frame.shape[:2]
small_frame = cv2.resize(frame, (int(width/resize_rate), int(height/resize_rate)))
cv2.imshow('frame', small_frame)
c = cv2.waitKey(int(1000/fps)) & 0xFF
write_file_name = count_padded + ".jpg "
cv2.imwrite(write_file_name, small_frame)
if c==27: # ESC
break
video_input.release()
cv2.destroyAllWindows()
Recommended Posts