-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.py
More file actions
171 lines (126 loc) · 3.79 KB
/
paint.py
File metadata and controls
171 lines (126 loc) · 3.79 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
# **** LIBRARY SETUPS ****
# pip install mediapipe
# pip install opencv-python
import mediapipe as mp
import cv2
import numpy as np
import time
# içerikler - contants
ml = 150
max_x, max_y = 250+ml, 50
curr_tool = "Arac Sec"
time_init = True
rad = 40
var_inits = False
thick = 2
prevx, prevy = 0,0
# Araçları seçme fonksiyonu - get tools function
def getTool(x):
if x < 50 + ml:
return "Duz Cizgi"
elif x<100 + ml:
return "Kare"
elif x < 150 + ml:
return "Ciz"
elif x<200 + ml:
return "Daire"
else:
return "Silgi"
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
# çizim araçları - drawing tools
tools = cv2.imread("araclar.png")
tools = tools.astype('uint8')
mask = np.ones((480, 640))*255
mask = mask.astype('uint8')
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), 1)
rad -= 1
if (ptime - ctime) > 0.8:
curr_tool = getTool(x)
print("Secilen arac : ", curr_tool)
time_init = True
rad = 40
else:
time_init = True
rad = 40
if curr_tool == "Ciz":
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 == "Duz Cizgi":
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 == "Kare":
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 == "Daire":
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 == "Silgi":
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), 70, (0,0,0), -1)
cv2.circle(mask, (x, y), 70, 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.7, frm[:max_y, ml:max_x], 0.3, 0)
cv2.putText(frm, curr_tool, (270+ml,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
cv2.imshow("Cizim Uygulamasi", frm)
if cv2.waitKey(1) == 27: # EXIT >>> ESC Key
cv2.destroyAllWindows()
cap.release()
break