import cv2
import sys
import os
import numpy as np
from tkinter import *
from tkinter.filedialog import *
from tkinter import filedialog
import threading
class WidgetsDemo:
def __init__(self):
window = Tk() # 创建一个窗口
#设置屏幕居中
sw=window.winfo_screenwidth()
sh=window.winfo_screenheight()
ww=400
wh=100
x = (sw-ww) / 2
y = (sh-wh) / 2
window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
window.title("Widgets Demo") # 设置标题
frame1 = Frame(window) # 创建一个框架
frame1.pack(side=TOP) # 将框架frame1放置在window中
self.label_1=Label(frame1,text = "视频路径:").grid(row = 0, column = 0)
self.path_1=StringVar()
self.entry_1=Entry(frame1, textvariable = self.path_1,width = 36)
self.entry_1.grid(row = 0, column = 1)
self.button_1=Button(frame1, text = "浏览", command = self.selectPath_1).grid(row = 0, column = 2)
self.label_2=Label(frame1,text = "图片路径:").grid(row = 1, column = 0)
self.path_2=StringVar()
self.entry_2=Entry(frame1, textvariable = self.path_2,width = 36)
self.entry_2.grid(row = 1, column = 1)
self.button_2=Button(frame1, text = "浏览", command = self.selectPath_2).grid(row = 1, column = 2)
frame2 = Frame(window) # 另外创建一个框架
frame2.pack() # 将框架frame2放置在window中
self.button_4=Button(frame2, text = "运行", width = 12,command = self.generate)
self.button_4.pack(side=TOP)
window.mainloop()
#设置显示路径
def selectPath_1(self):
path_ = filedialog.askopenfilename(title='打开文件', filetypes=[('视频', '*.MP4'), ('All Files', '*')])
self.path_1.set(path_)
def selectPath_2(self):
path_ = askdirectory()
self.path_2.set(path_)
def newthread(self):
#获取文件名
video_path=self.entry_1.get()
image_path=self.entry_2.get()
folder_name=video_path.split('/')[-1][:-4]
print(folder_name)
print(video_path)
video_capture = cv2.VideoCapture(video_path)
capture_count=1
capture_interval=3
frame_count=1
while True:
ret, frame = video_capture.read()
if ret==False:
break
else :
if(capture_count%capture_interval == 0):
print(image_path+folder_name+'_'+str(frame_count) + '.jpg')
cv2.imwrite(image_path+'/'+folder_name+'_'+str(frame_count) + '.jpg', frame)
frame_count += 1
capture_count += 1
video_capture.release()
cv2.destroyAllWindows()
print('采集完成')
def generate(self):
th=threading.Thread(target=self.newthread)
th.setDaemon(True)
th.start()
WidgetsDemo()