app_httpd.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "esp_http_server.h"
  15. #include "esp_timer.h"
  16. #include "esp_camera.h"
  17. #include "img_converters.h"
  18. #include "camera_index.h"
  19. #include "Arduino.h"
  20. #include "fb_gfx.h"
  21. #include "fd_forward.h"
  22. #include "fr_forward.h"
  23. #define ENROLL_CONFIRM_TIMES 5
  24. #define FACE_ID_SAVE_NUMBER 7
  25. #define FACE_COLOR_WHITE 0x00FFFFFF
  26. #define FACE_COLOR_BLACK 0x00000000
  27. #define FACE_COLOR_RED 0x000000FF
  28. #define FACE_COLOR_GREEN 0x0000FF00
  29. #define FACE_COLOR_BLUE 0x00FF0000
  30. #define FACE_COLOR_YELLOW (FACE_COLOR_RED | FACE_COLOR_GREEN)
  31. #define FACE_COLOR_CYAN (FACE_COLOR_BLUE | FACE_COLOR_GREEN)
  32. #define FACE_COLOR_PURPLE (FACE_COLOR_BLUE | FACE_COLOR_RED)
  33. typedef struct {
  34. size_t size; //number of values used for filtering
  35. size_t index; //current value index
  36. size_t count; //value count
  37. int sum;
  38. int * values; //array to be filled with values
  39. } ra_filter_t;
  40. typedef struct {
  41. httpd_req_t *req;
  42. size_t len;
  43. } jpg_chunking_t;
  44. #define PART_BOUNDARY "123456789000000000000987654321"
  45. static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  46. static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  47. static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  48. static ra_filter_t ra_filter;
  49. httpd_handle_t stream_httpd = NULL;
  50. httpd_handle_t camera_httpd = NULL;
  51. static mtmn_config_t mtmn_config = {0};
  52. static int8_t detection_enabled = 0;
  53. static int8_t recognition_enabled = 0;
  54. static int8_t is_enrolling = 0;
  55. static face_id_list id_list = {0};
  56. static ra_filter_t * ra_filter_init(ra_filter_t * filter, size_t sample_size){
  57. memset(filter, 0, sizeof(ra_filter_t));
  58. filter->values = (int *)malloc(sample_size * sizeof(int));
  59. if(!filter->values){
  60. return NULL;
  61. }
  62. memset(filter->values, 0, sample_size * sizeof(int));
  63. filter->size = sample_size;
  64. return filter;
  65. }
  66. static int ra_filter_run(ra_filter_t * filter, int value){
  67. if(!filter->values){
  68. return value;
  69. }
  70. filter->sum -= filter->values[filter->index];
  71. filter->values[filter->index] = value;
  72. filter->sum += filter->values[filter->index];
  73. filter->index++;
  74. filter->index = filter->index % filter->size;
  75. if (filter->count < filter->size) {
  76. filter->count++;
  77. }
  78. return filter->sum / filter->count;
  79. }
  80. static void rgb_print(dl_matrix3du_t *image_matrix, uint32_t color, const char * str){
  81. fb_data_t fb;
  82. fb.width = image_matrix->w;
  83. fb.height = image_matrix->h;
  84. fb.data = image_matrix->item;
  85. fb.bytes_per_pixel = 3;
  86. fb.format = FB_BGR888;
  87. fb_gfx_print(&fb, (fb.width - (strlen(str) * 14)) / 2, 10, color, str);
  88. }
  89. static int rgb_printf(dl_matrix3du_t *image_matrix, uint32_t color, const char *format, ...){
  90. char loc_buf[64];
  91. char * temp = loc_buf;
  92. int len;
  93. va_list arg;
  94. va_list copy;
  95. va_start(arg, format);
  96. va_copy(copy, arg);
  97. len = vsnprintf(loc_buf, sizeof(loc_buf), format, arg);
  98. va_end(copy);
  99. if(len >= sizeof(loc_buf)){
  100. temp = (char*)malloc(len+1);
  101. if(temp == NULL) {
  102. return 0;
  103. }
  104. }
  105. vsnprintf(temp, len+1, format, arg);
  106. va_end(arg);
  107. rgb_print(image_matrix, color, temp);
  108. if(len > 64){
  109. free(temp);
  110. }
  111. return len;
  112. }
  113. static void draw_face_boxes(dl_matrix3du_t *image_matrix, box_array_t *boxes, int face_id){
  114. int x, y, w, h, i;
  115. uint32_t color = FACE_COLOR_YELLOW;
  116. if(face_id < 0){
  117. color = FACE_COLOR_RED;
  118. } else if(face_id > 0){
  119. color = FACE_COLOR_GREEN;
  120. }
  121. fb_data_t fb;
  122. fb.width = image_matrix->w;
  123. fb.height = image_matrix->h;
  124. fb.data = image_matrix->item;
  125. fb.bytes_per_pixel = 3;
  126. fb.format = FB_BGR888;
  127. for (i = 0; i < boxes->len; i++){
  128. // rectangle box
  129. x = (int)boxes->box[i].box_p[0];
  130. y = (int)boxes->box[i].box_p[1];
  131. w = (int)boxes->box[i].box_p[2] - x + 1;
  132. h = (int)boxes->box[i].box_p[3] - y + 1;
  133. fb_gfx_drawFastHLine(&fb, x, y, w, color);
  134. fb_gfx_drawFastHLine(&fb, x, y+h-1, w, color);
  135. fb_gfx_drawFastVLine(&fb, x, y, h, color);
  136. fb_gfx_drawFastVLine(&fb, x+w-1, y, h, color);
  137. #if 0
  138. // landmark
  139. int x0, y0, j;
  140. for (j = 0; j < 10; j+=2) {
  141. x0 = (int)boxes->landmark[i].landmark_p[j];
  142. y0 = (int)boxes->landmark[i].landmark_p[j+1];
  143. fb_gfx_fillRect(&fb, x0, y0, 3, 3, color);
  144. }
  145. #endif
  146. }
  147. }
  148. static int run_face_recognition(dl_matrix3du_t *image_matrix, box_array_t *net_boxes){
  149. dl_matrix3du_t *aligned_face = NULL;
  150. int matched_id = 0;
  151. aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
  152. if(!aligned_face){
  153. Serial.println("Could not allocate face recognition buffer");
  154. return matched_id;
  155. }
  156. if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK){
  157. if (is_enrolling == 1){
  158. int8_t left_sample_face = enroll_face(&id_list, aligned_face);
  159. if(left_sample_face == (ENROLL_CONFIRM_TIMES - 1)){
  160. Serial.printf("Enrolling Face ID: %d\n", id_list.tail);
  161. }
  162. Serial.printf("Enrolling Face ID: %d sample %d\n", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
  163. rgb_printf(image_matrix, FACE_COLOR_CYAN, "ID[%u] Sample[%u]", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
  164. if (left_sample_face == 0){
  165. is_enrolling = 0;
  166. Serial.printf("Enrolled Face ID: %d\n", id_list.tail);
  167. }
  168. } else {
  169. matched_id = recognize_face(&id_list, aligned_face);
  170. if (matched_id >= 0) {
  171. Serial.printf("Match Face ID: %u\n", matched_id);
  172. rgb_printf(image_matrix, FACE_COLOR_GREEN, "Hello Subject %u", matched_id);
  173. } else {
  174. Serial.println("No Match Found");
  175. rgb_print(image_matrix, FACE_COLOR_RED, "Intruder Alert!");
  176. matched_id = -1;
  177. }
  178. }
  179. } else {
  180. Serial.println("Face Not Aligned");
  181. //rgb_print(image_matrix, FACE_COLOR_YELLOW, "Human Detected");
  182. }
  183. dl_matrix3du_free(aligned_face);
  184. return matched_id;
  185. }
  186. static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
  187. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  188. if(!index){
  189. j->len = 0;
  190. }
  191. if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
  192. return 0;
  193. }
  194. j->len += len;
  195. return len;
  196. }
  197. static esp_err_t capture_handler(httpd_req_t *req){
  198. camera_fb_t * fb = NULL;
  199. esp_err_t res = ESP_OK;
  200. int64_t fr_start = esp_timer_get_time();
  201. fb = esp_camera_fb_get();
  202. if (!fb) {
  203. Serial.println("Camera capture failed");
  204. httpd_resp_send_500(req);
  205. return ESP_FAIL;
  206. }
  207. httpd_resp_set_type(req, "image/jpeg");
  208. httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
  209. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  210. size_t out_len, out_width, out_height;
  211. uint8_t * out_buf;
  212. bool s;
  213. bool detected = false;
  214. int face_id = 0;
  215. if(!detection_enabled || fb->width > 400){
  216. size_t fb_len = 0;
  217. if(fb->format == PIXFORMAT_JPEG){
  218. fb_len = fb->len;
  219. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  220. } else {
  221. jpg_chunking_t jchunk = {req, 0};
  222. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  223. httpd_resp_send_chunk(req, NULL, 0);
  224. fb_len = jchunk.len;
  225. }
  226. esp_camera_fb_return(fb);
  227. int64_t fr_end = esp_timer_get_time();
  228. Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
  229. return res;
  230. }
  231. dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  232. if (!image_matrix) {
  233. esp_camera_fb_return(fb);
  234. Serial.println("dl_matrix3du_alloc failed");
  235. httpd_resp_send_500(req);
  236. return ESP_FAIL;
  237. }
  238. out_buf = image_matrix->item;
  239. out_len = fb->width * fb->height * 3;
  240. out_width = fb->width;
  241. out_height = fb->height;
  242. s = fmt2rgb888(fb->buf, fb->len, fb->format, out_buf);
  243. esp_camera_fb_return(fb);
  244. if(!s){
  245. dl_matrix3du_free(image_matrix);
  246. Serial.println("to rgb888 failed");
  247. httpd_resp_send_500(req);
  248. return ESP_FAIL;
  249. }
  250. box_array_t *net_boxes = face_detect(image_matrix, &mtmn_config);
  251. if (net_boxes){
  252. detected = true;
  253. if(recognition_enabled){
  254. face_id = run_face_recognition(image_matrix, net_boxes);
  255. }
  256. draw_face_boxes(image_matrix, net_boxes, face_id);
  257. free(net_boxes->score);
  258. free(net_boxes->box);
  259. free(net_boxes->landmark);
  260. free(net_boxes);
  261. }
  262. jpg_chunking_t jchunk = {req, 0};
  263. s = fmt2jpg_cb(out_buf, out_len, out_width, out_height, PIXFORMAT_RGB888, 90, jpg_encode_stream, &jchunk);
  264. dl_matrix3du_free(image_matrix);
  265. if(!s){
  266. Serial.println("JPEG compression failed");
  267. return ESP_FAIL;
  268. }
  269. int64_t fr_end = esp_timer_get_time();
  270. Serial.printf("FACE: %uB %ums %s%d\n", (uint32_t)(jchunk.len), (uint32_t)((fr_end - fr_start)/1000), detected?"DETECTED ":"", face_id);
  271. return res;
  272. }
  273. static esp_err_t stream_handler(httpd_req_t *req){
  274. camera_fb_t * fb = NULL;
  275. esp_err_t res = ESP_OK;
  276. size_t _jpg_buf_len = 0;
  277. uint8_t * _jpg_buf = NULL;
  278. char * part_buf[64];
  279. dl_matrix3du_t *image_matrix = NULL;
  280. bool detected = false;
  281. int face_id = 0;
  282. int64_t fr_start = 0;
  283. int64_t fr_ready = 0;
  284. int64_t fr_face = 0;
  285. int64_t fr_recognize = 0;
  286. int64_t fr_encode = 0;
  287. static int64_t last_frame = 0;
  288. if(!last_frame) {
  289. last_frame = esp_timer_get_time();
  290. }
  291. res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  292. if(res != ESP_OK){
  293. return res;
  294. }
  295. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  296. while(true){
  297. detected = false;
  298. face_id = 0;
  299. fb = esp_camera_fb_get();
  300. if (!fb) {
  301. Serial.println("Camera capture failed");
  302. res = ESP_FAIL;
  303. } else {
  304. fr_start = esp_timer_get_time();
  305. fr_ready = fr_start;
  306. fr_face = fr_start;
  307. fr_encode = fr_start;
  308. fr_recognize = fr_start;
  309. if(!detection_enabled || fb->width > 400){
  310. if(fb->format != PIXFORMAT_JPEG){
  311. bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
  312. esp_camera_fb_return(fb);
  313. fb = NULL;
  314. if(!jpeg_converted){
  315. Serial.println("JPEG compression failed");
  316. res = ESP_FAIL;
  317. }
  318. } else {
  319. _jpg_buf_len = fb->len;
  320. _jpg_buf = fb->buf;
  321. }
  322. } else {
  323. image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  324. if (!image_matrix) {
  325. Serial.println("dl_matrix3du_alloc failed");
  326. res = ESP_FAIL;
  327. } else {
  328. if(!fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item)){
  329. Serial.println("fmt2rgb888 failed");
  330. res = ESP_FAIL;
  331. } else {
  332. fr_ready = esp_timer_get_time();
  333. box_array_t *net_boxes = NULL;
  334. if(detection_enabled){
  335. net_boxes = face_detect(image_matrix, &mtmn_config);
  336. }
  337. fr_face = esp_timer_get_time();
  338. fr_recognize = fr_face;
  339. if (net_boxes || fb->format != PIXFORMAT_JPEG){
  340. if(net_boxes){
  341. detected = true;
  342. if(recognition_enabled){
  343. face_id = run_face_recognition(image_matrix, net_boxes);
  344. }
  345. fr_recognize = esp_timer_get_time();
  346. draw_face_boxes(image_matrix, net_boxes, face_id);
  347. free(net_boxes->score);
  348. free(net_boxes->box);
  349. free(net_boxes->landmark);
  350. free(net_boxes);
  351. }
  352. if(!fmt2jpg(image_matrix->item, fb->width*fb->height*3, fb->width, fb->height, PIXFORMAT_RGB888, 90, &_jpg_buf, &_jpg_buf_len)){
  353. Serial.println("fmt2jpg failed");
  354. res = ESP_FAIL;
  355. }
  356. esp_camera_fb_return(fb);
  357. fb = NULL;
  358. } else {
  359. _jpg_buf = fb->buf;
  360. _jpg_buf_len = fb->len;
  361. }
  362. fr_encode = esp_timer_get_time();
  363. }
  364. dl_matrix3du_free(image_matrix);
  365. }
  366. }
  367. }
  368. if(res == ESP_OK){
  369. size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
  370. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  371. }
  372. if(res == ESP_OK){
  373. res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
  374. }
  375. if(res == ESP_OK){
  376. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  377. }
  378. if(fb){
  379. esp_camera_fb_return(fb);
  380. fb = NULL;
  381. _jpg_buf = NULL;
  382. } else if(_jpg_buf){
  383. free(_jpg_buf);
  384. _jpg_buf = NULL;
  385. }
  386. if(res != ESP_OK){
  387. break;
  388. }
  389. int64_t fr_end = esp_timer_get_time();
  390. int64_t ready_time = (fr_ready - fr_start)/1000;
  391. int64_t face_time = (fr_face - fr_ready)/1000;
  392. int64_t recognize_time = (fr_recognize - fr_face)/1000;
  393. int64_t encode_time = (fr_encode - fr_recognize)/1000;
  394. int64_t process_time = (fr_encode - fr_start)/1000;
  395. int64_t frame_time = fr_end - last_frame;
  396. last_frame = fr_end;
  397. frame_time /= 1000;
  398. uint32_t avg_frame_time = ra_filter_run(&ra_filter, frame_time);
  399. Serial.printf("MJPG: %uB %ums (%.1ffps), AVG: %ums (%.1ffps), %u+%u+%u+%u=%u %s%d\n",
  400. (uint32_t)(_jpg_buf_len),
  401. (uint32_t)frame_time, 1000.0 / (uint32_t)frame_time,
  402. avg_frame_time, 1000.0 / avg_frame_time,
  403. (uint32_t)ready_time, (uint32_t)face_time, (uint32_t)recognize_time, (uint32_t)encode_time, (uint32_t)process_time,
  404. (detected)?"DETECTED ":"", face_id
  405. );
  406. }
  407. last_frame = 0;
  408. return res;
  409. }
  410. static esp_err_t cmd_handler(httpd_req_t *req){
  411. char* buf;
  412. size_t buf_len;
  413. char variable[32] = {0,};
  414. char value[32] = {0,};
  415. buf_len = httpd_req_get_url_query_len(req) + 1;
  416. if (buf_len > 1) {
  417. buf = (char*)malloc(buf_len);
  418. if(!buf){
  419. httpd_resp_send_500(req);
  420. return ESP_FAIL;
  421. }
  422. if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
  423. if (httpd_query_key_value(buf, "var", variable, sizeof(variable)) == ESP_OK &&
  424. httpd_query_key_value(buf, "val", value, sizeof(value)) == ESP_OK) {
  425. } else {
  426. free(buf);
  427. httpd_resp_send_404(req);
  428. return ESP_FAIL;
  429. }
  430. } else {
  431. free(buf);
  432. httpd_resp_send_404(req);
  433. return ESP_FAIL;
  434. }
  435. free(buf);
  436. } else {
  437. httpd_resp_send_404(req);
  438. return ESP_FAIL;
  439. }
  440. int val = atoi(value);
  441. sensor_t * s = esp_camera_sensor_get();
  442. int res = 0;
  443. if(!strcmp(variable, "framesize")) {
  444. if(s->pixformat == PIXFORMAT_JPEG) res = s->set_framesize(s, (framesize_t)val);
  445. }
  446. else if(!strcmp(variable, "quality")) res = s->set_quality(s, val);
  447. else if(!strcmp(variable, "contrast")) res = s->set_contrast(s, val);
  448. else if(!strcmp(variable, "brightness")) res = s->set_brightness(s, val);
  449. else if(!strcmp(variable, "saturation")) res = s->set_saturation(s, val);
  450. else if(!strcmp(variable, "gainceiling")) res = s->set_gainceiling(s, (gainceiling_t)val);
  451. else if(!strcmp(variable, "colorbar")) res = s->set_colorbar(s, val);
  452. else if(!strcmp(variable, "awb")) res = s->set_whitebal(s, val);
  453. else if(!strcmp(variable, "agc")) res = s->set_gain_ctrl(s, val);
  454. else if(!strcmp(variable, "aec")) res = s->set_exposure_ctrl(s, val);
  455. else if(!strcmp(variable, "hmirror")) res = s->set_hmirror(s, val);
  456. else if(!strcmp(variable, "vflip")) res = s->set_vflip(s, val);
  457. else if(!strcmp(variable, "awb_gain")) res = s->set_awb_gain(s, val);
  458. else if(!strcmp(variable, "agc_gain")) res = s->set_agc_gain(s, val);
  459. else if(!strcmp(variable, "aec_value")) res = s->set_aec_value(s, val);
  460. else if(!strcmp(variable, "aec2")) res = s->set_aec2(s, val);
  461. else if(!strcmp(variable, "dcw")) res = s->set_dcw(s, val);
  462. else if(!strcmp(variable, "bpc")) res = s->set_bpc(s, val);
  463. else if(!strcmp(variable, "wpc")) res = s->set_wpc(s, val);
  464. else if(!strcmp(variable, "raw_gma")) res = s->set_raw_gma(s, val);
  465. else if(!strcmp(variable, "lenc")) res = s->set_lenc(s, val);
  466. else if(!strcmp(variable, "special_effect")) res = s->set_special_effect(s, val);
  467. else if(!strcmp(variable, "wb_mode")) res = s->set_wb_mode(s, val);
  468. else if(!strcmp(variable, "ae_level")) res = s->set_ae_level(s, val);
  469. else if(!strcmp(variable, "face_detect")) {
  470. detection_enabled = val;
  471. if(!detection_enabled) {
  472. recognition_enabled = 0;
  473. }
  474. }
  475. else if(!strcmp(variable, "face_enroll")) is_enrolling = val;
  476. else if(!strcmp(variable, "face_recognize")) {
  477. recognition_enabled = val;
  478. if(recognition_enabled){
  479. detection_enabled = val;
  480. }
  481. }
  482. else {
  483. res = -1;
  484. }
  485. if(res){
  486. return httpd_resp_send_500(req);
  487. }
  488. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  489. return httpd_resp_send(req, NULL, 0);
  490. }
  491. static esp_err_t status_handler(httpd_req_t *req){
  492. static char json_response[1024];
  493. sensor_t * s = esp_camera_sensor_get();
  494. char * p = json_response;
  495. *p++ = '{';
  496. p+=sprintf(p, "\"framesize\":%u,", s->status.framesize);
  497. p+=sprintf(p, "\"quality\":%u,", s->status.quality);
  498. p+=sprintf(p, "\"brightness\":%d,", s->status.brightness);
  499. p+=sprintf(p, "\"contrast\":%d,", s->status.contrast);
  500. p+=sprintf(p, "\"saturation\":%d,", s->status.saturation);
  501. p+=sprintf(p, "\"sharpness\":%d,", s->status.sharpness);
  502. p+=sprintf(p, "\"special_effect\":%u,", s->status.special_effect);
  503. p+=sprintf(p, "\"wb_mode\":%u,", s->status.wb_mode);
  504. p+=sprintf(p, "\"awb\":%u,", s->status.awb);
  505. p+=sprintf(p, "\"awb_gain\":%u,", s->status.awb_gain);
  506. p+=sprintf(p, "\"aec\":%u,", s->status.aec);
  507. p+=sprintf(p, "\"aec2\":%u,", s->status.aec2);
  508. p+=sprintf(p, "\"ae_level\":%d,", s->status.ae_level);
  509. p+=sprintf(p, "\"aec_value\":%u,", s->status.aec_value);
  510. p+=sprintf(p, "\"agc\":%u,", s->status.agc);
  511. p+=sprintf(p, "\"agc_gain\":%u,", s->status.agc_gain);
  512. p+=sprintf(p, "\"gainceiling\":%u,", s->status.gainceiling);
  513. p+=sprintf(p, "\"bpc\":%u,", s->status.bpc);
  514. p+=sprintf(p, "\"wpc\":%u,", s->status.wpc);
  515. p+=sprintf(p, "\"raw_gma\":%u,", s->status.raw_gma);
  516. p+=sprintf(p, "\"lenc\":%u,", s->status.lenc);
  517. p+=sprintf(p, "\"vflip\":%u,", s->status.vflip);
  518. p+=sprintf(p, "\"hmirror\":%u,", s->status.hmirror);
  519. p+=sprintf(p, "\"dcw\":%u,", s->status.dcw);
  520. p+=sprintf(p, "\"colorbar\":%u,", s->status.colorbar);
  521. p+=sprintf(p, "\"face_detect\":%u,", detection_enabled);
  522. p+=sprintf(p, "\"face_enroll\":%u,", is_enrolling);
  523. p+=sprintf(p, "\"face_recognize\":%u", recognition_enabled);
  524. *p++ = '}';
  525. *p++ = 0;
  526. httpd_resp_set_type(req, "application/json");
  527. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  528. return httpd_resp_send(req, json_response, strlen(json_response));
  529. }
  530. static esp_err_t index_handler(httpd_req_t *req){
  531. httpd_resp_set_type(req, "text/html");
  532. httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
  533. sensor_t * s = esp_camera_sensor_get();
  534. s->set_vflip(s, 1);
  535. if (s->id.PID == OV3660_PID) {
  536. return httpd_resp_send(req, (const char *)index_ov3660_html_gz, index_ov3660_html_gz_len);
  537. }
  538. return httpd_resp_send(req, (const char *)index_ov2640_html_gz, index_ov2640_html_gz_len);
  539. }
  540. void startCameraServer(){
  541. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  542. httpd_uri_t index_uri = {
  543. .uri = "/",
  544. .method = HTTP_GET,
  545. .handler = index_handler,
  546. .user_ctx = NULL
  547. };
  548. httpd_uri_t status_uri = {
  549. .uri = "/status",
  550. .method = HTTP_GET,
  551. .handler = status_handler,
  552. .user_ctx = NULL
  553. };
  554. httpd_uri_t cmd_uri = {
  555. .uri = "/control",
  556. .method = HTTP_GET,
  557. .handler = cmd_handler,
  558. .user_ctx = NULL
  559. };
  560. httpd_uri_t capture_uri = {
  561. .uri = "/capture",
  562. .method = HTTP_GET,
  563. .handler = capture_handler,
  564. .user_ctx = NULL
  565. };
  566. httpd_uri_t stream_uri = {
  567. .uri = "/stream",
  568. .method = HTTP_GET,
  569. .handler = stream_handler,
  570. .user_ctx = NULL
  571. };
  572. ra_filter_init(&ra_filter, 20);
  573. mtmn_config.type = FAST;
  574. mtmn_config.min_face = 80;
  575. mtmn_config.pyramid = 0.707;
  576. mtmn_config.pyramid_times = 4;
  577. mtmn_config.p_threshold.score = 0.6;
  578. mtmn_config.p_threshold.nms = 0.7;
  579. mtmn_config.p_threshold.candidate_number = 20;
  580. mtmn_config.r_threshold.score = 0.7;
  581. mtmn_config.r_threshold.nms = 0.7;
  582. mtmn_config.r_threshold.candidate_number = 10;
  583. mtmn_config.o_threshold.score = 0.7;
  584. mtmn_config.o_threshold.nms = 0.7;
  585. mtmn_config.o_threshold.candidate_number = 1;
  586. face_id_init(&id_list, FACE_ID_SAVE_NUMBER, ENROLL_CONFIRM_TIMES);
  587. Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  588. if (httpd_start(&camera_httpd, &config) == ESP_OK) {
  589. httpd_register_uri_handler(camera_httpd, &index_uri);
  590. httpd_register_uri_handler(camera_httpd, &cmd_uri);
  591. httpd_register_uri_handler(camera_httpd, &status_uri);
  592. httpd_register_uri_handler(camera_httpd, &capture_uri);
  593. }
  594. config.server_port += 1;
  595. config.ctrl_port += 1;
  596. Serial.printf("Starting stream server on port: '%d'\n", config.server_port);
  597. if (httpd_start(&stream_httpd, &config) == ESP_OK) {
  598. httpd_register_uri_handler(stream_httpd, &stream_uri);
  599. }
  600. }