0% found this document useful (0 votes)
1 views2 pages

Handgesture

The document contains a Python script that uses OpenCV to capture video from a webcam and detect finger positions and names using specified landmarks. It counts how many fingers are raised or lowered based on their positions and displays the results in real-time. The script continues to run until the 'q' key is pressed, at which point it releases the webcam and closes all OpenCV windows.

Uploaded by

merintijaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Handgesture

The document contains a Python script that uses OpenCV to capture video from a webcam and detect finger positions and names using specified landmarks. It counts how many fingers are raised or lowered based on their positions and displays the results in real-time. The script continues to run until the 'q' key is pressed, at which point it releases the webcam and closes all OpenCV windows.

Uploaded by

merintijaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import cv2

from collections import Counter


from module import findnameoflandmark, findpostion

# Indices for fingertips and their corresponding landmarks


tip_indices = [8, 12, 16, 20]

# Open the webcam


cap = cv2.VideoCapture(0)

while True:
# Read frame from webcam
ret, frame = cap.read()
if not ret:
print("Failed to capture image")
break

# NOTE: Removed the flipping of the frame to avoid rotation issues.

# Find positions and landmark names


landmarks_positions = findpostion(frame)
landmark_names = findnameoflandmark(frame)

if landmarks_positions and landmark_names:


fingers = []
# Check thumb (index 0) separately because it moves differently from other
fingers
if landmarks_positions[4][1] > landmarks_positions[3][1]: # Adjusted logic
fingers.append(1)
print(landmark_names[4])
else:
fingers.append(0)

# Check other four fingers


for i in range(4):
if landmarks_positions[tip_indices[i]][2] <
landmarks_positions[tip_indices[i] - 2][2]:
fingers.append(1)
print(landmark_names[tip_indices[i]])
else:
fingers.append(0)

# Count how many fingers are up or down


finger_count = Counter(fingers)
up_count = finger_count[1]
down_count = finger_count[0]

print("Fingers up:", up_count)


print("Fingers down:", down_count)

# Display the original frame (not flipped)


cv2.imshow("Frame", frame)

# Break the loop on 'q' key press


if cv2.waitKey(1) & 0xFF == ord("q"):
break

# Release resources
cap.release()
cv2.destroyAllWindows()

You might also like