all thing is ok
This commit is contained in:
+238
@@ -0,0 +1,238 @@
|
||||
#include "esp_camera.h"
|
||||
#include "img_converters.h"
|
||||
#include "Arduino.h"
|
||||
#include "FS.h"
|
||||
#include "SD_MMC.h"
|
||||
#include "face_recognition_112_v1_s8.hpp"
|
||||
#define BOARD_HAS_PSRAM
|
||||
#define CONFIG_ESP_FACE_DETECT_ENABLED 1
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include "human_face_detect_msr01.hpp"
|
||||
#include "human_face_detect_mnp01.hpp"
|
||||
#include "fb_gfx.h"
|
||||
#include "esp32-hal-ledc.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
|
||||
#define PWDN_GPIO_NUM 32
|
||||
#define RESET_GPIO_NUM -1
|
||||
#define XCLK_GPIO_NUM 0
|
||||
#define SIOD_GPIO_NUM 26
|
||||
#define SIOC_GPIO_NUM 27
|
||||
|
||||
#define Y9_GPIO_NUM 35
|
||||
#define Y8_GPIO_NUM 34
|
||||
#define Y7_GPIO_NUM 39
|
||||
#define Y6_GPIO_NUM 36
|
||||
#define Y5_GPIO_NUM 21
|
||||
#define Y4_GPIO_NUM 19
|
||||
#define Y3_GPIO_NUM 18
|
||||
#define Y2_GPIO_NUM 5
|
||||
#define VSYNC_GPIO_NUM 25
|
||||
#define HREF_GPIO_NUM 23
|
||||
#define PCLK_GPIO_NUM 22
|
||||
|
||||
|
||||
camera_fb_t *fb = NULL;
|
||||
FaceRecognition112V1S8 recognizer;
|
||||
// void face_re(fb_data_t *fb, std::list<dl::detect::result_t> *results);
|
||||
// ساختن یک نمونه از مدل تشخیص چهره (Two-Stage)
|
||||
// HumanFaceDetectMSR01 detector(0.4F, 0.5F, 1, 0.2F);
|
||||
// HumanFaceDetectMNP01 detector(0.1F, 0.3F, 1);
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting...");
|
||||
|
||||
// تنظیمات دوربین
|
||||
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_RGB565; // ذخیره عکس به صورت JPEG
|
||||
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
|
||||
if (psramFound()) {
|
||||
config.frame_size = FRAMESIZE_VGA; // رزولوشن مناسب برای پردازش سریع
|
||||
config.jpeg_quality = 10;
|
||||
config.fb_count = 1;
|
||||
} else {
|
||||
config.frame_size = FRAMESIZE_VGA;
|
||||
config.jpeg_quality = 12;
|
||||
config.fb_count = 1;
|
||||
}
|
||||
|
||||
// راهاندازی دوربین
|
||||
esp_err_t err = esp_camera_init(&config);
|
||||
if (err != ESP_OK) {
|
||||
Serial.printf("Camera init failed with error 0x%x", err);
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Camera Ready! Send any character in Serial Monitor to take a photo and detect face.");
|
||||
|
||||
if (!SD_MMC.begin()) {
|
||||
Serial.println("card feild!");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t cardType = SD_MMC.cardType();
|
||||
if (cardType == CARD_NONE) {
|
||||
Serial.println("card not found");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("card finished.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// بررسی اینکه آیا دیتایی در سریال دریافت شده است یا خیر
|
||||
if (Serial.available() > 0) {
|
||||
// پاک کردن بافر سریال
|
||||
while (Serial.available() > 0) {
|
||||
Serial.read();
|
||||
}
|
||||
|
||||
Serial.println("Taking picture...");
|
||||
if (fb != NULL) {
|
||||
esp_camera_fb_return(fb);
|
||||
fb = NULL;
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// گرفتن عکس
|
||||
fb = esp_camera_fb_get();
|
||||
if (!fb) {
|
||||
Serial.println("Camera capture failed");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("Picture taken. Running face detection...");
|
||||
|
||||
// تخصیص حافظه برای تبدیل عکس به فرمت RGB888 که مدل دیتکت نیاز دارد
|
||||
// uint8_t *rgb_buf = (uint8_t *)malloc(fb->width * fb->height * 3);
|
||||
// if (!rgb_buf) {
|
||||
// Serial.println("Failed to allocate memory for RGB buffer");
|
||||
// esp_camera_fb_return(fb);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // تبدیل فریم به RGB888 fmt2rgb888(fb->buf, fb->len, fb->format, rgb_buf);
|
||||
// bool converted = fmt2rgb888(fb->buf, fb->len, fb->format, rgb_buf);
|
||||
// bool converted = true;
|
||||
// if (converted) {
|
||||
HumanFaceDetectMSR01 s1(0.1F, 0.5F, 1, 0.2F);
|
||||
HumanFaceDetectMNP01 s2(0.5F, 0.3F, 1);
|
||||
|
||||
// 2. مرحله اول: پیدا کردن چهرههای اولیه
|
||||
std::list<dl::detect::result_t> &candidates = s1.infer((uint16_t *)fb->buf, { (int)fb->height, (int)fb->width, 3 });
|
||||
|
||||
// 3. مرحله دوم: تایید نهایی و استخراج جزئیات (در صورت پیدا شدن کاندیدا)
|
||||
if (candidates.size() > 0) {
|
||||
std::list<dl::detect::result_t> &results = s2.infer((uint16_t *)fb->buf, { (int)fb->height, (int)fb->width, 3 }, candidates);
|
||||
|
||||
if (results.size() > 0) {
|
||||
Serial.printf(">>> SUCCESS: %d Face(s) Detected and Verified! <<<\n", results.size());
|
||||
uint8_t *rgb_buf = (uint8_t *)malloc(fb->width * fb->height * 3);
|
||||
if (!rgb_buf) {
|
||||
Serial.println("Failed to allocate memory for RGB buffer");
|
||||
esp_camera_fb_return(fb);
|
||||
return;
|
||||
}
|
||||
|
||||
// تبدیل فریم به RGB888 fmt2rgb888(fb->buf, fb->len, fb->format, rgb_buf);
|
||||
bool converted = fmt2rgb888(fb->buf, fb->len, fb->format, rgb_buf);
|
||||
if (!converted) {
|
||||
Serial.println("Convert ERROR!");
|
||||
return;
|
||||
}
|
||||
int width = (int)fb->width;
|
||||
int height = (int)fb->height;
|
||||
esp_camera_fb_return(fb);
|
||||
fb_data_t rfb;
|
||||
rfb.width = width;
|
||||
rfb.height = height;
|
||||
rfb.data = rgb_buf;
|
||||
rfb.bytes_per_pixel = 3;
|
||||
rfb.format = FB_BGR888;
|
||||
face_re(&rfb, &results);
|
||||
free(rgb_buf);
|
||||
} else {
|
||||
Serial.println("No face detected in the picture in 2 part.");
|
||||
}
|
||||
}
|
||||
// // اجرای مدل تشخیص چهره روی عکس
|
||||
// std::list<dl::detect::result_t> &candid1ates = detector.infer((uint16_t *)fb->buf, {(int)fb->height, (int)fb->width, 3});
|
||||
// //std::list<dl::detect::result_t> &candidates = detector.infer(rgb_buf, {(int)fb->width, (int)fb->height, 3});
|
||||
// // std::list<dl::detect::result_t> candidates;
|
||||
// // detector.infer((uint16_t *)fb->buf, {(int)fb->height, (int)fb->width, 3}, candidates);
|
||||
// if (candidates.size() > 0) {
|
||||
// Serial.printf(">>> SUCCESS: %d Face(s) Detected! <<<\n", candidates.size());
|
||||
// }
|
||||
else {
|
||||
Serial.println("No face detected in the picture in 1 part.");
|
||||
}
|
||||
// } else {
|
||||
// Serial.println("Format conversion to RGB888 failed.");
|
||||
// }
|
||||
|
||||
// آزاد کردن حافظه و فریم دوربین
|
||||
// free(rgb_buf);
|
||||
uint8_t *jpeg_buf = NULL;
|
||||
size_t jpeg_len = 0;
|
||||
bool jpeg_converted = frame2jpg(rgb_buf, 80, &jpeg_buf, &jpeg_len); // عدد 80 کیفیت عکس است (0-100)
|
||||
|
||||
if (jpeg_converted) {
|
||||
// ۶. ساخت یک نام فایل یکتا (با استفاده از شمارهانداز)
|
||||
static int pic_count = 0;
|
||||
String path = "/picture_" + String(pic_count) + ".jpg";
|
||||
|
||||
// ۷. ذخیره عکس روی کارت حافظه SDMMC
|
||||
File file = SD_MMC.open(path.c_str(), FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing!");
|
||||
} else {
|
||||
file.write(jpeg_buf, jpeg_len);
|
||||
Serial.printf("Saved: %s\n", path.c_str());
|
||||
pic_count++; // افزایش شماره برای عکس بعدی
|
||||
}
|
||||
file.close();
|
||||
|
||||
|
||||
free(jpeg_buf);
|
||||
} else {
|
||||
Serial.println("JPEG compression failed");
|
||||
}
|
||||
|
||||
|
||||
Serial.println("Ready for next command.\n-----------------------");
|
||||
}
|
||||
}
|
||||
|
||||
void face_re(fb_data_t *rfb, std::list<dl::detect::result_t> *results) {
|
||||
std::vector<int> landmarks = results->front().keypoint;
|
||||
Tensor<uint8_t> tensor;
|
||||
tensor.set_element((uint8_t *)rfb->data).set_shape({ rfb->height, rfb->width, 3 }).set_auto_free(false);
|
||||
int id = recognizer.enroll_id(tensor, landmarks, "", true);
|
||||
face_info_t recognize = recognizer.recognize(tensor, landmarks);
|
||||
Serial.printf("ID[%u]: %.2f \n", recognize.id, recognize.similarity);
|
||||
Serial.printf("Enrolled ID: %d", id);
|
||||
}
|
||||
Reference in New Issue
Block a user