Skip to content

Commit 7fae903

Browse files
authored
Logging and Debug improvements (#111)
* Logging format improvements plus debounce serial at startup * Be clearer about whether Stream or Image capture generated a particular error, also for face recognition errors * User defined face detection text
1 parent 5f6bb1d commit 7fae903

File tree

6 files changed

+118
-85
lines changed

6 files changed

+118
-85
lines changed

Docs/screenshot.png

-353 KB
Binary file not shown.

Docs/twocatsactually.png

555 KB
Loading

app_httpd.cpp

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ extern bool debugData;
6262
extern int sketchSize;
6363
extern int sketchSpace;
6464
extern String sketchMD5;
65+
extern char knownFaceText[];
66+
extern char unknownFaceText[];
6567

6668

6769
#include "fb_gfx.h"
@@ -212,96 +214,96 @@ static int run_face_recognition(dl_matrix3du_t *image_matrix, box_array_t *net_b
212214

213215
aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
214216
if(!aligned_face){
215-
Serial.println("Could not allocate face recognition buffer");
217+
Serial.println("FACE: could not allocate face recognition buffer");
216218
return matched_id;
217219
}
218220
if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK){
219221
if (is_enrolling == 1){
220-
int8_t this_face = id_list.tail;
222+
int8_t this_face = id_list.tail + 1;
221223
int8_t left_sample_face = enroll_face(&id_list, aligned_face);
222224

223225
if(left_sample_face == (ENROLL_CONFIRM_TIMES - 1)){
224-
Serial.printf("Enrolling Face ID: %d\n", this_face);
226+
Serial.printf("FACE: enrolling new face ID: %d\r\n", this_face);
225227
}
226-
Serial.printf("Enrolling Face ID: %d sample %d\n", this_face, ENROLL_CONFIRM_TIMES - left_sample_face);
228+
Serial.printf("FACE: enroll ID: %d sample %d\r\n", this_face, ENROLL_CONFIRM_TIMES - left_sample_face);
227229
rgb_printf(image_matrix, FACE_COLOR_CYAN, "ID[%u] Sample[%u]", this_face, ENROLL_CONFIRM_TIMES - left_sample_face);
228230
if (left_sample_face == 0){
229231
is_enrolling = 0;
230-
Serial.printf("Enrolled Face ID: %d\n", this_face);
232+
Serial.printf("FACE: enrolled face ID: %d\r\n", this_face);
231233
}
232234
} else {
233-
matched_id = recognize_face(&id_list, aligned_face);
234-
if (matched_id >= 0) {
235-
Serial.printf("Match Face ID: %u\n", matched_id);
236-
rgb_printf(image_matrix, FACE_COLOR_GREEN, "Hello Subject %u", matched_id);
235+
matched_id = recognize_face(&id_list, aligned_face) + 1;
236+
if (matched_id > 0) {
237+
Serial.printf("FACE: match ID: %u: ", matched_id);
238+
rgb_printf(image_matrix, FACE_COLOR_GREEN, "%s%u", knownFaceText, matched_id);
237239
} else {
238-
Serial.println("No Match Found");
239-
rgb_print(image_matrix, FACE_COLOR_RED, "Intruder Alert!");
240240
matched_id = -1;
241+
Serial.print("FACE: no match found: ");
242+
rgb_printf(image_matrix, FACE_COLOR_RED, "%s", unknownFaceText);
241243
}
242244
}
243245
} else {
244-
Serial.println("Face Not Aligned");
245-
//rgb_print(image_matrix, FACE_COLOR_YELLOW, "Human Detected");
246+
Serial.print("FACE: not aligned: ");
247+
rgb_print(image_matrix, FACE_COLOR_YELLOW, "???");
246248
}
247249

248250
dl_matrix3du_free(aligned_face);
249251
return matched_id;
250252
}
251253

252254
void serialDump() {
253-
Serial.println("\nPreferences file: ");
255+
Serial.println("\r\nPreferences file: ");
254256
dumpPrefs(SPIFFS);
255257
if (critERR.length() > 0) {
256-
Serial.printf("\n\nA critical error has occurred when initialising Camera Hardware, see startup megssages\n");
258+
Serial.printf("\r\n\r\nA critical error has occurred when initialising Camera Hardware, see startup megssages\r\n");
257259
}
258260
// Module
259-
Serial.printf("Name: %s\n", myName);
260-
Serial.printf("Firmware: %s (base: %s)\n", myVer, baseVersion);
261+
Serial.printf("Name: %s\r\n", myName);
262+
Serial.printf("Firmware: %s (base: %s)\r\n", myVer, baseVersion);
261263
float sketchPct = 100 * sketchSize / sketchSpace;
262-
Serial.printf("Sketch Size: %i (total: %i, %.1f%% used)\n", sketchSize, sketchSpace, sketchPct);
263-
Serial.printf("MD5: %s\n", sketchMD5.c_str());
264-
Serial.printf("ESP sdk: %s\n", ESP.getSdkVersion());
264+
Serial.printf("Sketch Size: %i (total: %i, %.1f%% used)\r\n", sketchSize, sketchSpace, sketchPct);
265+
Serial.printf("MD5: %s\r\n", sketchMD5.c_str());
266+
Serial.printf("ESP sdk: %s\r\n", ESP.getSdkVersion());
265267
// Network
266268
if (accesspoint) {
267269
if (captivePortal) {
268-
Serial.printf("WiFi Mode: AccessPoint with captive portal\n");
270+
Serial.printf("WiFi Mode: AccessPoint with captive portal\r\n");
269271
} else {
270-
Serial.printf("WiFi Mode: AccessPoint\n");
272+
Serial.printf("WiFi Mode: AccessPoint\r\n");
271273
}
272-
Serial.printf("WiFi SSID: %s\n", apName);
274+
Serial.printf("WiFi SSID: %s\r\n", apName);
273275
} else {
274-
Serial.printf("WiFi Mode: Client\n");
276+
Serial.printf("WiFi Mode: Client\r\n");
275277
String ssidName = WiFi.SSID();
276-
Serial.printf("WiFi Ssid: %s\n", ssidName.c_str());
277-
Serial.printf("WiFi Rssi: %i\n", WiFi.RSSI());
278+
Serial.printf("WiFi Ssid: %s\r\n", ssidName.c_str());
279+
Serial.printf("WiFi Rssi: %i\r\n", WiFi.RSSI());
278280
String bssid = WiFi.BSSIDstr();
279-
Serial.printf("WiFi BSSID: %s\n", bssid.c_str());
281+
Serial.printf("WiFi BSSID: %s\r\n", bssid.c_str());
280282
}
281-
Serial.printf("WiFi IP address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
283+
Serial.printf("WiFi IP address: %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
282284
if (!accesspoint) {
283-
Serial.printf("WiFi Netmask: %d.%d.%d.%d\n", net[0], net[1], net[2], net[3]);
284-
Serial.printf("WiFi Gateway: %d.%d.%d.%d\n", gw[0], gw[1], gw[2], gw[3]);
285+
Serial.printf("WiFi Netmask: %d.%d.%d.%d\r\n", net[0], net[1], net[2], net[3]);
286+
Serial.printf("WiFi Gateway: %d.%d.%d.%d\r\n", gw[0], gw[1], gw[2], gw[3]);
285287
}
286-
Serial.printf("WiFi Http port: %i, Stream port: %i\n", httpPort, streamPort);
288+
Serial.printf("WiFi Http port: %i, Stream port: %i\r\n", httpPort, streamPort);
287289
byte mac[6];
288290
WiFi.macAddress(mac);
289-
Serial.printf("WiFi MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
291+
Serial.printf("WiFi MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
290292
// System
291293
int64_t sec = esp_timer_get_time() / 1000000;
292294
int64_t upDays = int64_t(floor(sec/86400));
293295
int upHours = int64_t(floor(sec/3600)) % 24;
294296
int upMin = int64_t(floor(sec/60)) % 60;
295297
int upSec = sec % 60;
296-
Serial.printf("System up: %" PRId64 ":%02i:%02i:%02i (d:h:m:s)\n", upDays, upHours, upMin, upSec);
297-
Serial.printf("Active streams: %i, Previous streams: %lu, Images captured: %lu\n", streamCount, streamsServed, imagesServed);
298-
Serial.printf("Freq: %i MHz\n", ESP.getCpuFreqMHz());
299-
Serial.printf("Heap: %i, free: %i, min free: %i, max block: %i\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap());
300-
Serial.printf("Psram: %i, free: %i, min free: %i, max block: %i\n", ESP.getPsramSize(), ESP.getFreePsram(), ESP.getMinFreePsram(), ESP.getMaxAllocPsram());
298+
Serial.printf("System up: %" PRId64 ":%02i:%02i:%02i (d:h:m:s)\r\n", upDays, upHours, upMin, upSec);
299+
Serial.printf("Active streams: %i, Previous streams: %lu, Images captured: %lu\r\n", streamCount, streamsServed, imagesServed);
300+
Serial.printf("Freq: %i MHz\r\n", ESP.getCpuFreqMHz());
301+
Serial.printf("Heap: %i, free: %i, min free: %i, max block: %i\r\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap());
302+
Serial.printf("Psram: %i, free: %i, min free: %i, max block: %i\r\n", ESP.getPsramSize(), ESP.getFreePsram(), ESP.getMinFreePsram(), ESP.getMaxAllocPsram());
301303
if (filesystem) {
302-
Serial.printf("Spiffs: %i, used: %i\n", SPIFFS.totalBytes(), SPIFFS.usedBytes());
304+
Serial.printf("Spiffs: %i, used: %i\r\n", SPIFFS.totalBytes(), SPIFFS.usedBytes());
303305
}
304-
Serial.printf("Enrolled faces: %i (max %i)\n", id_list.count, id_list.size);
306+
Serial.printf("Enrolled faces: %i (max %i)\r\n", id_list.count, id_list.size);
305307
Serial.println();
306308
return;
307309
}
@@ -330,7 +332,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
330332

331333
fb = esp_camera_fb_get();
332334
if (!fb) {
333-
Serial.println("Camera capture failed");
335+
Serial.println("CAPTURE: failed to acquire frame");
334336
httpd_resp_send_500(req);
335337
if (autoLamp && (lampVal != -1)) setLamp(0);
336338
return ESP_FAIL;
@@ -359,7 +361,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
359361
esp_camera_fb_return(fb);
360362
int64_t fr_end = esp_timer_get_time();
361363
if (debugData) {
362-
Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
364+
Serial.printf("JPG: %uB %ums\r\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
363365
}
364366
imagesServed++;
365367
if (autoLamp && (lampVal != -1)) setLamp(0);
@@ -369,7 +371,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
369371
dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
370372
if (!image_matrix) {
371373
esp_camera_fb_return(fb);
372-
Serial.println("dl_matrix3du_alloc failed");
374+
Serial.println("CAPTURE: dl_matrix3du_alloc failed");
373375
httpd_resp_send_500(req);
374376
if (autoLamp && (lampVal != -1)) setLamp(0);
375377
return ESP_FAIL;
@@ -384,7 +386,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
384386
esp_camera_fb_return(fb);
385387
if(!s){
386388
dl_matrix3du_free(image_matrix);
387-
Serial.println("to rgb888 failed");
389+
Serial.println("CAPTURE: frame convert to rgb888 failed");
388390
httpd_resp_send_500(req);
389391
if (autoLamp && (lampVal != -1)) setLamp(0);
390392
return ESP_FAIL;
@@ -404,14 +406,14 @@ static esp_err_t capture_handler(httpd_req_t *req){
404406
s = fmt2jpg_cb(out_buf, out_len, out_width, out_height, PIXFORMAT_RGB888, 90, jpg_encode_stream, &jchunk);
405407
dl_matrix3du_free(image_matrix);
406408
if(!s){
407-
Serial.println("JPEG compression failed");
409+
Serial.println("CAPTURE: JPEG compression failed");
408410
if (autoLamp && (lampVal != -1)) setLamp(0);
409411
return ESP_FAIL;
410412
}
411413

412414
int64_t fr_end = esp_timer_get_time();
413415
if (debugData) {
414-
Serial.printf("FACE: %uB %ums %s%d\n", (uint32_t)(jchunk.len), (uint32_t)((fr_end - fr_start)/1000), detected?"DETECTED ":"", face_id);
416+
Serial.printf("JPG: %uB %ums %s%d\r\n", (uint32_t)(jchunk.len), (uint32_t)((fr_end - fr_start)/1000), detected?"DETECTED ":"", face_id);
415417
}
416418

417419
imagesServed++;
@@ -450,6 +452,7 @@ static esp_err_t stream_handler(httpd_req_t *req){
450452
if(res != ESP_OK){
451453
streamCount = 0;
452454
if (autoLamp && (lampVal != -1)) setLamp(0);
455+
Serial.println("STREAM: failed to set HTTP response type");
453456
return res;
454457
}
455458

@@ -460,7 +463,7 @@ static esp_err_t stream_handler(httpd_req_t *req){
460463
face_id = 0;
461464
fb = esp_camera_fb_get();
462465
if (!fb) {
463-
Serial.println("Camera capture failed");
466+
Serial.println("STREAM: failed to acquire frame");
464467
res = ESP_FAIL;
465468
} else {
466469
fr_start = esp_timer_get_time();
@@ -475,7 +478,7 @@ static esp_err_t stream_handler(httpd_req_t *req){
475478
esp_camera_fb_return(fb);
476479
fb = NULL;
477480
if(!jpeg_converted){
478-
Serial.println("JPEG compression failed");
481+
Serial.println("STREAM: JPEG compression failed");
479482
res = ESP_FAIL;
480483
}
481484
} else {
@@ -487,11 +490,11 @@ static esp_err_t stream_handler(httpd_req_t *req){
487490
image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
488491

489492
if (!image_matrix) {
490-
Serial.println("dl_matrix3du_alloc failed");
493+
Serial.println("STREAM: dl_matrix3du_alloc failed");
491494
res = ESP_FAIL;
492495
} else {
493496
if(!fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item)){
494-
Serial.println("fmt2rgb888 failed");
497+
Serial.println("STREAM: frame convert to rgb888 failed");
495498
res = ESP_FAIL;
496499
} else {
497500
fr_ready = esp_timer_get_time();
@@ -511,7 +514,7 @@ static esp_err_t stream_handler(httpd_req_t *req){
511514
draw_face_boxes(image_matrix, net_boxes, face_id);
512515
}
513516
if(!fmt2jpg(image_matrix->item, fb->width*fb->height*3, fb->width, fb->height, PIXFORMAT_RGB888, 90, &_jpg_buf, &_jpg_buf_len)){
514-
Serial.println("fmt2jpg failed");
517+
Serial.println("STREAM: fmt2jpg failed");
515518
res = ESP_FAIL;
516519
}
517520
esp_camera_fb_return(fb);
@@ -545,6 +548,8 @@ static esp_err_t stream_handler(httpd_req_t *req){
545548
_jpg_buf = NULL;
546549
}
547550
if(res != ESP_OK){
551+
// This is the only exit point from the stream loop.
552+
// We end the stream here only if a Hard failure has been encountered or the connection has been interrupted.
548553
break;
549554
}
550555

@@ -559,13 +564,19 @@ static esp_err_t stream_handler(httpd_req_t *req){
559564
frame_time /= 1000;
560565
uint32_t avg_frame_time = ra_filter_run(&ra_filter, frame_time);
561566
if (debugData) {
562-
Serial.printf("MJPG: %uB %ums (%.1ffps), AVG: %ums (%.1ffps), %u+%u+%u+%u=%u %s%d\n",
563-
(uint32_t)(_jpg_buf_len),
564-
(uint32_t)frame_time, 1000.0 / (uint32_t)frame_time,
565-
avg_frame_time, 1000.0 / avg_frame_time,
566-
(uint32_t)ready_time, (uint32_t)face_time, (uint32_t)recognize_time, (uint32_t)encode_time, (uint32_t)process_time,
567-
(detected)?"DETECTED ":"", face_id
568-
);
567+
if (detection_enabled) {
568+
Serial.printf("MJPG: %uB %ums (%.1ffps), AVG: %ums (%.1ffps), %u+%u+%u+%u=%u %s%d\r\n",
569+
(uint32_t)(_jpg_buf_len),
570+
(uint32_t)frame_time, 1000.0 / (uint32_t)frame_time,
571+
avg_frame_time, 1000.0 / avg_frame_time,
572+
(uint32_t)ready_time, (uint32_t)face_time, (uint32_t)recognize_time, (uint32_t)encode_time, (uint32_t)process_time,
573+
(detected)?"DETECTED ":"", face_id);
574+
} else {
575+
Serial.printf("MJPG: %uB %ums (%.1ffps), AVG: %ums (%.1ffps)\r\n",
576+
(uint32_t)(_jpg_buf_len),
577+
(uint32_t)frame_time, 1000.0 / (uint32_t)frame_time,
578+
avg_frame_time, 1000.0 / avg_frame_time);
579+
}
569580
}
570581
}
571582

@@ -793,7 +804,7 @@ static esp_err_t logo_svg_handler(httpd_req_t *req){
793804

794805
static esp_err_t dump_handler(httpd_req_t *req){
795806
flashLED(75);
796-
Serial.println("\nDump Requested via Web");
807+
Serial.println("\r\nDump Requested via Web");
797808
serialDump();
798809
static char dumpOut[2000] = "";
799810
char * d = dumpOut;
@@ -1100,7 +1111,7 @@ void startCameraServer(int hPort, int sPort){
11001111
// Request Handlers; config.max_uri_handlers (above) must be >= the number of handlers
11011112
config.server_port = hPort;
11021113
config.ctrl_port = hPort;
1103-
Serial.printf("Starting web server on port: '%d'\n", config.server_port);
1114+
Serial.printf("Starting web server on port: '%d'\r\n", config.server_port);
11041115
if (httpd_start(&camera_httpd, &config) == ESP_OK) {
11051116
if (critERR.length() > 0) {
11061117
httpd_register_uri_handler(camera_httpd, &error_uri);
@@ -1120,7 +1131,7 @@ void startCameraServer(int hPort, int sPort){
11201131

11211132
config.server_port = sPort;
11221133
config.ctrl_port = sPort;
1123-
Serial.printf("Starting stream server on port: '%d'\n", config.server_port);
1134+
Serial.printf("Starting stream server on port: '%d'\r\n", config.server_port);
11241135
if (httpd_start(&stream_httpd, &config) == ESP_OK) {
11251136
if (critERR.length() > 0) {
11261137
httpd_register_uri_handler(camera_httpd, &error_uri);

0 commit comments

Comments
 (0)