Skip to content

Commit c3fef2e

Browse files
committed
5.5
1 parent 8a77d9a commit c3fef2e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+19481
-48
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <freertos/FreeRTOS.h>
2+
#include <freertos/task.h>
3+
#include <freertos/queue.h>
4+
#include <list>
5+
6+
extern "C"
7+
{
8+
#include "esp_config.h"
9+
#include "esp_log.h"
10+
#include "py/obj.h"
11+
#include "py/runtime.h"
12+
#include "py/builtin.h"
13+
#include "py/mphal.h"
14+
#include "esp_camera.h"
15+
#include "fb_gfx.h"
16+
#include "img_converters.h" // 包含图像转换的库
17+
#include "extmod/modmachine.h"
18+
}
19+
20+
#include "who_ai_utils.hpp"
21+
#include "dl_image.hpp"
22+
#include "cat_face_detect_mn03.hpp"
23+
24+
// 定义队列的数据结构
25+
static struct {
26+
int num;
27+
int x;
28+
int y;
29+
} cat_result = {0, 0, 0};
30+
31+
static TaskHandle_t cat_task_handle = NULL;
32+
static SemaphoreHandle_t cat_mutex = NULL;
33+
34+
// 猫脸识别任务
35+
void cat_detection_task(void *pvParameters) {
36+
CatFaceDetectMN03 detector(0.4F, 0.3F, 10, 0.3F);
37+
while (true) {
38+
camera_fb_t *frame = esp_camera_fb_get();
39+
if (frame != NULL) {
40+
std::list<dl::detect::result_t> &detect_results = detector.infer((uint16_t *)frame->buf, {(int)frame->height, (int)frame->width, 3});
41+
cat_result.x = 0;
42+
cat_result.y = 0;
43+
cat_result.num = detect_results.size();
44+
if (!detect_results.empty()) {
45+
auto prediction = detect_results.begin();
46+
cat_result.x = ((prediction->box[0] + prediction->box[2]) / 2) - 160;
47+
cat_result.y = 120 - ((prediction->box[1] + prediction->box[3]) / 2);
48+
}
49+
50+
esp_camera_fb_return(frame);
51+
}
52+
vTaskDelay(pdMS_TO_TICKS(50)); // 降低CPU占用
53+
}
54+
}
55+
56+
// 初始化函数
57+
mp_obj_t esp_cat_detection_init(void) {
58+
if (cat_task_handle == NULL) {
59+
xTaskCreate(cat_detection_task, "cat_detection_task", 10 * 1024, NULL, 10, &cat_task_handle);
60+
}
61+
return mp_const_none;
62+
}
63+
64+
// 反初始化函数
65+
mp_obj_t esp_cat_detection_deinit(void) {
66+
if (cat_task_handle != NULL) {
67+
vTaskDelete(cat_task_handle);
68+
cat_task_handle = NULL;
69+
}
70+
return mp_const_none;
71+
}
72+
73+
// Python接口:获取最新的人脸检测结果
74+
mp_obj_t esp_cat_detection(void) {
75+
// xSemaphoreTake(cat_mutex, portMAX_DELAY);
76+
mp_obj_t elements[] = {
77+
mp_obj_new_int(cat_result.num),
78+
mp_obj_new_int(cat_result.x),
79+
mp_obj_new_int(cat_result.y),
80+
};
81+
return mp_obj_new_list(3, elements);
82+
}
83+
84+

examples/usercmodule/BSP/Image-Recognition/esp_color_detection.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ static void color_recognition_task(void *arg) {
116116
for (int j = 0; j < results[i].size(); ++j) {
117117
int x = results[i][j].center[0];
118118
int y = results[i][j].center[1];
119-
color_result.x = x;
120-
color_result.y = y;
119+
color_result.x = x - 160;
120+
color_result.y = 120 - y;
121121
}
122122
}
123123
}
@@ -156,7 +156,8 @@ mp_obj_t esp_color_discern(void) {
156156

157157
// 返回颜色位置
158158
mp_obj_t elements[] = {x, y};
159-
return mp_obj_new_list(2, elements);}
159+
return mp_obj_new_list(2, elements);
160+
}
160161

161162
mp_obj_t esp_color_deinit(void) {
162163
if (color_task_handle != NULL) {
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
extern "C"
2+
{
3+
#include "esp_config.h"
4+
#include "esp_log.h"
5+
#include "py/obj.h"
6+
#include "py/runtime.h"
7+
#include "py/builtin.h"
8+
#include "py/mphal.h"
9+
#include "esp_camera.h"
10+
#include "extmod/modmachine.h"
11+
}
12+
13+
#include <freertos/FreeRTOS.h>
14+
#include <freertos/task.h>
15+
#include <freertos/queue.h>
16+
#include <list>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
#include <stdint.h>
20+
#include <math.h>
21+
#include <vector>
22+
23+
24+
typedef struct {
25+
int r;
26+
int g;
27+
int b;
28+
} rgb_result_t;
29+
30+
// 识别区域结构体
31+
typedef struct {
32+
int x;
33+
int y;
34+
int w;
35+
int h;
36+
} region_t;
37+
38+
static TaskHandle_t color_task_handle = NULL;
39+
static SemaphoreHandle_t color_mutex = NULL;
40+
41+
static rgb_result_t color_result = {0, 0, 0};
42+
static region_t detect_region = {0, 0, 40, 40};
43+
44+
static void color_recognition_task(void *arg) {
45+
while (1) {
46+
camera_fb_t *camera_frame = esp_camera_fb_get();
47+
if (camera_frame == NULL) {
48+
vTaskDelay(pdMS_TO_TICKS(50));
49+
continue;
50+
}
51+
52+
int img_width = camera_frame->width;
53+
int img_height = camera_frame->height;
54+
55+
region_t region;
56+
xSemaphoreTake(color_mutex, portMAX_DELAY);
57+
region = detect_region;
58+
xSemaphoreGive(color_mutex);
59+
60+
int start_x = region.x;
61+
int start_y = region.y;
62+
int region_w = region.w;
63+
int region_h = region.h;
64+
// 限制区域合法
65+
if (start_x < 0) start_x = 0;
66+
if (start_y < 0) start_y = 0;
67+
if (start_x + region_w > img_width) region_w = img_width - start_x;
68+
if (start_y + region_h > img_height) region_h = img_height - start_y;
69+
70+
uint16_t *pixels = (uint16_t *)camera_frame->buf;
71+
72+
uint8_t max_r = 0, max_g = 0, max_b = 0;
73+
74+
for (int y = start_y; y < start_y + region_h; ++y) {
75+
for (int x = start_x; x < start_x + region_w; ++x) {
76+
int index = y * img_width + x;
77+
uint16_t pixel = pixels[index];
78+
79+
uint8_t r = ((pixel >> 11) & 0x1F) << 3;
80+
uint8_t g = ((pixel >> 5) & 0x3F) << 2;
81+
uint8_t b = (pixel & 0x1F) << 3;
82+
83+
if (r > max_r) max_r = r;
84+
if (g > max_g) max_g = g;
85+
if (b > max_b) max_b = b;
86+
}
87+
}
88+
89+
rgb_result_t temp_result = {max_r, max_g, max_b};
90+
91+
xSemaphoreTake(color_mutex, portMAX_DELAY);
92+
color_result = temp_result;
93+
xSemaphoreGive(color_mutex);
94+
95+
esp_camera_fb_return(camera_frame);
96+
vTaskDelay(pdMS_TO_TICKS(100));
97+
98+
}
99+
}
100+
101+
mp_obj_t esp_color_rgb_init(void) {
102+
if (color_mutex == NULL) {
103+
color_mutex = xSemaphoreCreateMutex();
104+
}
105+
if (color_task_handle == NULL) {
106+
xTaskCreate(color_recognition_task, "color_recognition_task", 6 * 1024, NULL, 2, &color_task_handle);
107+
}
108+
return mp_const_none;
109+
}
110+
111+
mp_obj_t esp_color_rgb(void) {
112+
mp_obj_t r, g, b;
113+
xSemaphoreTake(color_mutex, portMAX_DELAY);
114+
r = mp_obj_new_int(color_result.r);
115+
g = mp_obj_new_int(color_result.g);
116+
b = mp_obj_new_int(color_result.b);
117+
xSemaphoreGive(color_mutex);
118+
119+
mp_obj_t rgb_list[] = {r, g, b};
120+
return mp_obj_new_list(3, rgb_list);
121+
}
122+
123+
// 设置区域接口:传入 x, y, w, h
124+
mp_obj_t esp_color_set_region(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t w_obj, mp_obj_t h_obj) {
125+
int x = mp_obj_get_int(x_obj);
126+
int y = mp_obj_get_int(y_obj);
127+
int w = mp_obj_get_int(w_obj);
128+
int h = mp_obj_get_int(h_obj);
129+
130+
xSemaphoreTake(color_mutex, portMAX_DELAY);
131+
detect_region.x = x;
132+
detect_region.y = y;
133+
detect_region.w = w;
134+
detect_region.h = h;
135+
xSemaphoreGive(color_mutex);
136+
137+
return mp_const_none;
138+
}
139+
140+
mp_obj_t esp_color_rgb_deinit(void) {
141+
if (color_task_handle != NULL) {
142+
vTaskDelete(color_task_handle);
143+
color_task_handle = NULL;
144+
}
145+
if (color_mutex != NULL) {
146+
vSemaphoreDelete(color_mutex);
147+
color_mutex = NULL;
148+
}
149+
return mp_const_none;
150+
}

0 commit comments

Comments
 (0)