Face Detection Project Using Python & Arduino
Face Detection
This project detects hand gestures using MediaPipe in Python. The number of raised fingers is counted and sent to Arduino, which then turns on the corresponding number of LEDs. 🖐️➡️💡
🛠️ Requirements
🔹 Required Components
Component | Description |
Arduino Uno | Microcontroller to process data. |
Webcam | Captures real-time video for hand detection. |
5 LEDs | LEDs that light up based on the detected fingers. |
Resistors | Protect LEDs from excessive current. |
Jumper wires | Used for wiring connections. |
Computer | Runs the Python script. |
Library | Installation Command | Purpose |
OpenCV | pip install opencv-python | Used for image processing. |
MediaPipe | pip install mediapipe | Detects hand gestures. |
PySerial | pip install pyserial | Enables communication between Python & Arduino. |
Software | Installation Link | Purpose |
Python (pip) | Download Python | Runs the hand tracking script. |
Arduino IDE | Download Arduino IDE | Uploads code to the Arduino board. |
VS Code | Download VS Code | Code editor for Python and Arduino. |
pip install opencv-python mediapipe pyserial
int ledPins[] = {6,7,8,9,10}; // Pins for 5 LEDs
int numLeds = 5;
void setup() {
// Initialize LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Start serial communication
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// Read the number of fingers from serial input
int fingers = Serial.parseInt();
// Turn on LEDs based on the number of fingers
for (int i = 0; i < numLeds; i++) {
if (i < fingers) {
digitalWrite(ledPins[i], HIGH); // Turn on LED
} else {
digitalWrite(ledPins[i], LOW); // Turn off LED
}
}
}
}
import cv2
import mediapipe as mp
import serial
import time
# Initialize serial communication (update COM port as per your system)
arduino = serial.Serial('COM4', 9600) # Replace 'COM4' with your Arduino port
time.sleep(2) # Wait for Arduino to initialize
# Initialize MediaPipe Hand Detection
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mp_draw = mp.solutions.drawing_utils
# Helper function to count raised fingers
def count_raised_fingers(hand_landmarks, handedness_label):
finger_tips = [8, 12, 16, 20] # Index for fingertips
thumb_tip = 4 # Thumb tip
thumb_mcp = 2 # Thumb MCP (base)
count = 0
landmarks = hand_landmarks.landmark
# Check fingers (tip above middle joint)
for tip in finger_tips:
if landmarks[tip].y < landmarks[tip - 2].y:
count += 1
# REVERSED: Treat Right as Left, and Left as Right
if handedness_label == 'Left':
if landmarks[thumb_tip].x < landmarks[thumb_mcp].x:
count += 1
else: # Right hand (treated as Left)
if landmarks[thumb_tip].x > landmarks[thumb_mcp].x:
count += 1
return count
# OpenCV video capture
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
if not success:
break
# Convert to RGB for MediaPipe
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(img_rgb)
if results.multi_hand_landmarks and results.multi_handedness:
for hand_landmarks, handedness in zip(results.multi_hand_landmarks, results.multi_handedness):
# Get hand label (Left or Right) and REVERSE it
actual_label = handedness.classification[0].label
swapped_label = 'Left' if actual_label == 'Right' else 'Right'
# Draw landmarks
mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Count raised fingers
finger_count = count_raised_fingers(hand_landmarks, swapped_label)
# Display swapped hand and finger count
cv2.putText(img, f"{swapped_label} Hand", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.putText(img, f"Fingers: {finger_count}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Send finger count to Arduino
arduino.write(f"{finger_count}\n".encode())
# Show the image
cv2.imshow("Hand Tracking", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Cleanup
cap.release()
cv2.destroyAllWindows()
arduino.close()
1️⃣ Upload the Arduino code first.
2️⃣ Run the Python code and wait for the camera to start.
3️⃣ Raise your fingers in front of the camera:
4️⃣ Close your hand (0 fingers) = All LEDs OFF 🔄
Our Completed Projects
Face Detection
AI arduino project controlled by hand
روح المثابرة ومحبة العلم
إن فضولي العميق للمعرفة، بالإضافة إلى قدرتي القوية على التعلم الذاتي، ساهم بشكل كبير في تشكيل شخصيتي القوية ومتعددة المواهب في مختلف المجالات.