-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint2323.py
More file actions
64 lines (49 loc) · 2.41 KB
/
paint2323.py
File metadata and controls
64 lines (49 loc) · 2.41 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
import mediapipe as mp
import cv2
import numpy as np
# Initialize MediaPipe hands module
handsModule = mp.solutions.hands
# Initialize video capture
cap = cv2.VideoCapture(0)
# Confidence values and settings for hand tracking
with handsModule.Hands(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7, max_num_hands=1) as hands:
# Create a black canvas to draw on, the same size as the frames from the camera
canvas = None
prev_x, prev_y = None, None # Previous position of the index finger
while True:
ret, frame = cap.read()
if not ret:
break
# Flip the frame vertically
frame = cv2.flip(frame, 1)
# Resize the frame for performance
frame1 = cv2.resize(frame, (640, 480))
# Initialize canvas if it is None (first loop)
if canvas is None:
canvas = np.zeros_like(frame1) # Black canvas to draw on
# Process the frame for hand landmarks
results = hands.process(cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB))
# If hand landmarks are found, track only the index finger (no drawing hand framework)
if results.multi_hand_landmarks is not None:
for handLandmarks in results.multi_hand_landmarks:
# Find the location of the index finger (landmark 8)
index_finger_tip = handLandmarks.landmark[handsModule.HandLandmark.INDEX_FINGER_TIP]
index_x, index_y = int(index_finger_tip.x * 640), int(index_finger_tip.y * 480)
# If previous index finger position exists, draw a white line
if prev_x is not None and prev_y is not None:
cv2.line(canvas, (prev_x, prev_y), (index_x, index_y), (255, 255, 255), 5)
# Update the previous position to current position
prev_x, prev_y = index_x, index_y
else:
# Reset previous position if no hand is detected
prev_x, prev_y = None, None
# Combine the canvas and the current frame
frame_with_drawing = cv2.add(frame1, canvas)
# Show the output
cv2.imshow("Frame", frame_with_drawing)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"): # Stop if 'q' is pressed
break
# Release the camera and close all windows
cap.release()
cv2.destroyAllWindows()