309浏览
查看: 309|回复: 2

[项目] “ESP32-S3 AI智能摄像头”接入Kimi图像理解——盲人AI眼镜

[复制链接]
本帖最后由 云天 于 2025-3-27 19:47 编辑

【项目背景】
DF创客社区近期开启了福利发放活动,其中一项福利是提供人工智能设备“ESP32-S3 AI智能摄像头模块”的免费试用。尽管试用名单尚未公布,但我已按捺不住内心的期待,提前从DF创客商城购买了一台。如果我有幸入选试用名单,我计划开展一个双摄像头模块项目,探索更多的可能性。
【项目设计】
本项目的设计灵感来源于产品使用教程中的思路。项目的核心是“ESP32-S3 AI智能摄像头模块”,它负责采集图像数据。与此同时,电脑上的Python程序将对采集到的图像进行处理。具体而言,Python程序会将获取的图像发送给Kimi大模型进行图像理解,随后将模型返回的文本信息发送给讯飞语音合成平台,生成语音反馈。最终,利用Pygame库在电脑上播放反馈的音频。
【项目功能】
本项目将“ESP32-S3 AI智能摄像头模块”巧妙地固定在一副眼镜上,让盲人佩戴这副眼镜采集图像。电脑端会通过语音播放图像分析结果,帮助盲人了解当前室内的环境情况。这一功能旨在为视障人士提供更便捷、更直观的环境感知方式,提升他们的生活质量和独立性。
【迭代设想】
目前,项目的图像处理和语音合成功能主要依赖电脑端实现。为了进一步优化项目,我们计划将这些功能移植到“ESP32-S3 AI智能摄像头模块”上,使其能够独立运行。这样,项目将不再受限于电脑设备,能够更灵活地应用于各种场景,如户外、公共场所等,从而扩大项目的应用范围和实用性。
【程序设计一】
ESP32-S3端程序
“ESP32-S3 AI智能摄像头”接入Kimi图像理解——盲人AI眼镜图1
使用“产品维库”中的教程,上传程序——视频图传。
步骤
1.在Arduino IDE中选择File->Examples->ESP32->Camera->CameraWebServer示例
2.使用下面的代码替换CameraWebServer中的代码(注意:需要填入WIFI账号密码)
3.打开串口监视器查看ip地址
4.通过局域网内的设备通过浏览器访问ip,点击start即可看到监控画面
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. //
  4. // WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
  5. //            Ensure ESP32 Wrover Module or other board with PSRAM is selected
  6. //            Partial images will be transmitted if image exceeds buffer size
  7. //
  8. //            You must select partition scheme from the board menu that has at least 3MB APP space.
  9. //            Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
  10. //            seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well
  11. #define PWDN_GPIO_NUM     -1
  12. #define RESET_GPIO_NUM    -1
  13. #define XCLK_GPIO_NUM     5
  14. #define Y9_GPIO_NUM       4
  15. #define Y8_GPIO_NUM       6
  16. #define Y7_GPIO_NUM       7
  17. #define Y6_GPIO_NUM       14
  18. #define Y5_GPIO_NUM       17
  19. #define Y4_GPIO_NUM       21
  20. #define Y3_GPIO_NUM       18
  21. #define Y2_GPIO_NUM       16
  22. #define VSYNC_GPIO_NUM    1
  23. #define HREF_GPIO_NUM     2
  24. #define PCLK_GPIO_NUM     15
  25. #define SIOD_GPIO_NUM  8
  26. #define SIOC_GPIO_NUM  9
  27. // ===========================
  28. // Enter your WiFi credentials
  29. // ===========================
  30. const char *ssid = "**********";
  31. const char *password = "**********";
  32. void startCameraServer();
  33. void setupLedFlash(int pin);
  34. void setup() {
  35.   Serial.begin(115200);
  36.   Serial.setDebugOutput(true);
  37.   Serial.println();
  38.   camera_config_t config;
  39.   config.ledc_channel = LEDC_CHANNEL_0;
  40.   config.ledc_timer = LEDC_TIMER_0;
  41.   config.pin_d0 = Y2_GPIO_NUM;
  42.   config.pin_d1 = Y3_GPIO_NUM;
  43.   config.pin_d2 = Y4_GPIO_NUM;
  44.   config.pin_d3 = Y5_GPIO_NUM;
  45.   config.pin_d4 = Y6_GPIO_NUM;
  46.   config.pin_d5 = Y7_GPIO_NUM;
  47.   config.pin_d6 = Y8_GPIO_NUM;
  48.   config.pin_d7 = Y9_GPIO_NUM;
  49.   config.pin_xclk = XCLK_GPIO_NUM;
  50.   config.pin_pclk = PCLK_GPIO_NUM;
  51.   config.pin_vsync = VSYNC_GPIO_NUM;
  52.   config.pin_href = HREF_GPIO_NUM;
  53.   config.pin_sccb_sda = SIOD_GPIO_NUM;
  54.   config.pin_sccb_scl = SIOC_GPIO_NUM;
  55.   config.pin_pwdn = PWDN_GPIO_NUM;
  56.   config.pin_reset = RESET_GPIO_NUM;
  57.   config.xclk_freq_hz = 20000000;
  58.   config.frame_size = FRAMESIZE_UXGA;
  59.   config.pixel_format = PIXFORMAT_JPEG;  // for streaming
  60.   //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  61.   config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  62.   config.fb_location = CAMERA_FB_IN_PSRAM;
  63.   config.jpeg_quality = 12;
  64.   config.fb_count = 1;
  65.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  66.   //                      for larger pre-allocated frame buffer.
  67.   if (config.pixel_format == PIXFORMAT_JPEG) {
  68.     if (psramFound()) {
  69.       config.jpeg_quality = 10;
  70.       config.fb_count = 2;
  71.       config.grab_mode = CAMERA_GRAB_LATEST;
  72.     } else {
  73.       // Limit the frame size when PSRAM is not available
  74.       config.frame_size = FRAMESIZE_SVGA;
  75.       config.fb_location = CAMERA_FB_IN_DRAM;
  76.     }
  77.   } else {
  78.     // Best option for face detection/recognition
  79.     config.frame_size = FRAMESIZE_240X240;
  80. #if CONFIG_IDF_TARGET_ESP32S3
  81.     config.fb_count = 2;
  82. #endif
  83.   }
  84. #if defined(CAMERA_MODEL_ESP_EYE)
  85.   pinMode(13, INPUT_PULLUP);
  86.   pinMode(14, INPUT_PULLUP);
  87. #endif
  88.   // camera init
  89.   esp_err_t err = esp_camera_init(&config);
  90.   if (err != ESP_OK) {
  91.     Serial.printf("Camera init failed with error 0x%x", err);
  92.     return;
  93.   }
  94.   sensor_t *s = esp_camera_sensor_get();
  95.   // initial sensors are flipped vertically and colors are a bit saturated
  96.   if (s->id.PID == OV3660_PID) {
  97.     s->set_vflip(s, 1);        // flip it back
  98.     s->set_brightness(s, 1);   // up the brightness just a bit
  99.     s->set_saturation(s, -2);  // lower the saturation
  100.   }
  101.   // drop down frame size for higher initial frame rate
  102.   if (config.pixel_format == PIXFORMAT_JPEG) {
  103.     s->set_framesize(s, FRAMESIZE_QVGA);
  104.   }
  105. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  106.   s->set_vflip(s, 1);
  107.   s->set_hmirror(s, 1);
  108. #endif
  109. #if defined(CAMERA_MODEL_ESP32S3_EYE)
  110.   s->set_vflip(s, 1);
  111. #endif
  112. // Setup LED FLash if LED pin is defined in camera_pins.h
  113. #if defined(LED_GPIO_NUM)
  114.   setupLedFlash(LED_GPIO_NUM);
  115. #endif
  116.   WiFi.begin(ssid, password);
  117.   WiFi.setSleep(false);
  118.   Serial.print("WiFi connecting");
  119.   while (WiFi.status() != WL_CONNECTED) {
  120.     delay(500);
  121.     Serial.print(".");
  122.   }
  123.   Serial.println("");
  124.   Serial.println("WiFi connected");
  125.   startCameraServer();
  126.   Serial.print("Camera Ready! Use 'http://");
  127.   Serial.print(WiFi.localIP());
  128.   Serial.println("' to connect");
  129. }
  130. void loop() {
  131.   // Do nothing. Everything is done in another task by the web server
  132.   delay(10000);
  133. }
  134. 录音、播放声音
  135. 通过该示例可以实现录音、放音功能。烧录代码并复位开发板,LED灯点亮后开始录音5秒,LED灯熄灭后通过喇叭播放录音。
  136. #include <Arduino.h>
  137. #include <SPI.h>
  138. #include "ESP_I2S.h"
  139. #define SAMPLE_RATE     (16000)
  140. #define DATA_PIN        (GPIO_NUM_39)
  141. #define CLOCK_PIN       (GPIO_NUM_38)
  142. #define REC_TIME 5  //Recording time 5 seconds
  143. void setup()
  144. {
  145.   uint8_t *wav_buffer;
  146.   size_t wav_size;
  147.   I2SClass i2s;
  148.   I2SClass i2s1;
  149.   Serial.begin(115200);
  150.   pinMode(3, OUTPUT);
  151.   pinMode(41, OUTPUT);
  152.   i2s.setPinsPdmRx(CLOCK_PIN, DATA_PIN);
  153.   if (!i2s.begin(I2S_MODE_PDM_RX, SAMPLE_RATE, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO)) {
  154.     Serial.println("Failed to initialize I2S PDM RX");
  155.   }
  156.   i2s1.setPins(45, 46, 42);
  157.   if (!i2s1.begin(I2S_MODE_STD, SAMPLE_RATE, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO)) {
  158.     Serial.println("MAX98357 initialization failed!");
  159.   }
  160.   Serial.println("start REC");
  161.   digitalWrite(3, HIGH);
  162.   wav_buffer = i2s.recordWAV(REC_TIME, &wav_size);
  163.   digitalWrite(3, LOW);
  164.   //Play the recording
  165.   i2s1.playWAV(wav_buffer, wav_size);
  166. }
  167. void loop()
  168. {
  169.   
  170. }
复制代码
电脑端Python程序
  1. # 导入所需的库
  2. import os
  3. import base64
  4. from openai import OpenAI
  5. import cv2
  6. import urllib.request
  7. import numpy as np
  8. from df_xfyun_speech import XfTts  # 用于语音合成
  9. import pygame,time
  10. pygame.mixer.init()  # 初始化pygame的音频模块
  11. # 配置讯飞语音合成的参数
  12. appId = "f6e50ac1"  # 应用ID
  13. apiKey ="11ecfe2c70c42cadda1bae3916576ec4"  # API密钥
  14. apiSecret = "NGQwMjdiYTY0ZGZkZDI5MTJlMGQ4Yzhk"  # API密钥
  15. options = {}  # 配置选项
  16. tts = XfTts(appId, apiKey, apiSecret, options)  # 初始化语音合成对象
  17. # ESP32-CAM的IP地址,用于获取摄像头图像
  18. url = 'http://192.168.31.96:80/capture'  
  19. # 配置OpenAI的客户端
  20. client = OpenAI(
  21.     api_key="sk-l77I27a8yfbu1hie5jj9FJOIexixS1RRdLFVEv2xbRiFsb3k",  # Kimi的API密钥
  22.     base_url="https://api.moonshot.cn/v1",  # Kimi的API地址
  23. )
  24. while True:
  25.     # 从ESP32-CAM的URL获取图像数据
  26.     img_resp = urllib.request.urlopen(url)  # 打开URL获取图像数据
  27.     imgnp = np.array(bytearray(img_resp.read()), dtype=np.uint8)  # 将图像数据转换为NumPy数组
  28.     image_data = cv2.imdecode(imgnp, -1)  # 解码JPEG图像数据
  29.     # 创建一个窗口用于显示实时图像
  30.     cv2.namedWindow("live transmission", cv2.WINDOW_AUTOSIZE)  
  31.     cv2.imshow("live transmission", image_data)  # 显示实时图像
  32.     cv2.imwrite("Mind+.png", image_data)  # 将图像保存为文件
  33.     # 读取保存的图像文件
  34.     with open("Mind+.png", "rb") as f:
  35.         image_data = f.read()
  36.     # 按下 'q' 键退出
  37.     if cv2.waitKey(1) & 0xFF == ord('q'):
  38.         pass
  39.     # 将图片编码成 base64 格式的 image_url
  40.     image_url = f"data:image/{os.path.splitext('Mind+.png')[1]};base64,{base64.b64encode(image_data).decode('utf-8')}"
  41.     # 调用Kimi的API,将图片和文字指令发送给模型
  42.     completion = client.chat.completions.create(
  43.         model="moonshot-v1-8k-vision-preview",  # 使用的模型
  44.         messages=[
  45.             {"role": "system", "content": "你是 Kimi。"},  # 系统角色的提示
  46.             {
  47.                 "role": "user",
  48.                 # 用户的输入,包含图片和文字指令
  49.                 "content": [
  50.                     {
  51.                         "type": "image_url",  # 图片部分
  52.                         "image_url": {
  53.                             "url": image_url,  # 图片的base64编码
  54.                         },
  55.                     },
  56.                     {
  57.                         "type": "text",
  58.                         "text": "请描述图片的内容。",  # 文字指令
  59.                     },
  60.                 ],
  61.             },
  62.         ],
  63.     )
  64.     # 获取模型返回的描述信息
  65.     message = completion.choices[0].message.content
  66.     print(message)  # 打印描述信息
  67.     # 将描述信息合成语音
  68.     tts.synthesis(message, "speech.wav")  # 生成语音文件
  69.     pygame.mixer.Sound("speech.wav").play()  # 播放语音
  70.     time.sleep(15)
复制代码
【程序设计二】
使用TCP发送和接收图像,具体步骤:
1. 在ESP32的代码中添加定时器或使用循环中的延时来触发图像捕获和发送。
2. 捕获一帧图像,获取图像数据的缓冲区。
3. 建立TCP连接到电脑的指定IP和端口,发送图像数据。
4. 在电脑端,用Python编写一个TCP服务器,接收数据并保存或显示图像。
ESP32-S3端程序
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. const char* pc_ip = "192.168.1.110"; // 替换为电脑的IP
  4. const uint16_t pc_port = 12345;
  5. //
  6. // WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
  7. //            Ensure ESP32 Wrover Module or other board with PSRAM is selected
  8. //            Partial images will be transmitted if image exceeds buffer size
  9. //
  10. //            You must select partition scheme from the board menu that has at least 3MB APP space.
  11. //            Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
  12. //            seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well
  13. #define PWDN_GPIO_NUM     -1
  14. #define RESET_GPIO_NUM    -1
  15. #define XCLK_GPIO_NUM     5
  16. #define Y9_GPIO_NUM       4
  17. #define Y8_GPIO_NUM       6
  18. #define Y7_GPIO_NUM       7
  19. #define Y6_GPIO_NUM       14
  20. #define Y5_GPIO_NUM       17
  21. #define Y4_GPIO_NUM       21
  22. #define Y3_GPIO_NUM       18
  23. #define Y2_GPIO_NUM       16
  24. #define VSYNC_GPIO_NUM    1
  25. #define HREF_GPIO_NUM     2
  26. #define PCLK_GPIO_NUM     15
  27. #define SIOD_GPIO_NUM  8
  28. #define SIOC_GPIO_NUM  9
  29. // ===========================
  30. // Enter your WiFi credentials
  31. // ===========================
  32. const char *ssid = "TP-LINK_CB88";
  33. const char *password = "jiaoyan2";
  34. //void startCameraServer();
  35. void setupLedFlash(int pin);
  36. void setup() {
  37.   Serial.begin(115200);
  38.   Serial.setDebugOutput(true);
  39.   Serial.println();
  40.   camera_config_t config;
  41.   config.ledc_channel = LEDC_CHANNEL_0;
  42.   config.ledc_timer = LEDC_TIMER_0;
  43.   config.pin_d0 = Y2_GPIO_NUM;
  44.   config.pin_d1 = Y3_GPIO_NUM;
  45.   config.pin_d2 = Y4_GPIO_NUM;
  46.   config.pin_d3 = Y5_GPIO_NUM;
  47.   config.pin_d4 = Y6_GPIO_NUM;
  48.   config.pin_d5 = Y7_GPIO_NUM;
  49.   config.pin_d6 = Y8_GPIO_NUM;
  50.   config.pin_d7 = Y9_GPIO_NUM;
  51.   config.pin_xclk = XCLK_GPIO_NUM;
  52.   config.pin_pclk = PCLK_GPIO_NUM;
  53.   config.pin_vsync = VSYNC_GPIO_NUM;
  54.   config.pin_href = HREF_GPIO_NUM;
  55.   config.pin_sccb_sda = SIOD_GPIO_NUM;
  56.   config.pin_sccb_scl = SIOC_GPIO_NUM;
  57.   config.pin_pwdn = PWDN_GPIO_NUM;
  58.   config.pin_reset = RESET_GPIO_NUM;
  59.   config.xclk_freq_hz = 20000000;
  60.   config.frame_size = FRAMESIZE_UXGA;
  61.   config.pixel_format = PIXFORMAT_JPEG;  // for streaming
  62.   //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  63.   config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  64.   config.fb_location = CAMERA_FB_IN_PSRAM;
  65.   config.jpeg_quality = 12;
  66.   config.fb_count = 1;
  67.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  68.   //                      for larger pre-allocated frame buffer.
  69.   if (config.pixel_format == PIXFORMAT_JPEG) {
  70.     if (psramFound()) {
  71.       config.jpeg_quality = 10;
  72.       config.fb_count = 2;
  73.       config.grab_mode = CAMERA_GRAB_LATEST;
  74.     } else {
  75.       // Limit the frame size when PSRAM is not available
  76.       config.frame_size = FRAMESIZE_SVGA;
  77.       config.fb_location = CAMERA_FB_IN_DRAM;
  78.     }
  79.   } else {
  80.     // Best option for face detection/recognition
  81.     config.frame_size = FRAMESIZE_240X240;
  82. #if CONFIG_IDF_TARGET_ESP32S3
  83.     config.fb_count = 2;
  84. #endif
  85.   }
  86. #if defined(CAMERA_MODEL_ESP_EYE)
  87.   pinMode(13, INPUT_PULLUP);
  88.   pinMode(14, INPUT_PULLUP);
  89. #endif
  90.   // camera init
  91.   esp_err_t err = esp_camera_init(&config);
  92.   if (err != ESP_OK) {
  93.     Serial.printf("Camera init failed with error 0x%x", err);
  94.     return;
  95.   }
  96.   sensor_t *s = esp_camera_sensor_get();
  97.   // initial sensors are flipped vertically and colors are a bit saturated
  98.   if (s->id.PID == OV3660_PID) {
  99.     s->set_vflip(s, 1);        // flip it back
  100.     s->set_brightness(s, 1);   // up the brightness just a bit
  101.     s->set_saturation(s, -2);  // lower the saturation
  102.   }
  103.   // drop down frame size for higher initial frame rate
  104.   if (config.pixel_format == PIXFORMAT_JPEG) {
  105.     s->set_framesize(s, FRAMESIZE_QVGA);
  106.   }
  107. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  108.   s->set_vflip(s, 1);
  109.   s->set_hmirror(s, 1);
  110. #endif
  111. #if defined(CAMERA_MODEL_ESP32S3_EYE)
  112.   s->set_vflip(s, 1);
  113. #endif
  114. // Setup LED FLash if LED pin is defined in camera_pins.h
  115. #if defined(LED_GPIO_NUM)
  116.   setupLedFlash(LED_GPIO_NUM);
  117. #endif
  118.   WiFi.begin(ssid, password);
  119.   WiFi.setSleep(false);
  120.   Serial.print("WiFi connecting");
  121.   while (WiFi.status() != WL_CONNECTED) {
  122.     delay(500);
  123.     Serial.print(".");
  124.   }
  125.   Serial.println("");
  126.   Serial.println("WiFi connected");
  127.   //startCameraServer();
  128.   //Serial.print("Camera Ready! Use 'http://");
  129.   Serial.print(WiFi.localIP());
  130.   Serial.println("' to connect");
  131. }
  132. void loop() {
  133.   static unsigned long lastSendTime = 0;
  134.   if (millis() - lastSendTime >= 30000) { // 每30秒发送一次
  135.     lastSendTime = millis();
  136.    
  137.     camera_fb_t *fb = esp_camera_fb_get();
  138.     if (!fb) {
  139.       Serial.println("Camera capture failed");
  140.       return;
  141.     }
  142.    
  143.     WiFiClient client;
  144.     if (!client.connect(pc_ip, pc_port)) {
  145.       Serial.println("Failed to connect to PC");
  146.       esp_camera_fb_return(fb);
  147.       return;
  148.     }
  149.    
  150.     // 发送图像长度(4字节大端序)
  151.     uint32_t len = fb->len;
  152.     uint8_t lenBytes[4];
  153.     lenBytes[0] = (len >> 24) & 0xFF;
  154.     lenBytes[1] = (len >> 16) & 0xFF;
  155.     lenBytes[2] = (len >> 8) & 0xFF;
  156.     lenBytes[3] = len & 0xFF;
  157.     client.write(lenBytes, 4);
  158.    
  159.     // 发送图像数据
  160.     client.write(fb->buf, fb->len);
  161.     Serial.printf("Sent %d bytes\n", fb->len);
  162.    
  163.     client.stop();
  164.     esp_camera_fb_return(fb);
  165.   }
  166.   delay(1000); // 减少循环频率
  167. }
复制代码
电脑端Python程序
  1. import socket
  2. import struct
  3. import cv2
  4. import numpy as np
  5. import os
  6. import base64
  7. from openai import OpenAI
  8. from df_xfyun_speech import XfTts
  9. import pygame
  10. pygame.mixer.init()
  11. HOST = '0.0.0.0'  # 监听所有网络接口
  12. PORT = 12345      # 与ESP32程序相同的端口
  13. appId = "f6e50ac1"
  14. apiKey ="11ecfe2c70c42cadda1bae3916576ec4"
  15. apiSecret = "NGQwMjdiYTY0ZGZkZDI5MTJlMGQ4Yzhk"
  16. options = {}
  17. tts = XfTts(appId, apiKey, apiSecret, options)
  18. client = OpenAI(
  19.     api_key="sk-l77I27a8yfbu1hie5jj9FJOIexixS1RRdLFVEv2xbRiFsb3k",
  20.     base_url="https://api.moonshot.cn/v1",)
  21. def ai_image(image_data):
  22.     cv2.imwrite("Mind+.png", image_data)
  23.     with open("Mind+.png", "rb") as f:
  24.         image_data = f.read()
  25.     # 按下 'q' 键退出
  26.     if cv2.waitKey(1) & 0xFF == ord('q'):
  27.             pass
  28.     # 我们使用标准库 base64.b64encode 函数将图片编码成 base64 格式的 image_url
  29.     image_url = f"data:image/{os.path.splitext(image_data)[1]};base64,{base64.b64encode(image_data).decode('utf-8')}"
  30.     completion = client.chat.completions.create(
  31.         model="moonshot-v1-8k-vision-preview",
  32.         messages=[
  33.             {"role": "system", "content": "你是 Kimi。"},
  34.             {
  35.                 "role": "user",
  36.                 # 注意这里,content 由原来的 str 类型变更为一个 list,这个 list 中包含多个部分的内容,图片(image_url)是一个部分(part),
  37.                 # 文字(text)是一个部分(part)
  38.                 "content": [
  39.                     {
  40.                         "type": "image_url", # <-- 使用 image_url 类型来上传图片,内容为使用 base64 编码过的图片内容
  41.                         "image_url": {
  42.                             "url": image_url,
  43.                         },
  44.                     },
  45.                     {
  46.                         "type": "text",
  47.                         "text": "请描述图片的内容。", # <-- 使用 text 类型来提供文字指令,例如“描述图片内容”
  48.                     },
  49.                 ],
  50.             },
  51.         ],
  52.     )
  53.     message=completion.choices[0].message.content
  54.     print(message)
  55.     tts.synthesis(message, "speech.wav")
  56.     pygame.mixer.Sound("speech.wav").play()
  57. def show_image(data):
  58.     try:
  59.         # 将字节数据转换为numpy数组
  60.         img_array = np.frombuffer(data, dtype=np.uint8)
  61.         # 解码JPEG图像
  62.         img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
  63.         
  64.         if img is not None:
  65.             # 显示图像
  66.             cv2.imshow('ESP32 Camera Stream', img)
  67.             ai_image(img)
  68.             # 按'q'键退出
  69.             if cv2.waitKey(1) & 0xFF == ord('q'):
  70.                 return False
  71.             return True
  72.         return False
  73.     except Exception as e:
  74.         print(f"图像显示错误: {str(e)}")
  75.         return False
  76. def main():
  77.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  78.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  79.         s.bind((HOST, PORT))
  80.         s.listen(1)
  81.         print(f"等待ESP32连接在 {PORT} 端口...")
  82.         
  83.         while True:
  84.             conn, addr = s.accept()
  85.             print(f"已连接: {addr}")
  86.             
  87.             with conn:
  88.                 while True:
  89.                     # 接收图像长度头
  90.                     len_data = conn.recv(4)
  91.                     if not len_data:
  92.                         break
  93.                     
  94.                     # 解析图像长度
  95.                     if len(len_data) != 4:
  96.                         print("无效的长度头")
  97.                         break
  98.                     img_len = struct.unpack('>I', len_data)[0]
  99.                     
  100.                     # 接收图像数据
  101.                     received = 0
  102.                     img_data = b''
  103.                     while received < img_len:
  104.                         chunk = conn.recv(min(img_len - received, 4096))
  105.                         if not chunk:
  106.                             break
  107.                         img_data += chunk
  108.                         received += len(chunk)
  109.                     
  110.                     # 显示图像
  111.                     if received == img_len:
  112.                         if not show_image(img_data):
  113.                             break  # 用户按q键退出
  114.                     else:
  115.                         print(f"数据不完整: 期望 {img_len} 字节,收到 {received} 字节")
  116.                         break
  117.                
  118.                
  119.                 print("连接关闭")
  120. if __name__ == "__main__":
  121.     try:
  122.         main()
  123.     except KeyboardInterrupt:
  124.         print("程序终止")
  125.     finally:
  126.         cv2.destroyAllWindows()
复制代码



【硬件设计】
“ESP32-S3 AI智能摄像头”接入Kimi图像理解——盲人AI眼镜图2


“ESP32-S3 AI智能摄像头”接入Kimi图像理解——盲人AI眼镜图3


“ESP32-S3 AI智能摄像头”接入Kimi图像理解——盲人AI眼镜图4


“ESP32-S3 AI智能摄像头”接入Kimi图像理解——盲人AI眼镜图5



【演示视频】




木子哦  管理员

发表于 3 天前

哇哇哇哇
回复

使用道具 举报

glwz007  初级技匠

发表于 8 小时前

膜拜大神!感谢大神分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail