-
Notifications
You must be signed in to change notification settings - Fork 0
/
aircanvas.py
161 lines (142 loc) · 6.21 KB
/
aircanvas.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
# import lib
import cv2
import numpy as np
import mediapipe as mp
from collections import deque
# different arrays to handel colour points
bpoints = [deque(maxlen=1024)]
gpoints = [deque(maxlen=1024)]
rpoints = [deque(maxlen=1024)]
ypoints = [deque(maxlen=1024)]
#indexe used to mark the points in particular arrays of sepecific colour
blue_index = 0
green_index = 0
red_index = 0
yellow_index = 0
#the kernel to be used for dilation purpose
kernel = np.ones((5,5),np.uint8)
colors = [(255,0,0), (0,255,0), (0,0,255),(0,255,255)]
colorIndex = 0
#white canvas setup
paintWindow = np.zeros((471,636,3)) + 255
paintWindow = cv2.rectangle(paintWindow, (40,1),(140,65),(0,0,0),2)
paintWindow = cv2.rectangle(paintWindow, (160,1),(255,65),(255,0,0),2)
paintWindow = cv2.rectangle(paintWindow, (275,1),(370,65),(0,255,0),2)
paintWindow = cv2.rectangle(paintWindow, (390,1),(485,65),(0,0,255),2)
paintWindow = cv2.rectangle(paintWindow, (505,1),(600,65),(0,255,255),2)
cv2.putText(paintWindow, "CLEAR", (49,33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLUE", (185,33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),2, cv2.LINE_AA)
cv2.putText(paintWindow, "GREEN", (298,33),cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,0,0),2, cv2.LINE_AA)
cv2.putText(paintWindow, "RED", (420,33),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0),2, cv2.LINE_AA)
cv2.putText(paintWindow, "YELLOW", (520,33),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0),2, cv2.LINE_AA)
cv2.namedWindow("Paint", cv2.WINDOW_AUTOSIZE)
# instalize mediapipe
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=2,min_detection_confidence=0.7)
#this is used to draw land marks on detected hand
mpDraw = mp.solutions.drawing_utils
#initialize camera
cap = cv2.VideoCapture(0)
ret, frame_temp = cap.read()
while ret:
ret, frame = cap.read()
x,y,c=frame.shape
#flip the frame
frame = cv2.flip(frame,1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.rectangle(frame,(40,1),(140,65),(0,0,0),2)
frame = cv2.rectangle(frame,(160,1),(255,65),(255,0,0),2)
frame = cv2.rectangle(frame,(275,1),(370,65),(0,255,0),2)
frame = cv2.rectangle(frame,(390,1),(485,65),(0,0,255),2)
frame = cv2.rectangle(frame,(505,1),(600,65),(0,255,255),2)
cv2.putText(frame, "CLEAR", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2,cv2.LINE_AA)
cv2.putText(frame, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2,cv2.LINE_AA)
cv2.putText(frame, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2,cv2.LINE_AA)
cv2.putText(frame, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2,cv2.LINE_AA)
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#get hand landmark predication
result = hands.process(framergb)
#post process the result
if result.multi_hand_landmarks:
landmarks = []
for handslms in result.multi_hand_landmarks:
for lm in handslms.landmark:
## print(id,lm)
# print (lm.x)
# print(lm.y)
#Adjust according to your frame size
lmx = int(lm.x*640)
lmy = int(lm.y*640)
landmarks.append([lmx, lmy])
# Drawing landmarks on frames
mpDraw.draw_landmarks(frame, handslms, mpHands.HAND_CONNECTIONS)
fore_finger = (landmarks[8][0], landmarks[8][1])
center = fore_finger
thumb = (landmarks[4][0],landmarks[4][1])
cv2.circle(frame, center, 3, (0,255,0),-1)
print(center[1]-thumb[1])
if (thumb[1]-center[1]<30):
bpoints.append(deque(maxlen=512))
blue_index += 1
gpoints.append(deque(maxlen=512))
green_index += 1
rpoints.append(deque(maxlen=512))
red_index += 1
ypoints.append(deque(maxlen=512))
yellow_index += 1
elif center[1]<=65:
if 40<=center[0]<=140: #clear Button
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
blue_index = 0
green_index = 0
red_index = 0
yellow_index = 0
paintWindow[67:,:,:] = 255
elif 160 <= center[0]<=255:
colorIndex = 0 #blue
elif 275 <= center[0]<=370:
colorIndex = 1 # green
elif 390 <= center[0]<=485:
colorIndex = 2 #red
elif 505 <= center[0]<= 600:
colorIndex = 3 # yellow
else:
if colorIndex == 0:
bpoints[blue_index].appendleft(center)
elif colorIndex == 1:
gpoints[green_index].appendleft(center)
elif colorIndex == 2:
rpoints[red_index].appendleft(center)
elif colorIndex == 3:
ypoints[yellow_index].appendleft(center)
#append the next deques when nothing is detected to avois messing up
else:
bpoints.append(deque(maxlen=512))
blue_index += 1
gpoints.append(deque(maxlen=512))
green_index += 1
rpoints.append(deque(maxlen=512))
red_index += 1
ypoints.append(deque(maxlen=512))
yellow_index += 1
# draw lines of all the colors
points = [bpoints,gpoints,rpoints,ypoints]
for i in range(len(points)):
for j in range(len(points[i])):
for k in range(1, len(points[i][j])):
if points[i][j][k-1] is None or points[i][j][k] is None:
continue
cv2.line(frame,points[i][j][k-1],points[i][j][k], colors[i],2)
cv2.line(paintWindow, points[i][j][k-1], points[i][j][k],colors[i],2)
cv2.imshow("Output",frame)
cv2.imshow("paint",paintWindow)
if cv2.waitKey(1) == ord('q'):
break
# release the web cam and destory all active window
cap.release()
cv2.destroyAllWindows()