void open_door(WebsocketsClient &client) {
if (digitalRead(relay_pin) == LOW) {
digitalWrite(relay_pin, HIGH); //close (energise) relay so door unlocks
Serial.println("Door Unlocked");
client.send("door_open");
door_opened_millis = millis(); // time relay closed and door opened
}
}
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;
arduino coding
include "esp_camera.h"
include "camera_index.h"
include "Arduino.h"
include "fd_forward.h"
include "fr_forward.h"
include "fr_flash.h"
include
include
const char ssid = "gt-r"; const char password = "123456789";
define ENROLL_CONFIRM_TIMES 5
define FACE_ID_SAVE_NUMBER 7
define CAMERA_MODEL_AI_THINKER // Has PSRAM
include "camera_pins.h"
using namespace websockets; WebsocketsServer socket_server;
camera_fb_t * fb = NULL;
long current_millis; long last_detected_millis = 0;
define relay_pin 2 // pin 12 can also be used
unsigned long door_opened_millis = 0; long interval = 5000; // open lock for ... milliseconds bool face_recognised = false;
void app_facenet_main(); void app_httpserver_init();
typedef struct { uint8_t image; box_array_t net_boxes; dl_matrix3d_t *face_id; } http_img_process_result;
static inline mtmn_config_t app_mtmn_config() { mtmn_config_t mtmn_config = {0}; mtmn_config.type = FAST; mtmn_config.min_face = 80; mtmn_config.pyramid = 0.707; mtmn_config.pyramid_times = 4; mtmn_config.p_threshold.score = 0.6; mtmn_config.p_threshold.nms = 0.7; mtmn_config.p_threshold.candidate_number = 20; mtmn_config.r_threshold.score = 0.7; mtmn_config.r_threshold.nms = 0.7; mtmn_config.r_threshold.candidate_number = 10; mtmn_config.o_threshold.score = 0.7; mtmn_config.o_threshold.nms = 0.7; mtmn_config.o_threshold.candidate_number = 1; return mtmn_config; } mtmn_config_t mtmn_config = app_mtmn_config();
face_id_name_list st_face_list; static dl_matrix3du_t *aligned_face = NULL;
httpd_handle_t camera_httpd = NULL;
typedef enum { START_STREAM, START_DETECT, SHOW_FACES, START_RECOGNITION, START_ENROLL, ENROLL_COMPLETE, DELETE_ALL, } en_fsm_state; en_fsm_state g_state;
typedef struct { char enroll_name[ENROLL_NAME_LEN]; } httpd_resp_value;
httpd_resp_value st_name;
void setup() { Serial.begin(115200); Serial.setDebugOutput(true); Serial.println();
digitalWrite(relay_pin, LOW); pinMode(relay_pin, OUTPUT);
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; //init with high specs to pre-allocate larger buffers if (psramFound()) { config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; }
if defined(CAMERA_MODEL_ESP_EYE)
pinMode(13, INPUT_PULLUP); pinMode(14, INPUT_PULLUP);
endif
// camera init esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; }
sensor_t * s = esp_camera_sensor_get(); s->set_framesize(s, FRAMESIZE_QVGA);
if defined(CAMERA_MODEL_M5STACK_WIDE)
s->set_vflip(s, 1); s->set_hmirror(s, 1);
endif
WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected");
app_httpserver_init(); app_facenet_main(); socket_server.listen(82);
Serial.print("Camera Ready! Use 'http://"); Serial.print(WiFi.localIP()); Serial.println("' to connect"); }
static esp_err_t index_handler(httpd_req_t req) { httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "gzip"); return httpd_resp_send(req, (const char )index_ov2640_html_gz, index_ov2640_html_gz_len); }
httpd_uri_t index_uri = { .uri = "/", .method = HTTP_GET, .handler = index_handler, .user_ctx = NULL };
void app_httpserver_init () { httpd_config_t config = HTTPD_DEFAULT_CONFIG(); if (httpd_start(&camera_httpd, &config) == ESP_OK) Serial.println("httpd_start"); { httpd_register_uri_handler(camera_httpd, &index_uri); } }
void app_facenet_main() { face_id_name_init(&st_face_list, FACE_ID_SAVE_NUMBER, ENROLL_CONFIRM_TIMES); aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3); read_face_id_from_flash_with_name(&st_face_list); }
static inline int do_enrollment(face_id_name_list face_list, dl_matrix3d_t new_id) { ESP_LOGD(TAG, "START ENROLLING"); int left_sample_face = enroll_face_id_to_flash_with_name(face_list, new_id, st_name.enroll_name); ESP_LOGD(TAG, "Face ID %s Enrollment: Sample %d", st_name.enroll_name, ENROLL_CONFIRM_TIMES - left_sample_face); return left_sample_face; }
static esp_err_t send_face_list(WebsocketsClient &client) { client.send("delete_faces"); // tell browser to delete all faces face_id_node *head = st_face_list.head; char add_face[64]; for (int i = 0; i < st_face_list.count; i++) // loop current faces { sprintf(add_face, "listface:%s", head->id_name); client.send(add_face); //send face to browser head = head->next; } }
static esp_err_t delete_all_faces(WebsocketsClient &client) { delete_face_all_in_flash_with_name(&st_face_list); client.send("delete_faces"); }
void handle_message(WebsocketsClient &client, WebsocketsMessage msg) { if (msg.data() == "stream") { g_state = START_STREAM; client.send("STREAMING"); } if (msg.data() == "detect") { g_state = START_DETECT; client.send("DETECTING"); } if (msg.data().substring(0, 8) == "capture:") { g_state = START_ENROLL; char person[FACE_ID_SAVE_NUMBER ENROLL_NAME_LEN] = {0,}; msg.data().substring(8).toCharArray(person, sizeof(person)); memcpy(st_name.enroll_name, person, strlen(person) + 1); client.send("CAPTURING"); } if (msg.data() == "recognise") { g_state = START_RECOGNITION; client.send("RECOGNISING"); } if (msg.data().substring(0, 7) == "remove:") { char person[ENROLL_NAME_LEN FACE_ID_SAVE_NUMBER]; msg.data().substring(7).toCharArray(person, sizeof(person)); delete_face_id_in_flash_with_name(&st_face_list, person); send_face_list(client); // reset faces in the browser } if (msg.data() == "delete_all") { delete_all_faces(client); } }
void open_door(WebsocketsClient &client) { if (digitalRead(relay_pin) == LOW) { digitalWrite(relay_pin, HIGH); //close (energise) relay so door unlocks Serial.println("Door Unlocked"); client.send("door_open"); door_opened_millis = millis(); // time relay closed and door opened } }
void loop() { auto client = socket_server.accept(); client.onMessage(handle_message); dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, 320, 240, 3); http_img_process_result out_res = {0}; out_res.image = image_matrix->item;
send_face_list(client); client.send("STREAMING");
while (client.available()) { client.poll();
} }
app_htppd.cpp
include "esp_http_server.h"
include "esp_timer.h"
include "esp_camera.h"
include "img_converters.h"
include "camera_index.h"
include "Arduino.h"
include "fb_gfx.h"
include "fd_forward.h"
include "fr_forward.h"
define ENROLL_CONFIRM_TIMES 5
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)
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;
typedef struct { httpd_req_t *req; size_t len; } jpg_chunking_t;
define PART_BOUNDARY "123456789000000000000987654321"
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";
static ra_filter_t ra_filter; httpd_handle_t stream_httpd = NULL; httpd_handle_t camera_httpd = NULL;
static mtmn_config_t mtmn_config = {0}; static int8_t detection_enabled = 0; static int8_t recognition_enabled = 0; static int8_t is_enrolling = 0; static face_id_list id_list = {0};
static ra_filter_t ra_filter_init(ra_filter_t filter, size_t sample_size){ memset(filter, 0, sizeof(ra_filter_t));
}
static int ra_filter_run(ra_filter_t * filter, int value){ if(!filter->values){ return value; } filter->sum -= filter->values[filter->index]; filter->values[filter->index] = value; filter->sum += filter->values[filter->index]; filter->index++; filter->index = filter->index % filter->size; if (filter->count < filter->size) { filter->count++; } return filter->sum / filter->count; }
static void rgb_print(dl_matrix3du_t image_matrix, uint32_t color, const char str){ fb_data_t fb; fb.width = image_matrix->w; fb.height = image_matrix->h; fb.data = image_matrix->item; fb.bytes_per_pixel = 3; fb.format = FB_BGR888; fb_gfx_print(&fb, (fb.width - (strlen(str) * 14)) / 2, 10, color, str); }
static int rgb_printf(dl_matrix3du_t image_matrix, uint32_t color, const char format, ...){ char loc_buf[64]; char temp = loc_buf; int len; va_list arg; va_list copy; va_start(arg, format); va_copy(copy, arg); len = vsnprintf(loc_buf, sizeof(loc_buf), format, arg); va_end(copy); if(len >= sizeof(loc_buf)){ temp = (char)malloc(len+1); if(temp == NULL) { return 0; } } vsnprintf(temp, len+1, format, arg); va_end(arg); rgb_print(image_matrix, color, temp); if(len > 64){ free(temp); } return len; }
static void draw_face_boxes(dl_matrix3du_t image_matrix, box_array_t boxes, int face_id){ int x, y, w, h, i; uint32_t color = FACE_COLOR_YELLOW; if(face_id < 0){ color = FACE_COLOR_RED; } else if(face_id > 0){ color = FACE_COLOR_GREEN; } fb_data_t fb; fb.width = image_matrix->w; fb.height = image_matrix->h; fb.data = image_matrix->item; fb.bytes_per_pixel = 3; fb.format = FB_BGR888; for (i = 0; i < boxes->len; i++){ // rectangle box x = (int)boxes->box[i].box_p[0]; y = (int)boxes->box[i].box_p[1]; w = (int)boxes->box[i].box_p[2] - x + 1; h = (int)boxes->box[i].box_p[3] - y + 1; 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 0
endif
}
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;
}
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; } if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){ 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();
}
static esp_err_t stream_handler(httpd_req_t req){ camera_fb_t fb = NULL; esp_err_t res = ESP_OK; size_t _jpg_buf_len = 0; uint8_t _jpg_buf = NULL; char part_buf[64]; dl_matrix3du_t *image_matrix = NULL; bool detected = false; int face_id = 0; int64_t fr_start = 0; int64_t fr_ready = 0; int64_t fr_face = 0; int64_t fr_recognize = 0; int64_t fr_encode = 0;
}
static esp_err_t cmd_handler(httpd_req_t req){ char buf; size_t buf_len; char variable[32] = {0,}; char value[32] = {0,};
}
static esp_err_t status_handler(httpd_req_t *req){ static char json_response[1024];
}
static esp_err_t index_handler(httpd_req_t req){ httpd_resp_set_type(req, "text/html"); httpd_resp_set_hdr(req, "Content-Encoding", "gzip"); sensor_t s = esp_camera_sensor_get(); if (s->id.PID == OV3660_PID) { return httpd_resp_send(req, (const char )index_ov2640_html_gz_len, index_ov2640_html_gz_len); } return httpd_resp_send(req, (const char )index_ov2640_html_gz, index_ov2640_html_gz_len); }
static esp_err_t gpio12On_handler(httpd_req_t *req){ Serial.println("ON, 12.port HIGH"); digitalWrite(12, HIGH); return httpd_resp_send(req, NULL, 0); }
static esp_err_t gpio12Off_handler(httpd_req_t *req){ Serial.println("OFF, 12.port LOW"); digitalWrite(12, LOW); return httpd_resp_send(req, NULL, 0); }
static esp_err_t gpio13On_handler(httpd_req_t *req){ Serial.println("ON, 13.port HIGH"); digitalWrite(13, HIGH); return httpd_resp_send(req, NULL, 0); }
static esp_err_t gpio13Off_handler(httpd_req_t *req){ Serial.println("OF, 13.port LOW"); digitalWrite(13, LOW); return httpd_resp_send(req, NULL, 0); }
void startCameraServer(){ httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_uri_t stream_uri = { .uri = "/stream", .method = HTTP_GET, .handler = stream_handler, .user_ctx = NULL };
}