-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaceRecognition.ino
575 lines (502 loc) · 16.4 KB
/
faceRecognition.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include "Arduino.h"
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include "driver/rtc_io.h"
#include <WiFi.h>
#include <esp_now.h>
#include "FS.h" // SD Card ESP32
#include "SD_MMC.h" // SD Card ESP32
#include "esp_camera.h"
#include <esp_wifi.h>
esp_now_peer_info_t slave;
#define fileDatainMessage 240.0
#define CHANNEL 1
#define PRINTSCANRESULTS 0
#define DELETEBEFOREPAIR 0
// for esp now connect
unsigned long lastConnectNowAttempt;
unsigned long nextConnectNowGap = 1000;
bool isPaired = 0;
bool isConnected = 0;
// for photo name
byte takeNextPhotoFlag = 0;
byte recognizeFaceFlag = 0;
int pictureNumber = 1;
// for photo transmit
int currentTransmitCurrentPosition = 0;
int currentTransmitTotalPackages = 0;
byte sendNextPackageFlag = 0;
byte byteTransmitFlag = 0;
uint8_t lastRecognizeID = 200;
String fileName = "";
uint64_t ss;
uint64_t lastCaptureTime = 0;
uint64_t recognizeTime = 0;
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
// or another board which has PSRAM enabled
//
// Select camera model
//#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
const char* ssid = "admin";
const char* password = "1234567890";
const char* ap_ssid = "FaceRecognitionLock";
const char* ap_password = "123456789";
void startCameraServer();
int detect();
int recognizeFace();
uint8_t doorReturn();
void taskDelay(uint64_t timeDelay);
bool is_streaming();
int door_return = 0;
void InitESPNow() {
WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
// Retry InitESPNow, add a counte and then restart?
// InitESPNow();
// or Simply Restart
//ESP.restart();
}
}
// Check if the slave is already paired with the master.
// If not, pair the slave with master
bool manageSlave() {
if (slave.channel == CHANNEL) {
if (DELETEBEFOREPAIR) {
deletePeer();
}
Serial.print("Slave Status: ");
// check if the peer exists
bool exists = esp_now_is_peer_exist(slave.peer_addr);
if ( exists) {
// Slave already paired.
Serial.println("Already Paired");
return true;
} else {
// Slave not paired, attempt pair
esp_err_t addStatus = esp_now_add_peer(&slave);
if (addStatus == ESP_OK) {
// Pair success
Serial.println("Pair success");
return true;
} else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) {
// How did we get so far!!
Serial.println("ESPNOW Not Init");
return false;
} else if (addStatus == ESP_ERR_ESPNOW_ARG) {
Serial.println("Invalid Argument");
return false;
} else if (addStatus == ESP_ERR_ESPNOW_FULL) {
Serial.println("Peer list full");
return false;
} else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println("Out of memory");
return false;
} else if (addStatus == ESP_ERR_ESPNOW_EXIST) {
Serial.println("Peer Exists");
return true;
} else {
Serial.println("Not sure what happened");
return false;
}
}
} else {
// No slave found to process
Serial.println("No Slave found to process");
return false;
}
}
void deletePeer() {
esp_err_t delStatus = esp_now_del_peer(slave.peer_addr);
Serial.print("Slave Delete Status: ");
if (delStatus == ESP_OK) {
// Delete success
Serial.println("Success");
} else if (delStatus == ESP_ERR_ESPNOW_NOT_INIT) {
// How did we get so far!!
Serial.println("ESPNOW Not Init");
} else if (delStatus == ESP_ERR_ESPNOW_ARG) {
Serial.println("Invalid Argument");
} else if (delStatus == ESP_ERR_ESPNOW_NOT_FOUND) {
Serial.println("Peer not found.");
} else {
Serial.println("Not sure what happened");
}
}
// Scan for slaves in AP mode
void ScanForSlave() {
int8_t scanResults = WiFi.scanNetworks();
// reset on each scan
bool slaveFound = 0;
memset(&slave, 0, sizeof(slave));
Serial.println("");
if (scanResults == 0) {
Serial.println("No WiFi devices in AP Mode found");
} else {
Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices ");
for (int i = 0; i < scanResults; ++i) {
// Print SSID and RSSI for each device found
String SSID = WiFi.SSID(i);
int32_t RSSI = WiFi.RSSI(i);
String BSSIDstr = WiFi.BSSIDstr(i);
if (PRINTSCANRESULTS) {
Serial.print(i + 1);
Serial.print(": ");
Serial.print(SSID);
Serial.print(" (");
Serial.print(RSSI);
Serial.print(")");
Serial.println("");
}
delay(10);
// Check if the current device starts with `Slave`
if (SSID.indexOf("Slave") == 0) {
// SSID of interest
Serial.println("Found a Slave.");
Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println("");
// Get BSSID => Mac Address of the Slave
int mac[6];
if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) {
for (int ii = 0; ii < 6; ++ii ) {
slave.peer_addr[ii] = (uint8_t) mac[ii];
}
}
slave.channel = CHANNEL; // pick a channel
slave.encrypt = 0; // no encryption
slaveFound = 1;
// we are planning to have only one slave in this example;
// Hence, break after we find one, to be a bit efficient
break;
}
}
}
if (slaveFound) {
Serial.println("Slave Found, processing..");
if (slave.channel == CHANNEL) { // check if slave channel is defined
// `slave` is defined
// Add slave as peer if it has not been added already
isPaired = manageSlave();
if (isPaired) {
Serial.println("Slave pair success!");
} else {
// slave pair failed
Serial.println("Slave pair failed!");
}
}
} else {
Serial.println("Slave Not Found, trying again.");
}
// clean up ram
WiFi.scanDelete();
}
/* ***************************************************************** */
/* START TRASMIT */
/* ***************************************************************** */
void startTransmit()
{
Serial.println("Starting transmit");
fs::FS &fs = SD_MMC;
File file = fs.open(fileName.c_str(), FILE_READ);
if (!file) {
Serial.println("Failed to open file in writing mode");
return;
}
Serial.println(file.size());
int fileSize = file.size();
file.close();
currentTransmitCurrentPosition = 0;
currentTransmitTotalPackages = ceil(fileSize / fileDatainMessage);
Serial.println(currentTransmitTotalPackages);
uint8_t message[] = {0x01, currentTransmitTotalPackages >> 8, (byte) currentTransmitTotalPackages};
sendData(message, sizeof(message));
}
/* ***************************************************************** */
/* SEND NEXT PACKAGE */
/* ***************************************************************** */
void sendNextPackage()
{
// claer the flag
sendNextPackageFlag = 0;
// if got to AFTER the last package
if (currentTransmitCurrentPosition == currentTransmitTotalPackages)
{
currentTransmitCurrentPosition = 0;
currentTransmitTotalPackages = 0;
Serial.println("Done submiting files");
//transmitCompletedFlag = 1;
//takeNextPhotoFlag = 1;
return;
} //end if
//first read the data.
fs::FS &fs = SD_MMC;
File file = fs.open(fileName.c_str(), FILE_READ);
if (!file) {
Serial.println("Failed to open file in writing mode");
return;
}
// set array size.
int fileDataSize = fileDatainMessage;
// if its the last package - we adjust the size !!!
if (currentTransmitCurrentPosition == currentTransmitTotalPackages - 1)
{
Serial.println("*************************");
Serial.println(file.size());
Serial.println(currentTransmitTotalPackages - 1);
Serial.println((currentTransmitTotalPackages - 1)*fileDatainMessage);
fileDataSize = file.size() - ((currentTransmitTotalPackages - 1) * fileDatainMessage);
}
//Serial.println("fileDataSize=" + String(fileDataSize));
// define message array
uint8_t messageArray[fileDataSize + 3];
messageArray[0] = 0x02;
file.seek(currentTransmitCurrentPosition * fileDatainMessage);
currentTransmitCurrentPosition++; // set to current (after seek!!!)
//Serial.println("PACKAGE - " + String(currentTransmitCurrentPosition));
messageArray[1] = currentTransmitCurrentPosition >> 8;
messageArray[2] = (byte) currentTransmitCurrentPosition;
for (int i = 0; i < fileDataSize; i++)
{
if (file.available())
{
messageArray[3 + i] = file.read();
} //end if available
else
{
Serial.println("END !!!");
break;
}
} //end for
sendData(messageArray, sizeof(messageArray));
file.close();
}
//Send sigle data
void byteTransmit(int dat){
uint8_t message[] = {0x03, dat>>8, (byte)dat};
Serial.printf("Sending control information: %d\n",dat);
sendData(message, sizeof(message));
byteTransmitFlag = 1;
}
// send data array for pictures
void sendData(uint8_t * dataArray, uint8_t dataArrayLength) {
const uint8_t *peer_addr = slave.peer_addr;
//Serial.print("Sending: "); Serial.println(data);
//Serial.print("length: "); Serial.println(dataArrayLength);
esp_err_t result = esp_now_send(peer_addr, dataArray, dataArrayLength);
//Serial.print("Send Status: ");
if (result == ESP_OK) {
//Serial.println("Success");
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
// How did we get so far!!
Serial.println("ESPNOW not Init.");
} else if (result == ESP_ERR_ESPNOW_ARG) {
Serial.println("Invalid Argument");
} else if (result == ESP_ERR_ESPNOW_INTERNAL) {
Serial.println("Internal Error");
} else if (result == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println("ESP_ERR_ESPNOW_NO_MEM");
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
Serial.println("Peer not found.");
} else {
Serial.println("Not sure what happened");
}
}
// callback when data is sent from Master to Slave
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
if(byteTransmitFlag){
byteTransmitFlag = 0;
}
else if (currentTransmitTotalPackages)
{
sendNextPackageFlag = 1;
// if nto suecess 0 resent the last one
if (status != ESP_NOW_SEND_SUCCESS)
currentTransmitCurrentPosition--;
} //end if
}
/* ***************************************************************** */
/* INIT SD */
/* ***************************************************************** */
void initSD()
{
Serial.println("Starting SD Card");
if (!SD_MMC.begin("/sdcard", true)) {
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD Card attached");
return;
}
}
void capture(){
takeNextPhotoFlag = 0;
// sensor_t *s = esp_camera_sensor_get();
// s->set_framesize(s, FRAMESIZE_HQVGA);
//s->set_framesize(s, FRAMESIZE_QVGA);
delay(100);
camera_fb_t *fb = NULL;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Path where new picture will be saved in SD Card
String path = "/picture" + String(pictureNumber) + ".jpg";
fs::FS &fs = SD_MMC;
Serial.printf("Picture file name: %s\n", path.c_str());
fs.remove(path.c_str());
File file = fs.open(path.c_str(), FILE_WRITE);
if (!file) {
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.printf("Saved file to path: %s\n", path.c_str());
}
file.close();
esp_camera_fb_return(fb);
fb = NULL;
fileName = path;
if (isPaired)
startTransmit();
pictureNumber++;
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
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();
//initial sensors are flipped vertically and colors are a bit saturated
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1);//flip it back
s->set_brightness(s, 1);//up the blightness just a bit
s->set_saturation(s, -2);//lower the saturation
}
//drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_CIF);
s->set_hmirror(s, 1);
//s->set_brightness(s,1);
#if defined(CAMERA_MODEL_M5STACK_WIDE)
s->set_vflip(s, 1);
s->set_hmirror(s, 1);
#endif
initSD();
taskDelay(1000);
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(ap_ssid,ap_password);
Serial.print("Access Point IP: ");
Serial.println(WiFi.softAPIP());
Serial.println("' to connect");
// Init ESPNow with a fallback logic
// WiFi.disconnect();
InitESPNow();
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
delay(100);
startCameraServer();
Serial.println("Camera Ready!");
delay(40000);
Serial.println("Motion Sensor Ready!");
}
void loop() {
if (!isPaired && lastConnectNowAttempt + nextConnectNowGap < millis())
{
Serial.println("NOT CONNECTED -> TRY TO CONNECT");
ScanForSlave();
// if we connected
if (isPaired)
{
Serial.println("ESP-NOW CONNECTED\n");
}
else
{
nextConnectNowGap *= 2; // dbl the gap
}
// save last attmpe
lastConnectNowAttempt = millis();
}
if(detect() && !currentTransmitTotalPackages && !sendNextPackageFlag) {
takeNextPhotoFlag = 1;
recognizeFaceFlag = 1;
} //else connectWiFi();
door_return = doorReturn();
if(door_return && !currentTransmitTotalPackages && !sendNextPackageFlag) {
byteTransmit(door_return);
// door_return = 0;
} //else connectWiFi();
if(recognizeFaceFlag && !is_streaming() && !sendNextPackageFlag){
recognizeFaceFlag = 0;
if((millis()-recognizeTime)>500){
recognizeTime = millis();
uint8_t recognizeID = recognizeFace();
if(recognizeID != lastRecognizeID){
byteTransmit(recognizeID);
lastRecognizeID = recognizeID;
}
}
}
// if the sendNextPackageFlag is set
if (sendNextPackageFlag) {
sendNextPackage();
}
// if takeNextPhotoFlag is set
if (takeNextPhotoFlag && !is_streaming()) {
takeNextPhotoFlag = 0;
if((millis() - lastCaptureTime)>5000){
lastCaptureTime = millis();
capture();
// taskDelay(3000000); //2sec delay
}
}
}