-
Notifications
You must be signed in to change notification settings - Fork 0
/
doodle.1.6.py
106 lines (90 loc) · 3.93 KB
/
doodle.1.6.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
import cv2
import time
import math
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
#initiate webcam
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
width = 1280#cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = 720#cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
from tkinter import *
tk = Tk()
#tk.attributes('-alpha',0.2)
tk.wm_attributes("-transparentcolor", "white")
canvas = Canvas(tk, width = width, height = height)
canvas.configure(bg='white')
tk.title("4dot")
tk.iconphoto(False, PhotoImage(file = 'me.png'))
canvas.pack()
radius = 26
color = 'black'
colour = (0,0,0)
with mp_hands.Hands(
max_num_hands=1,
min_detection_confidence=0.6,
min_tracking_confidence=0.6) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
continue
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
try:
for hand_landmarks in results.multi_hand_landmarks:
INDEX_FINGER_TIP = [int(hand_landmarks.landmark[8].x * width), int(hand_landmarks.landmark[8].y * height)]
THUMB_MCP = [int(hand_landmarks.landmark[2].x * width), int(hand_landmarks.landmark[2].y * height)]
THUMB_TIP = [int(hand_landmarks.landmark[4].x * width), int(hand_landmarks.landmark[4].y * height)]
INDEX_FINGER_MCP = [int(hand_landmarks.landmark[5].x * width), int(hand_landmarks.landmark[5].y * height)]
MIDDLE_FINGER_TIP = [int(hand_landmarks.landmark[12].x * width), int(hand_landmarks.landmark[12].y * height)]
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
try:
s_point = (lx, ly)
except:
lx, ly = INDEX_FINGER_TIP[0], INDEX_FINGER_TIP[1]
e_point = (INDEX_FINGER_TIP[0], INDEX_FINGER_TIP[1])
l_click_angle = abs(math.degrees(math.atan2(INDEX_FINGER_TIP[1]-THUMB_MCP[1], INDEX_FINGER_TIP[0]-THUMB_MCP[0]) - math.atan2(THUMB_TIP[1]-THUMB_MCP[1], THUMB_TIP[0]-THUMB_MCP[0])))
r_click_angle = abs(math.degrees(math.atan2(INDEX_FINGER_TIP[1]-INDEX_FINGER_MCP[1], INDEX_FINGER_TIP[0]-INDEX_FINGER_MCP[0]) - math.atan2(MIDDLE_FINGER_TIP[1]-INDEX_FINGER_MCP[1], MIDDLE_FINGER_TIP[0]-INDEX_FINGER_MCP[0])))
if l_click_angle < 30 and r_click_angle > 30:
canvas.create_line(lx, ly, INDEX_FINGER_TIP[0], INDEX_FINGER_TIP[1], fill = color, width=6)
elif r_click_angle < 30:
canvas.create_oval(INDEX_FINGER_TIP[0]-radius, INDEX_FINGER_TIP[1]-radius, INDEX_FINGER_TIP[0]+radius, INDEX_FINGER_TIP[1]+radius, fill='white', outline="white")
cv2.circle(image, e_point, radius, (0,255,255), 2)
cv2.rectangle(image, (0, 0), (320, 24), (0,0,0), -1)
cv2.rectangle(image, (320, 0), (640, 24), (0,0,255), -1)
cv2.rectangle(image, (640, 0), (960, 24), (0,255,0), -1)
cv2.rectangle(image, (960, 0), (1280, 24), (255,0,0), -1)
cv2.putText(image, color, (24,48), cv2.FONT_HERSHEY_SIMPLEX, 1, colour,2)
cv2.imshow('arhsim', image)
tk.update()
lx, ly = INDEX_FINGER_TIP[0], INDEX_FINGER_TIP[1]
except:
cv2.imshow('arhsim', image)
tk.update()
continue
if INDEX_FINGER_TIP[1] < 24:
if INDEX_FINGER_TIP[0] in range(0,320):
color = 'black'
colour = (0,0,0)
if INDEX_FINGER_TIP[0] in range(320,640):
color = 'red'
colour = (0,0,255)
if INDEX_FINGER_TIP[0] in range(640,960):
color = 'green'
colour = (0,255,0)
if INDEX_FINGER_TIP[0] in range(960,1280):
color = 'blue'
colour = (255,0,0)
if l_click_angle > 120 and INDEX_FINGER_TIP[1] > INDEX_FINGER_MCP[1]:
print('gesture exit')
break
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
tk.destroy()
cv2.destroyAllWindows()