0% found this document useful (0 votes)
5 views9 pages

Homework Assignment 2

The document outlines the development of a WOW hand gesture recognition system utilizing MediaPipe Hand Landmarker for hand detection and Hough Transform for geometric analysis. It details the processes of hand landmark extraction, skeleton visualization, thumb-index circle detection, finger extension analysis, and gesture classification. The system identifies gestures in real-time, displaying results on a video feed, with specific conditions for recognizing WOW and PALM gestures.

Uploaded by

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

Homework Assignment 2

The document outlines the development of a WOW hand gesture recognition system utilizing MediaPipe Hand Landmarker for hand detection and Hough Transform for geometric analysis. It details the processes of hand landmark extraction, skeleton visualization, thumb-index circle detection, finger extension analysis, and gesture classification. The system identifies gestures in real-time, displaying results on a video feed, with specific conditions for recognizing WOW and PALM gestures.

Uploaded by

Arnav Bansal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Homework Assignment 2

Develop a WOW hand gesture recognition system using the following:

● MediaPipe Hand Landmarker (for detecting hand landmarks)

Link: [Link]

● Hough Transform (for detecting circles and lines formed by the landmarks)

Hand Detection and Landmark Extraction

The system uses the MediaPipe Hand Landmarker to detect a hand from a live webcam feed
and extract 21 precise landmarks representing the wrist, finger joints, and fingertips. These
landmarks provide accurate spatial information about the hand structure and remain stable
across different lighting conditions and backgrounds. The extracted landmark coordinates
form the core input for all subsequent processing stages.
Skeleton Visualization

After landmark extraction, the coordinates are converted into pixel values and connected
using predefined landmark indices to form a hand skeleton. Red points represent the joints,
while blue lines represent finger bones. This skeletal visualization helps in understanding
finger orientation, extension, and overall hand posture in real time, making the system more
interpretable.

Thumb–Index Circle Detection

To detect gestures involving finger interaction, the system analyzes the geometric relationship
between the thumb and index finger. When their fingertips are sufficiently close, a circular
shape is formed. The center and radius of this circle are computed and drawn on the video
frame, and shape-based reasoning inspired by the Hough Transform is used to confirm the
presence of the circular pattern, which is essential for recognizing the WOW gesture.

Finger Extension Analysis


Each finger is analyzed to determine whether it is extended or folded. This is achieved by
comparing the distance between the fingertip and the wrist with the distance between an
intermediate finger joint and the wrist. If the fingertip is farther from the wrist, the finger is
considered extended. This method provides a simple and effective way to characterize finger
states without requiring machine learning.

Gesture Classification and Output

Based on the detected circle and finger extension states, the system classifies the hand
posture into predefined gestures. A WOW gesture is identified when a thumb–index circle is
present and the remaining fingers are extended, while a PALM gesture is detected when all
fingers are extended without a circular shape. Gestures that do not meet these conditions are
labeled as UNKNOWN. The recognized gesture is displayed in real time on the video feed
along with the hand skeleton, providing a clear and interactive output.
!pip install mediapipe opencv-python numpy

Requirement already satisfied: mediapipe in c:\users\lenovo\anaconda3\


lib\site-packages (0.10.32)
Requirement already satisfied: opencv-python in c:\users\lenovo\
anaconda3\lib\site-packages ([Link])
Requirement already satisfied: numpy in c:\users\lenovo\anaconda3\lib\
site-packages (2.3.5)
Requirement already satisfied: absl-py~=2.3 in c:\users\lenovo\
anaconda3\lib\site-packages (from mediapipe) (2.4.0)
Requirement already satisfied: sounddevice~=0.5 in c:\users\lenovo\
anaconda3\lib\site-packages (from mediapipe) (0.5.5)
Requirement already satisfied: flatbuffers~=25.9 in c:\users\lenovo\
anaconda3\lib\site-packages (from mediapipe) (25.12.19)
Requirement already satisfied: opencv-contrib-python in c:\users\
lenovo\anaconda3\lib\site-packages (from mediapipe) ([Link])
Requirement already satisfied: matplotlib in c:\users\lenovo\
anaconda3\lib\site-packages (from mediapipe) (3.10.6)
Requirement already satisfied: cffi in c:\users\lenovo\anaconda3\lib\
site-packages (from sounddevice~=0.5->mediapipe) (2.0.0)
Requirement already satisfied: pycparser in c:\users\lenovo\anaconda3\
lib\site-packages (from cffi->sounddevice~=0.5->mediapipe) (2.23)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\lenovo\
anaconda3\lib\site-packages (from matplotlib->mediapipe) (1.3.3)
Requirement already satisfied: cycler>=0.10 in c:\users\lenovo\
anaconda3\lib\site-packages (from matplotlib->mediapipe) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\lenovo\
anaconda3\lib\site-packages (from matplotlib->mediapipe) (4.60.1)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\lenovo\
anaconda3\lib\site-packages (from matplotlib->mediapipe) (1.4.9)
Requirement already satisfied: packaging>=20.0 in c:\users\lenovo\
anaconda3\lib\site-packages (from matplotlib->mediapipe) (25.0)
Requirement already satisfied: pillow>=8 in c:\users\lenovo\anaconda3\
lib\site-packages (from matplotlib->mediapipe) (12.0.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\lenovo\
anaconda3\lib\site-packages (from matplotlib->mediapipe) (3.2.5)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\
lenovo\anaconda3\lib\site-packages (from matplotlib->mediapipe)
(2.9.0.post0)
Requirement already satisfied: six>=1.5 in c:\users\lenovo\anaconda3\
lib\site-packages (from python-dateutil>=2.7->matplotlib->mediapipe)
(1.17.0)

import [Link]

url =
"[Link]
landmarker/float16/1/hand_landmarker.task"
output_path = "hand_landmarker.task"
[Link](url, output_path)

print("Model downloaded successfully")

Model downloaded successfully

import os
print([Link]("hand_landmarker.task"))

True

import cv2
import numpy as np
import mediapipe as mp
from [Link] import python
from [Link] import vision

# ----------------------------
# Load MediaPipe model
# ----------------------------
model_path = "hand_landmarker.task"

BaseOptions = [Link]
HandLandmarker = [Link]
HandLandmarkerOptions = [Link]
VisionRunningMode = [Link]

options = HandLandmarkerOptions(
base_options=BaseOptions(model_asset_path=model_path),
running_mode=[Link],
num_hands=1
)

landmarker = HandLandmarker.create_from_options(options)

# ----------------------------
# WOW detection function
# ----------------------------

FINGER_CONNECTIONS = [
(0, 1), (1, 2), (2, 3), (3, 4),
(0, 5), (5, 6), (6, 7), (7, 8),
(0, 9), (9,10), (10,11), (11,12),
(0,13), (13,14), (14,15), (15,16),
(0,17), (17,18), (18,19), (19,20)
]
def detect_wow(landmarks, image_shape):
h, w = image_shape[:2]

pts = []
for lm in landmarks:
[Link]((int(lm.x * w), int(lm.y * h)))

# Blank mask for Hough


mask = [Link]((h, w), dtype=np.uint8)

for p in pts:
[Link](mask, p, 6, 255, -1)

thumb = pts[4]
index = pts[8]

center = ((thumb[0] + index[0]) // 2, (thumb[1] + index[1]) // 2)


radius = int([Link]([Link](thumb) - [Link](index)) /
2)

if radius > 8:
[Link](mask, center, radius, 255, 2)

# Hough Circle
circles = [Link](
mask,
cv2.HOUGH_GRADIENT,
dp=1.2,
minDist=50,
param1=100,
param2=15,
minRadius=10,
maxRadius=80
)

circle_found = circles is not None

# Hough Lines
edges = [Link](mask, 50, 150)
lines = [Link](
edges,
rho=1,
theta=[Link] / 180,
threshold=50,
minLineLength=40,
maxLineGap=10
)

long_lines = 0
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
if [Link](x2 - x1, y2 - y1) > 60:
long_lines += 1
# WOW condition
return circle_found and long_lines >= 3

def distance(p1, p2):


return [Link]([Link](p1) - [Link](p2))

def finger_extended(tip, pip, wrist):


return distance(tip, wrist) > distance(pip, wrist)

def classify_gesture(pts):
wrist = pts[0]

thumb_tip, thumb_ip = pts[4], pts[3]


index_tip, index_pip = pts[8], pts[6]
middle_tip, middle_pip = pts[12], pts[10]
ring_tip, ring_pip = pts[16], pts[14]
pinky_tip, pinky_pip = pts[20], pts[18]

thumb_index_dist = distance(thumb_tip, index_tip)

index_ext = finger_extended(index_tip, index_pip, wrist)


middle_ext = finger_extended(middle_tip, middle_pip, wrist)
ring_ext = finger_extended(ring_tip, ring_pip, wrist)
pinky_ext = finger_extended(pinky_tip, pinky_pip, wrist)

# WOW gesture
if thumb_index_dist < 40 and middle_ext and ring_ext and
pinky_ext:
return "WOW"

# PALM gesture
if index_ext and middle_ext and ring_ext and pinky_ext:
return "PALM"

return "UNKNOWN"

# ----------------------------
# Webcam loop
# ----------------------------

cap = [Link](0)
timestamp = 0

while [Link]():
ret, frame = [Link]()
if not ret:
break

rgb = [Link](frame, cv2.COLOR_BGR2RGB)


mp_image = [Link](image_format=[Link], data=rgb)
result = landmarker.detect_for_video(mp_image, timestamp)
timestamp += 1

if result.hand_landmarks:
hand_landmarks = result.hand_landmarks[0]

# WOW detection
if detect_wow(hand_landmarks, [Link]):
[Link](
frame,
"WOW DETECTED",
(40, 80),
cv2.FONT_HERSHEY_SIMPLEX,
2,
(0, 255, 0),
4
)

# ----------------------------
# Drawing (CORRECTLY INDENTED)
# ----------------------------
h, w = [Link][:2]

pts = []
for lm in hand_landmarks:
[Link]((int(lm.x * w), int(lm.y * h)))

gesture = classify_gesture(pts)

[Link](
frame,
f"GESTURE: {gesture}",
(30, 40),
cv2.FONT_HERSHEY_SIMPLEX,
1.2,
(0, 255, 255),
3
)

# Draw finger lines


for start, end in FINGER_CONNECTIONS:
[Link](
frame,
pts[start],
pts[end],
(255, 0, 0),
2
)

# Draw landmark points


for p in pts:
[Link](frame, p, 4, (0, 0, 255), -1)

# Draw thumb–index circle


thumb = pts[4]
index = pts[8]

center = (
(thumb[0] + index[0]) // 2,
(thumb[1] + index[1]) // 2
)
radius = int([Link]([Link](thumb) - [Link](index))
/ 2)

if radius > 8:
[Link](frame, center, radius, (0, 255, 0), 3)

[Link]("WOW Hand Gesture Recognition", frame)

if [Link](1) & 0xFF == 27:


break

[Link]()
[Link]()

You might also like