AbdelRahman Yassin
AbdelRahman Yassin

Reputation: 1

error to send a captured photo from esp32 cam to Ai model using flask

i have a project , when the esp32 cam captured a photo , the photo will send to model ai in my laptop and ai model will recognize the photo . When i coded the esp32 cam to send the photo , the final result after a successful capture photo is -1 from httprequest i searched in many websites to solve this problem . have you any suggestions to solve this problem ??

i tried to post the picture but give me result -1 `


  HTTPClient http;
  
  const char* serverURL = "http://YOUR_SERVER_IP:5000/process_image";
  const String boundary = "12345678900000000000012345678900";
  const String header = "--" + boundary + "\r\n" +
                        "Content-Disposition: form-data; name=\"image\"; filename=\"capture.jpg\"\r\n" +
                        "Content-Type: image/jpeg\r\n\r\n";
  const String footer = "\r\n--" + boundary + "--\r\n";

  // 4. Start Connection
  if (http.begin(client, serverURL)) {
    http.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    
    // 5. Calculate Content-Length
    int contentLength = header.length() + fb->len + footer.length();
    http.addHeader("Content-Length", String(contentLength));

    // 6. Stream Data Manually
    client = http.getStream();
    
    // Send header
    client.print(header);
    
    // Send image data in chunks
    size_t offset = 0;
    while(offset < fb->len) {
      size_t chunkSize = min((size_t)1024, fb->len - offset);
      client.write(fb->buf + offset, chunkSize);
      offset += chunkSize;
    }
    
    // Send footer
    client.print(footer);

    // 7. Handle Response
    int httpCode = http.GETResponseCode();
    if(httpCode == HTTP_CODE_OK) {
      Serial.println("Server response:");
      while(client.available()) {
        Serial.write(client.read());
      }
    } else {
      Serial.printf("HTTP error: %d\n", httpCode);
    }
  } else {
    Serial.println("HTTP connection failed");
  }

Upvotes: 0

Views: 16

Answers (0)

Related Questions