2021-01-07 20:25:38 +01:00
|
|
|
import PIL.Image
|
|
|
|
import PIL.ImageTk
|
2021-01-07 18:35:51 +01:00
|
|
|
import tkinter as tk
|
2021-01-16 15:04:34 +01:00
|
|
|
import os
|
2021-01-17 14:16:01 +01:00
|
|
|
import cv2
|
2021-01-07 18:35:51 +01:00
|
|
|
|
2021-01-16 15:04:34 +01:00
|
|
|
from constants import PROJECT_VIEW_NAME, FONT, PROJECTS_VIEW_NAME
|
2021-01-07 20:25:38 +01:00
|
|
|
from gestures.gesture_recognition import MyVideoCapture
|
2021-01-07 18:35:51 +01:00
|
|
|
from views.abstract_view import AbstractView
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectView(tk.Frame, AbstractView):
|
|
|
|
|
|
|
|
def __init__(self, parent, controller, main_view_controller):
|
|
|
|
tk.Frame.__init__(self, parent)
|
|
|
|
self.token = ''
|
2021-01-07 20:25:38 +01:00
|
|
|
self.window = controller
|
|
|
|
self.main_view_controller = main_view_controller
|
|
|
|
self.delay = 20
|
2021-01-16 15:04:34 +01:00
|
|
|
self.canvas_width = 800
|
|
|
|
self.canvas_height = 415
|
|
|
|
self.start_draw = False
|
2021-01-07 20:25:38 +01:00
|
|
|
self.vid = None
|
2021-01-07 22:13:39 +01:00
|
|
|
self.vid_canvas = None
|
2021-01-07 20:25:38 +01:00
|
|
|
self.canvas = None
|
|
|
|
self.back_button = None
|
2021-01-16 15:04:34 +01:00
|
|
|
self.clear_button = None
|
|
|
|
self.save_button = None
|
2021-01-07 22:13:39 +01:00
|
|
|
self.bottom_frame = None
|
|
|
|
self.top_frame = None
|
2021-01-17 14:16:01 +01:00
|
|
|
self.first_time = True
|
2021-01-24 17:35:10 +01:00
|
|
|
self.other_gestures_flag_possible = False
|
2021-01-07 18:35:51 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_view_name() -> str:
|
|
|
|
return PROJECT_VIEW_NAME
|
|
|
|
|
|
|
|
def start(self):
|
2021-01-07 20:25:38 +01:00
|
|
|
self.vid = MyVideoCapture()
|
2021-01-16 15:04:34 +01:00
|
|
|
self.canvas_width = self.window.winfo_width()
|
|
|
|
self.canvas_height = self.window.winfo_height() - 185
|
2021-01-07 22:13:39 +01:00
|
|
|
self.top_frame = tk.Frame(self, relief=tk.RAISED, borderwidth=0)
|
|
|
|
self.top_frame.pack(fill=tk.BOTH, side=tk.TOP)
|
2021-01-16 15:04:34 +01:00
|
|
|
self.canvas = tk.Canvas(self.top_frame, width=self.canvas_width, height=self.canvas_height, bg="white")
|
|
|
|
self.canvas.grid(row=0, column=0)
|
2021-01-07 20:25:38 +01:00
|
|
|
|
2021-01-07 22:13:39 +01:00
|
|
|
self.bottom_frame = tk.Frame(self, relief=tk.RAISED, borderwidth=1)
|
|
|
|
self.bottom_frame.pack(fill=tk.BOTH, side=tk.BOTTOM)
|
|
|
|
self.bottom_frame.columnconfigure(0, weight=1)
|
|
|
|
self.bottom_frame.columnconfigure(1, weight=1)
|
2021-01-16 15:04:34 +01:00
|
|
|
self.bottom_frame.columnconfigure(2, weight=15)
|
2021-01-07 22:13:39 +01:00
|
|
|
self.back_button = tk.Button(self.bottom_frame, text="Back", font=FONT,
|
|
|
|
command=lambda: self.back_to_projects_view())
|
2021-01-16 15:04:34 +01:00
|
|
|
self.back_button.grid(row=0, column=0, sticky=tk.SW, pady=10, padx=5)
|
|
|
|
self.clear_button = tk.Button(self.bottom_frame, text="Clear", font=FONT,
|
|
|
|
command=lambda: self.clear_canvas())
|
|
|
|
self.clear_button.grid(row=0, column=1, sticky=tk.SW, pady=10, padx=5)
|
|
|
|
self.save_button = tk.Button(self.bottom_frame, text="Save", font=FONT,
|
|
|
|
command=lambda: self.save())
|
|
|
|
self.save_button.grid(row=0, column=2, sticky=tk.SW, pady=10, padx=5)
|
2021-01-07 22:13:39 +01:00
|
|
|
self.vid_canvas = tk.Canvas(self.bottom_frame, width=self.vid.width, height=self.vid.height)
|
2021-01-16 15:04:34 +01:00
|
|
|
self.vid_canvas.grid(row=0, column=3, sticky=tk.E)
|
2021-01-07 20:25:38 +01:00
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
2021-01-17 14:16:01 +01:00
|
|
|
def create_window(self):
|
|
|
|
window = tk.Toplevel()
|
|
|
|
e1 = tk.Entry(window)
|
|
|
|
e1.grid(row=0, column=1)
|
|
|
|
okVar = tk.IntVar()
|
|
|
|
tk.Button(window,text = 'Write',command=lambda: okVar.set(1)).grid(row=1,
|
|
|
|
column=1,
|
|
|
|
sticky=tk.W,
|
|
|
|
pady=4)
|
|
|
|
#print(okVar)
|
|
|
|
self.window.wait_variable(okVar)
|
|
|
|
self.text_to_write = e1.get()
|
|
|
|
#print(self.text_to_write)
|
|
|
|
window.destroy()
|
|
|
|
#self.window.wait_window(window)
|
|
|
|
|
|
|
|
|
2021-01-07 20:25:38 +01:00
|
|
|
def update(self):
|
|
|
|
# Get a frame from the video source
|
|
|
|
if self.vid is not None:
|
|
|
|
fingers, frame, success = self.vid.get_frame()
|
2021-01-07 22:13:39 +01:00
|
|
|
if fingers is not None:
|
2021-01-16 15:04:34 +01:00
|
|
|
index_finger = fingers['index']
|
2021-01-24 17:35:10 +01:00
|
|
|
middle_finger = fingers['middle']
|
2021-01-16 15:04:34 +01:00
|
|
|
start_stop = fingers['start_stop']
|
2021-01-24 17:35:10 +01:00
|
|
|
if fingers['ninja'] != 'nothing' and self.other_gestures_flag_possible == True:
|
2021-01-17 14:16:01 +01:00
|
|
|
#print('trying')
|
|
|
|
self.create_window()
|
|
|
|
|
|
|
|
cv2.waitKey(10)
|
2021-01-24 17:35:10 +01:00
|
|
|
self.other_gestures_flag_possible = False
|
2021-01-17 14:16:01 +01:00
|
|
|
x = index_finger['x']
|
|
|
|
y = index_finger['y']
|
|
|
|
x, y = self.scale_points(x, y)
|
|
|
|
self.canvas.create_text(x,y,fill="darkblue", text= self.text_to_write, tags='text')
|
|
|
|
|
2021-01-16 15:04:34 +01:00
|
|
|
if start_stop == 'start':
|
|
|
|
print('start')
|
|
|
|
self.start_draw = True
|
2021-01-17 10:19:13 +01:00
|
|
|
self.first_time = True
|
2021-01-24 17:35:10 +01:00
|
|
|
self.other_gestures_flag_possible = False
|
2021-01-17 14:16:01 +01:00
|
|
|
|
2021-01-16 15:04:34 +01:00
|
|
|
elif start_stop == 'stop':
|
|
|
|
print('stop')
|
|
|
|
self.start_draw = False
|
2021-01-17 10:19:13 +01:00
|
|
|
self.first_time = False
|
2021-01-24 17:35:10 +01:00
|
|
|
self.other_gestures_flag_possible = True
|
|
|
|
|
|
|
|
if self.start_draw and index_finger['straight'] and middle_finger['straight']:
|
|
|
|
|
|
|
|
x = index_finger['x']
|
|
|
|
y = index_finger['y']
|
|
|
|
x, y = self.scale_points(x, y)
|
|
|
|
if self.first_time == True:
|
|
|
|
self.previous_x = x
|
|
|
|
self.previous_y = y
|
|
|
|
self.first_time = False
|
|
|
|
#print("first_time")
|
|
|
|
canvas_id = self.canvas.create_line(x, y, x + 2, y + 2, self.previous_x, self.previous_y, tags='point', width=3, fill = 'red')
|
|
|
|
self.canvas.after(100, self.canvas.delete, canvas_id)
|
|
|
|
#self.canvas.create_line(x, y, x+1, y+1, self.previous_x, self.previous_y, tags='point', width=3)
|
|
|
|
self.previous_x = x
|
|
|
|
self.previous_y = y
|
|
|
|
|
|
|
|
if self.start_draw and index_finger['straight'] and not middle_finger['straight']:
|
2021-01-17 10:19:13 +01:00
|
|
|
|
2021-01-16 15:04:34 +01:00
|
|
|
x = index_finger['x']
|
|
|
|
y = index_finger['y']
|
|
|
|
x, y = self.scale_points(x, y)
|
2021-01-17 10:19:13 +01:00
|
|
|
if self.first_time == True:
|
|
|
|
self.previous_x = x
|
|
|
|
self.previous_y = y
|
|
|
|
self.first_time = False
|
|
|
|
#print("first_time")
|
|
|
|
self.canvas.create_line(x, y, x+1, y+1, self.previous_x, self.previous_y, tags='point', width=3)
|
2021-01-24 17:35:10 +01:00
|
|
|
|
2021-01-17 10:19:13 +01:00
|
|
|
self.previous_x = x
|
|
|
|
self.previous_y = y
|
2021-01-07 20:25:38 +01:00
|
|
|
if success:
|
|
|
|
self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))
|
2021-01-07 22:13:39 +01:00
|
|
|
self.vid_canvas.create_image(0, 0, image=self.photo, anchor=tk.NW)
|
2021-01-07 20:25:38 +01:00
|
|
|
|
|
|
|
self.window.after(self.delay, self.update)
|
|
|
|
|
|
|
|
def back_to_projects_view(self):
|
|
|
|
self.main_view_controller.show_frame(PROJECTS_VIEW_NAME)
|
|
|
|
self.vid.release()
|
2021-01-07 22:13:39 +01:00
|
|
|
self.destroy_widgets()
|
2021-01-07 20:25:38 +01:00
|
|
|
|
2021-01-16 15:04:34 +01:00
|
|
|
def clear_canvas(self):
|
|
|
|
self.canvas.delete('all')
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
self.canvas.postscript(file='image.eps')
|
|
|
|
img = PIL.Image.open('image.eps')
|
|
|
|
img.save('image.png', 'png')
|
|
|
|
os.remove('image.eps')
|
|
|
|
|
2021-01-07 22:13:39 +01:00
|
|
|
def destroy_widgets(self):
|
2021-01-07 20:25:38 +01:00
|
|
|
self.vid = None
|
2021-01-07 22:13:39 +01:00
|
|
|
self.top_frame.destroy()
|
2021-01-07 20:25:38 +01:00
|
|
|
self.canvas.destroy()
|
2021-01-07 22:13:39 +01:00
|
|
|
self.bottom_frame.destroy()
|
|
|
|
self.vid_canvas.destroy()
|
2021-01-07 20:25:38 +01:00
|
|
|
self.back_button.destroy()
|
2021-01-16 15:04:34 +01:00
|
|
|
|
|
|
|
def scale_points(self, x, y):
|
|
|
|
return x*self.canvas_width, y*self.canvas_height
|