# OpenCV + ESP32-CAM 方案(和刚才Edge Impulse FOMO对比)
>
> 这个方案分工:**ESP32-CAM只负责采集画面、WiFi推MJPEG流;YOLO/图像处理全部交给电脑Python+OpenCV跑**
> ✅ 优点:帧率高、可以直接跑原版YOLOv5/v8,不用受ESP32内存限制;
> ❌ 缺点:必须联网同局域网,不能断网本地推理
## 一、整体流程
ESP32-CAM(WiFi)→ HTTP MJPEG视频流 → PC OpenCV读取每一帧 → YOLO检测、画框、显示
## 二、ESP32-CAM 推流Arduino代码(直接复制烧录)
```
#include "esp_camera.h"
#include <WiFi.h>
// WiFi信息
const char* ssid = "你的WiFi名称";
const char* password = "你的WiFi密码";
// AI-Thinker ESP32-CAM 引脚
#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
WiFiServer server(80);
void setup() {
Serial.begin(115200);
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;
config.frame_size = FRAMESIZE_QVGA; // 320×240,流畅;FRAMESIZE_VGA=640×480
config.jpeg_quality = 12;
config.fb_count = 2;
// 相机初始化
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed: %d", err);
return;
}
// WiFi连接
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP地址:");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String req = client.readStringUntil('\r');
client.flush();
if (req.indexOf("/mjpeg") != -1) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: multipart/x-mixed-replace; boundary=frame");
client.println("Connection: close");
client.println();
while (client.connected()) {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) break;
client.println("--frame");
client.println("Content-Type: image/jpeg");
client.print("Content-Length: ");
client.println(fb->len);
client.println();
client.write(fb->buf, fb->len);
client.println();
esp_camera_fb_return(fb);
delay(20);
}
}
client.stop();
}
}
```
>
> 烧录后,串口会打印IP,流地址:`http://xxx.xxx.xxx.xxx/mjpeg`
## 三、PC端Python环境
```
pip install opencv-python ultralytics
```
## 四、Python OpenCV + YOLOv8 检测完整代码
```
import cv2
from ultralytics import YOLO
# 改成你ESP32-CAM的mjpeg地址
stream_url = "http://192.168.1.100/mjpeg"
cap = cv2.VideoCapture(stream_url)
model = YOLO("yolov8n.pt") # 轻量版yolov8,也可以换成自己训练的模型
while True:
ret, frame = cap.read()
if not ret:
print("读取流失败,重连")
cap.release()
cap = cv2.VideoCapture(stream_url)
continue
results = model(frame, conf=0.6)
annotated = results[0].plot()
cv2.imshow("ESP32-CAM YOLO检测", annotated)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
```
## 五、两种方案对比(你前面两个教程)
| 方案 | ESP32-CAM+Edge Impulse FOMO | ESP32-CAM+OpenCV(PC跑YOLO) |
| --- | --- | --- |
| 推理位置 | ESP32本地 | 电脑上位机 |
| 模型 | FOMO轻量化,不能原版YOLO | YOLOv5/v8任意版本,自定义训练 |
| 帧率 | 1~3FPS | 15~30FPS |
| 断网 | 可以离线运行 | 必须同WiFi局域网 |
| 难度 | EI云端标注训练,代码简单 | PC Python环境,调参灵活 |
| 内存限制 | 严格,只能96×96 | 无,电脑显卡跑 |
## 六、常见坑
1. OpenCV `VideoCapture` 读取mjpeg经常卡死:改用requests循环抓单帧(`/capture` 单图接口)稳定性更好
2. 画面卡顿:ESP32分辨率改成QVGA(320×240),不要VGA
3. 连不上流:电脑和ESP32必须同一个路由器WiFi,关闭防火墙
4. 供电:ESP32-CAM供电不稳会花屏、断流,优先独立3.3V电源
如果你想要,我给你一份**requests抓单帧版本(解决OpenCV mjpeg卡死)**的代码。
|
|