Face Detection Project Using Python & Arduino
Date: فبراير 25, 2025
Value: Free

👀 Face Detection Project Using Python & Arduino 🎯

Simple Explanation:
This project uses a camera to detect faces with Python. When a face is detected, a signal is sent to Arduino to turn on an LED. If no face is detected, the LED turns off. 🤖💡

 

 

🛠️ Requirements

🔹 Required Components

Component

Description

Arduino Uno

The microcontroller board for processing data.

Webcam

Captures real-time video for face detection.

LED

Lights up when a face is detected.

220-ohm resistor

Protects LED from excessive current.

Jumper wires

Used for wiring connections.

Computer

Runs the Python script.

🔹 Required Libraries

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.

🔹 Required Software

SoftwareInstallation LinkPurpose
Python (pip)Download PythonRuns the hand tracking script.
Arduino IDEDownload Arduino IDEUploads code to the Arduino board.
VS CodeDownload VS CodeCode editor for Python and Arduino.

📌 Steps to Follow:

🖥️ 1- Setting Up Python Environment:

make sure that you install the Python program and activate the pip function.

Then Open CMD in your PC and copy this comand

				
					pip install opencv-python pyserial
				
			
Ensure the Haar Cascade File is in the Same Directory: The face detection model, haarcascade_frontalface_default.xml, must be in the same folder as your Python script. If it’s missing, download it from OpenCV GitHub and place it in the correct location.

 

 

📌 Steps to Follow:

🖥️ 1- Setting Up Python Environment:

make sure that you install the Python program and activate the pip function.

Then Open CMD in your PC and copy this comand

🔌 2- Connecting the Components:

🔧 3- Uploading the Arduino Code:

Make sure you select the right port then upload this code to your Arduino using Arduino IDE:

				
					int ledPin = 13;  // LED connected to pin 13

void setup() {
    Serial.begin(9600);  // Start serial communication at 9600 baud
    pinMode(ledPin, OUTPUT);  // Set LED pin as output
}

void loop() {
    if (Serial.available() > 0) {
        // Read the incoming byte
        char command = Serial.read();

        if (command == '1') {
            digitalWrite(ledPin, HIGH);  // Turn on LED
            Serial.println("Face Detected: LED ON");
        } else if (command == '0') {
            digitalWrite(ledPin, LOW);  // Turn off LED
            Serial.println("No Face Detected: LED OFF");
        }
    }
}

				
			

2️⃣ Run the Python Code Below:

 please check the Python program you selected same port as the Arduino.
				
					import cv2
import serial
import time
# Set up serial communication (make sure to use the correct COM port for your system)
arduino = serial.Serial('COM9', 9600)  # Replace with the correct port for your system
time.sleep(2)  # Wait for Arduino to initialize

# Load the face cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Start capturing video
cap = cv2.VideoCapture(0)

while cap.isOpened():
    _, img = cap.read()

    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Detect faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    # If faces are detected, send '1' to Arduino, else send '0'
    if len(faces) > 0:
        arduino.write(b'1')  # Send '1' to indicate face detected
        print("Face detected")
    else:
        arduino.write(b'0')  # Send '0' to indicate no face detected
        print("No face detected")

    # Draw rectangles around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)

    # Display the image
    cv2.imshow('img', img)

    # Exit on pressing 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the video capture and close the serial port
cap.release()
arduino.close()
cv2.destroyAllWindows()

				
			
🚀 Testing the Project:
1️⃣ Run the Arduino code first.
2️⃣ Run the Python code and wait for the camera to start.
3️⃣ If a face appears in front of the camera, the LED will turn ON! 💡
4️⃣ If no face is detected, the LED will turn OFF.

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

Previous