Skip to content

Commit cc07ef4

Browse files
authored
Revert "camera 优化:在原有的RGB565处理下,容易超时改为JPEG格式 (#1029)" (#1085)
This reverts commit d6b1414.
1 parent cf4afde commit cc07ef4

File tree

3 files changed

+30
-78
lines changed

3 files changed

+30
-78
lines changed

main/boards/bread-compact-wifi-s3cam/compact_wifi_board_s3cam.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ class CompactWifiBoardS3Cam : public WifiBoard {
158158
config.pin_pwdn = CAMERA_PIN_PWDN;
159159
config.pin_reset = CAMERA_PIN_RESET;
160160
config.xclk_freq_hz = XCLK_FREQ_HZ;
161-
config.pixel_format = PIXFORMAT_JPEG;
161+
config.pixel_format = PIXFORMAT_RGB565;
162162
config.frame_size = FRAMESIZE_QVGA;
163-
config.jpeg_quality = 10;
163+
config.jpeg_quality = 12;
164164
config.fb_count = 1;
165165
config.fb_location = CAMERA_FB_IN_PSRAM;
166166
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;

main/boards/bread-compact-wifi-s3cam/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#define CAMERA_PIN_SIOD GPIO_NUM_4
5252
#define CAMERA_PIN_PWDN GPIO_NUM_NC
5353
#define CAMERA_PIN_RESET GPIO_NUM_NC
54-
#define XCLK_FREQ_HZ 50000000
54+
#define XCLK_FREQ_HZ 20000000
5555

5656

5757
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_38

main/boards/common/esp32_camera.cc

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ Esp32Camera::Esp32Camera(const camera_config_t& config) {
6565
ESP_LOGE(TAG, "Failed to allocate memory for preview image");
6666
return;
6767
}
68-
6968
}
7069

7170
Esp32Camera::~Esp32Camera() {
@@ -116,27 +115,14 @@ bool Esp32Camera::Capture() {
116115
// 显示预览图片
117116
auto display = Board::GetInstance().GetDisplay();
118117
if (display != nullptr) {
119-
// 检查摄像头输出格式
120-
if (fb_->format == PIXFORMAT_JPEG) {
121-
// JPEG格式需要先解码为RGB565
122-
// 分配足够的缓冲区用于RGB565数据
123-
bool converted = jpg2rgb565(fb_->buf, fb_->len, preview_image_.data, JPG_SCALE_NONE);
124-
if (converted) {
125-
display->SetPreviewImage(&preview_image_);
126-
} else {
127-
ESP_LOGE(TAG, "Failed to convert JPEG to RGB565 for preview");
128-
}
129-
} else {
130-
// RGB565等格式需要进行字节交换处理
131-
auto src = (uint16_t*)fb_->buf;
132-
auto dst = (uint16_t*)preview_image_.data;
133-
size_t pixel_count = fb_->len / 2;
134-
for (size_t i = 0; i < pixel_count; i++) {
135-
// 交换每个16位字内的字节
136-
dst[i] = __builtin_bswap16(src[i]);
137-
}
138-
display->SetPreviewImage(&preview_image_);
118+
auto src = (uint16_t*)fb_->buf;
119+
auto dst = (uint16_t*)preview_image_.data;
120+
size_t pixel_count = fb_->len / 2;
121+
for (size_t i = 0; i < pixel_count; i++) {
122+
// 交换每个16位字内的字节
123+
dst[i] = __builtin_bswap16(src[i]);
139124
}
125+
display->SetPreviewImage(&preview_image_);
140126
}
141127
return true;
142128
}
@@ -202,56 +188,26 @@ std::string Esp32Camera::Explain(const std::string& question) {
202188
return "{\"success\": false, \"message\": \"Image explain URL or token is not set\"}";
203189
}
204190

205-
QueueHandle_t jpeg_queue = nullptr;
206-
// If the format is not JPEG, we need to convert it to JPEG first
207-
if (fb_->format != PIXFORMAT_JPEG) {
208-
// 创建局部的 JPEG 队列, 40 entries is about to store 512 * 40 = 20480 bytes of JPEG data
209-
jpeg_queue = xQueueCreate(40, sizeof(JpegChunk));
210-
if (jpeg_queue == nullptr) {
211-
ESP_LOGE(TAG, "Failed to create JPEG queue");
212-
return "{\"success\": false, \"message\": \"Failed to create JPEG queue\"}";
213-
}
191+
// 创建局部的 JPEG 队列, 40 entries is about to store 512 * 40 = 20480 bytes of JPEG data
192+
QueueHandle_t jpeg_queue = xQueueCreate(40, sizeof(JpegChunk));
193+
if (jpeg_queue == nullptr) {
194+
ESP_LOGE(TAG, "Failed to create JPEG queue");
195+
return "{\"success\": false, \"message\": \"Failed to create JPEG queue\"}";
196+
}
214197

215-
// We spawn a thread to encode the image to JPEG
216-
encoder_thread_ = std::thread([this, jpeg_queue]() {
217-
frame2jpg_cb(fb_, 80, [](void* arg, size_t index, const void* data, size_t len) -> unsigned int {
218-
auto jpeg_queue = (QueueHandle_t)arg;
219-
JpegChunk chunk = {
220-
.data = (uint8_t*)heap_caps_aligned_alloc(16, len, MALLOC_CAP_SPIRAM),
221-
.len = len
222-
};
223-
if (chunk.data != nullptr) {
224-
memcpy(chunk.data, data, len);
225-
xQueueSend(jpeg_queue, &chunk, portMAX_DELAY);
226-
}
227-
return len;
228-
}, jpeg_queue);
229-
// Add a null chunk to indicate the end of the queue
230-
JpegChunk null_chunk = { .data = nullptr, .len = 0 };
231-
xQueueSend(jpeg_queue, &null_chunk, portMAX_DELAY);
232-
});
233-
} else {
234-
// JPEG format, we can send it directly
235-
// To keep the logic consistent, we still use a queue to send the data
236-
jpeg_queue = xQueueCreate(2, sizeof(JpegChunk));
237-
if (jpeg_queue == nullptr) {
238-
ESP_LOGE(TAG, "Failed to create JPEG queue");
239-
return "{\"success\": false, \"message\": \"Failed to create JPEG queue\"}";
240-
}
241-
JpegChunk chunk = {
242-
.data = (uint8_t*)heap_caps_aligned_alloc(16, fb_->len, MALLOC_CAP_SPIRAM),
243-
.len = fb_->len
244-
};
245-
if (chunk.data != nullptr) {
246-
memcpy(chunk.data, fb_->buf, fb_->len);
198+
// We spawn a thread to encode the image to JPEG
199+
encoder_thread_ = std::thread([this, jpeg_queue]() {
200+
frame2jpg_cb(fb_, 80, [](void* arg, size_t index, const void* data, size_t len) -> unsigned int {
201+
auto jpeg_queue = (QueueHandle_t)arg;
202+
JpegChunk chunk = {
203+
.data = (uint8_t*)heap_caps_aligned_alloc(16, len, MALLOC_CAP_SPIRAM),
204+
.len = len
205+
};
206+
memcpy(chunk.data, data, len);
247207
xQueueSend(jpeg_queue, &chunk, portMAX_DELAY);
248-
} else {
249-
ESP_LOGE(TAG, "Failed to allocate memory for JPEG chunk");
250-
}
251-
// Add a null chunk to indicate the end of the queue
252-
JpegChunk null_chunk = { .data = nullptr, .len = 0 };
253-
xQueueSend(jpeg_queue, &null_chunk, portMAX_DELAY);
254-
}
208+
return len;
209+
}, jpeg_queue);
210+
});
255211

256212
auto network = Board::GetInstance().GetNetwork();
257213
auto http = network->CreateHttp(3);
@@ -269,9 +225,7 @@ std::string Esp32Camera::Explain(const std::string& question) {
269225
if (!http->Open("POST", explain_url_)) {
270226
ESP_LOGE(TAG, "Failed to connect to explain URL");
271227
// Clear the queue
272-
if (encoder_thread_.joinable()) {
273-
encoder_thread_.join();
274-
}
228+
encoder_thread_.join();
275229
JpegChunk chunk;
276230
while (xQueueReceive(jpeg_queue, &chunk, portMAX_DELAY) == pdPASS) {
277231
if (chunk.data != nullptr) {
@@ -319,9 +273,7 @@ std::string Esp32Camera::Explain(const std::string& question) {
319273
heap_caps_free(chunk.data);
320274
}
321275
// Wait for the encoder thread to finish
322-
if (encoder_thread_.joinable()) {
323-
encoder_thread_.join();
324-
}
276+
encoder_thread_.join();
325277
// 清理队列
326278
vQueueDelete(jpeg_queue);
327279

0 commit comments

Comments
 (0)