新版的编译器要求字符串双引号""里的引号前要加\反斜杠
就像这样#define Json_begin "{"image":"" //Json封装头
改成这样#define Json_begin "{\"image\":\"" //Json封装头
如果有人在String POST_Code=Json_begin+base64+Json_end;位置报错的话,可以试试用我改过的代码
- #define BLINKER_WIFI
- //#define BLINKER_ESP_SMARTCONFIG //自动配网设置
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- #include <base64.h>
- #include <Blinker.h>
- #include "ESP32_CAM_SERVER.h"
- char auth[] = "************";//blinker的设备号
- char ssid[] = "********";//wifi名称
- char pswd[] = "********";//wifi密码
- #define client_id "************************" //API Key【百度云应用的AK】
- #define client_secret "********************************"//Secret Key【百度云应用的SK】
- #define Json_begin "{"image":"" //Json封装头
- #define Json_end "","image_type":"BASE64","group_id_list":"test"}" //Json封装尾 test改成你的用户组,如果一直提示未检测到人脸信息,可能是填错了
- String access_token;
- String Group_list_str;
- bool setup_camera = false;
- #define BUTTON_1 "ButtonKey"
- BlinkerButton Button1(BUTTON_1);
- //人脸验证程序,图像数据需要经过base64编码后以JSON格式发送
- static void Baidu_AI(camera_fb_t* &fb){
- String base64 = base64::encode(fb->buf,fb->len);
- uint32_t len = base64.length();
- //Serial.print(base64);
- HTTPClient http;
- http.begin("https://aip.baidubce.com/rest/2.0/face/v3/search?access_token="+ access_token);
- String POST_Code=Json_begin+base64+Json_end;
- http.setReuse(true);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- http.addHeader("Accept","application/json");
- http.addHeader("Content-Length",String(POST_Code.length()));
- http.addHeader("X-Custom-Header", "custom-header-value");
- int httpCode = http.POST(POST_Code);//图片太大就会发送失败
- if (httpCode > 0) {
- // HTTP header has been send and Server response header has been handled
- Serial.println("验证人脸... ");
- // file found at server
- if (httpCode == HTTP_CODE_OK) {
- String payload=http.getString();
- DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2)+strlen(payload.c_str())*sizeof(String));
- DeserializationError error = deserializeJson(jsonBuffer, payload);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- BLINKER_LOG(error.c_str());
- http.end();
- return;
- }
- JsonObject reload_Json = jsonBuffer.as<JsonObject>();
- int search_buff_error_code=reload_Json["error_code"].as<int>();
-
-
- if (search_buff_error_code==0){
- String search_buff_user_id=reload_Json["result"]["user_list"][0]["user_id"].as<const char*>();
- String search_buff_group_id=reload_Json["result"]["user_list"][0]["group_id"].as<const char*>();
- float search_buff_score=reload_Json["result"]["user_list"][0]["score"].as<float>();
- Serial.print("用户名:"+search_buff_user_id);
- Serial.println(" 相似度: "+String(search_buff_score));
-
- Blinker.print("用户名:"+search_buff_user_id);
- Blinker.print("相似度: "+String(search_buff_score));
- if(search_buff_score>=79){
- //官方建议相似度超过80%即可视为验证通过,可以在这里插入控制门禁设备代码
- Blinker.print("门已开启");
- Serial.print("门已开启");
- }
-
- }else{
-
- Serial.println(" 未检测到人脸信息!");
- }
- }
- } else {
- Serial.printf("[HTTP] ... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- }
- //获取百度鉴权认证认证token
- static void Baidu_AI_token(){
- HTTPClient http;
- Serial.printf("获取百度_AI_token ... ");
- http.begin("https://aip.baidubce.com/oauth/2.0/token");
- http.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
-
- int httpCode = http.POST("grant_type=client_credentials&client_id="+String(client_id) +"&client_secret="+String(client_secret));
- if (httpCode > 0) {
- // HTTP header has been send and Server response header has been handled
- // file found at server
- if (httpCode == HTTP_CODE_OK) {
- String payload=http.getString();
- DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2)+strlen(payload.c_str())*sizeof(String));
- //DynamicJsonDocument jsonBuffer(20000);
- DeserializationError error = deserializeJson(jsonBuffer, payload);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.c_str());
- http.end();
- return;
- }
-
- JsonObject reload_Json = jsonBuffer.as<JsonObject>();
- access_token=reload_Json["access_token"].as<const char*>();
- Serial.println("SUCCESS");
- Serial.print("access_token=");
- Serial.println(access_token);
-
- }
- } else {
- Serial.printf("[HTTP] ... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- delay(500);
- }
- //获取用户组列表
- static void Baidu_AI_Group_list(){
- HTTPClient http;
- Serial.println("[HTTPS] 获取组列表...");
- http.begin("https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getlist");
- http.addHeader("Content-Type", "application/json; charset=UTF-8");
- String POST_Code="access_token="+access_token;
- int httpCode = http.POST(POST_Code);
- if (httpCode > 0) {
- Serial.println("[HTTP] 获取用户列表 ... ");
- // HTTP header has been send and Server response header has been handled
- // file found at server
- if (httpCode == HTTP_CODE_OK) {
- String payload=http.getString();
- int payload_len=strlen(payload.c_str());
- DynamicJsonDocument group_list_jsonBuffer(JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2)+payload_len*sizeof(String));
- DeserializationError error = deserializeJson(group_list_jsonBuffer, payload);
- if (error) {
- Serial.print(F("获取用户组列表失败: "));
- Serial.println(error.f_str());
- http.end();
- return;
- }
- //获取组列表
- JsonObject group_list_Json = group_list_jsonBuffer.as<JsonObject>();
- int Group_len=group_list_Json["result"]["group_id_list"].size();
- Serial.print("用户组数量=");
- Serial.println(Group_len);
- int User_list_count=0;
- //http.end();
- if(Group_len>0){
- Group_list_str=group_list_Json["result"]["group_id_list"][0].as<const char*>();
- for(int i=0;i<Group_len-1;i++){
- Group_list_str=Group_list_str+","+group_list_Json["result"]["group_id_list"][i+1].as<const char*>();
- }
- Serial.println(Group_list_str);
- }
- }
- } else {
- Serial.printf("[HTTP] ... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- delay(500);
- }
- void button1_callback(const String & state)
- {
- digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
- BLINKER_LOG("get button state: ", state);
- camera_fb_t *fb = esp_camera_fb_get();
- if(!fb)return;
- //BLINKER_LOG("Got image");
- Baidu_AI(fb); //人脸验证程序,图像数据需要经过base64编码后以JSON格式发送
- esp_camera_fb_return(fb);
-
- }
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
-
- uint32_t BlinkerTime = millis();
-
- Blinker.print("millis", BlinkerTime);
- }
- void setup()
- {
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
-
- //Blinker.begin(auth);//自动配网设置
- Blinker.begin(auth,ssid,pswd);
- Blinker.attachData(dataRead);
- Button1.attach(button1_callback);
- }
- void loop()
- {
- Blinker.run();
- if (Blinker.connected() && !setup_camera)
- {
- setupCamera();
-
- sensor_t * s = esp_camera_sensor_get();//设置分辨率
- s->set_framesize(s, FRAMESIZE_QQVGA);//分辨率降为QQVGA
- s->set_brightness(s, 2);
- setup_camera = true;
- Blinker.printObject("video", "{"str":"mjpg","url":"http://"+ WiFi.localIP().toString() + ""}");
- //Blinker.printObject("video", "{"str":"mjpg","url":"http://"+ WiFi.localIP().toString() + ""}");
- if(access_token==NULL){
- BLINKER_LOG("成功连接网络,尝试获取百度AI_token");
- Baidu_AI_token();//获取百度鉴权认证认证token
- Baidu_AI_Group_list();//获取用户组列表
- Serial.println("设备准备就绪可以进行人脸识别 ... ...");
- Blinker.print("设备准备就绪可以进行人脸识别 ... ...");
- }
- }
- // delay(1000);//每隔1000ms识别一次
- // camera_fb_t *fb = esp_camera_fb_get();
- // if(!fb)return;
- // //BLINKER_LOG("Got image");
- // Baidu_AI(fb); //人脸验证程序,图像数据需要经过base64编码后以JSON格式发送
- // esp_camera_fb_return(fb);
- }
复制代码
另外我想问下楼主,用POST发送https时,如果请求信息太长的话,也就是图片分辨率太大时,请求长度超过5000左右时,服务器就会接收失败,请问楼主有解决方法吗,实在弄不来了我用抓包都抓不到什么东西 |