-
Notifications
You must be signed in to change notification settings - Fork 0
/
Virtual_Paint.py
89 lines (69 loc) · 2.26 KB
/
Virtual_Paint.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
import cv2
import numpy as np
import mediapipe as mp
# Initialize Mediapipe Hand model
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(min_detection_confidence=0.8, min_tracking_confidence=0.8)
mp_draw = mp.solutions.drawing_utils
# Initialize the canvas
canvas = np.ones((480, 640, 3), dtype="uint8") * 255
# Brush parameters
brush_thickness = 3
brush_color = (114, 161, 223) # SkyBlue Color color in BGR
# Drawing state
drawing = False
ix, iy = -1, -1
# Start video capture
cap = cv2.VideoCapture(0)
def select_color(key):
global brush_color
if key == ord('r'):
brush_color = (0, 0, 255) # Red
elif key == ord('g'):
brush_color = (0, 255, 0) # Green
elif key == ord('b'):
brush_color = (255, 0, 0) # Blue
elif key == ord('y'):
brush_color = (0, 255, 255) # Yellow
elif key == ord('k'):
brush_color = (0, 0, 0) # Black
elif key == ord('w'):
brush_color = (255, 255, 255) # White (eraser)
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
h, w, c = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb_frame)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
for id, lm in enumerate(hand_landmarks.landmark):
cx, cy = int(lm.x * w), int(lm.y * h)
if id == 5: # Index finger tip
if drawing:
cv2.line(canvas, (ix, iy), (cx, cy), brush_color, brush_thickness)
ix, iy = cx, cy
mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
cv2.imshow('Paint', canvas)
cv2.imshow('Camera', frame)
key = cv2.waitKey(1) & 0xFF
# Brush size control
if key == ord('+') and brush_thickness < 20:
brush_thickness += 1
elif key == ord('-') and brush_thickness > 1:
brush_thickness -= 1
# Color selection
select_color(key)
# Clear canvas
if key == ord('c'):
canvas = np.ones((480, 640, 3), dtype="uint8") * 255
# Start/Stop drawing
if key == ord('d'):
drawing = not drawing
# Exit
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()