ESP32 Cam Face Enroll Error in AP Mode

Sorush
Posts: 1
Joined: Fri Sep 29, 2023 9:14 am

ESP32 Cam Face Enroll Error in AP Mode

Postby Sorush » Fri Sep 29, 2023 9:34 am

Hi Everyone!

All the Fd and Fr functions work perfectly in the camerawebserver sample codes but for some reason when I try to enroll a new face in my AP mode code I get guru meditation error:
  1. Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
  2. Core 1 register dump:
  3. PC      : 0x401178b9  PS      : 0x00060930  A0      : 0x800d2010  A1      : 0x3ffb1e60  
  4. A2      : 0xfb00005e  A3      : 0x3ffe1230  A4      : 0x3ffb8488  A5      : 0x000000f0  
  5. A6      : 0x3ffe1230  A7      : 0x3ffb8488  A8      : 0x8000beca  A9      : 0x3ffb1e80  
  6. A10     : 0x3ffb80c0  A11     : 0x3ffe4374  A12     : 0x3ffb9380  A13     : 0x00000000  
  7. A14     : 0x00000000  A15     : 0x3ffc1f5c  SAR     : 0x0000001b  EXCCAUSE: 0x0000001c  
  8. EXCVADDR: 0xfb00005e  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  
  9.  
  10. Backtrace: 0x401178b9:0x3ffb1e60 0x400d200d:0x3ffb1ec0 0x400d76e1:0x3ffb1fb0 0x4008db91:0x3ffb1fd0
  11.  
  12. Rebooting...

Using Arduino ide 2.2.1
ESP32 1.0.4 Core
ESP32_CAM Module

I also tried:
External power
Code from main example in app_httpd.cpp file
Code from Fr-flash library

Main code is:


  1.  
  2. #include "esp_camera.h"
  3. #include <WiFi.h>
  4. #include <WebSocketsServer.h>
  5. #include <EEPROM.h>
  6. #include <Preferences.h>
  7.  
  8. #include <stdio.h>
  9. #include "driver/uart.h"
  10. #include "driver/gpio.h"
  11. #include "sdkconfig.h"
  12.  
  13. #define CAMERA_MODEL_AI_THINKER
  14. #include "camera_pins.h"
  15.  
  16. /////////////face detection?//////////////////
  17. #include "fb_gfx.h"
  18. #include "fd_forward.h"
  19. #include "fr_forward.h"
  20. #include <fr_flash.h>
  21. #define FACE_COLOR_WHITE  0x00FFFFFF
  22. #define FACE_COLOR_BLACK  0x00000000
  23. #define FACE_COLOR_RED    0x000000FF
  24. #define FACE_COLOR_GREEN  0x0000FF00
  25. #define FACE_COLOR_BLUE   0x00FF0000
  26. #define FACE_COLOR_YELLOW (FACE_COLOR_RED | FACE_COLOR_GREEN)
  27. #define FACE_COLOR_CYAN   (FACE_COLOR_BLUE | FACE_COLOR_GREEN)
  28. #define FACE_COLOR_PURPLE (FACE_COLOR_BLUE | FACE_COLOR_RED)
  29. static int8_t recognition_enabled = 1;
  30. static int8_t is_enrolling = 0;
  31. #define ENROLL_CONFIRM_TIMES 5
  32. #define FACE_ID_SAVE_NUMBER 7
  33. face_id_list id_list = {0};
  34.  
  35. // Face detection/recognition variables
  36. camera_fb_t *fb;                              // Frame buffer pointer for picture from camera
  37. box_array_t *detected_face;                   // Information for a detected face
  38. dl_matrix3du_t *image_matrix;                 // Image matrix pointer
  39. dl_matrix3du_t *aligned_face;                 // Aligned face pointer
  40. //dl_matrix3d_t *face_id;                       // Face ID pointer
  41. face_id_node *face_recognized;                // Recognized face pointer
  42. mtmn_config_t mtmn_config;                    // MTMN detection settings
  43. char enroll_name[ENROLL_NAME_LEN+1];          // Name for face ID to be stored
  44. face_id_name_list st_face_list;
  45.  
  46. const int numReadings = 5;
  47. int readings[numReadings];      // the readings from the analog input
  48. int readIndex = 0;              // the index of the current reading
  49. int total = 0;                  // the running total
  50. int average_face_size = 0;      // the average
  51. int face_distance;
  52. int face_id = 0;
  53.  
  54. char time_str[40];
  55.  
  56. typedef struct
  57. {
  58.   char enroll_name[ENROLL_NAME_LEN];
  59. } httpd_resp_value;
  60.  
  61. httpd_resp_value st_name ;
  62.  
  63. typedef struct
  64. {
  65.   uint8_t *image;
  66.   box_array_t *net_boxes;
  67.   dl_matrix3d_t *face_id;
  68. } http_img_process_result;
  69. /////////////face detection?//////////////////
  70.  
  71. Preferences prefs;
  72. const uint8_t* ssid2;
  73. String cmd;
  74. char inByte;
  75. bool commandEnd=0;
  76.  
  77. WebSocketsServer webSocket = WebSocketsServer(9898);
  78. WebSocketsServer webSocket2 = WebSocketsServer(8888);
  79. WebSocketsServer webSocket3 = WebSocketsServer(8585);
  80.  
  81. bool isClientConnected;
  82.  
  83.  
  84.  
  85. static void rgb_print(dl_matrix3du_t *image_matrix, uint32_t color, const char * str){
  86.     fb_data_t fb;
  87.     fb.width = image_matrix->w;
  88.     fb.height = image_matrix->h;
  89.     fb.data = image_matrix->item;
  90.     fb.bytes_per_pixel = 3;
  91.     fb.format = FB_BGR888;
  92.     fb_gfx_print(&fb, (fb.width - (strlen(str) * 14)) / 2, 10, color, str);
  93. }
  94.  
  95. static int rgb_printf(dl_matrix3du_t *image_matrix, uint32_t color, const char *format, ...){
  96.     char loc_buf[64];
  97.     char * temp = loc_buf;
  98.     int len;
  99.     va_list arg;
  100.     va_list copy;
  101.     va_start(arg, format);
  102.     va_copy(copy, arg);
  103.     len = vsnprintf(loc_buf, sizeof(loc_buf), format, arg);
  104.     va_end(copy);
  105.     if(len >= sizeof(loc_buf)){
  106.         temp = (char*)malloc(len+1);
  107.         if(temp == NULL) {
  108.             return 0;
  109.         }
  110.     }
  111.     vsnprintf(temp, len+1, format, arg);
  112.     va_end(arg);
  113.     rgb_print(image_matrix, color, temp);
  114.     if(len > 64){
  115.         free(temp);
  116.     }
  117.     return len;
  118. }
  119.  
  120. static inline int do_enrollment(face_id_name_list *face_list, dl_matrix3d_t *new_id)
  121. {
  122.   ESP_LOGD(TAG, "START ENROLLING");
  123.   int left_sample_face = enroll_face_id_to_flash_with_name(face_list, new_id, st_name.enroll_name);
  124.   ESP_LOGD(TAG, "Face ID %s Enrollment: Sample %d",
  125.            st_name.enroll_name,
  126.            ENROLL_CONFIRM_TIMES - left_sample_face);
  127.   return left_sample_face;
  128. }
  129.  
  130. static int run_face_recognition(dl_matrix3du_t *image_matrix, box_array_t *net_boxes){
  131.     dl_matrix3du_t *aligned_face = NULL;
  132.     int matched_id = 0;
  133.  
  134.     aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
  135.     if(!aligned_face){
  136.         Serial.println("Could not allocate face recognition buffer");
  137.         return matched_id;
  138.     }
  139.     if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK){
  140.  
  141.             matched_id = recognize_face(&id_list, aligned_face);
  142.             if (matched_id >= 0) {
  143.                 Serial.printf("Match Face ID: %u\n", matched_id);
  144.                 rgb_printf(image_matrix, FACE_COLOR_GREEN, "Sorush", matched_id);
  145.             } else {
  146.                 //Serial.println("No Match Found");
  147.                 rgb_print(image_matrix, FACE_COLOR_RED, "Not Sorush!");
  148.                 matched_id = -1;
  149.             }
  150.      }else {
  151.         //Serial.println("Face Not Aligned");
  152.         //rgb_print(image_matrix, FACE_COLOR_YELLOW, "Human Detected");
  153.     }
  154.  
  155.     dl_matrix3du_free(aligned_face);
  156.     return matched_id;
  157. }
  158.  
  159. static void draw_face_boxes(dl_matrix3du_t *image_matrix, box_array_t *boxes)
  160. {
  161.   int x, y, w, h, i, half_width, half_height;
  162.   uint32_t color = FACE_COLOR_YELLOW;
  163.     if(face_id < 0){
  164.         color = FACE_COLOR_RED;
  165.     } else if(face_id > 0){
  166.         color = FACE_COLOR_GREEN;
  167.     }
  168.   fb_data_t fb;
  169.   fb.width = image_matrix->w;
  170.   fb.height = image_matrix->h;
  171.   fb.data = image_matrix->item;
  172.   fb.bytes_per_pixel = 3;
  173.   fb.format = FB_BGR888;
  174.   for (i = 0; i < boxes->len; i++) {
  175.  
  176.     // Convoluted way of finding face centre...
  177.     x = ((int)boxes->box[i].box_p[0]);
  178.     w = (int)boxes->box[i].box_p[2] - x + 1;
  179.     half_width = w / 2;
  180.     int face_center_pan = x + half_width; // current face centre x co-ordinate
  181.  
  182.     y = (int)boxes->box[i].box_p[1];
  183.     h = (int)boxes->box[i].box_p[3] - y + 1;
  184.     half_height = h / 2;
  185.     int face_center_tilt = y + half_height;  // current face centre y co-ordinate
  186.  
  187.     //Serial.println(h);
  188.  
  189.     fb_gfx_drawFastHLine(&fb, x, y, w, color);
  190.     fb_gfx_drawFastHLine(&fb, x, y + h - 1, w, color);
  191.     fb_gfx_drawFastVLine(&fb, x, y, h, color);
  192.     fb_gfx_drawFastVLine(&fb, x + w - 1, y, h, color);
  193.  
  194.  
  195.     // subtract the last reading:
  196.     total = total - readings[readIndex];
  197.     // add current face height:
  198.     readings[readIndex] = h;
  199.     // add the reading to the total:
  200.     total = total + readings[readIndex];
  201.     // advance to the next position in the array:
  202.     readIndex = readIndex + 1;
  203.  
  204.     // if we're at the end of the array...
  205.     if (readIndex >= numReadings) {
  206.       // ...wrap around to the beginning:
  207.       readIndex = 0;
  208.     }
  209.  
  210.     // calculate the average:
  211.     average_face_size = total / numReadings;
  212.  
  213.     int eq_top = 3.6 * 200 * 240; //f(mm) x real height(mm) x image height(px)
  214.     int eq_bottom = average_face_size * 2.7; //object height(px) x sensor height(mm)
  215.     int face_distance = eq_top / eq_bottom;
  216.  
  217.     /*Serial.print('<'); // start marker
  218.     Serial.print(face_center_pan);
  219.     Serial.print(','); // comma separator
  220.     Serial.print(face_center_tilt);
  221.     Serial.print(','); // comma separator
  222.     Serial.print(face_distance);
  223.     Serial.println('>'); // end marker*/
  224.     Serial.print("pan=");
  225.     Serial.println(face_center_pan);
  226.     Serial.print("tilt=");
  227.     Serial.println(face_center_tilt);
  228.     Serial.print("distance=");
  229.     Serial.println(face_distance);
  230.  
  231.   }
  232. }
  233.  
  234. void saveSSID(uint8_t ssid[]){
  235.   prefs.putBytes("ssid", ssid, sizeof(ssid));
  236. }
  237.  
  238. void savePassword(uint8_t pass[]){
  239.   prefs.putBytes("password", pass, sizeof(pass));
  240. }
  241.  
  242. char* getSSID(){
  243.   size_t cfgLen = prefs.getBytesLength("ssid");
  244.   char buffer[cfgLen];
  245.   prefs.getBytes("ssid", buffer, cfgLen);
  246.   return buffer;
  247. }
  248.  
  249. char* getPassword(){
  250.   size_t cfgLen = prefs.getBytesLength("password");
  251.   char buffer[cfgLen];
  252.   prefs.getBytes("password", buffer, cfgLen);
  253.   return buffer;
  254. }
  255.  
  256. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  257.  
  258.     switch(type) {
  259.         case WStype_DISCONNECTED:
  260.             {
  261.               webSocket.sendTXT(num, "Disconnected!");
  262.             }
  263.             break;
  264.         case WStype_CONNECTED:
  265.             {
  266.                 IPAddress ip = webSocket.remoteIP(num);
  267.                 Serial.print("Connected IP address:");
  268.                 Serial.println(ip);
  269.                 isClientConnected = true;
  270.                 webSocket.sendTXT(num, "phase2");
  271.                 webSocket.sendTXT(num, "connected");
  272.                
  273.             }
  274.             break;
  275.     case WStype_TEXT:
  276.             {
  277.               String msg = (char*)payload;
  278.               Serial.println(msg);
  279.              
  280.               if(msg.startsWith("ssid=")){
  281.                 ssid2 = reinterpret_cast<const uint8_t*>(msg.substring(5).c_str());
  282.                 //EEP_Write_String(1,msg);
  283.                 //Serial.print("aaaaaaaaaaaaaaaa");
  284.                 //Serial.println(ssid2.toString());
  285.                 String s = msg.substring(5);
  286.                 prefs.begin("config");
  287.                 prefs.begin("ssid");
  288.                 prefs.putBytes("ssid", s.c_str() , sizeof(s));
  289.               }
  290.  
  291.               else if(msg.startsWith("password=")){
  292.                 const uint8_t* pass = reinterpret_cast<const uint8_t*>(msg.substring(9).c_str());
  293.                 //EEP_Write_String(3,"12345678");
  294.                 //prefs.putBytes("password", pass, sizeof(pass));
  295.               }
  296.  
  297.               else if(msg.startsWith("getSSID")){
  298.                 size_t cfgLen = prefs.getBytesLength("ssid");
  299.                 char ssid[cfgLen];
  300.                 //prefs.getBytes("ssid", ssid, cfgLen);
  301.                 webSocket.sendTXT(num, msg + "=" + "ssid");
  302.               }
  303.  
  304.               else if(msg.startsWith("getPassword")){
  305.                 size_t cfgLen = prefs.getBytesLength("password");
  306.                 char pass[cfgLen];
  307.                 //prefs.getBytes("password", pass, cfgLen);
  308.                 webSocket.sendTXT(num, msg + "=" + "pass");
  309.               }
  310.  
  311.               else if(msg.startsWith("uv-true")){
  312.                 is_enrolling = 1;
  313.                 //prefs.getBytes("password", pass, cfgLen);
  314.                 webSocket.sendTXT(num, "enrolling");
  315.               }
  316.  
  317.               else {
  318.                 //webSocket.sendTXT(num, msg);
  319.               }
  320.              
  321.             }
  322.     case WStype_BIN:
  323.     case WStype_ERROR:      
  324.     case WStype_FRAGMENT_TEXT_START:
  325.     case WStype_FRAGMENT_BIN_START:
  326.     case WStype_FRAGMENT:
  327.     case WStype_FRAGMENT_FIN:
  328.       break;
  329.     }
  330.  
  331. }
  332.  
  333. void webSocketEvent3(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  334.  
  335.     switch(type) {
  336.         case WStype_DISCONNECTED:
  337.             {
  338.               Serial.println("DC");
  339.             }
  340.             break;
  341.         case WStype_CONNECTED:
  342.             {
  343.               isClientConnected = true;
  344.               Serial.println("C");
  345.             }
  346.             break;
  347.     }
  348. }
  349.  
  350. void setup() {
  351.   Serial.begin(9600);
  352.   Serial.println();
  353.   prefs.begin("config");
  354.   prefs.begin("ssid");
  355.   prefs.begin("password");
  356.  
  357.   size_t cfgLen = prefs.getBytesLength("config");
  358.   char buffer[cfgLen];
  359.   prefs.getBytes("config", buffer, cfgLen);
  360.  
  361.   uint8_t ssidd[] = "Aibot";
  362.   uint8_t passs[] = "11223344";
  363.  
  364.   prefs.putBytes("ssid", ssidd, sizeof(ssidd));
  365.   prefs.putBytes("password", passs, sizeof(passs));
  366.  
  367.   size_t cfgLen3 = prefs.getBytesLength("ssid");
  368.   char ssid[cfgLen3];
  369.   prefs.getBytes("ssid", ssid, cfgLen3);
  370.   //Serial.print("serialllllllllllllllllllll");
  371.   Serial.println(ssid);
  372.  
  373.   size_t cfgLen4 = prefs.getBytesLength("password");
  374.   char pass[cfgLen4];
  375.   prefs.getBytes("password", pass, cfgLen4);
  376.  
  377.   camera_config_t config;
  378.   config.ledc_channel = LEDC_CHANNEL_0;
  379.   config.ledc_timer = LEDC_TIMER_0;
  380.   config.pin_d0 = Y2_GPIO_NUM;
  381.   config.pin_d1 = Y3_GPIO_NUM;
  382.   config.pin_d2 = Y4_GPIO_NUM;
  383.   config.pin_d3 = Y5_GPIO_NUM;
  384.   config.pin_d4 = Y6_GPIO_NUM;
  385.   config.pin_d5 = Y7_GPIO_NUM;
  386.   config.pin_d6 = Y8_GPIO_NUM;
  387.   config.pin_d7 = Y9_GPIO_NUM;
  388.   config.pin_xclk = XCLK_GPIO_NUM;
  389.   config.pin_pclk = PCLK_GPIO_NUM;
  390.   config.pin_vsync = VSYNC_GPIO_NUM;
  391.   config.pin_href = HREF_GPIO_NUM;
  392.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  393.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  394.   config.pin_pwdn = PWDN_GPIO_NUM;
  395.   config.pin_reset = RESET_GPIO_NUM;
  396.   config.xclk_freq_hz = 10000000;
  397.   config.pixel_format = PIXFORMAT_JPEG;
  398.   //init with high specs to pre-allocate larger buffers
  399.   if(psramFound()){
  400.     config.frame_size = FRAMESIZE_QVGA;//VGA
  401.     config.jpeg_quality = 10;
  402.     config.fb_count = 2;
  403.   } else {
  404.     config.frame_size = FRAMESIZE_QVGA;//SVGA
  405.     config.jpeg_quality = 12;
  406.     config.fb_count = 1;
  407.   }
  408.  
  409.     mtmn_config = mtmn_init_config();     // Set MTMN face detection details (default values)
  410.  
  411.     image_matrix = dl_matrix3du_alloc(1, 320, 240, 3);                 // Allocate memory for image matrix
  412.     aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);  // Allocate memory for aligned face
  413.  
  414.   // camera init
  415.   esp_err_t err = esp_camera_init(&config);
  416.   if (err != ESP_OK) {
  417.     Serial.printf("Camera init failed with error 0x%x", err);
  418.     return;
  419.   }
  420.  
  421.   delay(1000);
  422.  
  423.   WiFi.softAP(ssid, pass);
  424.   IPAddress IP = WiFi.softAPIP();
  425.   Serial.print("AP IP address: ");
  426.   Serial.println(IP);
  427.  
  428.   webSocket.begin();
  429.   webSocket.onEvent(webSocketEvent);
  430.  
  431.   webSocket2.begin();
  432.   webSocket3.begin();
  433.   webSocket3.onEvent(webSocketEvent3);
  434. }
  435.  
  436. void loop() {
  437.   webSocket.loop();
  438.   webSocket2.loop();
  439.   webSocket3.loop();
  440.   if(isClientConnected){
  441.     camera_fb_t *fb = NULL;
  442.     esp_err_t res = ESP_OK;
  443.     fb = esp_camera_fb_get();
  444.     if(!fb){
  445.       Serial.println("Camera capture failed");
  446.       esp_camera_fb_return(fb);
  447.       return;
  448.     }
  449.     face_id = 0;
  450.     size_t fb_len = 0;
  451.     uint8_t * _jpg_buf = NULL;
  452.     dl_matrix3du_t *image_matrix = NULL;
  453.     if(fb->format != PIXFORMAT_JPEG){
  454.       Serial.println("Non-JPEG data not implemented");
  455.       return;
  456.     }
  457.  
  458.     fb_len = fb->len;
  459.     _jpg_buf = fb->buf;
  460.     image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  461.  
  462.     fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item);
  463.  
  464.     box_array_t *net_boxes = NULL;
  465.     net_boxes = face_detect(image_matrix, &mtmn_config);
  466.  
  467.     if (net_boxes) {
  468.       if(recognition_enabled){
  469.         face_id = run_face_recognition(image_matrix, net_boxes);
  470.       }
  471.       draw_face_boxes(image_matrix, net_boxes);
  472.       free(net_boxes->score);
  473.       free(net_boxes->box);
  474.       free(net_boxes->landmark);
  475.       free(net_boxes);
  476.     }
  477.     fmt2jpg(image_matrix->item, fb->width * fb->height * 3, fb->width, fb->height, PIXFORMAT_RGB888, 90, &_jpg_buf, &fb_len);
  478.     ////////////////////////////////Video Stream////////////////////////////////
  479.     webSocket2.broadcastBIN((const uint8_t*) _jpg_buf, fb_len);
  480.     ////////////////////////////////Video Stream////////////////////////////////
  481.     esp_camera_fb_return(fb);  
  482.  
  483.     fb = NULL;
  484.     dl_matrix3du_free(image_matrix);
  485.     free(_jpg_buf);
  486.     _jpg_buf = NULL;
  487.  
  488.     /////////////////////////////face detection////////////////////////////////
  489.     if(is_enrolling){
  490.     if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK )
  491.           {
  492.             http_img_process_result out_res = {0};
  493.             out_res.image = image_matrix->item;
  494.             int left_sample_face = do_enrollment(&st_face_list, out_res.face_id);
  495.             char enrolling_message[64];
  496.             sprintf(enrolling_message, "SAMPLE NUMBER %d FOR %s", ENROLL_CONFIRM_TIMES - left_sample_face, st_name.enroll_name);
  497.             webSocket.broadcastTXT(enrolling_message);
  498.             if (left_sample_face == 0)
  499.             {
  500.               ESP_LOGI(TAG, "Enrolled Face ID: %s", st_face_list.tail->id_name);
  501.               char captured_message[64];
  502.               sprintf(captured_message, "FACE CAPTURED FOR %s", st_face_list.tail->id_name);
  503.               is_enrolling = 0;
  504.               webSocket.broadcastTXT(captured_message);
  505.  
  506.             }
  507.           }
  508.   }
  509.     /////////////////////////////face detection////////////////////////////////
  510.  
  511.       /*char qwe = Serial.read();
  512.       String asd;
  513.       asd=qwe;
  514.       webSocket.sendTXT(sizeof(asd),asd);*/
  515.  
  516.       ///////////////////////////////////////////////////////////////
  517.       if(Serial.available() > 0){
  518.       if(commandEnd){
  519.       cmd="";
  520.       commandEnd=false;
  521.     }
  522.       inByte = Serial.read();
  523.       if (inByte != '\n') {
  524.       cmd += inByte;
  525.       }else{
  526.         webSocket.broadcastTXT(cmd);
  527.         commandEnd=true;
  528.       }
  529.     }
  530.     ////////////////////////////////////////////////////////////////
  531.   }
  532.  
  533. }
  534.  
  535. //*****************************************************************************
  536. // Handle uptime request
  537. // Last start date/time and free heap size (to detect memory leaks)
  538. //
  539. void handleUptime() {                          
  540.   char text[80];
  541.   snprintf(text, 80, "Last start: %s \nFree Heap: %d\n", time_str, ESP.getFreeHeap());
  542.   //webSocket.broadcastTXT(200, "text/plain", text);  
  543. }
  544.  


also the previous code leads to the same error:
  1.  
  2. static int run_face_recognition(dl_matrix3du_t *image_matrix, box_array_t *net_boxes){
  3.     dl_matrix3du_t *aligned_face = NULL;
  4.     int matched_id = 0;
  5.  
  6.     aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
  7.     if(!aligned_face){
  8.         Serial.println("Could not allocate face recognition buffer");
  9.         return matched_id;
  10.     }
  11.     if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK){
  12.         if (is_enrolling == 1){
  13.             int8_t left_sample_face = enroll_face(&id_list, aligned_face);
  14.  
  15.             if(left_sample_face == (ENROLL_CONFIRM_TIMES - 1)){
  16.                 Serial.printf("Enrolling Face ID: %d\n", id_list.tail);
  17.             }
  18.             Serial.printf("Enrolling Face ID: %d sample %d\n", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
  19.             rgb_printf(image_matrix, FACE_COLOR_CYAN, "ID[%u] Sample[%u]", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
  20.             if (left_sample_face == 0){
  21.                 is_enrolling = 0;
  22.                 Serial.printf("Enrolled Face ID: %d\n", id_list.tail);
  23.             }
  24.         } else {
  25.             matched_id = recognize_face(&id_list, aligned_face);
  26.             if (matched_id >= 0) {
  27.                 Serial.printf("Match Face ID: %u\n", matched_id);
  28.                 rgb_printf(image_matrix, FACE_COLOR_GREEN, "Hello Subject %u", matched_id);
  29.             } else {
  30.                 Serial.println("No Match Found");
  31.                 rgb_print(image_matrix, FACE_COLOR_RED, "Intruder Alert!");
  32.                 matched_id = -1;
  33.             }
  34.         }
  35.     } else {
  36.         Serial.println("Face Not Aligned");
  37.         //rgb_print(image_matrix, FACE_COLOR_YELLOW, "Human Detected");
  38.     }
  39.  
  40.     dl_matrix3du_free(aligned_face);
  41.     return matched_id;
  42. }
  43.  

Who is online

Users browsing this forum: No registered users and 86 guests