esp-who/components/modules/ai/who_cat_face_detection.cpp

87 lines
2.4 KiB
C++

#include "who_cat_face_detection.hpp"
#include "esp_log.h"
#include "esp_camera.h"
#include "dl_image.hpp"
#include "cat_face_detect_mn03.hpp"
#include "who_ai_utils.hpp"
static const char *TAG = "cat_face_detection";
static QueueHandle_t xQueueFrameI = NULL;
static QueueHandle_t xQueueEvent = NULL;
static QueueHandle_t xQueueFrameO = NULL;
static QueueHandle_t xQueueResult = NULL;
static bool gEvent = true;
static bool gReturnFB = true;
static void task_process_handler(void *arg)
{
camera_fb_t *frame = NULL;
CatFaceDetectMN03 detector(0.4F, 0.3F, 10, 0.3F);
while (true)
{
if (gEvent)
{
bool is_detected = false;
if (xQueueReceive(xQueueFrameI, &frame, portMAX_DELAY))
{
std::list<dl::detect::result_t> &detect_results = detector.infer((uint16_t *)frame->buf, {(int)frame->height, (int)frame->width, 3});
if (detect_results.size() > 0)
{
draw_detection_result((uint16_t *)frame->buf, frame->height, frame->width, detect_results);
print_detection_result(detect_results);
is_detected = true;
}
}
if (xQueueFrameO)
{
xQueueSend(xQueueFrameO, &frame, portMAX_DELAY);
}
else if (gReturnFB)
{
esp_camera_fb_return(frame);
}
else
{
free(frame);
}
if (xQueueResult)
{
xQueueSend(xQueueResult, &is_detected, portMAX_DELAY);
}
}
}
}
static void task_event_handler(void *arg)
{
while (true)
{
xQueueReceive(xQueueEvent, &(gEvent), portMAX_DELAY);
}
}
void register_cat_face_detection(const QueueHandle_t frame_i,
const QueueHandle_t event,
const QueueHandle_t result,
const QueueHandle_t frame_o,
const bool camera_fb_return)
{
xQueueFrameI = frame_i;
xQueueFrameO = frame_o;
xQueueEvent = event;
xQueueResult = result;
gReturnFB = camera_fb_return;
xTaskCreatePinnedToCore(task_process_handler, "cat_face_process", 3 * 1024, NULL, 5, NULL, 1);
if (xQueueEvent)
xTaskCreatePinnedToCore(task_event_handler, "cat_face_event", 1 * 1024, NULL, 5, NULL, 1);
}