SHARE
TWEET

Untitled

a guest Feb 11th, 2020 4,807 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "esp_camera.h"
  2. #include "fd_forward.h"
  3. #include "fr_forward.h"
  4. #include "fr_flash.h"
  5.  
  6. #define relayPin 2 // pin 12 can also be used
  7. unsigned long currentMillis = 0;
  8. unsigned long openedMillis = 0;
  9. long interval = 5000;           // open lock for ... milliseconds
  10.  
  11. #define ENROLL_CONFIRM_TIMES 5
  12. #define FACE_ID_SAVE_NUMBER 7
  13.  
  14. static inline mtmn_config_t app_mtmn_config()
  15. {
  16.   mtmn_config_t mtmn_config = {0};
  17.   mtmn_config.type = FAST;
  18.   mtmn_config.min_face = 80;
  19.   mtmn_config.pyramid = 0.707;
  20.   mtmn_config.pyramid_times = 4;
  21.   mtmn_config.p_threshold.score = 0.6;
  22.   mtmn_config.p_threshold.nms = 0.7;
  23.   mtmn_config.p_threshold.candidate_number = 20;
  24.   mtmn_config.r_threshold.score = 0.7;
  25.   mtmn_config.r_threshold.nms = 0.7;
  26.   mtmn_config.r_threshold.candidate_number = 10;
  27.   mtmn_config.o_threshold.score = 0.7;
  28.   mtmn_config.o_threshold.nms = 0.7;
  29.   mtmn_config.o_threshold.candidate_number = 1;
  30.   return mtmn_config;
  31. }
  32. mtmn_config_t mtmn_config = app_mtmn_config();
  33.  
  34. static face_id_list id_list = {0};
  35. dl_matrix3du_t *image_matrix =  NULL;
  36. camera_fb_t * fb = NULL;
  37.  
  38. dl_matrix3du_t *aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
  39.  
  40. void setup() {
  41.   Serial.begin(115200);
  42.  
  43.   digitalWrite(relayPin, LOW);
  44.   pinMode(relayPin, OUTPUT);
  45.  
  46.   camera_config_t config;
  47.   config.ledc_channel = LEDC_CHANNEL_0;
  48.   config.ledc_timer = LEDC_TIMER_0;
  49.   config.pin_d0 = 5;
  50.   config.pin_d1 = 18;
  51.   config.pin_d2 = 19;
  52.   config.pin_d3 = 21;
  53.   config.pin_d4 = 36;
  54.   config.pin_d5 = 39;
  55.   config.pin_d6 = 34;
  56.   config.pin_d7 = 35;
  57.   config.pin_xclk = 0;
  58.   config.pin_pclk = 22;
  59.   config.pin_vsync = 25;
  60.   config.pin_href = 23;
  61.   config.pin_sscb_sda = 26;
  62.   config.pin_sscb_scl = 27;
  63.   config.pin_pwdn = 32;
  64.   config.pin_reset = -1;
  65.   config.xclk_freq_hz = 20000000;
  66.   config.pixel_format = PIXFORMAT_JPEG;
  67.   config.frame_size = FRAMESIZE_SVGA;
  68.   config.jpeg_quality = 12;
  69.   config.fb_count = 1;
  70.  
  71.   esp_err_t err = esp_camera_init(&config);
  72.   if (err != ESP_OK) {
  73.     Serial.printf("Camera init failed with error 0x%x", err);
  74.     return;
  75.   }
  76.  
  77.   //drop down frame size for higher initial frame rate
  78.   sensor_t * s = esp_camera_sensor_get();
  79.   s->set_framesize(s, FRAMESIZE_QVGA);
  80.  
  81.   face_id_init(&id_list, FACE_ID_SAVE_NUMBER, ENROLL_CONFIRM_TIMES);
  82.   read_face_id_from_flash(&id_list);// Read current face data from on-board flash
  83. }
  84.  
  85. void rzoCheckForFace() {
  86.   currentMillis = millis();
  87.   if (run_face_recognition()) { // face recognition function has returned true
  88.     Serial.println("Face recognised");
  89.     digitalWrite(relayPin, HIGH); //close (energise) relay
  90.     openedMillis = millis(); //time relay closed
  91.   }
  92.   if (currentMillis - interval > openedMillis){ // current time - face recognised time > 5 secs
  93.     digitalWrite(relayPin, LOW); //open relay
  94.   }
  95. }
  96.  
  97. bool run_face_recognition() {
  98.   bool faceRecognised = false; // default
  99.   int64_t start_time = esp_timer_get_time();
  100.   fb = esp_camera_fb_get();
  101.   if (!fb) {
  102.     Serial.println("Camera capture failed");
  103.     return false;
  104.   }
  105.  
  106.   int64_t fb_get_time = esp_timer_get_time();
  107.   Serial.printf("Get one frame in %u ms.\n", (fb_get_time - start_time) / 1000); // this line can be commented out
  108.  
  109.   image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  110.   uint32_t res = fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item);
  111.   if (!res) {
  112.     Serial.println("to rgb888 failed");
  113.     dl_matrix3du_free(image_matrix);
  114.   }
  115.  
  116.   esp_camera_fb_return(fb);
  117.  
  118.   box_array_t *net_boxes = face_detect(image_matrix, &mtmn_config);
  119.  
  120.   if (net_boxes) {
  121.     if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK) {
  122.  
  123.       int matched_id = recognize_face(&id_list, aligned_face);
  124.       if (matched_id >= 0) {
  125.         Serial.printf("Match Face ID: %u\n", matched_id);
  126.         faceRecognised = true; // function will now return true
  127.       } else {
  128.         Serial.println("No Match Found");
  129.         matched_id = -1;
  130.       }
  131.     } else {
  132.       Serial.println("Face Not Aligned");
  133.     }
  134.  
  135.     free(net_boxes->box);
  136.     free(net_boxes->landmark);
  137.     free(net_boxes);
  138.   }
  139.  
  140.   dl_matrix3du_free(image_matrix);
  141.   return faceRecognised;
  142. }
  143.  
  144. void loop() {
  145.   rzoCheckForFace();
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top