import subprocess
# 导入所需要的库
import cv2
import numpy as np
import os
# 定义保存图片函数
# image:要保存的图片名字
# addr;图片地址与相片名字的前部分
# num: 相片,名字的后缀。int 类型
def save_image(image, addr, num):
address = addr + str(num) + '.jpg'
cv2.imwrite(address, image)
# 通过摄像头的方式
# videoCapture=cv2.VideoCapture(1)
# 找mp4
def traverse(f):
fs = os.listdir(f)
i = 0
timeF = 25
j = 0
for f1 in fs:
tmp_path = os.path.join(f, f1)
#后缀名
hou = os.path.splitext(tmp_path)[1]
if hou in [".mp4"]:
# 读取视频文件
videoCapture = cv2.VideoCapture(tmp_path)
# 读帧
success, frame = videoCapture.read()
while success:
i = i + 1
if (i % timeF == 0):
j = j + 1
save_image(frame, 'E:/darknet-master1/build/darknet/x64/picture/', j)
success, frame = videoCapture.read()
print('文件: %s' % tmp_path)
#总的把图片文件按顺序写入txt
def save_pic():
path = 'E:/darknet-master1/build/darknet/x64/'
traverse(path)
input_image_list_txt=open('E:/darknet-master1/build/darknet/x64/input_image_list.txt','w')
fs1 = os.listdir("E:/darknet-master1/build/darknet/x64/picture/")
fs1.sort(key=lambda x: int(x.split('.')[0]))//按顺序排序
for f11 in fs1:
tmp_path = os.path.join("E:/darknet-master1/build/darknet/x64/picture/", f11)
input_image_list_txt.write(tmp_path+'\n')
save_pic() //下方使用subprocess时必须把这个封装成函数,否则不写入txt
python把MP4分成图片并“按序”写入txt
最新推荐文章于 2022-10-14 21:53:57 发布