ESP32 Google Vision 人脸检测请求失败

问题描述 投票:0回答:1

我正在尝试将网络上的图像提交给 Google Vision 人脸检测。 直接在Google Vision页面提交图像:

https://cloud.google.com/vision/docs/ocr?apix_params=%7B%22resource%22%3A%7B%22requests%22%3A%5B%7B%22features%22%3A%5B%7B%22type%22%3A%22FACE_DETECTION%22%7D%5D%2C%22image%22%3A%7B%22source%22%3A%7B%22imageUri%22%3A%22http%3A%2F%2Fwww.newdesignfile.com%2Fpostpic%2F2010%2F05%2Ffree-stock-photos-people_102217.jpg%22%7D%7D%2C%22imageContext%22%3A%7B%22languageHints%22%3A%5B%22pt%22%5D%7D%7D%5D%7D%7D

工作正常。 在树莓派上使用 NodeRed 也可以正常工作。 但我无法让它与 ESP32 一起工作。 它总是在控制台上响应: HTTP 响应代码:-5 错误代码:-5

我的代码如下:

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "mySSID";
const char* password = "myPass";

//Your Domain name with URL path or IP address with path
const char* serverName   = "https://vision.googleapis.com/v1/images:annotate?key=MyAPIKey";

String _request = "{\"requests\":[{\"image\":{\"source\":{\"imageUri\":\"http://www.newdesignfile.com/postpic/2010/05/free-stock-photos-people_102217.jpg\"}},\"features\":[{\"type\":\"FACE_DETECTION\",\"maxResults\":10}]}]}";


// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
unsigned long timerDelay = 15000;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;
    
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);
      Serial.print("Conected to Server: ");
      Serial.println(serverName);
      
      //If you need an HTTP request with a content type: application/json, use the following:
      http.addHeader("Content-Type", "application/json");
      Serial.print("Posting request: ");    
      Serial.println(_request);
      int httpResponseCode = http.POST(_request); 
      
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);

      String payload = "{}"; 

      if(httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        payload = http.getString();
        Serial.println(payload);        
      }else{
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }    
        
      // Free resources
      http.end();
    }else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

欢迎协助。

esp32 detection face-detection
1个回答
1
投票

您正在尝试通过 HTTP 连接加载 HTTPS URL。

您需要使用

WiFiClientSecure
才能使用 HTTPS - 您可以在
HTTPClient
中找到示例。

您的代码需要如下所示:

      WiFiClientSecure client;
      HTTPClient http;

      client.setCACert(rootCACertificate);

      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);

您需要查找并存储您正在访问的域的根证书颁发机构证书的副本。如果它发生变化(并且最终会过期),您将需要更新它。一般来说,您会在文件的开头定义它 - 大多数人不必要地将其定义为全局变量,如下所示:

#include <WiFi.h>
#include <HTTPClient.h>

const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
...
"-----END CERTIFICATE-----\n";

您可以将

...
替换为证书的实际内容。您可以通过搜索引擎或在 Stack Overflow 上找到有关如何执行此操作的许多说明。确保将其每一行写为以
\n" \
结尾的 C 字符串,以便将各行连接成单个字符串。

您可以通过编写如下代码来避免对根 CA 证书的需求:

      WiFiClientSecure client;
      HTTPClient http;

      client.setInsecure();

      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);

这对于测试来说很好,但它破坏了 HTTPS 为您提供的安全性,并且不应该在生产或部署的代码中使用。以不安全方式运行意味着您不知道您连接的服务器是正确的服务器,也不知道您与其交换的数据(包括任何密码或 API 密钥)实际上对于任何试图窃听的人来说都是私有的关于网络流量。

© www.soinside.com 2019 - 2024. All rights reserved.