-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (86 loc) · 3.69 KB
/
main.py
File metadata and controls
105 lines (86 loc) · 3.69 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
import cv2
import mediapipe as mp
import numpy as np
import json
from collections import deque
class HandDrawingApp:
def __init__(self):
self.mp_hands = mp.solutions.hands
self.mp_drawing = mp.solutions.drawing_utils
self.hands = self.mp_hands.Hands(
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7
)
self.cap = cv2.VideoCapture(0)
self.drawing = False
self.drawn_points = []
self.smooth_buffer = deque(maxlen=5)
self.brush_color = (255, 0, 0)
self.brush_size = 3
def calculate_distance(self, p1, p2):
return np.linalg.norm(np.array(p1) - np.array(p2))
def clear_canvas(self):
self.drawn_points = []
def save_canvas(self, frame):
cv2.imwrite("drawing_output.png", frame)
with open("drawing_data.json", "w") as f:
json.dump(self.drawn_points, f)
print("Drawing saved as image and JSON")
def run(self):
while self.cap.isOpened():
ret, frame = self.cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
h, w, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = self.hands.process(rgb_frame)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
thumb_tip = (
hand_landmarks.landmark[self.mp_hands.HandLandmark.THUMB_TIP].x * w,
hand_landmarks.landmark[self.mp_hands.HandLandmark.THUMB_TIP].y * h
)
index_tip = (
hand_landmarks.landmark[self.mp_hands.HandLandmark.INDEX_FINGER_TIP].x * w,
hand_landmarks.landmark[self.mp_hands.HandLandmark.INDEX_FINGER_TIP].y * h
)
distance = self.calculate_distance(thumb_tip, index_tip)
self.drawing = distance < 35
if self.drawing:
self.smooth_buffer.append(index_tip)
avg_x = int(np.mean([p[0] for p in self.smooth_buffer]))
avg_y = int(np.mean([p[1] for p in self.smooth_buffer]))
point = (avg_x, avg_y)
self.drawn_points.append(point)
cv2.circle(frame, point, 5, (0, 255, 0), -1)
self.mp_drawing.draw_landmarks(
frame, hand_landmarks, self.mp_hands.HAND_CONNECTIONS
)
for i in range(1, len(self.drawn_points)):
if self.drawn_points[i - 1] and self.drawn_points[i]:
cv2.line(frame, self.drawn_points[i - 1], self.drawn_points[i], self.brush_color, self.brush_size)
cv2.imshow("Hand Drawing", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
elif key == ord("c"):
self.clear_canvas()
elif key == ord("s"):
self.save_canvas(frame)
elif key == ord("1"):
self.brush_color = (255, 0, 0)
elif key == ord("2"):
self.brush_color = (0, 255, 0)
elif key == ord("3"):
self.brush_color = (0, 0, 255)
elif key == ord("+"):
self.brush_size = min(10, self.brush_size + 1)
elif key == ord("-"):
self.brush_size = max(1, self.brush_size - 1)
self.cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
app = HandDrawingApp()
app.run()