欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3、python 视频帧转图片

程序员文章站 2024-03-25 22:50:46
...

背景:从视频中获取图片,进行数据标注,然后进行训练;

指定文件夹读取视频然后产生相应图片工具

# -*- coding: utf-8 -*-
import os
import cv2
import os.path
source_path=r'/home/ubuntu/video/'
destion_path=r'/home/ubuntu/image/'
rule=r'.mp4'
def get_files(path, rule):
	all = []
	for fpathe,dirs,fs in os.walk(path):
		for f in fs:
			filename = os.path.join(fpathe,f)
			if filename.endswith(rule): 
				all.append(filename)
	return all
b = get_files(source_path,rule)
for path_video in b:
	vc = cv2.VideoCapture(path_video)
	print (path_video)
	(filepath, tempfilename) = os.path.split(path_video)
	(filename, extension) = os.path.splitext(tempfilename)
	print (filename)
	c=0
	rval=vc.isOpened()
	while rval:
		c = c + 1
		rval, frame = vc.read()
		if rval:
			cv2.imwrite(destion_path+filename+str(c) + '.jpg', frame)
			img = cv2.imread(destion_path+filename+str(c) + '.jpg')			
			rows,cols,chanel = img.shape
			M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),180,1)
			dst = cv2.warpAffine(img,M,(cols,rows))
			cv2.imwrite(destion_path+filename+str(c) + '.jpg', dst)
			print(c)
		else:
			break
	vc.release()