Python脚本之视频抽帧及GUI界面

项目要用到一个简单的抽帧GUI界面,自己动手改了改
用到wxpython

程序

直接运行就行

# -*- coding:utf-8 -*-
import os
from random import randint
from shutil import move
from traceback import print_tb
from cv2 import IMWRITE_JPEG_QUALITY, imwrite, VideoCapture, CAP_PROP_POS_FRAMES, waitKey
import wx
 

class Frame(wx.Frame):
    def __init__(self, path=None, cap=None, frames_num=None, dlg=None,size=(700,300)):
        wx.Frame.__init__(self, None, title='LabelPoint视频抽帧提取器',  name='frame')
        #icon = wx.Icon(r"C:\Users\YuQing\Desktop\logo2.png")
        #self.SetIcon(icon)
        self.SetSize(size)
        self.panel = wx.Panel(self)
        self.Centre()
        
        self.bq1 = wx.StaticText(self.panel, label='可提取视频帧数目:', name='staticText',)
        bq1_Font = wx.Font(12, 74, 90, 400, False, 'Microsoft YaHei UI', 15)
        self.bq1.SetFont(bq1_Font)


        self.bq2 = wx.StaticText(self.panel, label='抽帧间隔:', name='staticText')
        bq2_Font = wx.Font(12, 74, 90, 400, False, 'Microsoft YaHei UI', 15)
        self.bq2.SetFont(bq2_Font)

        self.bq3 = wx.StaticText(self.panel, label='提取进度',name='staticText')
        bq3_Font = wx.Font(12, 74, 90, 400, False, 'Microsoft YaHei UI', 15)
        self.bq2.SetFont(bq3_Font)
        self.jdt3 = wx.Gauge(self.panel,  name='gauge', style=4)
        
        
        self.bjk1 = wx.TextCtrl(self.panel, value='', name='text', style=0)
        self.an1 = wx.Button(self.panel, label='1.打开文件', name='button')
        self.an1.Bind(wx.EVT_BUTTON, self.an1_anbdj)
    
        self.an2 = wx.Button(self.panel, label='2:选择保存路径', name='button')
        self.an2.Bind(wx.EVT_BUTTON, self.an2_anbdj)


        self.an3 = wx.Button(self.panel, label='4:开始', name='button')
        self.an3.Bind(wx.EVT_BUTTON, self.an3_anbdj)

        self.box1=wx.BoxSizer()#定义横向的box1
        self.box1.Add(self.bq1, proportion=10, flag=wx.EXPAND | wx.ALL,border=5)

        self.box2=wx.BoxSizer()
        self.box2.Add(self.bq2, proportion=5, flag=wx.EXPAND | wx.ALL,border=5)
        self.box2.Add(self.bjk1, proportion=5, flag=wx.EXPAND | wx.ALL,border=5)

        self.box3=wx.BoxSizer()
        self.box3.Add(self.an1, proportion=3.5, flag=wx.EXPAND | wx.ALL,border=5)
        self.box3.Add(self.an2, proportion=3.5,flag=wx.EXPAND | wx.ALL,border=5)
        self.box3.Add(self.an3, proportion=4,flag=wx.EXPAND | wx.ALL,border=5)

        self.box4=wx.BoxSizer()
        self.box4.Add(self.bq3, proportion=5, flag=wx.EXPAND | wx.ALL,border=5)
        self.box4.Add(self.jdt3, proportion=5, flag=wx.EXPAND | wx.ALL,border=5)

        self.v_box=wx.BoxSizer(wx.VERTICAL)#定义一个纵向的v_box
        self.v_box.Add(self.box1, proportion=3,flag=wx.EXPAND | wx.ALL,border=5)
        self.v_box.Add(self.box2, proportion=1,flag=wx.EXPAND | wx.ALL,border=5)
        self.v_box.Add(self.box3, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        self.v_box.Add(self.box4, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)

        self.path = path
        self.cap = cap
        self.frames_num = frames_num
        self.dlg = dlg
        self.panel.SetSizer(self.v_box)
 
    def 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()
        #  获取视频名字:str(self.path.split("\\")[-1].split(".")[0])
        self.cap = VideoCapture(self.path)

        self.frames_num = self.cap.get(7)
        self.bq1.SetLabelText('可提取视频帧数目:{}'.format(int(self.frames_num)))

        if self.frames_num==0:
            wx.MessageBox("您没有选择视频,请选择!", "确认", wx.OK|wx.ICON_QUESTION)
            #self.Destroy()
        else:
            wx.MessageBox("您选择的视频是:%s" % self.path, "确认", wx.OK|wx.ICON_QUESTION)

         
    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())
            K = 1
        else:
            K = 0
        os.getcwd()
        if K == 0:
            wx.MessageBox("您没有选择文件夹,请选择!", "确认", wx.OK|wx.ICON_QUESTION)
        else :
            wx.MessageBox("您选择的路径是:%s" % self.dlg.GetPath(), "确认", wx.OK|wx.ICON_QUESTION)


        #r = wx.MessageBox("您已完成选择保存文件夹操作", "确认", wx.CANCEL|wx.OK|wx.ICON_QUESTION)
        #if r == wx.ID_OK:
        #    self.Destroy()
 
    def an3_anbdj(self, event):
        key_num = int(int(self.frames_num) / int(self.bjk1.GetValue()))
        if key_num > self.frames_num:
            key_num = self.frames_num
        #bq3 = 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)
        self.jdt3.Range=int(key_num-1)
        self.jdt3.SetValue(0)
        pirce = {}
        i = 0
        while len(pirce) < key_num:
            pirce[randint(0, self.frames_num)] = i
            i += 1
        k=1
        for 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])
                self.jdt3.SetValue(k)
                self.bq3.SetLabelText('下载进度:{}/{}'.format(k,int(key_num)))
                k += 1
                waitKey(1)
 
        sourcepath = os.getcwd()
        #path = sourcepath + "/" + str(key_num)
        path = sourcepath + "/" + str(self.path.split("\\")[-1].split(".")[0])
        #print(path)
        isExists = os.path.exists(path)
        if not isExists:
            os.makedirs(path)
        else:
            pass
        sourcefiles = os.listdir(sourcepath)
        destinationpath = path
        for file in sourcefiles:
            if file.endswith('.jpg'):
                move(os.path.join(sourcepath, file), os.path.join(destinationpath, file))
        self.bq3.SetLabelText("下载进度:       !!!!当前任务完成!!!!")
        r = wx.MessageBox("您已完成视频抽帧操作,点击确定退出!!!", "确定", wx.OK|wx.ICON_QUESTION)
        #if r == wx.ID_OK:
        self.Destroy()

### 程序

class myApp(wx.App):
    def OnInit(self):
        self.frame = Frame()
        self.frame.Show(True)
        return True
 
 
if __name__ == '__main__':
    app = myApp()
    app.MainLoop()


参考程序

这是让窗口改变尺寸的参考程序

import wx
 
class ChatFrame(wx.Frame):
    def __init__(self, parent, id, title, size):
        # 初始化,添加控件并绑定事件
        wx.Frame.__init__(self, parent, id, title)
        self.SetSize(size)#设置对话框的大小
        self.Center()#设置弹窗在屏幕中间               
        
        #使用尺寸器改写,改写后拉大或者缩小窗口,中间的控件会随着窗口的大小已固定的尺寸而改变
        panel=wx.Panel(self)#self表示实例即ChatFrame,创建一个面板
        #定义panel中的元件
        self.receiveLabel = wx.StaticText(panel,label="Receive Msg")
        self.sendLabel = wx.StaticText(panel,label="Send Msg")
        self.noticeLabel = wx.StaticText(panel,label="Notice")
        self.chatFrame1 = wx.TextCtrl(panel,style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_LEFT)
        self.chatFrame2 = wx.TextCtrl(panel,style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RIGHT)
        self.noticeFrame = wx.TextCtrl(panel,style=wx.TE_MULTILINE | wx.TE_READONLY )
        self.message = wx.TextCtrl(panel,value='input message')#设置发送消息的文本输入框的位置和尺寸
        self.toUser=wx.TextCtrl(panel,value='input username')#设置指定用户的文本输入框的位置和尺寸
        self.sendButton = wx.Button(panel, label="Send")
        self.sendDesignButton = wx.Button(panel, label="SendDesign")  
        self.closeButton = wx.Button(panel, label="Close")
        self.usersButton = wx.Button(panel, label="Online")
        
        self.box1=wx.BoxSizer()#定义横向的box1
        self.box1.Add(self.receiveLabel, proportion=4, flag=wx.EXPAND | wx.ALL,border=5)#该元素占box1的比例为40%,方式为伸缩,边界为5
        self.box1.Add(self.sendLabel, proportion=4,flag=wx.EXPAND | wx.ALL,border=5)
        self.box1.Add(self.noticeLabel, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        
        self.box2=wx.BoxSizer()#定义横向的box2
        self.box2.Add(self.chatFrame1, proportion=4,flag=wx.EXPAND | wx.ALL,border=5)
        self.box2.Add(self.chatFrame2, proportion=4,flag=wx.EXPAND | wx.ALL,border=5)
        self.box2.Add(self.noticeFrame, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        
        self.box3=wx.BoxSizer()#定义横向的box3
        self.box3.Add(self.message, proportion=6, flag=wx.EXPAND | wx.ALL,border=5)
        self.box3.Add(self.sendButton, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        self.box3.Add(self.usersButton, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        
        self.box4=wx.BoxSizer()#定义横向的box3
        self.box4.Add(self.toUser, proportion=6, flag=wx.EXPAND | wx.ALL,border=5)
        self.box4.Add(self.sendDesignButton, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        self.box4.Add(self.closeButton, proportion=2,flag=wx.EXPAND | wx.ALL,border=5)
        
        self.v_box=wx.BoxSizer(wx.VERTICAL)#定义一个纵向的v_box
        self.v_box.Add(self.box1, proportion=1,flag=wx.EXPAND | wx.ALL,border=5)#添加box1,比例为1
        self.v_box.Add(self.box2, proportion=7,flag=wx.EXPAND | wx.ALL,border=5)#添加box2,比例为7
        self.v_box.Add(self.box3, proportion=1,flag=wx.EXPAND | wx.ALL,border=5)#添加box3,比例为1
        self.v_box.Add(self.box4, proportion=1,flag=wx.EXPAND | wx.ALL,border=5)#添加box4,比例为1
        
        panel.SetSizer(self.v_box)                       
        self.Show()
 
if __name__ == '__main__':
    app = wx.App()#实例化一个主循环
    ChatFrame(None, -1, title="ShiYanLou Chat Client", size=(780,500))
    app.MainLoop()#启动主循环
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值