由于毕业设计的方向是深度学习中YOLOV5算法在于视频检测中的应用。
在这种需要训练数据集的深度学习中,数据集的采集是首要目标。
程序作用:1、反馈视频总帧数
2、根据所需要的图片数目生成相应数目的随机数字序号,进行提取对应帧数图片。
3、根据每次数目不同建立不同文件夹。
程序代码块:
# -*- coding:utf-8 -*-
"""
制作人:Albert
时间:2021.11.08
地点:海南热带海洋学院图书馆
"""
import os
from random import randint
from shutil import move
from cv2 import IMWRITE_JPEG_QUALITY, imwrite, VideoCapture, CAP_PROP_POS_FRAMES
import wxclass Frame(wx.Frame):def __init__(self, path=None, cap=None, frames_num=None, dlg=None):wx.Frame.__init__(self, None, title='月兔视频帧图片提取器', size=(372, 418), name='frame', style=541071360)icon = wx.Icon(r'D:\Users\lenovo\Downloads\y.png')self.SetIcon(icon)self.panel = wx.Panel(self)self.Centre()self.bq1 = wx.StaticText(self.panel, size=(235, 38), pos=(52, 56), label='可提取视频帧数目:', name='staticText',style=17)bq1_Font = wx.Font(12, 74, 90, 400, False, 'Microsoft YaHei UI', 28)self.bq1.SetFont(bq1_Font)self.bq2 = wx.StaticText(self.panel, size=(104, 23), pos=(41, 125), label='提取图片数目', name='staticText',style=2321)bq2_Font = wx.Font(11, 74, 90, 400, False, 'Microsoft YaHei UI', 28)self.bq2.SetFont(bq2_Font)self.bjk1 = wx.TextCtrl(self.panel, size=(104, 27), pos=(196, 121), value='', name='text', style=0)self.an1 = wx.Button(self.panel, size=(74, 29), pos=(41, 203), label='1.打开文件', name='button')self.an1.Bind(wx.EVT_BUTTON, self.an1_anbdj)self.an2 = wx.Button(self.panel, size=(88, 29), pos=(143, 203), label='2:选择保存路径', name='button')self.an2.Bind(wx.EVT_BUTTON, self.an2_anbdj)self.an3 = wx.Button(self.panel, size=(74, 29), pos=(263, 203), label='4:开始', name='button')self.an3.Bind(wx.EVT_BUTTON, self.an3_anbdj)self.path = pathself.cap = capself.frames_num = frames_numself.dlg = dlgdef an1_anbdj(self, event):wildcard = u"视频文件 (*.mp4)|*.mp4|" \"All files (*.*)|*.*"dlg = wx.FileDialog(self, message=u"选择视频文件",defaultDir=os.getcwd(),defaultFile="",wildcard=wildcard,style=wx.FD_OPEN)if dlg.ShowModal() == wx.ID_OK:self.path = dlg.GetPath()self.cap = VideoCapture(self.path)self.frames_num = self.cap.get(7)self.bq1.SetLabelText('可提取视频帧数目:{}'.format(int(self.frames_num)))def an2_anbdj(self, event):self.dlg = wx.DirDialog(self, u"选择保存文件夹", style=wx.DD_DEFAULT_STYLE)if self.dlg.ShowModal() == wx.ID_OK:os.chdir(self.dlg.GetPath())os.getcwd()def an3_anbdj(self, event):key_num = int(self.bjk1.GetValue())if key_num > self.frames_num:key_num = self.frames_numbq3 = wx.StaticText(self.panel, size=(257, 22),pos=(47, 276),label='下载进度',name='staticText',style=17)jdt3 = wx.Gauge(self.panel, range=int(key_num-1), size=(261, 39), pos=(47, 316), name='gauge', style=4)jdt3.SetValue(0)pirce = {}i = 0while len(pirce) < key_num:pirce[randint(0, self.frames_num)] = ii += 1k=1for x in pirce.keys():self.cap.set(CAP_PROP_POS_FRAMES, int(x))being, picture = self.cap.read()if being:imwrite('{}.jpg'.format(x), picture, [IMWRITE_JPEG_QUALITY, 100])jdt3.SetValue(k)bq3.SetLabelText('下载进度:{}/{}'.format(k,int(key_num)))k += 1sourcepath = os.getcwd()path = sourcepath + "/" + str(key_num)isExists = os.path.exists(path)if not isExists:os.makedirs(path)else:passsourcefiles = os.listdir(sourcepath)destinationpath = pathfor file in sourcefiles:if file.endswith('.jpg'):move(os.path.join(sourcepath, file), os.path.join(destinationpath, file))bq3.SetLabelText("下载进度: 当前任务完成")class myApp(wx.App):def OnInit(self):self.frame = Frame()self.frame.Show(True)return Trueif __name__ == '__main__':app = myApp()app.MainLoop()