-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.py
173 lines (140 loc) · 5.38 KB
/
Canvas.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
# Importing packages
import cv2
import numpy as np
import mediapipe as mp
from PIL import Image
import shutil
import os
def nothing(x):
pass
path = os.getcwd()
# General Variable values
cv2.namedWindow("Configurations")
cv2.createTrackbar("Pen Size", "Configurations", 15, 100, nothing)
cv2.createTrackbar("Eraser Size", "Configurations", 50, 200, nothing)
imgCanvas = np.ones((720, 1280, 3), np.uint8)
imgCanvas[:] = 255
drawColor= (255, 0, 255)
eraserColor=(255,255,255)
pathNew = path + '\Output'
if not os.path.exists("Output"):
os.mkdir("Output")
else:
shutil.rmtree("Output")
os.mkdir("Output")
# Modules
class handTracker():
def __init__(self, mode=False, maxHands=2,complexity = 1, detectionCon=0.7, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.complexity = complexity
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.complexity, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
self.tipIds = [4, 8, 12, 16, 20]
def handsFinder(self, image, draw=True):
imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imageRGB)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(image, handLms, self.mpHands.HAND_CONNECTIONS)
return image
def positionFinder(self, image, handNo=0, draw=False):
lmlist = []
if self.results.multi_hand_landmarks:
Hand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(Hand.landmark):
h, w, c = image.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lmlist.append([id, cx, cy])
if draw:
cv2.circle(image, (cx, cy), 15, drawColor, cv2.FILLED)
return lmlist
# Main Function
def main():
global imgCanvas
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
xp = 0
yp = 0
alpha =0.7
stop=0
n=0
tracker = handTracker()
tipIds = [4, 8, 12, 16, 20]
while True:
success, image = cap.read()
image = cv2.flip(image, 1)
brushThickness = cv2.getTrackbarPos("Pen Size", "Configurations")
eraserThickness = cv2.getTrackbarPos("Eraser Size", "Configurations")
image = tracker.handsFinder(image)
lmList = tracker.positionFinder(image)
if len(lmList) != 0:
x1, y1 = lmList[8][1:]
x2, y2 = lmList[12][1:]
# fingers = tracker.fingersUp()
fingers = []
# Thumb
if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
# Fingers
for id in range(1, 5):
if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
if fingers[1] and fingers[2]:
if fingers[3] and fingers[4] == False:
cv2.circle(image, (x2, y2), 15, drawColor, cv2.FILLED)
print("Erasing Mode")
if xp == 0 and yp == 0:
xp, yp = x2, y2
cv2.line(image, (x2, y2), (x2, y2), eraserColor, eraserThickness)
cv2.line(imgCanvas, (x2, y2), (x2, y2), eraserColor, eraserThickness)
xp, yp = x1, y1
else:
xp, yp = 0, 0
print("Selection Mode")
cv2.rectangle(image, (x1, y1 - 25), (x2, y2 + 25), drawColor, cv2.FILLED)
if fingers[1] and fingers[2] == False:
cv2.circle(image, (x1, y1), 15, drawColor, cv2.FILLED)
print("Drawing Mode")
if xp == 0 and yp == 0:
xp, yp = x1, y1
cv2.line(image, (xp, yp), (x1, y1), drawColor, brushThickness)
cv2.line(imgCanvas, (xp, yp), (x1, y1), drawColor, brushThickness)
xp,yp=x1,y1
if all(x >= 1 for x in fingers):
stop=stop+1
if stop==38:
saved = Image.fromarray(imgCanvas)
name = str(n) + '.jpg'
slash = str('/')
source = path +slash+ name
saved.save(name)
shutil.move(source, pathNew)
n = n + 1
imgCanvas = np.ones((720, 1280, 3), np.uint8)
imgCanvas[:] = 255
stop=0
#imgGray = cv2.cvtColor(imgCanvas, cv2.COLOR_BGR2GRAY)
#_, imgInv = cv2.threshold(imgGray, 50, 255, cv2.THRESH_BINARY_INV)
#imgInv = cv2.cvtColor(imgInv, cv2.COLOR_GRAY2BGR)
#imgEx1 = image[0 : 576, 0: 1000]
#imgEx = cv2.bitwise_and(imgEx1, imgInv)
#imgEx = cv2.bitwise_or(imgEx, imgCanvas)
image = cv2.addWeighted(image, alpha, imgCanvas, 1 - alpha, 1.0)
cv2.imshow("Video", image)
cv2.imshow("Canvas", imgCanvas)
if (cv2.waitKey(1) & 0xFF == ord('d')):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()