-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCanvas.py
More file actions
198 lines (165 loc) · 8.06 KB
/
Canvas.py
File metadata and controls
198 lines (165 loc) · 8.06 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
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
# All the imports go here
import cv2
import numpy as np
import mediapipe as mp
from collections import deque
# Giving different arrays to handle colour points of different colour
bpoints = [deque(maxlen=1024)]
gpoints = [deque(maxlen=1024)]
rpoints = [deque(maxlen=1024)]
ypoints = [deque(maxlen=1024)]
# These indexes will be used to mark the points in particular arrays of specific 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
def draw_square_with_white_border(image, center, size, color, border_thickness):
x, y = center
half_size = size // 2
# Draw filled square
cv2.rectangle(image, (x - half_size, y - half_size), (x + half_size, y + half_size), color, -1)
# Draw white border
border_color = (255, 255, 255)
cv2.rectangle(image, (x - half_size - border_thickness, y - half_size - border_thickness),
(x + half_size + border_thickness, y + half_size + border_thickness), border_color, border_thickness)
# initialize mediapipe
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mpDraw = mp.solutions.drawing_utils
# Initialize the webcam
cap = cv2.VideoCapture(0)
ret = True
while ret:
# Read each frame from the webcam
ret, frame = cap.read()
x, y, c = frame.shape
# Flip the frame vertically
frame = cv2.flip(frame, 1)
#hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Here is code for Canvas setup
paintWindow = np.zeros((x, y, 3)) + 0
paintWindow = cv2.rectangle(paintWindow, (30, 10), (110, 90), (255, 255, 255), -1)
paintWindow = cv2.rectangle(paintWindow, (160, 10), (240, 90), (255, 0, 0), -1)
paintWindow = cv2.rectangle(paintWindow, (290, 10), (370, 90), (0, 255, 0), -1)
paintWindow = cv2.rectangle(paintWindow, (420, 10), (500, 90), (0, 0, 255), -1)
paintWindow = cv2.rectangle(paintWindow, (545, 10), (625, 90), (0, 255, 255), -1)
draw_square_with_white_border(paintWindow, (70, 50), 80, (255, 255, 255), 3)
draw_square_with_white_border(paintWindow, (200, 50), 80, (255, 0, 0), 3)
draw_square_with_white_border(paintWindow, (330, 50), 80, (0, 255, 0), 3)
draw_square_with_white_border(paintWindow, (460, 50), 80, (0, 0, 255), 3)
draw_square_with_white_border(paintWindow, (585, 50), 80, (0, 255, 255), 3)
cv2.putText(paintWindow, "CLEAR", (45, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLUE", (175, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "GREEN", (300, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "RED", (440, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "YELLOW", (555, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE)
frame = cv2.rectangle(frame, (30, 10), (110, 90), (255, 255, 255), -1)
frame = cv2.rectangle(frame, (160, 10), (240, 90), (255, 0, 0), -1)
frame = cv2.rectangle(frame, (290, 10), (370 , 90), (0, 255, 0), -1)
frame = cv2.rectangle(frame, (420 , 10 ), (500, 90), (0, 0, 255), -1)
frame = cv2.rectangle(frame, (545, 10), (625 , 90), (0, 255, 255), -1)
draw_square_with_white_border(frame, (70, 50), 80, (255, 255, 255), 3)
draw_square_with_white_border(frame, (200, 50), 80, (255, 0, 0), 3)
draw_square_with_white_border(frame, (330, 50), 80, (0, 255, 0), 3)
draw_square_with_white_border(frame, (460, 50), 80, (0, 0, 255), 3)
draw_square_with_white_border(frame, (585, 50), 80, (0, 255, 255), 3)
cv2.putText(frame, "CLEAR", (45, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "BLUE", (175, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "GREEN", (300, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "RED", (440, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "YELLOW", (555, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
#frame = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
# Get hand landmark prediction
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)
lmx = int(lm.x * y)
lmy = int(lm.y * x)
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] <= 90:
if 30 <= center[0] <= 110: # 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[100:,:,:] = 0
elif 160<= center[0] <= 240:
colorIndex = 0 # Blue
elif 290 <= center[0] <= 370:
colorIndex = 1 # Green
elif 420<= center[0] <= 500:
colorIndex = 2 # Red
elif 545 <= center[0] <= 665:
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 on the canvas and frame
points = [bpoints, gpoints, rpoints, ypoints]
# for j in range(len(points[0])):
# for k in range(1, len(points[0][j])):
# if points[0][j][k - 1] is None or points[0][j][k] is None:
# continue
# cv2.line(paintWindow, points[0][j][k - 1], points[0][j][k], colors[0], 2)
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 webcam and destroy all active windows
cap.release()
cv2.destroyAllWindows()