Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog
- import numpy as np
- from PIL import Image, ImageTk
- from hand_translation import translation
- import cv2
- HEADER_FONT = ("Supermarket", 28, "bold")
- LARGE_FONT = ("Supermarket", 22)
- BTN_FONT = ("Supermarket", 18)
- class HandTranslatorApp(tk.Tk):
- def __init__(self, *args, **kwargs):
- tk.Tk.__init__(self, *args, **kwargs)
- container = tk.Frame(self)
- container.pack(side="top", fill="both", expand=True)
- container.grid_rowconfigure(0, weight=1)
- container.grid_columnconfigure(0, weight=1)
- self.state("zoomed")
- self.title("Hand Detection")
- self.frames = {}
- page = [VideoPage]
- for F in page:
- frame = F(container, self)
- self.frames[F] = frame
- frame.grid(row=0, column=0, sticky="nsew")
- self.show_frame(VideoPage)
- def show_frame(self, cont):
- frame = self.frames[cont]
- frame.tkraise()
- # Update -> Open window before do anything
- # frame.update()
- frame.run()
- class HomePage(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.parent = parent
- self.controller = controller
- how_to_text = """แนะนำ"""
- tk.Label(
- self,
- text="\nโปรแกรมแปลภาษามือ",
- font=HEADER_FONT,
- anchor="ne",
- justify="left",
- ).grid(row=0, column=1, sticky="N")
- tk.Label(self, text=how_to_text, font=LARGE_FONT, justify="left").grid(
- row=1, column=1, sticky="N"
- )
- self.grid_rowconfigure(0, weight=0)
- self.grid_rowconfigure(1, weight=0)
- self.grid_rowconfigure(2, weight=0)
- self.grid_columnconfigure(0, weight=1)
- self.grid_columnconfigure(1, weight=1)
- self.grid_columnconfigure(2, weight=1)
- text = "Video"
- tk_btn = tk.Button(self, text=text, font=BTN_FONT,
- command=self.go_to_vdopage)
- tk_btn.grid(row=3, column=1, sticky="N")
- text = "WebCam"
- tk_btn = tk.Button(self, text=text, font=BTN_FONT,
- command=self.go_to_vdopage)
- tk_btn.grid(row=4, column=1, sticky="N")
- def run(self):
- pass
- def go_to_homepage(self):
- self.controller.show_frame(HomePage)
- def go_to_vdopage(self):
- self.controller.show_frame(VideoPage)
- class VideoPage(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.parent = parent
- self.controller = controller
- default_img = np.zeros((540,960),np.uint8)
- self.original_image = Image.fromarray(default_img)
- self.original_image = ImageTk.PhotoImage(self.original_image)
- self.webcam = False
- self.grid_rowconfigure(0, weight=0)
- self.grid_rowconfigure(1, weight=0)
- self.grid_rowconfigure(2, weight=0)
- self.grid_rowconfigure(3, weight=0)
- self.grid_rowconfigure(4, weight=0)
- self.grid_columnconfigure(0, weight=1)
- self.grid_columnconfigure(1, weight=1)
- self.grid_columnconfigure(2, weight=1)
- self.grid_columnconfigure(3, weight=1)
- self.grid_columnconfigure(4, weight=1)
- tk.Label(
- self,
- text="\nโปรแกรมแปลภาษามือ",
- font=HEADER_FONT,
- # anchor="nwe",
- # justify="left",
- ).grid(row=0, column=2, sticky="NWE")
- self.image_panel = tk.Label(self, image=self.original_image)
- self.image_panel.image = self.original_image
- self.image_panel.grid(row=3, column=2, sticky="N")
- self.cap = None
- self.tk_txt = tk.Label(self, text=" Select input source\n", font=BTN_FONT)
- self.tk_txt.grid(row=1, column=1, sticky="NW")
- self.tk_txt = tk.Label(self, text="Display", font=HEADER_FONT)
- self.tk_txt.grid(row=1, column=2, sticky="NWE")
- self.tk_txt_res = tk.Label(self, text=" ผลการแปล:", font=HEADER_FONT)
- self.tk_txt_res.grid(row=2, column=2, sticky="NW")
- text = "Open video file"
- self.tk_btn = tk.Button(self, text=text, font=BTN_FONT,
- command=self.select_vdo)
- self.tk_btn.grid(row=2, column=1, sticky="NW")
- text = "Open webcam"
- self.tk_btn = tk.Button(self, text=text, font=BTN_FONT,
- command=self.start_webcam)
- self.tk_btn.grid(row=2, column=1, sticky="NE")
- text = "Stop video or webcam"
- self.tk_btn = tk.Button(self, text=text, font=BTN_FONT,
- command=self.stop)
- self.tk_btn.grid(row=3, column=1, sticky="NWE")
- print("init")
- self.update()
- def update(self):
- print("update")
- if self.cap is not None and self.cap.isOpened():
- ret, img = self.cap.read()
- if self.webcam:
- print("flip")
- img = cv2.flip(img,1)
- else:
- img = cv2.resize(img.copy(), (0,0), fx=0.5, fy=0.5)
- if ret:
- res = translation(img)
- # print(res)
- res = cv2.cvtColor(res,cv2.COLOR_BGR2RGB)
- res = Image.fromarray(res)
- res = ImageTk.PhotoImage(res)
- self.image_panel.configure(image=res)
- self.image_panel.image = res
- self.after(1, self.update)
- def select_vdo(self):
- path = filedialog.askopenfilename()
- if len(path) > 0:
- # self.vdo_path = path
- # print(self.vdo_path)
- self.start_video(path)
- self.webcam = False
- # try:
- # self.translate()
- # except:
- # pass
- def stop(self):
- self.cap.release()
- self.image_panel.configure(image=self.original_image)
- self.image_panel.image = self.original_image
- def run(self):
- print("run")
- pass
- def start_video(self, path):
- self.cap = cv2.VideoCapture(path)
- def start_webcam(self):
- self.cap = cv2.VideoCapture(0)
- self.webcam = True
- # def update_image(self, img):
- # self.image_panel.configure(image=img)
- # self.image_panel.image = img
- def go_to_homepage(self, event):
- self.controller.show_frame(HomePage)
- app = HandTranslatorApp()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement