-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
106 lines (88 loc) · 4.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os.path
import shutil
import tkinter as tk
from tkinter import *
from tkinter.filedialog import askopenfilename, askdirectory
from tkinter import messagebox
from tkinter import font
import cv2
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.video_file_path = tk.StringVar()
self.pic_dir_path = tk.StringVar()
self.interval = tk.StringVar(value="10")
self.btn_text = tk.StringVar(value="")
# 设置窗口大小
master.geometry('520x220')
master.title("视频抽帧")
self.initial_frame()
def initial_frame(self):
video_file_frame = Frame(self)
Label(video_file_frame, text="请选择视频文件:", width=14, anchor="e").grid(row=0, column=0, padx=3)
Button(video_file_frame, text="请选择...", command=self.select_video_file).grid(row=0, column=1)
dir_path_frame = Frame(self)
Label(dir_path_frame, text="请选择图片存储路径:", width=14, anchor="e").grid(row=1, column=0, padx=3)
Button(dir_path_frame, text="请选择...", command=self.select_pic_dir).grid(row=1, column=1)
video_file_frame.grid(pady=6)
dir_path_frame.grid(pady=6)
tips_frame = Frame(self)
Label(tips_frame, text="输入图片保存的间隔:", width=16, anchor="e").grid(row=1, column=0)
Entry(tips_frame, text=self.interval, width=8).grid(row=1, column=1)
tips_frame.grid(pady=6)
Button(self, text="开始抽帧", command=self.start_frame_draw).grid(pady=6)
Label(self, textvariable=self.btn_text, fg="red", font=font.Font(size=16)).grid(pady=6)
# 选择视频文件方法
def select_video_file(self):
self.video_file_path = askopenfilename(title=u'选择视频文件')
file_name_lists = str(self.video_file_path).split("/")
messagebox.showinfo("文件提示", "选择的文件为 %s" % list(file_name_lists)[-1])
# 选择文件夹
def select_pic_dir(self):
self.pic_dir_path = askdirectory(title=u'选择图片存放路径')
messagebox.showinfo("目录提示", "选择的存储目录为 %s" % self.pic_dir_path)
# 开始抽帧程序
def start_frame_draw(self):
interval = str(self.interval.get())
video_file = str(self.video_file_path)
pic_dir_path = str(self.pic_dir_path)
if len(video_file) == 0 or video_file == 'PY_VAR4' or video_file == "None":
messagebox.showerror("警告", "还没有选择视频文件")
return
if len(pic_dir_path) == 0 or pic_dir_path == "PY_VAR5" or pic_dir_path == 'None':
messagebox.showerror("警告", "还没有选择图片的存放路径")
return
if len(interval) == 0:
messagebox.showerror("警告", "你还没有输入视频抽帧的间隔")
return
self.btn_text.set("抽帧中...")
self.get_video_frame(video_file, pic_dir_path, interval)
def get_video_frame(self, video_path, dir_path, interval):
# 获取视频名称
video_name = video_path.split("/")[-1].split(".")[0]
shutil.rmtree(dir_path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
else:
os.makedirs(dir_path)
video_capture = cv2.VideoCapture(video_path)
i = 0
count = 0
while True:
success, frame = video_capture.read()
i += 1
if i % int(interval) == 0:
count += 1
pic_path = "{}/{}_{}.jpg".format(dir_path, video_name, str(i))
cv2.imencode(".jpg", frame)[1].tofile(pic_path)
if not success:
self.btn_text.set("程序已完成抽帧,请前往目录查看")
self.video_file_path = tk.StringVar().set("")
self.pic_dir_path = tk.StringVar().set("")
break
if __name__ == '__main__':
root = tk.Tk()
app = Application(master=root)
app.mainloop()