ESP32 Projects

Face recognition based on ESP32-CAM

Nowadays, security is everyone’s biggest concern, whether it is data security or home security. With the advancement of technology and the increasing popularity of the Internet of Things, digital door locks have become very common recently. Digital locks do not require any physical keys, but they can use RFID, fingerprints, facial recognition codes, and passwords to control the door lock. Previously, we have developed many digital door lock applications using these various technologies. In this article, we use ESP32-CAM to make a Face ID controlled digital door access system.

The AI-Thinker ESP32-CAM module is a low-cost development board with a very compact OV2640 camera and a micro SD card slot. It has an ESP32 S chip with built-in Wi-Fi and Bluetooth connectivity, as well as 2 high -performance 32-bit LX6 CPUs and a 7-stage pipeline architecture. In this article, we will use the ESP32-CAM and relay modules and electromagnetic locks to make a face recognition based access control system.

Required Components

● ESP32 CAM module

● FTDI circuit board

● Relay module

● Electromagnetic lock

● Jumper

Electromagnetic lock

Electromagnetic locks are based on an electromagnetic mechanical lock mechanism. This type of lock has a panel with an angled cutout and a good mounting bracket. When powered, the DC creates a magnetic field that moves the panel inside and holds the door in the unlocked position . The panel will maintain its position until the power is removed. When the power is disconnected, the panel moves outward and locks the door. In the locked state, it does not use any power. To drive the electromagnetic lock, you need a power supply that can provide 12V @ 500mA.

Face recognition based on ESP32-CAM

Circuit Schematic

The circuit diagram of the ESP32-CAM face recognition access control system is as follows:

Face recognition based on ESP32-CAM

The circuit above combines the FTDI board, relay module, and electromagnetic lock. The FTDI board is used to flash the code into the ESP32-CAM as it does not have a USB connector, while the module is used to open or close the electromagnetic lock. The VCC and GND pins of the FTDI board and relay module are connected to the Vcc and GND pins of the ESP32-CAM. The TX and RX of the FTDI board are connected to the RX and TX of the ESP32, and the IN pin of the relay module is connected to the IO4 of the ESP32-CAM.

NOTE: Before uploading the code, connect IO0 to ground. IO0 determines whether the ESP32 is in blinking mode or not. When GPIO 0 is connected to GND, the ESP32 is in blinking mode.

After connecting the hardware according to the circuit diagram, the effect should be as follows:

Installing the ESP32 Development Board on the Arduino IDE

In this article, we will use Arduino IDE to program ESP32-CAM. So, first, install the ESP32 plugin on Arduino IDE.

 

To install the ESP32 board in the Arduino IDE, go to File > Preferences .

 

Next, copy the following link and paste it into the Additional Board Manager URLs field as shown in the image below. Then, click the OK button: https://dl.espressif.com/dl/package_esp32_index.json

Now go to Tools > Board > Boards Manager .

 

In the Board Manager, search for ESP32 and install ” ESP32 by Espressif Systems “.

 

Code Description

The complete code is divided into four parts. One of them is the main code for the camera and relay module, where the ESP32 locks or unlocks the door based on face recognition, and the other three codes are for the web page, camera index, and camera pins. Here, we explain some important parts of the code.

 

First include all the library files.

  1. #include “esp_camera.h”
  2. #include <WiFi.h>
  3. #include “camera_pins.h”

Copy code

In the next line, uncomment the camera module used with the ESP32. In the code, five different camera models are defined. In this article, we are using the AI-THINKER model.

  1. //#define CAMERA_MODEL_WROVER_KIT
  2. //#define CAMERA_MODEL_ESP_EYE
  3. //#define CAMERA_MODEL_M5STACK_PSRAM
  4. //#define CAMERA_MODEL_M5STACK_WIDE
  5. #define CAMERA_MODEL_AI_THINKER

Copy code

Afterwards, substitute your network credentials into the following variables:

  1. const char* ssid = “Wi-Fi Name”;
  2. const char* password = “Wi-Fi password”;

Copy code

Then define the pins to which the relay module is connected. We will use the millis() function to lock the door after it has been unlocked for the defined time interval, here 5 seconds.

  1. #define relay 4
  2. long prevMillis = 0;
  3. int interval = 5000;

Copy code

In the setup() function, the serial monitor is initialized for debugging at a baud rate of 115200. Then, in the next few lines of code, the pin mode of the relay module is defined and the relay is initialized to a low level.

  1. void setup() {
  2.   Serial.begin(115200);
  3.   pinMode(relay, OUTPUT);
  4.   digitalWrite(relay, LOW);

Copy code

Inside the loop() function, check if the face matches the registered face. If it matches, unlock the door for 5 seconds and then lock the door again after 5 seconds.

  1. void loop() {
  2.     if (matchFace == true && activeRelay == false){
  3.       activeRelay = true;
  4.       digitalWrite (relay, HIGH);
  5.       prevMillis = millis();
  6.     }
  7.     if(activeRelay == true && millis()- prevMillis > interval){
  8.       activeRelay = false;
  9.       matchFace = false;
  10.       digitalWrite(relay, LOW);
  11.     }

Copy code

Testing the ESP32-CAM Face Recognition Access Control System

Finally, upload the code, connect the FDTI board to your laptop, and select ” ESP32 Wrover Module ” for the board. Also, change other settings as shown in the following image:

Face recognition based on ESP32-CAM

Before uploading the code, don’t forget to connect the IO0 pin to GND, then press the ESP32 reset button and hit the upload button.

After uploading the code, remove the connection between IO0 and GND pins. Then open the serial monitor and change the baud rate to 115200. Then, press the ESP32 reset button and it will print the ESP IP address and port number on the serial monitor.

Face recognition based on ESP32-CAM

Now navigate to your browser and enter the ESP IP address copied from the serial monitor to access the video stream. It will take you to the streaming page. To start the video streaming, click on the “Start Stream” button at the bottom of the page.

Face recognition based on ESP32-CAM

To recognize a face using ESP32-CAM, first, we have to enroll the face. To do this, enable the facial recognition and detection feature from the settings and click on the “Enroll Face” button. It will take multiple attempts to save the face. Once the face is saved, it will detect the face as subject 0, where the face number is zero.

Face recognition based on ESP32-CAM

 

After registering the face, if a face is recognized in the video stream, the ESP32 will make the relay module become high level and unlock the door.

That’s how to make a face recognition based security system using ESP32-CAM. If you have any questions, feel free to leave a reply below this post.

 

Source

Back to top button