-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (60 loc) · 2.64 KB
/
main.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
import cv2
import numpy as np
# Load the cascade classifier for hand detection
cascade_classifier = cv2.CascadeClassifier('haarcascade_hand.xml')
# Function to count fingers
def count_fingers(contour):
hull = cv2.convexHull(contour, returnPoints=False)
if len(hull) > 3:
defects = cv2.convexityDefects(contour, hull)
if defects is not None:
finger_count = 0
for i in range(defects.shape[0]):
s, e, f, d = defects[i, 0]
start = tuple(contour[s][0])
end = tuple(contour[e][0])
far = tuple(contour[f][0])
a = np.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = np.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = np.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c))
if angle <= np.pi / 2:
finger_count += 1
return finger_count
return 0
# Capture video from webcam
cap = cv2.VideoCapture(0)
while True:
# Read a frame from the video capture
ret, frame = cap.read()
if not ret:
break
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect hands
hands = cascade_classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Draw bounding boxes around the detected hands and count fingers
for (x, y, w, h) in hands:
# Draw rectangle around the hand
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Extract region of interest (ROI) for hand
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
# Threshold the ROI
ret, thresh = cv2.threshold(roi_gray, 127, 255, 0)
# Find contours in the thresholded image
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Count fingers using contour analysis
for contour in contours:
if cv2.contourArea(contour) > 1000:
fingers = count_fingers(contour)
# Draw finger count on the frame
cv2.putText(frame, f'Fingers: {fingers}', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Display the frame
cv2.imshow('Hand Detection', frame)
# Check for key press and exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()