I want to detect face with the faceid and send topic over local MQTT using ESP32-Cam. Please help me in coding this.
Post your code and show where you are having an issue.
Here is some ESP32 MQTT code to get you started.
#include "esp_system.h"
#include "freertos/FreeRTOS.h" //freeRTOS items to be used
#include "freertos/task.h"
#include "certs.h"
#include "esp_camera.h"
#include "soc/soc.h" // Disable brownout problems
#include "soc/rtc_cntl_reg.h" // Disable brownout problems
#include "driver/rtc_io.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include "ESP32_FTPClient.h"
#include <PubSubClient.h>
#include <ESP32Time.h>
//
WiFiClient wifiClient; // do the WiFi instantiation thing
PubSubClient MQTTclient( mqtt_server, mqtt_port, wifiClient ); //do the MQTT instantiation thing
ESP32_FTPClient ftp (ftp_server, ftp_user, ftp_pass, 5000, 2);
ESP32Time rtc;
////
//#define evtDoMQTTParse ( 1 << 0 ) // declare an event
//EventGroupHandle_t eg; // variable for the event group handle
////
SemaphoreHandle_t sema_MQTT_KeepAlive;
SemaphoreHandle_t sema_mqttOK;
////
QueueHandle_t xQ_Message; // payload and topic queue of MQTT payload and topic
const int payloadSize = 300;
struct stu_message
{
char payload [payloadSize] = {'\0'};
String topic ;
} x_message;
////
int mqttOK = 0;
bool TimeSet = false;
bool FlashMode = false;
//
void IRAM_ATTR WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case SYSTEM_EVENT_STA_CONNECTED:
log_i("Connected to WiFi access point");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
log_i("Disconnected from WiFi access point");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
log_i("WiFi client disconnected");
break;
default: break;
}
} // void IRAM_ATTR WiFiEvent(WiFiEvent_t event)
//
void IRAM_ATTR mqttCallback(char* topic, byte * payload, unsigned int length)
{
// clear locations
memset( x_message.payload, '\0', payloadSize );
x_message.topic = ""; //clear string buffer
x_message.topic = topic;
int i = 0;
for ( i; i < length; i++)
{
x_message.payload[i] = ((char)payload[i]);
}
x_message.payload[i] = '\0';
xQueueOverwrite( xQ_Message, (void *) &x_message );// send data to queue
} // void mqttCallback(char* topic, byte* payload, unsigned int length)
////
void setup()
{
pinMode( GPIO_NUM_4, OUTPUT);
digitalWrite( GPIO_NUM_4, LOW);
//
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
//
//eg = xEventGroupCreate(); // get an event group handle
//
x_message.topic.reserve( payloadSize );
//
xQ_Message = xQueueCreate( 1, sizeof(stu_message) );
//
sema_mqttOK = xSemaphoreCreateBinary();
xSemaphoreGive( sema_mqttOK );
//
xTaskCreatePinnedToCore( MQTTkeepalive, "MQTTkeepalive", 7000, NULL, 3, NULL, 1 );
xTaskCreatePinnedToCore( fparseMQTT, "fparseMQTT", 9000, NULL, 4, NULL, 1 ); // assign all to core 1, WiFi in use.
xTaskCreatePinnedToCore( fmqttWatchDog, "fmqttWatchDog", 3000, NULL, 2, NULL, 1 );
if ( configInitCamera() )
{
log_i(" start camera task" );
xTaskCreatePinnedToCore( capturePhoto_sendFTP, "capturePhoto_sendFTP", 60000, NULL, 6, NULL, 1 );
} else {
log_i( " camera failed to initilize, rebooting in 5." );
vTaskDelay( 2000 );
ESP.restart();
}
} // void setup()
////
void capturePhoto_sendFTP( void *pvParameters )
{
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 1000 * 15; //delay for mS
//!!!!!Must wait for MQTT connection and proper setup of the sema_MQTT_KeepAlive!!!!
while ( !MQTTclient.connected() )
{
vTaskDelay( 500 );
}
for (;;)
{
log_i( "tick");
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
{
camera_fb_t * fb = NULL; // pointer
if( FlashMode )
{
digitalWrite( GPIO_NUM_4, HIGH );
vTaskDelay( 20 );
}
fb = esp_camera_fb_get();
digitalWrite( GPIO_NUM_4, LOW );
if (!fb)
{
log_i( "Camera capture failed" );
esp_camera_fb_return(fb);
} else
{
ftp.OpenConnection(); // try open FTP
if ( ftp.isConnected() )
{
//try send file ftp
ftp.ChangeWorkDir( ftp_path );
ftp.DeleteFile( ftp_file_name );
ftp.InitFile( ftp_file_type ); //"Type I"
ftp.NewFile( ftp_file_name );
ftp.WriteData( (unsigned char *)fb->buf, fb->len );
ftp.CloseFile();
ftp.CloseConnection();
}
esp_camera_fb_return(fb); //return the frame buffer back to the driver for reuse
}
}
xSemaphoreGive( sema_MQTT_KeepAlive );
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
vTaskDelete( NULL );
} //void capturePhoto_sendFTP( void *pvParameters )
////
void configureCameraSettings_grpE( int _lenC, int _Hmirror, int _Vflip, int _dcw, int _colorbar )
{
sensor_t * s = esp_camera_sensor_get();
s->set_lenc(s, _lenC); // 0 = disable , 1 = enable
s->set_hmirror(s, _Hmirror); // 0 = disable , 1 = enable
s->set_vflip(s, _Vflip); // 0 = disable , 1 = enable
s->set_dcw(s, _dcw); // 0 = disable , 1 = enable
////s->set_colorbar(s, _colorbar); // 0 = disable , 1 = enable
}
////
void configureCameraSettings_grpD( int _gc, int _agc, int _celing, int _bpc, int _wpc, int _gma )
{
sensor_t * s = esp_camera_sensor_get();
s->set_gain_ctrl(s, _gc); // 0 = disable , 1 = enable
s->set_agc_gain(s, _agc); // 0 to 30
s->set_gainceiling(s, (gainceiling_t)_celing); // 0 to 6
s->set_bpc(s, _bpc); // 0 = disable , 1 = enable
s->set_wpc(s, _wpc); // 0 = disable , 1 = enable
s->set_raw_gma(s, _gma); // 0 = disable , 1 = enable
}
////
void configureCameraSettings_grpC( int _exctl, int _aec2, int _ae, int _aec )
{
sensor_t * s = esp_camera_sensor_get();
s->set_exposure_ctrl(s, _exctl); // 0 = disable , 1 = enable
s->set_aec2(s, _aec2); // 0 = disable , 1 = enable
s->set_ae_level(s, _ae); // -2 to 2
////s->set_aec_value(s, _aec); // 0 to 1200 CAUSES BLACK IMAGE????
}
////
void configureCameraSettings_grpB( int se, int wb, int awb, int wbmode )
{
sensor_t * s = esp_camera_sensor_get();
s->set_special_effect(s, se); // 0 to 6 (0 - No Effect, 1 - Negative, 2 - Grayscale, 3 - Red Tint, 4 - Green Tint, 5 - Blue Tint, 6 - Sepia)
s->set_whitebal(s, wb); // 0 = disable , 1 = enable
s->set_awb_gain(s, awb); // 0 = disable , 1 = enable
s->set_wb_mode(s, wbmode); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
}
////
void configureCameraSettings_grpA(int brightness, int contrast, int saturation )
{
if ( ((brightness > -3) && (brightness < 3)) && ((contrast > -3) && (contrast < 3)) && ((saturation > -3) && (saturation < 3)) )
{
sensor_t * s = esp_camera_sensor_get();
s->set_brightness( s, brightness ); // -2 to 2
s->set_contrast( s, contrast ); // -2 to 2
s->set_saturation( s, saturation ); // -2 to 2
}
} //void configureCameraSettings()
////
void configureCameraSettings()
{
sensor_t * s = esp_camera_sensor_get(); //see certs.h for more info
s->set_brightness(s, -1); // -2 to 2 **************************
s->set_contrast(s, 0); // -2 to 2
s->set_saturation(s, 0); // -2 to 2
s->set_special_effect(s, 0); // 0 to 6 (0 - No Effect, 1 - Negative, 2 - Grayscale, 3 - Red Tint, 4 - Green Tint, 5 - Blue Tint, 6 - Sepia)
s->set_whitebal(s, 1); // 0 = disable , 1 = enable
s->set_awb_gain(s, 1); // 0 = disable , 1 = enable
s->set_wb_mode(s, 0); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
s->set_exposure_ctrl(s, 1); // 0 = disable , 1 = enable
s->set_aec2(s, 0); // 0 = disable , 1 = enable
s->set_ae_level(s, 0); // -2 to 2
s->set_aec_value(s, 300); // 0 to 1200
s->set_gain_ctrl(s, 1); // 0 = disable , 1 = enable
s->set_agc_gain(s, 0); // 0 to 30
s->set_gainceiling(s, (gainceiling_t)0); // 0 to 6
s->set_bpc(s, 0); // 0 = disable , 1 = enable
s->set_wpc(s, 1); // 0 = disable , 1 = enable
s->set_raw_gma(s, 1); // 0 = disable , 1 = enable
s->set_lenc(s, 1); // 0 = disable , 1 = enable
s->set_hmirror(s, 0); // 0 = disable , 1 = enable
s->set_vflip(s, 0); // 0 = disable , 1 = enable
s->set_dcw(s, 1); // 0 = disable , 1 = enable
s->set_colorbar(s, 0); // 0 = disable , 1 = enable
} //void configureCameraSettings()
////
bool configInitCamera()
{
camera_config_t config = {}; // Stores the camera configuration parameters
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = GPIO_NUM_5; //Y2
config.pin_d1 = GPIO_NUM_18; //Y3
config.pin_d2 = GPIO_NUM_19; //Y4
config.pin_d3 = GPIO_NUM_21; //Y5
config.pin_d4 = GPIO_NUM_36; //Y6
config.pin_d5 = GPIO_NUM_39; //Y7
config.pin_d6 = GPIO_NUM_34; //Y8
config.pin_d7 = GPIO_NUM_35; // Y9
config.pin_xclk = GPIO_NUM_0; //XCLK
config.pin_pclk = GPIO_NUM_22; //PCLK
config.pin_vsync = GPIO_NUM_25; //VSSYNC
config.pin_href = GPIO_NUM_23; // HREF
config.pin_sscb_sda = GPIO_NUM_26; //SIOD
config.pin_sscb_scl = GPIO_NUM_27; //SIOC
config.pin_pwdn = GPIO_NUM_32; //PWDN
config.pin_reset = -1; //RESET
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG; //assuming default is has PSRAM
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
config.jpeg_quality = 10; //0-63 lower number means higher quality
config.fb_count = 2;
// Initialize the Camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
log_i("Camera init failed with error %d", err );
return false;
} else {
configureCameraSettings();
return true;
}
} //void configInitCamera()
////
void connectToWiFi()
{
int TryCount = 0;
while ( WiFi.status() != WL_CONNECTED )
{
TryCount++;
WiFi.disconnect();
WiFi.begin( SSID, PASSWORD );
vTaskDelay( 2500 );
if ( TryCount == 10 )
{
ESP.restart();
}
}
WiFi.onEvent( WiFiEvent );
}
////
void MQTTkeepalive( void *pvParameters )
{
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive( sema_MQTT_KeepAlive ); // found keep alive can mess with a publish, stop keep alive during publish
MQTTclient.setKeepAlive( 90 ); // setting keep alive to 90 seconds makes for a very reliable connection, must be set before the 1st connection is made.
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 250; //delay for ms
for (;;)
{
//check for a is-connected and if the WiFi 'thinks' its connected, found checking on both is more realible than just a single check
if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
{
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // !!!!!whiles MQTTlient.loop() is running no other mqtt operations should be in process!!!!!
MQTTclient.loop();
xSemaphoreGive( sema_MQTT_KeepAlive );
}
else {
if ( !(wifiClient.connected()) || !(WiFi.status() == WL_CONNECTED) )
{
connectToWiFi();
}
connectToMQTT();
}
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
vTaskDelete ( NULL );
}
////
void connectToMQTT()
{
byte mac[5]; // create client ID from mac address
WiFi.macAddress(mac); // get mac address
String clientID = String(mac[0]) + String(mac[4]) ; // use mac address to create clientID
while ( !MQTTclient.connected() )
{
MQTTclient.connect( clientID.c_str(), mqtt_username, mqtt_password );
vTaskDelay( 250 );
}
MQTTclient.setCallback( mqttCallback );
MQTTclient.subscribe( topicOK );
MQTTclient.subscribe( topicGrpA );
MQTTclient.subscribe( topicGrpB );
MQTTclient.subscribe( topicGrpC );
MQTTclient.subscribe( topicGrpD );
MQTTclient.subscribe( topicGrpE );
MQTTclient.subscribe( topicFlash );
} //void connectToMQTT()
//////
void fmqttWatchDog( void * paramater )
{
int UpdateImeTrigger = 86400; //seconds in a day
int UpdateTimeInterval = 85000; // get another reading when = UpdateTimeTrigger
int maxNonMQTTresponse = 3;
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 60000; //delay for mS
for (;;)
{
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
xSemaphoreTake( sema_mqttOK, portMAX_DELAY ); // update mqttOK
mqttOK++;
xSemaphoreGive( sema_mqttOK );
if ( mqttOK >= maxNonMQTTresponse )
{
log_i( "mqtt watchdog rest" );
vTaskDelay( 200 );
ESP.restart();
}
UpdateTimeInterval++; // trigger new time get
if ( UpdateTimeInterval >= UpdateImeTrigger )
{
TimeSet = false; // sets doneTime to false to get an updated time after a days count of seconds
UpdateTimeInterval = 0;
}
}
vTaskDelete( NULL );
} //void fmqttWatchDog( void * paramater )
////
void fparseMQTT( void *pvParameters )
{
struct stu_message px_message;
for (;;)
{
if ( xQueueReceive(xQ_Message, &px_message, portMAX_DELAY) == pdTRUE )
{
xSemaphoreTake( sema_mqttOK, portMAX_DELAY );
mqttOK = 0;
xSemaphoreGive( sema_mqttOK );
if ( !TimeSet)
{
if ( String(px_message.topic) == topicOK )
{
String temp = "";
temp = px_message.payload[0];
temp += px_message.payload[1];
temp += px_message.payload[2];
temp += px_message.payload[3];
int year = temp.toInt();
temp = "";
temp = px_message.payload[5];
temp += px_message.payload[6];
int month = temp.toInt();
temp = "";
temp = px_message.payload[8];
temp += px_message.payload[9];
int day = temp.toInt();
temp = "";
temp = px_message.payload[11];
temp += px_message.payload[12];
int hour = temp.toInt();
temp = "";
temp = px_message.payload[14];
temp += px_message.payload[15];
int min = temp.toInt();
rtc.setTime( 0, min, hour, day, month, year );
log_i( "%s rtc %s ", px_message.payload, rtc.getTime() );
TimeSet = true;
}
}
if ( String(px_message.topic) == topicGrpA )
{
//finding first letter
String sTmp = String( px_message.payload );
int commaIndex = sTmp.indexOf(',');
if ( sTmp.substring(0, commaIndex) == "a" );
{
int commaIndex = sTmp.indexOf(',');
int brt = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int ctrast = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) ); // chop off begining of message
int sat = (sTmp.substring(0, commaIndex)).toInt();
configureCameraSettings_grpA( brt, ctrast, sat );
}
}
if ( String(px_message.topic) == topicGrpB )
{
String sTmp = String( px_message.payload );
int commaIndex = sTmp.indexOf(',');
if ( sTmp.substring(0, commaIndex) == "b" );
{
int commaIndex = sTmp.indexOf(',');
int se = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int wbb = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int wba = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int wbm = (sTmp.substring(0, commaIndex)).toInt();
configureCameraSettings_grpB( se, wbb, wba, wbm );
}
}
if ( String(px_message.topic) == topicGrpC )
{
String sTmp = String( px_message.payload );
int commaIndex = sTmp.indexOf(',');
if ( sTmp.substring(0, commaIndex) == "c" );
{
int commaIndex = sTmp.indexOf(',');
int ex = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int aec2 = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int ae = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int aec = (sTmp.substring(0, commaIndex)).toInt();
configureCameraSettings_grpC( ex, aec2, ae, aec );
}
}
if ( String(px_message.topic) == topicGrpD )
{
String sTmp = String( px_message.payload );
int commaIndex = sTmp.indexOf(',');
if ( sTmp.substring(0, commaIndex) == "d" );
{
int commaIndex = sTmp.indexOf(',');
int gc = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int agc = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int celing = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int bpc = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int wpc = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int gma = (sTmp.substring(0, commaIndex)).toInt();
configureCameraSettings_grpD( gc, agc, celing, bpc, wpc, gma );
}
}
if ( String(px_message.topic) == topicGrpE )
{
String sTmp = String( px_message.payload );
int commaIndex = sTmp.indexOf(',');
if ( sTmp.substring(0, commaIndex) == "e" );
{
int commaIndex = sTmp.indexOf(',');
int lenC = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int Hmirror = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int Vflip = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int dcw = (sTmp.substring(0, commaIndex)).toInt();
sTmp.remove( 0, (commaIndex + 1) );
int colorbar = (sTmp.substring(0, commaIndex)).toInt();
configureCameraSettings_grpE( lenC, Hmirror, Vflip, dcw, colorbar );
}
}
if( String(px_message.topic) == topicFlash )
{
FlashMode = !FlashMode;
}
} //if ( xQueueReceive(xQ_Message, &px_message, portMAX_DELAY) == pdTRUE )
} //for(;;)
vTaskDelete( NULL );
} // void fparseMQTT( void *pvParameters )
////
void loop() {}
I'm trying to debug this but i'm getting error ESP Wrover Module. I have set Partition Scheme to Huge APP, and Board as ESP32 Wrover Module. Any help would be appreciated
#include "SPIFFS.h"
#include "esp_camera.h"
#include <WiFi.h>
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "fb_gfx.h"
#include "fd_forward.h"
#include "fr_forward.h"
#include "FS.h" // sd card esp32
#include "SD_MMC.h" // sd card esp32
#include "soc/soc.h" // disable brownour problems
#include "soc/rtc_cntl_reg.h" // disable brownour problems
#include <WiFi.h> // used for internet time
#include "dl_lib.h"
#include "esp_http_server.h"
#define CAMERA_MODEL_AI_THINKER
#define PART_BOUNDARY "123456789000000000000987654321"
#define ENROLL_CONFIRM_TIMES 8
#define FACE_ID_SAVE_NUMBER 7
#define FACE_COLOR_WHITE 0x00FFFFFF
#define FACE_COLOR_BLACK 0x00000000
#define FACE_COLOR_RED 0x000000FF
#define FACE_COLOR_GREEN 0x0000FF00
#define FACE_COLOR_BLUE 0x00FF0000
#define FACE_COLOR_YELLOW (FACE_COLOR_RED | FACE_COLOR_GREEN)
#define FACE_COLOR_CYAN (FACE_COLOR_BLUE | FACE_COLOR_GREEN)
#define FACE_COLOR_PURPLE (FACE_COLOR_BLUE | FACE_COLOR_RED)
#include <PubSubClient.h>
#include "EEPROM.h"
WiFiClient espClient;
PubSubClient client(espClient);
// Update these with WiFi network values
const char* ssid ="your_ssid"; // your network SSID (name)
const char* password ="your_password"; // your network password
const char* mqtt_server="your_mqtt_ip"; //your mqtt server ip
static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
typedef struct {
size_t size; // number of values used for filtering
size_t index; // current value index
size_t count; // value count
int sum;
int * values; // array to be filled with values
} ra_filter_t;
static ra_filter_t ra_filter;
httpd_handle_t stream_httpd = NULL;
httpd_handle_t camera_httpd = NULL;
String ver = "1.03E";
static mtmn_config_t mtmn_config = {0};
static int8_t recognition_enabled = 0;
static int8_t is_enrolling = 0;
static face_id_list id_list = {0};
typedef struct {
httpd_req_t *req;
size_t len;
} jpg_chunking_t;
const char* ntpServer = "pool.ntp.org"; // internet time server
const long gmtOffset_sec = 3600;// internet time server
const int daylightOffset_sec = 3600;// internet time server
static int8_t detection_enabled = 0;
int yes_id=0;
int8_t left_sample_face;
String face_id;
static int run_face_recognition(dl_matrix3du_t *image_matrix, box_array_t *net_boxes) {
dl_matrix3du_t *aligned_face = NULL;
int matched_id = 0;
aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
if (!aligned_face) {
Serial.println("Could not allocate face recognition buffer");
return matched_id;
}
if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK) {
if (is_enrolling == 1) {
left_sample_face = enroll_face(&id_list, aligned_face);
yes_id=3;
if (left_sample_face == (ENROLL_CONFIRM_TIMES - 1)) {
Serial.printf("Enrolling Face ID: %d\n", id_list.tail);
}
Serial.printf("Enrolling Face ID: %d sample %d\n", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
rgb_printf(image_matrix, FACE_COLOR_CYAN, "ID[%u] Sample[%u]", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
if (left_sample_face == 0) {
is_enrolling = 0;
Serial.printf("Enrolled Face ID: %d\n", id_list.tail);
yes_id=4;
}
} else {
matched_id = recognize_face(&id_list, aligned_face);
if (matched_id >= 0) {
yes_id=1;
face_id=String(matched_id);
Serial.printf("Match Face ID: %u\n", matched_id);
rgb_printf(image_matrix, FACE_COLOR_GREEN, "ID: %u", matched_id);
} else {
yes_id=2;
//loop();
Serial.println("No Match Found");
rgb_print(image_matrix, FACE_COLOR_RED, "NO MATCH!");
matched_id = -1;
}
}
} else {
Serial.println("Face Not Aligned");
rgb_print(image_matrix, FACE_COLOR_YELLOW, "Human Detected");
}
dl_matrix3du_free(aligned_face);
return matched_id;
}
// sd methods
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels) {
listDir(fs, file.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char * path) {
Serial.printf("Creating Dir: %s\n", path);
if (fs.mkdir(path)) {
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char * path) {
Serial.printf("Removing Dir: %s\n", path);
if (fs.rmdir(path)) {
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
void readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
}
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
//fwrite(fb->buf, 1, fb->len, file);
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
}
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
}
void renameFile(fs::FS &fs, const char * path1, const char * path2) {
Serial.printf("Renaming file %s to %s\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("File renamed");
} else {
Serial.println("Rename failed");
}
}
void deleteFile(fs::FS &fs, const char * path) {
Serial.printf("Deleting file: %s\n", path);
if (fs.remove(path)) {
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
void testFileIO(fs::FS &fs, const char * path) {
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = millis();
uint32_t end = start;
if (file) {
len = file.size();
size_t flen = len;
start = millis();
while (len) {
size_t toRead = len;
if (toRead > 512) {
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = millis() - start;
Serial.printf("%u bytes read for %u ms\n", flen, end);
file.close();
} else {
Serial.println("Failed to open file for reading");
}
file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
size_t i;
start = millis();
for (i = 0; i < 2048; i++) {
file.write(buf, 512);
}
end = millis() - start;
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
file.close();
}
// end sd methods
static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len) {
jpg_chunking_t *j = (jpg_chunking_t *)arg;
if (!index) {
j->len = 0;
Serial.println("jpg-encode-stream !index (with FACE recognition pass here)");
}
if (httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK) {
Serial.println("http send chunk");
return 0;
}
j->len += len;
return len;
}
static esp_err_t capture_handler(httpd_req_t *req) {
camera_fb_t * fb = NULL;
esp_err_t res = ESP_OK;
int64_t fr_start = esp_timer_get_time();
fb = esp_camera_fb_get(); // get picture from cam
if (!fb) {
Serial.println("Camera capture failed");
httpd_resp_send_500(req);
return ESP_FAIL;
}
httpd_resp_set_type(req, "image/jpeg");
httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
size_t out_len, out_width, out_height;
uint8_t * out_buf;
bool s;
bool detected = false;
int face_id = 0;
if (!detection_enabled || fb->width > 400) {
// Serial.println("lijn493");
size_t fb_len = 0;
if (fb->format == PIXFORMAT_JPEG) {
Serial.println("pixs format jpg for http send");
fb_len = fb->len;
//typedef struct {
// httpd_req_t *req;
// size_t len; //
//} jpg_chunking_t;
res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
} else {
jpg_chunking_t jchunk = {req, 0};
Serial.println(jchunk.len);//
res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
Serial.println("frame2jpg");
httpd_resp_send_chunk(req, NULL, 0);
fb_len = jchunk.len;
}
}
}
I don;t suppose you know how to use code tags? Figure them out and edit your post with the code.
Is the error message a secret that you will not share with others? if it is a debug error have you loaded the core dump into the ESP Exception Decoder? Look it up on the internetie thingy.
You know huge app means small file system?
What parts of your app have you commented out to see if they still produce the error?
How long have you had the error but kept on packing on code? At what point did the error manifest its self,? That would be a good starting point of where to look.
Are you using a wrover or a ESP32CAM? Different animals.
What kind of remaining memory messages are you getting?
SPIFF's have been depreciated on a ESP32 use LittleFileSystem instead.
Quite frankly, you should have sought to find this issue before your code got so bloated.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.