-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_paint_app.py
179 lines (129 loc) · 3.94 KB
/
virtual_paint_app.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
import mediapipe as mp
import cv2
import numpy as np
import time
#contants
ml = 150
max_x, max_y = 250+ml, 50
curr_tool = "select tool"
time_init = True
rad = 40
var_inits = False
thick = 4
prevx, prevy = 0,0
#get tools function
def getTool(x):
if x < 50 + ml:
return "line"
elif x<100 + ml:
return "rectangle"
elif x < 150 + ml:
return"draw"
elif x<200 + ml:
return "circle"
else:
return "erase"
def index_raised(yi, y9):
if (y9 - yi) > 40:
return True
return False
hands = mp.solutions.hands
hand_landmark = hands.Hands(min_detection_confidence=0.6, min_tracking_confidence=0.6, max_num_hands=1)
draw = mp.solutions.drawing_utils
# drawing tools
tools = cv2.imread("tools.png")
# tools = tools.astype('uint8')
mask = np.ones((480, 640))*255
mask = mask.astype('uint8')
tools = np.zeros((max_y+5, max_x+5, 3), dtype="uint8")
cv2.rectangle(tools, (0,0), (max_x, max_y), (0,0,255), 2)
cv2.line(tools, (50,0), (50,50), (0,0,255), 2)
cv2.line(tools, (100,0), (100,50), (0,0,255), 2)
cv2.line(tools, (150,0), (150,50), (0,0,255), 2)
cv2.line(tools, (200,0), (200,50), (0,0,255), 2)
cap = cv2.VideoCapture(0)
while True:
_, frm = cap.read()
frm = cv2.flip(frm, 1)
rgb = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)
op = hand_landmark.process(rgb)
if op.multi_hand_landmarks:
for i in op.multi_hand_landmarks:
draw.draw_landmarks(frm, i, hands.HAND_CONNECTIONS)
x, y = int(i.landmark[8].x*640), int(i.landmark[8].y*480)
if x < max_x and y < max_y and x > ml:
if time_init:
ctime = time.time()
time_init = False
ptime = time.time()
cv2.circle(frm, (x, y), rad, (0,255,255), 2)
rad -= 1
if (ptime - ctime) > 0.8:
curr_tool = getTool(x)
print("your current tool set to : ", curr_tool)
time_init = True
rad = 40
else:
time_init = True
rad = 40
if curr_tool == "draw":
xi, yi = int(i.landmark[12].x*640), int(i.landmark[12].y*480)
y9 = int(i.landmark[9].y*480)
if index_raised(yi, y9):
cv2.line(mask, (prevx, prevy), (x, y), 0, thick)
prevx, prevy = x, y
else:
prevx = x
prevy = y
elif curr_tool == "line":
xi, yi = int(i.landmark[12].x*640), int(i.landmark[12].y*480)
y9 = int(i.landmark[9].y*480)
if index_raised(yi, y9):
if not(var_inits):
xii, yii = x, y
var_inits = True
cv2.line(frm, (xii, yii), (x, y), (50,152,255), thick)
else:
if var_inits:
cv2.line(mask, (xii, yii), (x, y), 0, thick)
var_inits = False
elif curr_tool == "rectangle":
xi, yi = int(i.landmark[12].x*640), int(i.landmark[12].y*480)
y9 = int(i.landmark[9].y*480)
if index_raised(yi, y9):
if not(var_inits):
xii, yii = x, y
var_inits = True
cv2.rectangle(frm, (xii, yii), (x, y), (0,255,255), thick)
else:
if var_inits:
cv2.rectangle(mask, (xii, yii), (x, y), 0, thick)
var_inits = False
elif curr_tool == "circle":
xi, yi = int(i.landmark[12].x*640), int(i.landmark[12].y*480)
y9 = int(i.landmark[9].y*480)
if index_raised(yi, y9):
if not(var_inits):
xii, yii = x, y
var_inits = True
cv2.circle(frm, (xii, yii), int(((xii-x)**2 + (yii-y)**2)**0.5), (255,255,0), thick)
else:
if var_inits:
cv2.circle(mask, (xii, yii), int(((xii-x)**2 + (yii-y)**2)**0.5), (0,255,0), thick)
var_inits = False
elif curr_tool == "erase":
xi, yi = int(i.landmark[12].x*640), int(i.landmark[12].y*480)
y9 = int(i.landmark[9].y*480)
if index_raised(yi, y9):
cv2.circle(frm, (x, y), 30, (0,0,0), -1)
cv2.circle(mask, (x, y), 30, 255, -1)
op = cv2.bitwise_and(frm, frm, mask=mask)
frm[:, :, 1] = op[:, :, 1]
frm[:, :, 2] = op[:, :, 2]
# frm[:max_y, ml:max_x] = cv2.addWeighted(tools, 0.3, frm[:max_y, ml:max_x], 0.6, 0)
cv2.putText(frm, curr_tool, (270+ml,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
cv2.imshow("paint app", frm)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
cap.release()
break