-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesturephoto.py
More file actions
105 lines (85 loc) · 4.05 KB
/
gesturephoto.py
File metadata and controls
105 lines (85 loc) · 4.05 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
import cv2
import mediapipe as mp
import time
import numpy as np
# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
mp_draw = mp.solutions.drawing_utils
# Filters to cycle through
filters = [
None, # No filter
'GRAYSCALE', # Grayscale filter
'Se'
'SEPIA', # Sepia filter
'NEGATIVE' # Negative filter
'BLUR' # Blur filter
]
current_filter = 0 # Starting filter
# Webcam setup
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not access webcam.")
exit()
# Timestamp for debouncing gestures
last_action_time = 0
debounce_time = 1 # second debounce between actions
# Function to apply filters
def apply_filter(frame, filter_type):
if filter_type == 'GRAYSCALE':
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
elif filter_type == 'SEPIA':
sepia_filter = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
sepia_frame = cv2.transform(frame, sepia_filter)
sepia_frame = np.clip(sepia_frame, 0, 255) # Clip values to ensure valid range
return sepia_frame.astype(np.uint8)
elif filter_type == 'NEGATIVE':
return cv2.bitwise_not(frame)
elif filter_type == 'BLUR':
return cv2.GaussianBlur(frame, (15, 15), 0)
return frame
while True:
success, img = cap.read()
if not success:
print("Failed to read frame from webcam.")
break
img = cv2.flip(img, 1) # Flip the image for a mirror effect
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(img_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Get key landmarks
thumb_tip = hand_landmarks.landmarks[mp_hands.HandLandmark.THUMB_TIP]
index_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
middle_tip = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP]
ring_tip = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP]
pinky_tip = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_FINGER_TIP]
# Frame dimensions
frame_height, frame_width, _ = img.shape
# Convert normalized coordinates to pixel coordinates
thumb_tip_x, thumb_tip_y = int(thumb_tip.x * frame_width), int(thumb_tip.y * frame_height)
index_tip_x, index_tip_y = int(index_tip.x * frame_width), int(index_tip.y * frame_height)
middle_tip_x, middle_tip_y = int(middle_tip.x * frame_width), int(middle_tip.y * frame_height)
ring_tip_x, ring_tip_y = int(ring_tip.x * frame_width), int(ring_tip.y * frame_height)
pinky_tip_x, pinky_tip_y = int(pinky_tip.x * frame_width), int(pinky_tip.y * frame_height)
# Draw circles for landmarks
cv2.circle(img, (thumb_tip_x, thumb_tip_y), 10, (255, 0, 0), cv2.FILLED)
cv2.circle(img, (index_tip_x, index_tip_y), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (middle_tip_x, middle_tip_y), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (ring_tip_x, ring_tip_y), 10, (255, 255, 0), cv2.FILLED)
cv2.circle(img, (pinky_tip_x, pinky_tip_y), 10, (255, 0, 255), cv2.FILLED)
# Gesture logic
current_time = time.time()
# Click picture: Thumb touches Index finger
if abs (thumb_x - index_x) < 30 and abs (thumb_y - index_y) < 30:
if (current_time ,last_action_time) > debounce_time:
cv2.putText(img, "Picture Captured!", (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
last_action_time = current_time
cv2.imwrite(f"picture_{int(time.time())}.jpg", img)
print("Picture saved!")
# Change filter: Thumb touches any other finger
elif (abs (thumb_x, middle_x))