-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGUI_Camera 3.py
211 lines (157 loc) · 7.37 KB
/
GUI_Camera 3.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
209
210
211
from tkinter import *
import cv2
from PIL import Image, ImageTk
import mediapipe as mp
import math
#make constants
WIDTH = 400
HEIGHT = 500
#mediapipe setup
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
MYHAND = .07 #set scale of tested hand
#Make constants
DEBUG = False
FIND = False
ID = True
CONFIRM = True
#Confirm og values
global Read
global HasRead
global String
global ReadCount
global NewRead
Read = ""
HasRead = ""
String = ""
ReadCount = 0
NewRead = 0
# Euclidean distance
def compute_distance(landmark1, landmark2):
return ((landmark1.x - landmark2.x)**2 + (landmark1.y - landmark2.y)**2)**0.5
#initiate camera
vid = cv2.VideoCapture(0)
vid.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
app = Tk()
app.bind('<Escape>', lambda e: app.quit())
label_widget = Label(app)
label_widget.pack()
# Status bar
status_bar1 = Label(app, text="YES", bd=1, relief=SUNKEN, anchor=W)
status_bar1.pack(side=BOTTOM, fill=X)
# Vertical Status bar
status_bar = Label(app, text="Reading, Input, queue", bd=1, relief=SUNKEN, anchor=W,height=20,width=30)
status_bar.pack(side=RIGHT, fill=Y)
#status bar
confirmed_letter = Text(app, bd=1, relief=SUNKEN, height=1, width=2) # Adjust the height and width accordingly
confirmed_letter.pack(side=TOP, fill=X)
confirmed_letter.place(relx=1.0, rely=0.0, anchor="ne")
confirmed_letter.tag_configure("center", justify='center')
def open_camera():
_, frame = vid.read()
global Read, HasRead, String, ReadCount, NewRead
frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
results = hands.process(frame)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
#get markers for each point
Tt = hand_landmarks.landmark[4] #thumb tip
Pt = hand_landmarks.landmark[20] #pinky tip
H0 = hand_landmarks.landmark[0] #Hand 0 (wrist)
Mt = hand_landmarks.landmark[12] #middle tip
Hi = hand_landmarks.landmark[5] #index start
Ps = hand_landmarks.landmark[17] #pinky start
Rt = hand_landmarks.landmark[16] #Ring tip
It = hand_landmarks.landmark[8] #Index tip
Ts = hand_landmarks.landmark[2] #Thumb start
z_dist = -10000000*H0.z #set z distance constant
z_scale = round(11/z_dist,2)
#set hand scale
scale = round((round(compute_distance(H0, Hi)*(z_scale), 2)) * (round(compute_distance(Ps, Hi)*(z_scale), 2)),2)
comp = (round(scale/MYHAND,4))
PTdist = abs(round(compute_distance(Pt, Tt)*(z_scale), 2)) #Pinky-Thumb
HPdist = abs(round(compute_distance(H0, Pt)*(z_scale), 2)) #Wrist-Pinky tip
HTdist = abs(round(compute_distance(H0, Tt)*(z_scale), 2)) #Wrist-Thumb tip
HMdist = abs(round(compute_distance(H0, Mt)*(z_scale), 2)) #Wrist-Middle tip
HRdist = abs(round(compute_distance(H0, Rt)*(z_scale), 2)) #Wrist-Ring tip
HIdist = abs(round(compute_distance(H0, It)*(z_scale), 2)) #Wrist-Index tip
HTs_dist = abs(round(compute_distance(H0, Ts)*(z_scale), 2)) #Wrist-Thumb start
TIdist = abs(round(compute_distance(Ts, It)*(z_scale), 2)) #Thumb start - Index tip
TtPs_dist = abs(round(compute_distance(Tt, Ps)*(z_scale), 2)) #Thumb tip - Pinky start
if (ID):
if ((comp*PTdist) > (.30) and (comp*HMdist < 0.3) and (comp*HPdist > 0.3) and \
(comp*HRdist < 0.4) and (comp*HIdist < 0.4) and (comp*TtPs_dist > 0.2)):
#print("Y")
Read = "Y"
elif((comp*HRdist < 0.75) and (comp*HIdist < 0.9) and (comp*HMdist < 0.75) \
and (HPdist < 0.75)):
#print("***Fingers Down***")
if (comp*TtPs_dist < 0.5):
#print("Hand Closed")
if (comp*HTdist < 0.7) and (comp*HTdist > 0.18)\
and (comp*TtPs_dist < 0.2):
#print("E")
Read = "E"
elif (comp*HTdist < 1.35) and (comp*HTdist > 0.6):
#print("S")
Read = "S"
else:
Read = "-"
print("")
if (DEBUG):
print("")
print("DEBUG")
print("PTdist: {} Upper: {} Lower: {}".format((PTdist), comp*(1.1), comp*(.7)))
print("HTdist: {} Upper: {} Lower: {}".format((HTdist), comp*(0.47), comp*(0.37)))
print("z: {}".format(z_dist))
print("% = {}, area = {}".format(comp, scale))
print("PTdist: {}, HPdist: {}, HTdist: {}, HMdist: {}".format(PTdist, HPdist, HTdist, HMdist))
print("")
if (FIND):
print("")
print("FIND:")
print("% = {}, scale = {}".format(comp, scale))
print("PT:{}, HM:{}, HR:{}, HI:{}".format(PTdist, HMdist, HRdist, HIdist)) #for 'y'
print("HTdist:{}, HTs_dist:{}, TIdist:{}".format(HTdist, HTs_dist, TIdist))
print("HR: {}, HI: {}, HM: {}, HP: {}, TtPs: {}".format(HRdist, HIdist, HMdist, HPdist, TtPs_dist))
print("")
pass
print("Read: {}".format(Read))
if (CONFIRM):
if (NewRead > 10):
HasRead = Read
NewRead = 0
print("New letter!")
if (Read == HasRead):
ReadCount += 1
NewRead -= 1
if (NewRead < 0):
NewRead = 0
else:
ReadCount -= 1
NewRead += 1
if (ReadCount < 0):
ReadCount = 0
if ((ReadCount > 40) and (Read != "-")):
print("Confirm Letter: {}".format(Read))
confirmed_letter.delete("1.0", END) # Delete current content
confirmed_letter.insert(END, Read) # Insert the new letter
confirmed_letter.tag_add("center", "1.0", "end")
NewRead = 0
ReadCount = 0
String += Read
status_bar1.config(text=String)
opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
captured_image = Image.fromarray(opencv_image)
photo_image = ImageTk.PhotoImage(image=captured_image)
label_widget.photo_image = photo_image
label_widget.configure(image=photo_image)
label_widget.after(10, open_camera)
button1 = Button(app, text="Open Camera", command=open_camera)
button1.pack()
app.mainloop()