Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. import numpy as np
  4. from PIL import Image, ImageTk
  5. from hand_translation import translation
  6. import cv2
  7.  
  8. HEADER_FONT = ("Supermarket", 28, "bold")
  9. LARGE_FONT = ("Supermarket", 22)
  10. BTN_FONT = ("Supermarket", 18)
  11.  
  12.  
  13. class HandTranslatorApp(tk.Tk):
  14. def __init__(self, *args, **kwargs):
  15. tk.Tk.__init__(self, *args, **kwargs)
  16. container = tk.Frame(self)
  17. container.pack(side="top", fill="both", expand=True)
  18. container.grid_rowconfigure(0, weight=1)
  19. container.grid_columnconfigure(0, weight=1)
  20.  
  21. self.state("zoomed")
  22. self.title("Hand Detection")
  23. self.frames = {}
  24.  
  25. page = [VideoPage]
  26. for F in page:
  27. frame = F(container, self)
  28. self.frames[F] = frame
  29. frame.grid(row=0, column=0, sticky="nsew")
  30. self.show_frame(VideoPage)
  31.  
  32. def show_frame(self, cont):
  33. frame = self.frames[cont]
  34. frame.tkraise()
  35. # Update -> Open window before do anything
  36. # frame.update()
  37. frame.run()
  38.  
  39.  
  40. class HomePage(tk.Frame):
  41. def __init__(self, parent, controller):
  42. tk.Frame.__init__(self, parent)
  43.  
  44. self.parent = parent
  45. self.controller = controller
  46.  
  47. how_to_text = """แนะนำ"""
  48.  
  49. tk.Label(
  50. self,
  51. text="\nโปรแกรมแปลภาษามือ",
  52. font=HEADER_FONT,
  53. anchor="ne",
  54. justify="left",
  55. ).grid(row=0, column=1, sticky="N")
  56.  
  57. tk.Label(self, text=how_to_text, font=LARGE_FONT, justify="left").grid(
  58. row=1, column=1, sticky="N"
  59. )
  60.  
  61. self.grid_rowconfigure(0, weight=0)
  62. self.grid_rowconfigure(1, weight=0)
  63. self.grid_rowconfigure(2, weight=0)
  64.  
  65. self.grid_columnconfigure(0, weight=1)
  66. self.grid_columnconfigure(1, weight=1)
  67. self.grid_columnconfigure(2, weight=1)
  68.  
  69. text = "Video"
  70. tk_btn = tk.Button(self, text=text, font=BTN_FONT,
  71. command=self.go_to_vdopage)
  72. tk_btn.grid(row=3, column=1, sticky="N")
  73.  
  74. text = "WebCam"
  75. tk_btn = tk.Button(self, text=text, font=BTN_FONT,
  76. command=self.go_to_vdopage)
  77. tk_btn.grid(row=4, column=1, sticky="N")
  78.  
  79. def run(self):
  80. pass
  81.  
  82. def go_to_homepage(self):
  83. self.controller.show_frame(HomePage)
  84.  
  85. def go_to_vdopage(self):
  86. self.controller.show_frame(VideoPage)
  87.  
  88. class VideoPage(tk.Frame):
  89. def __init__(self, parent, controller):
  90. tk.Frame.__init__(self, parent)
  91.  
  92. self.parent = parent
  93. self.controller = controller
  94. default_img = np.zeros((540,960),np.uint8)
  95. self.original_image = Image.fromarray(default_img)
  96. self.original_image = ImageTk.PhotoImage(self.original_image)
  97.  
  98.  
  99.  
  100.  
  101. self.webcam = False
  102.  
  103. self.grid_rowconfigure(0, weight=0)
  104. self.grid_rowconfigure(1, weight=0)
  105. self.grid_rowconfigure(2, weight=0)
  106. self.grid_rowconfigure(3, weight=0)
  107. self.grid_rowconfigure(4, weight=0)
  108.  
  109. self.grid_columnconfigure(0, weight=1)
  110. self.grid_columnconfigure(1, weight=1)
  111. self.grid_columnconfigure(2, weight=1)
  112. self.grid_columnconfigure(3, weight=1)
  113. self.grid_columnconfigure(4, weight=1)
  114.  
  115.  
  116. tk.Label(
  117. self,
  118. text="\nโปรแกรมแปลภาษามือ",
  119. font=HEADER_FONT,
  120. # anchor="nwe",
  121. # justify="left",
  122. ).grid(row=0, column=2, sticky="NWE")
  123.  
  124. self.image_panel = tk.Label(self, image=self.original_image)
  125. self.image_panel.image = self.original_image
  126. self.image_panel.grid(row=3, column=2, sticky="N")
  127.  
  128. self.cap = None
  129.  
  130. self.tk_txt = tk.Label(self, text=" Select input source\n", font=BTN_FONT)
  131. self.tk_txt.grid(row=1, column=1, sticky="NW")
  132.  
  133. self.tk_txt = tk.Label(self, text="Display", font=HEADER_FONT)
  134. self.tk_txt.grid(row=1, column=2, sticky="NWE")
  135.  
  136.  
  137. self.tk_txt_res = tk.Label(self, text=" ผลการแปล:", font=HEADER_FONT)
  138. self.tk_txt_res.grid(row=2, column=2, sticky="NW")
  139.  
  140. text = "Open video file"
  141. self.tk_btn = tk.Button(self, text=text, font=BTN_FONT,
  142. command=self.select_vdo)
  143. self.tk_btn.grid(row=2, column=1, sticky="NW")
  144.  
  145. text = "Open webcam"
  146. self.tk_btn = tk.Button(self, text=text, font=BTN_FONT,
  147. command=self.start_webcam)
  148. self.tk_btn.grid(row=2, column=1, sticky="NE")
  149.  
  150. text = "Stop video or webcam"
  151. self.tk_btn = tk.Button(self, text=text, font=BTN_FONT,
  152. command=self.stop)
  153. self.tk_btn.grid(row=3, column=1, sticky="NWE")
  154.  
  155. print("init")
  156. self.update()
  157.  
  158. def update(self):
  159. print("update")
  160. if self.cap is not None and self.cap.isOpened():
  161. ret, img = self.cap.read()
  162. if self.webcam:
  163. print("flip")
  164. img = cv2.flip(img,1)
  165. else:
  166. img = cv2.resize(img.copy(), (0,0), fx=0.5, fy=0.5)
  167. if ret:
  168. res = translation(img)
  169. # print(res)
  170. res = cv2.cvtColor(res,cv2.COLOR_BGR2RGB)
  171. res = Image.fromarray(res)
  172. res = ImageTk.PhotoImage(res)
  173. self.image_panel.configure(image=res)
  174. self.image_panel.image = res
  175.  
  176. self.after(1, self.update)
  177.  
  178. def select_vdo(self):
  179. path = filedialog.askopenfilename()
  180. if len(path) > 0:
  181. # self.vdo_path = path
  182. # print(self.vdo_path)
  183. self.start_video(path)
  184. self.webcam = False
  185. # try:
  186. # self.translate()
  187. # except:
  188. # pass
  189. def stop(self):
  190. self.cap.release()
  191. self.image_panel.configure(image=self.original_image)
  192. self.image_panel.image = self.original_image
  193.  
  194.  
  195. def run(self):
  196. print("run")
  197. pass
  198.  
  199. def start_video(self, path):
  200. self.cap = cv2.VideoCapture(path)
  201.  
  202.  
  203. def start_webcam(self):
  204. self.cap = cv2.VideoCapture(0)
  205. self.webcam = True
  206.  
  207.  
  208.  
  209.  
  210. # def update_image(self, img):
  211. # self.image_panel.configure(image=img)
  212. # self.image_panel.image = img
  213.  
  214. def go_to_homepage(self, event):
  215. self.controller.show_frame(HomePage)
  216.  
  217. app = HandTranslatorApp()
  218. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement