1int ledPin = 9; // PWM pin for the LED
2
3void setup() {
4 // Start serial communication at 9600 baud rate
5 Serial.begin(9600);
6 pinMode(ledPin, OUTPUT);
7}
8
9void loop() {
10 // Check if Python is sending data
11 if (Serial.available() > 0) {
12 // Read the incoming byte (0 to 255)
13 int brightness = Serial.read();
14
15 // Apply the brightness to the LED
16 analogWrite(ledPin, brightness);
17 }
18}
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37//Python code
38
39
40
41
42
43
44
45
46import cv2
47import mediapipe as mp
48import serial
49import math
50import numpy as np
51
52# --- SET YOUR ARDUINO PORT HERE ---
53# Windows: 'COM3', 'COM4', etc.
54# Mac/Linux: '/dev/cu.usbmodem14101' or similar
55arduino = serial.Serial('COM3', 9600)
56
57# Initialize MediaPipe Hand Tracking
58mp_hands = mp.solutions.hands
59hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
60mp_draw = mp.solutions.drawing_utils
61
62# Open the webcam
63cap = cv2.VideoCapture(0)
64
65while True:
66 success, img = cap.read()
67 if not success:
68 break
69
70 img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
71 results = hands.process(img_rgb)
72
73 if results.multi_hand_landmarks:
74 for hand_landmarks in results.multi_hand_landmarks:
75 # Get coordinates of Thumb Tip (Landmark 4) and Index Tip (Landmark 8)
76 thumb_x, thumb_y = hand_landmarks.landmark[4].x, hand_landmarks.landmark[4].y
77 index_x, index_y = hand_landmarks.landmark[8].x, hand_landmarks.landmark[8].y
78
79 # Convert normalized coordinates to actual pixel values
80 h, w, _ = img.shape
81 thumb_x, thumb_y = int(thumb_x * w), int(thumb_y * h)
82 index_x, index_y = int(index_x * w), int(index_y * h)
83
84 # Draw circles on the fingertips and a line between them for a cool visual
85 cv2.circle(img, (thumb_x, thumb_y), 10, (0, 255, 255), cv2.FILLED)
86 cv2.circle(img, (index_x, index_y), 10, (0, 255, 255), cv2.FILLED)
87 cv2.line(img, (thumb_x, thumb_y), (index_x, index_y), (255, 0, 0), 3)
88
89 # Calculate the distance between the two fingers
90 length = math.hypot(index_x - thumb_x, index_y - thumb_y)
91
92 # Map the finger distance to the LED brightness (PWM range: 0 to 255)
93 # You might need to adjust the [30, 200] range depending on how close you are to the camera
94 brightness = np.interp(length, [30, 200], [0, 255])
95 brightness = int(brightness)
96
97 # Send the brightness value to the Arduino as a single byte
98 arduino.write(bytes([brightness]))
99
100 # Draw the rest of the hand skeleton
101 mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS)
102
103 # Show the webcam feed
104 cv2.imshow("Gesture Controller", img)
105
106 # Press 'q' to quit
107 if cv2.waitKey(1) & 0xFF == ord('q'):
108 break
109
110# Clean up and close connections
111cap.release()
112cv2.destroyAllWindows()
113arduino.close()