Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:78 Xiaozhi esp32 Camera Drivers

From Leeroopedia
Knowledge Sources
Domains Camera, Embedded_Systems
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete camera drivers for XiaoZhi voice assistant devices, providing Esp32Camera for ESP32-S3 using the esp_camera component and EspVideo for ESP32-P4 using the V4L2-based esp_video subsystem, both implementing the Camera interface with JPEG encoding and AI explain capabilities.

Description

Two camera drivers under main/boards/common/ implement the abstract Camera interface:

  • Esp32Camera (esp32_camera.h/.cc, 389 lines) — ESP32-S3 camera using the esp_camera component. Captures frames from DVP/SCI sensors, supports horizontal/vertical mirror and byte swap for RGB565. Runs a background encoder thread that converts captured frames to JPEG via image_to_jpeg. Supports Explain() which sends the captured JPEG to an AI explain endpoint with a user question.
  • EspVideo (esp_video.h/.cc, 1127 lines) — ESP32-P4 camera using the V4L2-based esp_video_init subsystem. Supports MIPI-CSI sensors with mmap'd V4L2 buffers. Handles multiple pixel formats (v4l2_pix_fmt_t), optional 90-degree rotation via CONFIG_XIAOZHI_ENABLE_ROTATE_CAMERA_IMAGE. Also converts to JPEG and supports Explain().

Both drivers:

  • Store the current frame buffer (camera_fb_t or FrameBuffer) for LVGL preview and JPEG encoding
  • Run JPEG encoding on a separate std::thread to avoid blocking the main loop
  • Accept an explain URL and token for AI vision queries via SetExplainUrl()

Usage

Reference this implementation when:

  • Adding a board with a camera (ESP32-S3 or ESP32-P4)
  • Understanding camera capture, mirror/flip, and JPEG encoding
  • Implementing AI vision explain functionality

Code Reference

Source Location

Signature

// ESP32-S3 camera (esp_camera component)
class Esp32Camera : public Camera {
public:
    Esp32Camera(const camera_config_t& config);
    ~Esp32Camera();
    virtual void SetExplainUrl(const std::string& url, const std::string& token) override;
    virtual bool Capture() override;
    virtual bool SetHMirror(bool enabled) override;
    virtual bool SetVFlip(bool enabled) override;
    virtual bool SetSwapBytes(bool enabled) override;
    virtual std::expected<std::string, std::string> Explain(const std::string& question) override;
};

// ESP32-P4 camera (V4L2 esp_video subsystem)
class EspVideo : public Camera {
public:
    EspVideo(const esp_video_init_config_t& config);
    ~EspVideo() override;
    virtual void SetExplainUrl(const std::string& url, const std::string& token);
    virtual bool Capture();
    virtual bool SetHMirror(bool enabled) override;
    virtual bool SetVFlip(bool enabled) override;
    virtual std::expected<std::string, std::string> Explain(const std::string& question) override;
};

Import

#include "esp32_camera.h"  // Esp32Camera (ESP32-S3)
#include "esp_video.h"     // EspVideo (ESP32-P4)

I/O Contract

Inputs

Name Type Required Description
config camera_config_t / esp_video_init_config_t Yes Camera configuration (pins, format, frequency)
question (Explain) std::string Yes Natural language question about the captured image
url/token (SetExplainUrl) std::string Yes AI explain endpoint URL and auth token
enabled (SetHMirror/SetVFlip) bool Yes Enable/disable mirror or flip

Outputs

Name Type Description
Capture() bool True if frame captured successfully
Explain() std::expected<string, string> AI response or error message
JPEG uint8_t* Encoded JPEG of current frame for protocol transmission

Usage Examples

ESP32-S3 Camera

#include "esp32_camera.h"

camera_config_t config = {
    .pin_d0 = CONFIG_D0, .pin_d1 = CONFIG_D1, /* ... */
    .xclk_freq_hz = 20000000,
    .pixel_format = PIXFORMAT_RGB565,
    .frame_size = FRAMESIZE_QVGA,
    .jpeg_quality = 12,
};
auto camera = std::make_unique<Esp32Camera>(config);
camera->SetSwapBytes(true);  // for LVGL RGB565
Board::GetInstance()->SetCamera(std::move(camera));

ESP32-P4 Camera with Explain

#include "esp_video.h"

esp_video_init_config_t config = { /* MIPI-CSI config */ };
auto camera = std::make_unique<EspVideo>(config);
camera->SetExplainUrl("https://api.example.com/explain", "Bearer token");
if (camera->Capture()) {
    auto result = camera->Explain("What do you see?");
    if (result.has_value()) {
        ESP_LOGI("Camera", "AI says: %s", result->c_str());
    }
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment