基于百度AI的Camera人脸识别
本帖最后由 vany5921 于 2020-6-29 15:03 编辑使用百度智能云提供的人脸识别服务能非常方便的进行大数据识别,无需提前进行任何模型训练,支持人脸识别、人脸搜索、身份证信息真人核实等功能,以下示例演示如何使用M5Camera直接对人脸进行采集识别,不同的REST请求格式不同,在使用时需要注意百度参考文档https://cloud.baidu.com/doc/FACE/s/yk37c1u4t
#include "esp_camera.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <WiFiClientSecure.h>
#include <base64.h>
#define SSID "yourssid"
#define PASSWORD "yourpassword"
#define ACCESS_TOKEN "4.ab9ba6cfb80d9a94392bea337d15142e.2592000.1595430949.282335-20541124" //TOKEN获取,30天过期,长期使用可通过发送http请求定时获取,这里为了验证功能直接使用postman获取。
#define Json_begin "{\"image\":\"" //Json封装头
#define Json_end "\",\"image_type\":\"BASE64\",\"group_id_list\":\"......\"}" //Json封装尾
//根据摄像头引脚定义修改(以下为PSRAM版本)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
#define SIOD_GPIO_NUM 22
#define SIOC_GPIO_NUM 23
#define Y9_GPIO_NUM 19
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 18
#define Y6_GPIO_NUM 39
#define Y5_GPIO_NUM 5
#define Y4_GPIO_NUM 34
#define Y3_GPIO_NUM 35
#define Y2_GPIO_NUM 32
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 26
#define PCLK_GPIO_NUM 21
//图像数据需要经过base64编码后以JSON格式发送
static void Baidu_AI(camera_fb_t* &fb){
String base64 = base64::encode(fb->buf,fb->len);
uint16_t len = base64.length();
WiFiClientSecure client;
if(client.connect("aip.baidubce.com",443)){
Serial.println("Connection succeeded");
client.println("POST /rest/2.0/face/v3/detect?access_token=" + String(ACCESS_TOKEN) + " HTTP/1.1");
client.println(F("Host: aip.baidubce.com"));
client.println("Content-Length: " + String(len + strlen(Json_begin Json_end)));
client.println(F("Content-Type: application/json"));
client.println();
client.print(F(Json_begin));
for(uint16_t i = 0;i < len;i += 4096) //分段发送
if(len > i + 4096)
client.print(base64.substring(i,i+4096));
else{
client.print(base64.substring(i));
break;
}
client.print(F(Json_end));
Serial.println("Waiting for response...");
uint8_t i = 0;
while (!client.available()){
i += 1;
delay(100);
if(i > 200){ //timeout
Serial.println("No response...");
break;
}
}
while (client.available()){
Serial.print(char(client.read()));
}
client.stop();
Serial.println();
}else Serial.println("Connection failed");
}
void setup()
{
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG,0); //关闭欠压检测(防止低压重启)
Serial.begin(115200);
WiFi.begin(SSID,PASSWORD);
while (WiFi.status() != WL_CONNECTED){
Serial.println("Waitting for wifi connection...");
delay(500);
}
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;
config.frame_size = FRAMESIZE_VGA;
config.jpeg_quality = 10;
config.fb_count = 1;
while(esp_camera_init(&config) != ESP_OK){
Serial.println("Waitting for camera init...");
delay(500);
}
sensor_t * s = esp_camera_sensor_get();
s->set_vflip(s, 1);
}
void loop()
{
while(WiFi.status() != WL_CONNECTED){ //断线重连
WiFi.reconnect();
delay(500);
}
camera_fb_t *fb = esp_camera_fb_get();
if(!fb)return;
Serial.println("Got image");
Baidu_AI(fb);
esp_camera_fb_return(fb);
}
页:
[1]