C++ 如何通过插槽传输ESP32 Cam拍摄的图像?

C++ 如何通过插槽传输ESP32 Cam拍摄的图像?,c++,c,arduino,camera,esp32,C++,C,Arduino,Camera,Esp32,我有一个ESP32摄像头,我想将它拍摄的图像保存到我的桌面上。最终的目标是在Android应用程序中显示图像,但这是另一回事。 为了做到这一点,我想我必须用插座传输图像。 下面的代码显示了我的示例代码。我有一个非常简单的python程序,它连接到WifiServer并打印client.write()正在编写的任何内容 然而,我不知道我应该如何以这种方式传输图像。这可能吗?因为我必须传输图像缓冲区(在本例中为fb->buf),对吗 但是,一旦我收到缓冲区(例如在我的python代码中),我将如何从

我有一个ESP32摄像头,我想将它拍摄的图像保存到我的桌面上。最终的目标是在Android应用程序中显示图像,但这是另一回事。 为了做到这一点,我想我必须用插座传输图像。 下面的代码显示了我的示例代码。我有一个非常简单的python程序,它连接到WifiServer并打印client.write()正在编写的任何内容

然而,我不知道我应该如何以这种方式传输图像。这可能吗?因为我必须传输图像缓冲区(在本例中为fb->buf),对吗

但是,一旦我收到缓冲区(例如在我的python代码中),我将如何从中生成一个图像呢

如果有人能帮我运行,那就太好了,因为我以前从未使用过这个(图像/套接字)

ESP代码

#include "esp_camera.h"
#include "Arduino.h"
#include "WiFi.h"

// Pin definition for CAMERA_MODEL_AI_THINKER
#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

// Variables
camera_fb_t * fb = NULL;
WiFiServer wifiServer(5005);
const char* ssid = "ssid";
const char* password = "password";

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

  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi.");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("\nConnected!");
  Serial.println(WiFi.localIP());

  wifiServer.begin();

  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;

  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

  // Init Camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
}

void loop() {
  WiFiClient client = wifiServer.available();
  if (client) {
    fb = esp_camera_fb_get();  
    if(!fb) {
      Serial.println("Camera capture failed");
      return;
    } else {
      Serial.println("Camera capture succesfull!");
    }

    while (client.connected()) {
        // How do I transfer the image? What do I have to do?
        client.write(fb->buf);
    }

    client.stop();
    Serial.println("Client disconnected");

  }
}
Python代码

import socket               

sock = socket.socket()

host = "192.168.178.103" # esp ip
port = 5005              # esp port   

sock.connect((host, port))

data = sock.recv(256)

print(data)

sock.close()

下面是为我工作的Arduino代码(从循环函数开始)

接收图像的python代码

import io
import socket
import time

import numpy as np
from PIL import Image

import connection_and_network_constants


serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# address '0.0.0.0' or '' work to allow connections from other machines.  'localhost' disallows external connections.
# see https://www.raspberrypi.org/forums/viewtopic.php?t=62108
serv.bind(('0.0.0.0', connection_and_network_constants.SOCKET_PORT))
serv.listen(5)
print("Ready to accept 5 connections")


def create_image_from_bytes(image_bytes) -> Image.Image:
    stream = io.BytesIO(image_bytes)
    return Image.open(stream)


while True:
    conn, addr = serv.accept()
    array_from_client = bytearray()
    shape = None
    chunks_received = 0
    start = time.time()
    shape_string = ''
    while True:
        # print('waiting for data')
        # Try 4096 if unsure what buffer size to use. Large transfer chunk sizes (which require large buffers) can cause corrupted results
        data = conn.recv(connection_and_network_constants.BUFFER_SIZE)
        if not data or data == b'tx_complete':
            break
        elif shape is None:
            shape_string += data.decode("utf-8")
            # Find the end of the line.  An index other than -1 will be returned if the end has been found because 
            # it has been received
            if shape_string.find('\r\n') != -1:
                width_index = shape_string.find('width:')
                height_index = shape_string.find('height:')
                width = int(shape_string[width_index + len('width:'): height_index])
                height = int(shape_string[height_index + len('height:'): ])
                shape = (width, height)
            print("shape is {}".format(shape))
        else:
            chunks_received += 1
            # print(chunks_received)
            array_from_client.extend(data)
            # print(array_from_client)
        conn.sendall(b'ack')
        # print("sent acknowledgement")
    #     TODO: need to check if sending acknowledgement of the number of chunks and the total length of the array is a good idea
    print("chunks_received {}. Number of bytes {}".format(chunks_received, len(array_from_client)))
    img: Image.Image = create_image_from_bytes(array_from_client)
    img.show()
    array_start_time = time.time()
    image_array = np.asarray(img)
    print('array conversion took {} s'.format(time.time() - array_start_time))
    conn.close()
    print('client disconnected')

下面是为我工作的Arduino代码(从循环函数开始)

接收图像的python代码

import io
import socket
import time

import numpy as np
from PIL import Image

import connection_and_network_constants


serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# address '0.0.0.0' or '' work to allow connections from other machines.  'localhost' disallows external connections.
# see https://www.raspberrypi.org/forums/viewtopic.php?t=62108
serv.bind(('0.0.0.0', connection_and_network_constants.SOCKET_PORT))
serv.listen(5)
print("Ready to accept 5 connections")


def create_image_from_bytes(image_bytes) -> Image.Image:
    stream = io.BytesIO(image_bytes)
    return Image.open(stream)


while True:
    conn, addr = serv.accept()
    array_from_client = bytearray()
    shape = None
    chunks_received = 0
    start = time.time()
    shape_string = ''
    while True:
        # print('waiting for data')
        # Try 4096 if unsure what buffer size to use. Large transfer chunk sizes (which require large buffers) can cause corrupted results
        data = conn.recv(connection_and_network_constants.BUFFER_SIZE)
        if not data or data == b'tx_complete':
            break
        elif shape is None:
            shape_string += data.decode("utf-8")
            # Find the end of the line.  An index other than -1 will be returned if the end has been found because 
            # it has been received
            if shape_string.find('\r\n') != -1:
                width_index = shape_string.find('width:')
                height_index = shape_string.find('height:')
                width = int(shape_string[width_index + len('width:'): height_index])
                height = int(shape_string[height_index + len('height:'): ])
                shape = (width, height)
            print("shape is {}".format(shape))
        else:
            chunks_received += 1
            # print(chunks_received)
            array_from_client.extend(data)
            # print(array_from_client)
        conn.sendall(b'ack')
        # print("sent acknowledgement")
    #     TODO: need to check if sending acknowledgement of the number of chunks and the total length of the array is a good idea
    print("chunks_received {}. Number of bytes {}".format(chunks_received, len(array_from_client)))
    img: Image.Image = create_image_from_bytes(array_from_client)
    img.show()
    array_start_time = time.time()
    image_array = np.asarray(img)
    print('array conversion took {} s'.format(time.time() - array_start_time))
    conn.close()
    print('client disconnected')

图像只不过是存储在文件中的一系列字节。打开图像文件,读取这些字节并将其存储在
char*
缓冲区中,然后使用套接字发送该缓冲区的内容。图像只是存储在文件中的字节序列。打开图像文件,读取这些字节并将其存储在
char*
缓冲区中,然后使用套接字发送该缓冲区的内容。您好,可以直接从ESP32发送JPEG流并将其保存在python服务器上吗?从外观上看,您引用的代码正在循环拍摄图片并发送出去的代码(并且比我发布的代码具有更强大的错误处理能力)。您应该能够轻松地将我发布的代码中的概念改编为保存jpg流。尝试从“//capture camera frame”到“esp_camera_fb_return(fb);”连续循环代码。请注意,保存图片要比拍摄图片并根据我观察的结果发送图片慢得多。如果这有用,请更新我的答案。您好,这是选项之一,但这里的问题是,它不是一个连续流,因为每次向服务器发送jpeg,我都需要几毫秒的延迟。此外,像vlc这样的客户端也不会以流的形式接收这些图像。我已以问题的形式发布了图像,以获取详细解释您好,是否可以直接从ESP32 CAM在python服务器上发送和保存JPEG流从外观上看,您引用的代码正在循环拍摄图片并发送出去的代码(并且比我发布的代码具有更强大的错误处理能力)。您应该能够轻松地将我发布的代码中的概念改编为保存jpg流。尝试从“//capture camera frame”到“esp_camera_fb_return(fb);”连续循环代码。请注意,保存图片要比拍摄图片并根据我观察的结果发送图片慢得多。如果这有用,请更新我的答案。您好,这是选项之一,但这里的问题是,它不是一个连续流,因为每次向服务器发送jpeg,我都需要几毫秒的延迟。此外,像vlc这样的客户端也不会以流的形式接收这些图片。我已经以问题的形式发布了详细的图片解释