File-->examples-->esp32-->camera-->CameraWebServer
Its worth taking some time to get to know this example. You can adjust the video feed, detect faces, and train the program to recognise specific faces too. I think Espressif really pushed the boundaries of computer vision with this code. Most computer vision or machine learning programs require large operating systems, compilers, and libraries to process video as data. This is so stream-lined and embedded into a RISC-V instruction set, they must have pre-trained the tensors and used a quantisation/approximation. There aren't many edge products that can do computer vision so well on a tiny ESP32 processor.
Camera, computer vision, web server, and servo driver. All on a board that weights less than 5 grams, and written in less than a megabyte of code. Is this the most advanced product on the market today?
What you need for this project:
Hardware components:
esp32cam with MICRO USB to Serial Port adapter
2 x micro servos
a Micro USB cable
a 3D printer
2 x lm7805 5 volt regulators
a breadboard and wire
some 2.54mm header pins
jumper wires
a 2s lipo battery or some power supply
Software:
the Arduino IDE
The Build:
Pan and Tilt:
You can download the .stl files needed for this project on thingiverse:
https://www.thingiverse.com/thing:6290073
You will need to drill, screw, and/or glue the pieces together to work for you. Mount the servos into the holder, drill and screw the servo horns onto the holders as shown in the pictures.
If you don't want to use this mount, there are plenty of pan and tilt options on thingiverse that will work just fine.
Electronics:
Use your breadboard or PCB board to build the following circuit.
It's important to have clean power for the esp32cam controller. Motors will often cause voltage sag and harmonics that will interfere with the micro-controller's operation. For good connectivity and facial detection, clean power, strong WiFi, and good lighting is needed.
Software:
Libraries:
Make sure you have chosen the "AI thinker ESP32-CAM" under your boards menu.
Install the 'ESP32Servo by Kevin Harrington' library into your Arduino IDE.
There are 4 files in this example program:
(1)CameraWebServer.ino
(2)app_httpd.cpp
(3)camera_index.h
(4)camera_pins.h
We will only modify the "app_httpd.cpp" main program file.
Copy and paste #include "ESP32Servo.h" in to line 22 of the app_httpd.cpp file.
This will install the servo driver library.
Code:
STEP1. Insert this code to line 75 of the app_httpd.cpp file:
int servoPin1 = 15;
int servoPin2 = 14;
int x_pos;
int y_pos;
int delta_x;
int delta_y;
Servo myservo1;
Servo myservo2;
This assigns our GPIO pins and declares some variables.
STEP2. Insert this code to line 243 of the app_httpd.cpp file:
////pan tilt code
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
if (!myservo1.attached()) {
myservo1.setPeriodHertz(50); // standard 50 hz servo
myservo1.attach(servoPin1, 1000, 2000); // Attach the servo after it has been detached
}
if (!myservo2.attached()) {
myservo2.setPeriodHertz(50); // standard 50 hz servo
myservo2.attach(servoPin2, 1000, 2000); // Attach the servo after it has been detached
}
int x_cent = x + (w/2); //find centre point on the x axis
int y_cent = y + (h/2); //find centre point on the y axis
int delta_x = ((x_cent - 120)/3)^2;//tune the response of the servos by varying the divisor and exponential values
int delta_y = ((y_cent - 120)/3)^2;
int x_pos_temp = x_pos;
int y_pos_temp = y_pos;
for (i = 1; i <= 10; i++) { //use 10 steps for each adjustment
x_pos = x_pos_temp - (i*delta_x)/10; //pan
if (x_pos > 180) { //upper x servo limit
x_pos = 180; }
if (x_pos < 0) { //lower x servo limit
x_pos = 0; }
myservo1.write(x_pos); // tell servo to go to position
delay(15); // waits 15ms for the servo
y_pos = y_pos_temp - (i*delta_y)/10; //tilt
if (y_pos > 140) { //upper y servo limit
y_pos = 140; }
if (y_pos < 40) { //lower y servo limit
y_pos = 40; }
myservo2.write(y_pos); // tell servo to go to position
delay(15); // waits 15ms for the servo
delay(50); //varies the speed of the servos
}
////end pan tilt code
This code takes x,y,h,and w of the facial detection box and finds how off centre(120, 120) it is. There are also some software servo limits you can adjust, and some threading for smooth movement. You can tune the speed and degree of the servos as they find the new centres.
And that's it. Now you are ready to go. Select your com port, upload the new code, make note of the IP address, and use your computer or phone to connect to it. You now should see the Expressif web-page. You will need to change the resolution to '240x240' for the face detection to work.
You also need to select 'Face Detection', and select 'Start Stream'.