ESP32-CAM | ESP32 Camera Module with Face Recognition

ES32-CAM-esp-32-camera-module.jpg

A step by step guide to setting up the Ai-Thinker ESP32-CAM with Espressif’s ESP32 development environment and the ESP-WHO libraries for face detection and recognition.

With this module you get face detection for ~ $7!  They have them on AliExpress here.

The AI-Thinker ESP32-CAM module features an ESP32-S chip, an OV2640 camera and a microSD card slot. The software needs to be compiled and uploaded using the Espressif development environment.

Follow the steps below to download and install everything you need to develop on the ESP32 including packages for the ESP-IDF and the ESP32 toolchain. If you prefer to use the Arduino IDE (it’s a lot easier to set up the web server demo) there’s another tutorial here: ESP32-CAM using Arduino IDE

ESP32 Development Environment

Download the 32bit version (-i686-) of MYSYS from here: http://www.msys2.org/
Install following the steps on the page above but uncheck the “Run MSYS2 32-bit now” checkbox at the end.
Navigate to C:\msys32\ and double-click mingw32.exe to run the application.
Copy the contents of the file here https://raw.githubusercontent.com/espressif/esp-idf/master/tools/windows/windows_install_prerequisites.sh to a new file in C:\msys32\
Copy or paste the command below and press enter:

C:/msys32/windows_install_prerequisites.sh

You are likely to see an error message similar to this:

***fatal error - cygheap base mismatch detected

Close the terminal window and again navigate to C:\msys32\ and double-click mingw32.exe to re-open the terminal.
Press the up arrow to see the C:/msys32/windows_install_prerequisites.sh command and press enter.

When the script has completed, type or paste the following commands one at a time.

cd ~
mkdir esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git

NOTE: If you run into an error: cannot fork() type or paste the command below:

c:/msys32/autorebase.bat

and run the git clone command again.

If you look in c:\msys32\home\**yourusername**\esp\ you will see all the APIs and libraries for the ESP32 have been installed.

A user profile script needs to be added so projects will build without the path to these files having to be entered every time.

Find the path to the ESP-IDF. Following this tutorial it will be C:\msys32\home\**yourusername**\esp\esp-idf\
Create a new file named export_idf_path.sh in C:/msys32/etc/profile.d/
Open it in Notepad and add the following line to the new export_idf_path.sh file replacing the IDF_PATH with your own. Check you are using forward slashes.

export IDF_PATH="C:/msys32/home/**yourusername**/esp/esp-idf"

Save and close this new file.

Close the MSYS2 terminal window and then double click mingw32.exe to  open it again. Check if IDF_PATH is set by typing:

printenv IDF_PATH

The ESP32 development environment is now set up.

ESP-WHO Face Detection and Recognition Libraries

To set up the face detection and recognition libraries and demos, type or paste the following commands in the MSYS2 terminal window

cd ~
git clone --recursive https://github.com/espressif/esp-who.git

If you look in c:\msys32\home\**yourusername**\esp-who\ you will see the ESP-WHO libraries and examples have been installed.

Connections

The ESP32-CAM doesn’t come with a USB connector so you need either a CP2102 or a FT232RL USB to TTL Serial Converter to connect it to your PC. When using WiFi the ESP32 can use more current than is supplied via USB through these devices resulting in the module crashing and rebooting. I found the top device worked fine (I bought it here) but the FT232RL wouldn’t work without a separate supply to the 5V pin.

USB to ESP32-CAM Wiring Diagram

 

Wire the module according to the diagram above and plug into your PC. The green connection should be connected when flashing the module and disconnected when running.

Open Device Manager and make a note of the port number of the serial convertor:

COM Port

Camera Web Server Example

In the MSYS2 terminal window type or paste the following:

cd ~/esp-who/examples/single_chip/camera_web_server/
make menuconfig

In the window that opens you need to change the following settings

Serial flasher config —> Default serial port (Change to the port number shown in Device Manager – ie COM3)
Camera Web Server —> WiFi Settings —> (Add your WiFi SSID and Password)
Camera Web Server —> Camera Pins —> Select Camera Pinout —> (Select ESP32-CAM by AI-Thinker)
Component config —> ESP32-specific —> SPI Ram config —> Type of SPI RAM chip (Select Auto-detect)

Save the configuration.

In the terminal window, type:

make flash

If you get an error that the device fails to connect. Check that the IO0 pin (green in the diagram) is connected to GRD and press the reset button under the module and try again.

When the device has completed flashing. Unplug IO0 from GND and type:

make monitor

Press the reset button on the ESP32-CAM and watch the start up sequence in the terminal.

Look for the IP address the ESP32 has been given on your network camera wifi where it outputs got ip:xxx.xxx.xxx.xxx

Connected to WiFi

Type that IP address into your browser. You should be able to see a GUI on the left where you can control elements of the camera, set face detection and face recognition. Click Get Still to take a photo. Click Start Stream to see a video stream from the camera.

ESP-WHO GUI

Resources

Here’s a list of resources that I used to get this working

https://github.com/espressif/esp-who – Homepage for the face recognition library
https://docs.espressif.com/projects/esp-idf/en/latest/get-started/windows-setup-scratch.html – Setting up the ESP Dev Environment from scratch. I found the prebuilt version had problems
https://github.com/VirgiliaBeatrice/esp32-devenv-vscode/blob/master/tutorial.md – Alternative setup method with further info about debugging etc
http://wiki.aithinker.com/esp32-cam – Chinese site for the module manufacturer

20 Replies to “ESP32-CAM | ESP32 Camera Module with Face Recognition”

  1. nocode says:

    how and where can you edit the html page ? which file has the sourcecode of the html of the webserver ?

    i programmed via arduino ide .

    https://i.imgur.com/qmeV07s.png

    1. WordBot says:

      I’m still looking into this but I think it’s this: https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Camera/CameraWebServer/camera_index.h . The page html is gzipped and converted to a const char array of hex values.

  2. mhogan says:

    This was extremely helpful. Thanks! Is it possible to save an image to the built-in SD card? I’ve been trying to get the card to work but so far the ESP32-CAM won’t recognize it.

    1. WordBot says:

      Here’s a start – https://github.com/raphaelbs/esp32-cam-ai-thinker/tree/master/examples/sd_jpg this is for the Espressif IDF. I’ve not tried it myself.

  3. Gautham says:

    Nice tutorial , thanks for sharing ,
    Is it possible to handle face recognition , movement detection and other task like registering the name of the face recognized in cloud with this , are all these possible with the 4MB RAM ?

    1. WordBot says:

      Not sure about movement detection with the current library but face detection and sending the name of the person to the cloud would be possible with some work.

  4. Jairo says:

    I think there’s an error in export_idf_path needs an “export” so that the variable is available outside that script i.e.
    export IDF_PATH=”C:/msys32/home/username/esp/esp-idf”

    1. WordBot says:

      You’re right! Thanks for pointing that out. I’ve fixed it in the article.

  5. Emmanuel says:

    Running into issues of pySerial not being installed when I run Make Flash. Any workaround for this?

  6. David says:

    Hi,
    thanks for the details on getting this up an running.
    I am only get MJPG: 10003B 1324ms (0.8fps), AVG: 2156ms (0.5fps), 0+0+0+0=0 0 for qVGA where I see some people saying the get 30fps + , any ideas why my frame rate would be so bad? I had a look at the timings and the initial connect is 261ms, waiting TTFB is 544ms and download is 1.30s. I have no issues streaming and have good download speeds with everything else vir wifi.

    Regards
    David.

  7. varun says:

    hi,
    how to solve python dependencies error .because of this error i could not able to flash the unit.
    can anyone send the link to download this tool chain version (crosstool-ng-1.22.0-80-g6c4433a). i need in both 32 and 64 bit.

    1. WordBot says:

      Hi, Maybe something here will help: https://esp32.com/viewtopic.php?t=7017&start=20 Is the tutorial above not working at the moment?

  8. ram says:

    cant able to install toolchain set up for UBUNTU 16.04 os.Is there anyother way to install the toolchain setup?

  9. Rockman says:

    Which kit is this ESP32 cam comes up on from these three?
    ESP32 DevKitC
    ESP-WROVER-KIT
    ESP32-PICO-KIT

    1. WordBot says:

      It’s the Ai-Thinker. You should see it as an option when you make menuconfig. It’s here in the code: https://github.com/espressif/esp-who/blob/master/examples/single_chip/camera_web_server/main/include/app_camera.h

  10. Rockman says:

    Serial flasher config —> Default serial port (Change to the port number shown in Device Manager – ie COM3)

    This changes cannot able to do in UBUNTU 16.04 .How to to do this please comment

  11. sunil says:

    HI @WordBot,
    i can able to take a still with the camera but face detection and face reorganization is not happening, Do any one know for what reason it is not happening.please help me out to solve this issue.

    1. WordBot says:

      When you turn Face Detection on and press Start Stream do you get a green box around your face? It has to be the right way up. Here’s a video: https://www.youtube.com/watch?v=gZgGniTLCiU

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top