-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
208 lines (167 loc) · 8.27 KB
/
GUI.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import tkinter as tk
from tkinter import filedialog
import os
import pandas as pd
from PIL import Image,ImageTk
import subprocess
import cv2
import shutil
import tkinter.messagebox as tkm
from demoYUCmd import runDemo
import tkinter.font as tkFont
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.flag = 0
self.create_widgets()
self.tmpVidInPath = '/tmp/vidIn.mp4'
self.tmpImgInPath = '/tmp/imgIn'
def start(self):
self.flag = 1
self.video_get_In()
def stop(self):
self.flag = 0 if self.flag == 1 else 1
self.video_loop_In()
def start_res(self):
self.flag = 1
self.video_get_Res()
def stop_res(self):
self.flag = 0 if self.flag == 1 else 1
self.video_loop_Res()
def create_widgets(self):
self.fileModel = tk.StringVar()
self.getModel_bt = tk.Button(self.master, width=15, height=1,
text="Open your model file", command=self._getModel)
self.getModel_bt.grid(row=0, column=0, sticky=tk.W)
self.modelPathText = tk.Label(self.master, width=60, height=1, text='model path', anchor=tk.NW)
self.modelPathText.grid(row=1, column=0, sticky=tk.W)
self.outputPath = tk.StringVar()
self.getOutput_bt = tk.Button(self.master, width=15, height=1, text="Open your output file",
state='disable', command=self._getOutFile)
self.getOutput_bt.grid(row=2, column=0, sticky=tk.W)
self.outPathText = tk.Label(self.master, width=50, height=1, text='Output path', anchor=tk.NW)
self.outPathText.grid(row=3, column=0, sticky=tk.W)
self.fileImgPath = tk.StringVar()
self.getImg_bt = tk.Button(self.master, width=15, height=1, text="Open your image path", command=self._getFile, state='disable')
self.getImg_bt.grid(row=4, column=0, sticky=tk.W)
self.imgPathText = tk.Label(self.master, width = 50, height = 1, text='image path', anchor=tk.NW)
self.imgPathText.grid(row=5, column=0, sticky=tk.W)
self.movieLabel = tk.Label(self.master,text='Input video')
self.movieLabel.grid(row=6, column=0, sticky=tk.W)
self.start_button = tk.Button(self.master, text='Restart', width=20, command=self.start, state = 'disable')
self.start_button.grid(row=7, column=0, sticky=tk.W)
self.stop_button = tk.Button(self.master, text='Pause/Resume', width=20, command=self.stop, state = 'disable')
self.stop_button.grid(row=8, column=0, sticky=tk.W)
self.run_button = tk.Button(self.master, text='Run model on input', width=20, command=self.runOnInput, state = 'disable')
self.run_button.grid(row=4, column=1, sticky=tk.W)
self.movie3DLabel = tk.Label(self.master, text='Output video')
self.movie3DLabel.grid(row=6, column=1, sticky=tk.W)
self.start_button_res = tk.Button(self.master, text='Restart', width=20, command=self.start_res,
state='disable')
self.start_button_res.grid(row=7, column=1, sticky=tk.W)
self.stop_button_res = tk.Button(self.master, text='Pause/Resume', width=20, command=self.stop_res,
state='disable')
self.stop_button_res.grid(row=8, column=1, sticky=tk.W)
f1 = tkFont.Font(family='times', size=20, weight='bold')
self.statusText = tk.Label(self.master, width=60, height=1, text='Step1: Select pretrained model', anchor=tk.NW, font=f1)
self.statusText.grid(row=0, column=1, sticky=tk.W, rowspan=2)
def runOnInput(self):
self.statusText['text'] = 'Running! Please wait for the result!'
self.statusText.update()
runDemo(self.fileImgPath, self.outputPath, self.fileModel)
self.outputVid = os.path.join(self.outputPath, 'vibe_result.mp4')
self.video_get_Res()
self.start_button_res['state'] = 'normal'
self.stop_button_res['state'] = 'normal'
self.statusText['text'] = 'Done!'
self.statusText.update()
def _getOutFile(self):
default_dir = r""
self.outputPath = tk.filedialog.askdirectory(title=u'select your file', initialdir=(os.path.expanduser(default_dir)))
self.outPathText['text'] = self.outputPath
self.outPathText.update()
self.getImg_bt['state'] = 'normal'
self.getImg_bt.update()
self.statusText['text'] = 'Step3: Select image path'
self.statusText.update()
def _getFile(self):
default_dir = r""
self.fileImgPath = tk.filedialog.askdirectory(title=u'select your file', initialdir=(os.path.expanduser(default_dir)))
self.imgPathText['text'] = self.fileImgPath
self.imgPathText.update()
# tkmBox = tkm.showwarning('Warning', 'Processing images for visualization! \nPlease wait!')
if os.path.exists(self.tmpImgInPath):
shutil.rmtree(self.tmpImgInPath)
if os.path.exists(self.tmpVidInPath):
os.remove(self.tmpVidInPath)
os.mkdir(self.tmpImgInPath)
self.statusText['text'] = 'Preprocessing, please wait!'
self.statusText.update()
for ind, file in enumerate(os.listdir(self.fileImgPath)):
img = cv2.imread(os.path.join(self.fileImgPath, file))
tgtfile = os.path.join(self.tmpImgInPath, str(ind).zfill(6) + '.png')
cv2.imwrite(tgtfile, img)
self._genVid()
self.start_button['state'] = 'normal'
self.start_button.update()
self.stop_button['state'] = 'normal'
self.stop_button.update()
self.run_button['state'] = 'normal'
self.run_button.update()
self.video_get_In()
self.statusText['text'] = 'Step4: Run the model!'
self.statusText.update()
def _getModel(self):
default_dir = r""
self.fileModel = tk.filedialog.askopenfilename(title=u'select your file',initialdir=(os.path.expanduser(default_dir)))
self.modelPathText['text'] = self.fileModel
self.modelPathText.update()
self.getOutput_bt['state'] = 'normal'
self.getOutput_bt.update()
self.statusText['text'] = 'Step2: Select output path'
self.statusText.update()
def _genVid(self):
command = [
'ffmpeg', '-y', '-threads', '16', '-i', f'{self.tmpImgInPath}/%06d.png', '-profile:v', 'baseline',
'-level', '3.0', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-an', '-v', 'error', self.tmpVidInPath,
]
print(f'Running \"{" ".join(command)}\"')
subprocess.call(command)
print('Done')
def video_get_In(self):
self.capIn = cv2.VideoCapture(self.tmpVidInPath) # 获取视频
self.wait_time = 500 / self.capIn.get(5) # 视频频率
self.video_loop_In()
def video_loop_In(self):
ret, frame = self.capIn.read() # 读取照片
if ret:
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
current_image = Image.fromarray(img).resize((480, 270)) # 将图像转换成Image对象
imgtk = ImageTk.PhotoImage(image=current_image)
self.movieLabel.imgtk = imgtk
self.movieLabel.config(image=imgtk)
self.movieLabel.update()
if self.flag == 1:
self.master.after(int(self.wait_time), lambda: self.video_loop_In())
def video_get_Res(self):
self.capRes = cv2.VideoCapture(self.outputVid) # 获取视频
self.wait_time = 500 / self.capRes.get(5) # 视频频率
self.video_loop_Res()
def video_loop_Res(self):
ret, frame = self.capRes.read() # 读取照片
if ret:
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
current_image = Image.fromarray(img).resize((480, 270)) # 将图像转换成Image对象
imgtk = ImageTk.PhotoImage(image=current_image)
self.movie3DLabel.imgtk = imgtk
self.movie3DLabel.config(image=imgtk)
self.movie3DLabel.update()
if self.flag == 1:
self.master.after(int(self.wait_time), lambda: self.video_loop_Res())
root = tk.Tk()
root.title("End-to-end 3D human shape")
root.geometry("1000x600+50+50")
root.grid_columnconfigure(0, minsize=500) # Here
app = Application(master=root)
app.mainloop()