I have combined face detection code from CameraWebServer with camera code I made to get this:
#include "esp_camera.h"
#include <TFT_eSPI.h>
#include "pins.h"
#include <vector>
#include "human_face_detect_msr01.hpp"
#include "human_face_detect_mnp01.hpp"
#include "fb_gfx.h"
#define TWO_STAGE 1
TFT_eSPI tft = TFT_eSPI();
void setup() {
Serial.begin(115200);
Serial.println();
cam_init();
tft_init(1);
}
void loop() {
face_detect();
}
void cam_init(){
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 10000000;
config.pixel_format = PIXFORMAT_RGB565;
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 12;
config.fb_count = 2;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
while(1);
}
Serial.println("Camera init success!");
sensor_t * s = esp_camera_sensor_get();
// This is what looks best for me
s->set_vflip(s, 1);
s->set_brightness(s, -2);
s->set_saturation(s, 3);
s->set_hmirror(s, 1);
s->set_contrast(s, 3);
s->set_gainceiling(s, (gainceiling_t)511);
}
void draw_cam_buf() {
camera_fb_t * fb = esp_camera_fb_get();
if(!fb){
Serial.println("Image capture error!");
return;
}
tft.pushImage(0, 0, fb->width, fb->height, (uint16_t *) fb->buf);
esp_camera_fb_return(fb);
}
void tft_init(int rtn){
tft.begin();
tft.setRotation(rtn);
tft.fillScreen(TFT_BLACK);
}
static void draw_face_boxes(fb_data_t *fb, std::list<dl::detect::result_t> *results, int face_id)
{
int x, y, w, h;
uint32_t color = TFT_YELLOW;
if (face_id < 0)
{
color = TFT_RED;
}
else if (face_id > 0)
{
color = TFT_GREEN;
}
if(fb->bytes_per_pixel == 2){
//color = ((color >> 8) & 0xF800) | ((color >> 3) & 0x07E0) | (color & 0x001F);
color = ((color >> 16) & 0x001F) | ((color >> 3) & 0x07E0) | ((color << 8) & 0xF800);
}
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results->begin(); prediction != results->end(); prediction++, i++)
{
// rectangle box
x = (int)prediction->box[0];
y = (int)prediction->box[1];
w = (int)prediction->box[2] - x + 1;
h = (int)prediction->box[3] - y + 1;
if((x + w) > fb->width){
w = fb->width - x;
}
if((y + h) > fb->height){
h = fb->height - y;
}
fb_gfx_drawFastHLine(fb, x, y, w, color);
fb_gfx_drawFastHLine(fb, x, y + h - 1, w, color);
fb_gfx_drawFastVLine(fb, x, y, h, color);
fb_gfx_drawFastVLine(fb, x + w - 1, y, h, color);
#if TWO_STAGE
// landmarks (left eye, mouth left, nose, right eye, mouth right)
int x0, y0, j;
for (j = 0; j < 10; j+=2) {
x0 = (int)prediction->keypoint[j];
y0 = (int)prediction->keypoint[j+1];
fb_gfx_fillRect(fb, x0, y0, 3, 3, color);
}
#endif
}
}
void face_detect() {
camera_fb_t * fb = esp_camera_fb_get();
#if TWO_STAGE
HumanFaceDetectMSR01 s1(0.1F, 0.5F, 10, 0.2F);
HumanFaceDetectMNP01 s2(0.5F, 0.3F, 5);
std::list<dl::detect::result_t> &candidates = s1.infer((uint16_t *)fb->buf, {(int)fb->height, (int)fb->width, 3});
std::list<dl::detect::result_t> &results = s2.infer((uint16_t *)fb->buf, {(int)fb->height, (int)fb->width, 3}, candidates);
#else
HumanFaceDetectMSR01 s1(0.3F, 0.5F, 10, 0.2F);
std::list<dl::detect::result_t> &results = s1.infer((uint16_t *)fb->buf, {(int)fb->height, (int)fb->width, 3});
#endif
if (results.size() > 0) {
fb_data_t rfb;
rfb.width = fb->width;
rfb.height = fb->height;
rfb.data = fb->buf;
rfb.bytes_per_pixel = 2;
rfb.format = FB_RGB565;
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
detected = true;
#endif
draw_face_boxes(&rfb, &results, 0);
}
tft.pushImage(0, 0, fb->width, fb->height, (uint16_t *) fb->buf);
esp_camera_fb_return(fb);
}