-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
104 lines (85 loc) · 3.95 KB
/
camera.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
import cv2
import mediapipe as mp
mpDraw = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
pencil_img = cv2.imread("images/pencil.png")
pencil_img = cv2.resize(pencil_img, (100, 100))
undo_img = cv2.imread("images/undo.png")
undo_img = cv2.resize(undo_img, (100, 100))
cancel_img = cv2.imread("images/cancel.png")
cancel_img = cv2.resize(cancel_img, (100, 100))
x_offset = 500
y_offset = 10
pencil = False
undo = False
points = []
def get_frame():
global pencil, undo, points
with mp.solutions.hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.8,
min_tracking_confidence=0.8,
) as hand_mesh:
while cap.isOpened():
success, image = cap.read()
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.flip(image, 1)
results = hand_mesh.process(image)
# Draw the face mesh annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image = cv2.rectangle(image, (30, 30), (420, 420), (0, 255, 0), 3)
image[50 : 50 + 100, x_offset : x_offset + 100] = pencil_img
image[170 : 170 + 100, x_offset : x_offset + 100] = undo_img
image[290 : 290 + 100, x_offset : x_offset + 100] = cancel_img
cv2.putText(image, "By: alizahidraja", (250, 470), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
if pencil:
image = cv2.rectangle(image, (500, 50), (600, 150), (0, 255, 0), 3)
if undo:
image = cv2.rectangle(image, (500, 170), (600, 270), (0, 255, 0), 3)
for i in points:
cv2.circle(image, i, 5, (0, 255, 0), cv2.FILLED)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
# print(id,lm)
h, w, c = image.shape
cx, cy = int(lm.x * w), int(lm.y * h)
if id == 4:
tipx = cx
tipy = cy
if id == 8:
factor = 30
if (
tipx >= cx - factor
and tipx <= cx + factor
and tipy >= cy - factor
and tipy <= cy + factor
):
a = int((tipx + cx) / 2)
b = int((tipy + cy) / 2)
# within grid
if pencil and a >= 30 and a <= 420 and b >= 30 and b <= 420:
points.append((a, b))
elif a >= 500 and a <= 600 and b >= 50 and b <= 150:
pencil = True
undo = False
elif a >= 500 and a <= 600 and b >= 170 and b <= 270:
pencil = False
undo = True
if len(points) > 0:
points.pop()
elif a >= 500 and a <= 600 and b >= 290 and b <= 390:
pencil = False
undo = False
points = []
#if id ==0:
# cv2.circle(image, (cx, cy), 3, (255, 0, 255), cv2.FILLED)
#mpDraw.draw_landmarks(image, handLms, mp.solutions.hands.HAND_CONNECTIONS)
ret, jpg = cv2.imencode('.jpg', image)
detection = jpg.tobytes()
return detection