Aayush Rawat
Published

iLLume – Gesture-Based Smart Control for All

A touch-free, gesture-based switch system that makes everyday spaces more accessible, hygienic, and easy to use for everyone.

IntermediateWork in progress15 hours1
iLLume – Gesture-Based Smart Control for All

Things used in this project

Story

Read more

Code

Python Code

Python
This script uses computer vision to detect hand gestures through a webcam and counts the number of fingers shown in real time.
import cv2
import mediapipe as mp
import serial
import time

# ==== SETUP SERIAL COMMUNICATION WITH ARDUINO ====  
try:
    arduino = serial.Serial('COM3', 9600)  # Replace with your port
    time.sleep(2)
    print("✅ Connected to Arduino on COM7")
except:
    print("❌ Could not connect to Arduino. Check the COM port.")
    arduino = None

# ==== MEDIAPIPE HAND TRACKING SETUP ====
mp_hands = mp.solutions.hands
mp_draw = mp.solutions.drawing_utils
hands = mp_hands.Hands(
    max_num_hands=1,
    min_detection_confidence=0.7,
    min_tracking_confidence=0.7
)

# ==== TRY DIFFERENT CAMERA INDEXES ====
print("🔍 Trying to access webcam...")

cap = None
for i in range(3):
    temp_cap = cv2.VideoCapture(i)
    if temp_cap.isOpened():
        cap = temp_cap
        print(f"✅ Camera opened successfully on index {i}")
        break
    else:
        print(f"❌ Camera not found on index {i}")
        temp_cap.release()

if cap is None or not cap.isOpened():
    print("🚫 No working webcam found. Exiting...")
    exit()

# ==== FUNCTION TO DETERMINE FINGER STATES ====
def get_finger_status(landmarks):
    finger_tips = [4, 8, 12, 16, 20]  # Thumb, Index, Middle, Ring, Pinky
    fingers = []

    # Thumb: compare x-axis
    if landmarks[finger_tips[0]].x < landmarks[finger_tips[0] - 1].x:
        fingers.append(1)
    else:
        fingers.append(0)

    # Other fingers: compare y-axis
    for tip in finger_tips[1:]:
        if landmarks[tip].y < landmarks[tip - 2].y:
            fingers.append(1)
        else:
            fingers.append(0)

    return ''.join(str(bit) for bit in fingers)  # e.g. '10101'

# ==== MAIN LOOP ====
while True:
    ret, frame = cap.read()
    if not ret:
        print("⚠ Failed to grab frame.")
        continue

    frame = cv2.flip(frame, 1)
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(rgb)

    if result.multi_hand_landmarks:
        for hand_landmarks in result.multi_hand_landmarks:
            mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)

            command = get_finger_status(hand_landmarks.landmark)
            print("✋ Gesture Command:", command)

            # Send command to Arduino
            if arduino:
                arduino.write((command + "\n").encode())
                time.sleep(0.1)

    cv2.imshow("🤖 Hand Gesture Control", frame)

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

# ==== CLEANUP ====
cap.release()
cv2.destroyAllWindows()
if arduino:
    arduino.close()

Credits

Aayush Rawat
1 project • 0 followers

Comments