-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_app.py
More file actions
149 lines (136 loc) · 6.42 KB
/
desktop_app.py
File metadata and controls
149 lines (136 loc) · 6.42 KB
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
import tkinter
import tkinter.font as tkFont
from tkinter import *
from PIL import Image, ImageTk
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
from logic import HandClassifierHandler
def desktop_app(model, hch):
# colors here:
bg_color1 = "#EDEAD0"
bg_color2 = "#86BAA1"
# widnow SETTINGS
win = Tk()
win.title("PSL Recognition")
win.geometry("600x500+200+30")
win.resizable(False, False)
win.configure(bg=bg_color1)
w = 400
h = 300
frame = Frame(win, width=600, height=320, bg=bg_color2).place(x=0, y=0)
v = Label(frame, width=w, height=h)
v.place(x=10, y=10)
cap = cv2.VideoCapture(0)
# display text:
about = " Polish Sign Language Recognition Tool - " \
"show signs to your device's camera"
about_box = tkinter.Message(win, anchor="e", bg=bg_color2, justify="center", text=about, font=tkFont.Font(size=12))
about_box.place(x=420, y=30, width=160, height=160)
left_label = tkinter.Label(win, bg=bg_color1, fg="#414361", justify="center", text="Left hand - last detected:", font=tkFont.Font(size=10))
left_label.place(x=50, y=340, width=180, height=30)
right_label = tkinter.Label(win, bg=bg_color1, fg="#414361", justify="center", text="Right hand - last detected:", font=tkFont.Font(size=10))
right_label.place(x=400, y=340, width=180, height=30)
detect_left_label = tkinter.Label(win, bg=bg_color1, fg="#414361", justify="center", text="None", font=tkFont.Font(size=14))
detect_left_label.place(x=80, y=400, width=120, height=30)
detect_right_label = tkinter.Label(win, bg=bg_color1, fg="#414361", justify="center", text="None", font=tkFont.Font(size=14))
detect_right_label.place(x=430, y=400, width=120, height=30)
def select_img():
with mp_hands.Hands(
model_complexity=0,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
_, img = cap.read()
img = cv2.resize(img, (w, h))
img.flags.writeable = False
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(img)
# Draw the hand annotations on the image:
img.flags.writeable = True
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
message = results.multi_handedness
# obtain result from classificator:
result = hch.get_result(model=model, handlandmarks=results.multi_hand_landmarks[0],
is_R=hch.is_right(message))
resutl_name = hch.result_parser(result=result)
# check which hand:
if hch.is_right(message):
print('Right')
detect_left_label.config(text=resutl_name)
else:
print('Left')
detect_right_label.config(text=resutl_name)
# draw handlandmarks on image:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
img,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Flip the image horizontally for a selfie-view display: cv2.flip(image, 1)
# or leave for more video like effect
imgPIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
imgtk = ImageTk.PhotoImage(imgPIL)
v.configure(image=imgtk)
v.image = imgtk
v.after(10, select_img)
select_img()
win.mainloop()
def widnow_view_opencv(model, hch):
# https://github.com/google/mediapipe/blob/master/docs/solutions/hands.md
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
model_complexity=0,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
message = results.multi_handedness
print(len(results.multi_hand_landmarks))
if hch.is_right(message):
print('Right')
#cv2.putText(image, "Right", (100, 300), cv2.FONT_HERSHEY_SIMPLEX, 5, (255, 50, 50), 4)
else:
print('Left')
result = hch.get_result(model=model, handlandmarks=results.multi_hand_landmarks[0],
is_R=hch.is_right(message))
# print(results.multi_hand_landmarks)
# print(((results.multi_hand_landmarks[0]).ListFields()[0][1][0]).ListFields()[2][1])
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
########################################################################################################################
if __name__ == '__main__':
# setup
hch = HandClassifierHandler.HandClassifierHandler()
model = hch.load_model()
# run desktop app
desktop_app(model, hch)