ESP32cam switch from frame to photo

Anyone looking for a code that only detects faces, this one does it. When detecting, the led-flash turns on and keeps it on as long as the detection is true.

What I'm researching is how to take a picture only when a face is detected. That is, switching from frame to photo, taking the photo, then switching back to frame and continuing to detect faces.

If anyone knows and wants to help by posting how this code should look, I appreciate it.

#include "esp_camera.h"
#include "fd_forward.h"

int cron;

#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22
#define flash 4
 
bool initCamera() {
 
  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 = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_CIF;
  config.jpeg_quality = 10;
  config.fb_count = 1;
 
  esp_err_t result = esp_camera_init(&config);
 
  if (result != ESP_OK) {
    return false;
  }
 
  return true;
}
 
mtmn_config_t mtmn_config = {0};
int detections = 0;
 
void setup() {
  Serial.begin(115200);
  
  pinMode(flash, OUTPUT);
  
  if (!initCamera()) {
 
    Serial.printf("Failed to initialize camera...");
    return;
  }
 
  mtmn_config = mtmn_init_config();

}
 
void loop() {
   
  camera_fb_t * frame;
  frame = esp_camera_fb_get();
 
  dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, frame->width, frame->height, 3);
  fmt2rgb888(frame->buf, frame->len, frame->format, image_matrix->item);
 
  esp_camera_fb_return(frame);
 
  box_array_t *boxes = face_detect(image_matrix, &mtmn_config);
 
  if (boxes != NULL) {
    detections = detections+1;
    //Serial.printf("Faces detected %d times \n", detections);
    digitalWrite(flash, HIGH);
    cron = 0;
          
    dl_lib_free(boxes->score);
    dl_lib_free(boxes->box);
    dl_lib_free(boxes->landmark);
    dl_lib_free(boxes);

  } else {
    cron++;
    Serial.println(cron);
      if (cron > 1) {
        digitalWrite(flash, LOW);
        cron = 0;
//      Serial.println("Stop detection");
      }
    }
  dl_matrix3du_free(image_matrix);
  }
 
}

I thought it was something more complicated, but it was just a wrong connection here and that was causing rebooting.

So just call this void below by placing it there on the line of digitalWrite(flash, HIGH);

That is, when it detects a face, instead of turning on the led-flash (pin 4) it will call this void and take the picture.

Then you move on with your code.

See you soon.

NOTE: If anyone knows how to connect an RC-522 RFID to the esp32-cam...

void takephoto()   {
  
  camera_fb_t * fb = NULL;
    delay(100);
  fb = esp_camera_fb_get();
    
  if (!fb) {
    Serial.println("Camera capture failed");
    delay(1000);
    ESP.restart();
  }

 esp_camera_fb_return(fb);


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.