-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirSketch.py
200 lines (175 loc) · 8.81 KB
/
AirSketch.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
import cv2
import numpy as np
import mediapipe as mp
from collections import deque
# Giving different arrays to handle colour points of different colours
bpoints = [deque(maxlen=1024)]
gpoints = [deque(maxlen=1024)]
rpoints = [deque(maxlen=1024)]
ypoints = [deque(maxlen=1024)]
ppoints = [deque(maxlen=1024)] # Purple points
opoints = [deque(maxlen=1024)] # Orange points
# 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
purple_index = 0
orange_index = 0
# The kernel to be used for dilation purpose
kernel = np.ones((5, 5), np.uint8)
# Define colors and their corresponding indexes
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255), (128, 0, 128), (255, 165, 0)]
colorIndex = 0
# Here is code for Canvas setup
paintWindow = np.zeros((800, 1640, 3)) + 255 # Adjusted canvas width to 1640
paintWindow = cv2.rectangle(paintWindow, (40, 1), (340, 120), (0, 0, 0), 2) # Adjusted rectangle width
paintWindow = cv2.rectangle(paintWindow, (360, 1), (660, 120), (255, 0, 0), 2) # Adjusted rectangle width
paintWindow = cv2.rectangle(paintWindow, (680, 1), (980, 120), (0, 255, 0), 2) # Adjusted rectangle width
paintWindow = cv2.rectangle(paintWindow, (1000, 1), (1300, 120), (0, 0, 255), 2) # Adjusted rectangle width
paintWindow = cv2.rectangle(paintWindow, (1320, 1), (1620, 120), (0, 255, 255), 2) # Adjusted rectangle width
paintWindow = cv2.rectangle(paintWindow, (40, 150), (340, 270), (128, 0, 128), 2) # Purple rectangle
paintWindow = cv2.rectangle(paintWindow, (360, 150), (660, 270), (255, 165, 0), 2) # Orange rectangle
cv2.putText(paintWindow, "CLEAR", (115, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLUE", (415, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(paintWindow, "GREEN", (695, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(paintWindow, "RED", (1025, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(paintWindow, "YELLOW", (1355, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(paintWindow, "PURPLE", (115, 220), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Added
cv2.putText(paintWindow, "SKYBLUE", (430, 220), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Added
cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE)
# 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)
# Check if the webcam is opened correctly
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
while True:
# Read each frame from the webcam
ret, frame = cap.read()
# Check if the frame is valid
if not ret:
print("Error: Failed to capture frame from webcam.")
break
x, y, c = frame.shape
# Flip the frame vertically
frame = cv2.flip(frame, 1)
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.rectangle(frame, (40, 1), (340, 120), (0, 0, 0), 2) # Adjusted rectangle width
frame = cv2.rectangle(frame, (360, 1), (660, 120), (255, 0, 0), 2) # Adjusted rectangle width
frame = cv2.rectangle(frame, (680, 1), (980, 120), (0, 255, 0), 2) # Adjusted rectangle width
frame = cv2.rectangle(frame, (1000, 1), (1300, 120), (0, 0, 255), 2) # Adjusted rectangle width
frame = cv2.rectangle(frame, (1320, 1), (1620, 120), (0, 255, 255), 2) # Adjusted rectangle width
frame = cv2.rectangle(frame, (40, 150), (340, 270), (128, 0, 128), 2) # Purple rectangle
frame = cv2.rectangle(frame, (360, 150), (660, 270), (255, 165, 0), 2) # Orange rectangle
cv2.putText(frame, "CLEAR", (115, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(frame, "BLUE", (415, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(frame, "GREEN", (695, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(frame, "RED", (1025, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(frame, "YELLOW", (1355, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Adjusted position
cv2.putText(frame, "PURPLE", (115, 220), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Added
cv2.putText(frame, "SKYBLUE", (430, 220), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 0), 2, cv2.LINE_AA) # Added
# 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:
lmx = int(lm.x * 1640) # Scaling x coordinates to new width
lmy = int(lm.y * 800) # Scaling y coordinates to new height
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)
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
ppoints.append(deque(maxlen=512)) # Purple
purple_index += 1
opoints.append(deque(maxlen=512)) # Orange
orange_index += 1
elif center[1] <= 120:
if 40 <= center[0] <= 340: # Clear Button
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]
ppoints = [deque(maxlen=512)] # Purple
opoints = [deque(maxlen=512)] # Orange
blue_index = 0
green_index = 0
red_index = 0
yellow_index = 0
purple_index = 0
orange_index = 0
paintWindow[122:,:,:] = 255
elif 360 <= center[0] <= 660:
colorIndex = 0 # Blue
elif 680 <= center[0] <= 980:
colorIndex = 1 # Green
elif 1000 <= center[0] <= 1300:
colorIndex = 2 # Red
elif 1320 <= center[0] <= 1620:
colorIndex = 3 # Yellow
elif 150 <= center[1] <= 270:
if 40 <= center[0] <= 340: # Purple
colorIndex = 4
elif 360 <= center[0] <= 660: # Orange
colorIndex = 5
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)
elif colorIndex == 4:
ppoints[purple_index].appendleft(center)
elif colorIndex == 5:
opoints[orange_index].appendleft(center)
# Append the next deques when nothing is detected to avoid 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
ppoints.append(deque(maxlen=512)) # Purple
purple_index += 1
opoints.append(deque(maxlen=512)) # sky blue
orange_index += 1
# Draw lines of all the colors on the canvas and frame with increased thickness
points = [bpoints, gpoints, rpoints, ypoints, ppoints, opoints]
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], 4) # Increased thickness
cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k], colors[i], 4) # Increased thickness
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()