♻️ app_common -> who_ai_utils

pull/190/head
Ye Hang Yang 2021-09-10 10:20:58 +08:00
parent 9f86332250
commit 1404385bc2
3 changed files with 112 additions and 120 deletions

View File

@ -1,102 +0,0 @@
#include "app_common.hpp"
#include "esp_log.h"
#include "esp_camera.h"
#include "dl_image.hpp"
void draw_detection_result(uint16_t *image_ptr, int image_height, int image_width, std::list<dl::detect::result_t> &results)
{
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results.begin(); prediction != results.end(); prediction++, i++)
{
dl::image::draw_hollow_rectangle(image_ptr, image_height, image_width,
DL_MAX(prediction->box[0], 0),
DL_MAX(prediction->box[1], 0),
DL_MAX(prediction->box[2], 0),
DL_MAX(prediction->box[3], 0),
COLOR_GREEN);
#if CONFIG_DL_HUMAN_FACE_DETECTION_S2_ENABLED
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[0], 0), DL_MAX(prediction->keypoint[1], 0), 4, COLOR_RED); // left eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[2], 0), DL_MAX(prediction->keypoint[3], 0), 4, COLOR_RED); // mouth left corner
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[4], 0), DL_MAX(prediction->keypoint[5], 0), 4, COLOR_GREEN); // nose
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[6], 0), DL_MAX(prediction->keypoint[7], 0), 4, COLOR_BLUE); // right eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[8], 0), DL_MAX(prediction->keypoint[9], 0), 4, COLOR_BLUE); // mouth right corner
#endif
}
}
void draw_detection_result(uint8_t *image_ptr, int image_height, int image_width, std::list<dl::detect::result_t> &results)
{
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results.begin(); prediction != results.end(); prediction++, i++)
{
dl::image::draw_hollow_rectangle(image_ptr, image_height, image_width,
DL_MAX(prediction->box[0], 0),
DL_MAX(prediction->box[1], 0),
DL_MAX(prediction->box[2], 0),
DL_MAX(prediction->box[3], 0),
COLOR_GREEN);
#if CONFIG_DL_HUMAN_FACE_DETECTION_S2_ENABLED
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[0], 0), DL_MAX(prediction->keypoint[1], 0), 4, COLOR_RED); // left eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[2], 0), DL_MAX(prediction->keypoint[3], 0), 4, COLOR_RED); // mouth left corner
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[4], 0), DL_MAX(prediction->keypoint[5], 0), 4, COLOR_GREEN); // nose
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[6], 0), DL_MAX(prediction->keypoint[7], 0), 4, COLOR_BLUE); // right eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[8], 0), DL_MAX(prediction->keypoint[9], 0), 4, COLOR_BLUE); // mouth right corner
#endif
}
}
void print_detection_result(std::list<dl::detect::result_t> &results)
{
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results.begin(); prediction != results.end(); prediction++, i++)
{
ESP_LOGI("detection_result", "[%d]: (%3d, %3d, %3d, %3d)"
#if CONFIG_DL_HUMAN_FACE_DETECTION_S2_ENABLED
" | left eye: (%3d, %3d), right eye: (%3d, %3d), nose: (%3d, %3d), mouth left: (%3d, %3d), mouth right: (%3d, %3d)"
#endif
,
i,
prediction->box[0], prediction->box[1], prediction->box[2], prediction->box[3]
#if CONFIG_DL_HUMAN_FACE_DETECTION_S2_ENABLED
,
prediction->keypoint[0], prediction->keypoint[1], // left eye
prediction->keypoint[6], prediction->keypoint[7], // right eye
prediction->keypoint[4], prediction->keypoint[5], // nose
prediction->keypoint[2], prediction->keypoint[3], // mouth left corner
prediction->keypoint[8], prediction->keypoint[9]
#endif
);
}
}
void *app_camera_decode(camera_fb_t *fb)
{
if (fb->format == PIXFORMAT_RGB565)
{
return (void *)fb->buf;
}
else
{
uint8_t *image_ptr = (uint8_t *)malloc(fb->height * fb->width * 3 * sizeof(uint8_t));
if (image_ptr)
{
if (fmt2rgb888(fb->buf, fb->len, fb->format, image_ptr))
{
return (void *)image_ptr;
}
else
{
ESP_LOGE(TAG, "fmt2rgb888 failed");
dl::tool::free_aligned(image_ptr);
}
}
else
{
ESP_LOGE(TAG, "malloc memory for image rgb888 failed");
}
}
return NULL;
}

View File

@ -0,0 +1,112 @@
#include "who_ai_utils.hpp"
#include "esp_log.h"
#include "esp_camera.h"
#include "dl_image.hpp"
static const char *TAG = "ai_utils";
// +-------+--------------------+----------+
// | | RGB565 | RGB888 |
// +=======+====================+==========+
// | Red | 0b0000000011111000 | 0x0000FF |
// +-------+--------------------+----------+
// | Green | 0b1110000000000111 | 0x00FF00 |
// +-------+--------------------+----------+
// | Blue | 0b0001111100000000 | 0xFF0000 |
// +-------+--------------------+----------+
void draw_detection_result(uint16_t *image_ptr, int image_height, int image_width, std::list<dl::detect::result_t> &results)
{
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results.begin(); prediction != results.end(); prediction++, i++)
{
dl::image::draw_hollow_rectangle(image_ptr, image_height, image_width,
DL_MAX(prediction->box[0], 0),
DL_MAX(prediction->box[1], 0),
DL_MAX(prediction->box[2], 0),
DL_MAX(prediction->box[3], 0),
0b1110000000000111);
if (prediction->keypoint.size() == 10)
{
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[0], 0), DL_MAX(prediction->keypoint[1], 0), 4, 0b0000000011111000); // left eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[2], 0), DL_MAX(prediction->keypoint[3], 0), 4, 0b0000000011111000); // mouth left corner
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[4], 0), DL_MAX(prediction->keypoint[5], 0), 4, 0b1110000000000111); // nose
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[6], 0), DL_MAX(prediction->keypoint[7], 0), 4, 0b0001111100000000); // right eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[8], 0), DL_MAX(prediction->keypoint[9], 0), 4, 0b0001111100000000); // mouth right corner
}
}
}
void draw_detection_result(uint8_t *image_ptr, int image_height, int image_width, std::list<dl::detect::result_t> &results)
{
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results.begin(); prediction != results.end(); prediction++, i++)
{
dl::image::draw_hollow_rectangle(image_ptr, image_height, image_width,
DL_MAX(prediction->box[0], 0),
DL_MAX(prediction->box[1], 0),
DL_MAX(prediction->box[2], 0),
DL_MAX(prediction->box[3], 0),
0x00FF00);
if (prediction->keypoint.size() == 10)
{
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[0], 0), DL_MAX(prediction->keypoint[1], 0), 4, 0x0000FF); // left eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[2], 0), DL_MAX(prediction->keypoint[3], 0), 4, 0x0000FF); // mouth left corner
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[4], 0), DL_MAX(prediction->keypoint[5], 0), 4, 0x00FF00); // nose
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[6], 0), DL_MAX(prediction->keypoint[7], 0), 4, 0xFF0000); // right eye
dl::image::draw_point(image_ptr, image_height, image_width, DL_MAX(prediction->keypoint[8], 0), DL_MAX(prediction->keypoint[9], 0), 4, 0xFF0000); // mouth right corner
}
}
}
void print_detection_result(std::list<dl::detect::result_t> &results)
{
int i = 0;
for (std::list<dl::detect::result_t>::iterator prediction = results.begin(); prediction != results.end(); prediction++, i++)
{
ESP_LOGI("detection_result", "[%2d]: (%3d, %3d, %3d, %3d)", i, prediction->box[0], prediction->box[1], prediction->box[2], prediction->box[3]);
if (prediction->keypoint.size() == 10)
{
ESP_LOGI("detection_result", " left eye: (%3d, %3d), right eye: (%3d, %3d), nose: (%3d, %3d), mouth left: (%3d, %3d), mouth right: (%3d, %3d)",
prediction->keypoint[0], prediction->keypoint[1], // left eye
prediction->keypoint[6], prediction->keypoint[7], // right eye
prediction->keypoint[4], prediction->keypoint[5], // nose
prediction->keypoint[2], prediction->keypoint[3], // mouth left corner
prediction->keypoint[8], prediction->keypoint[9]); // mouth right corner
}
}
}
void *app_camera_decode(camera_fb_t *fb)
{
if (fb->format == PIXFORMAT_RGB565)
{
return (void *)fb->buf;
}
else
{
uint8_t *image_ptr = (uint8_t *)malloc(fb->height * fb->width * 3 * sizeof(uint8_t));
if (image_ptr)
{
if (fmt2rgb888(fb->buf, fb->len, fb->format, image_ptr))
{
return (void *)image_ptr;
}
else
{
ESP_LOGE(TAG, "fmt2rgb888 failed");
dl::tool::free_aligned(image_ptr);
}
}
else
{
ESP_LOGE(TAG, "malloc memory for image rgb888 failed");
}
}
return NULL;
}

View File

@ -3,24 +3,6 @@
#include <list>
#include "dl_detect_define.hpp"
#include "esp_camera.h"
// #include "who_define.h"
#if CONFIG_CAMERA_PIXEL_FORMAT_RGB565
#define IMAGE_T uint16_t
#define COLOR_RED 0b0000000011111000
#define COLOR_GREEN 0b1110000000000111
#define COLOR_BLUE 0b0001111100000000
#define COLOR_BLACK 0b0000000000000000
#else
#define IMAGE_T uint8_t
#define COLOR_RED 0x0000FF
#define COLOR_GREEN 0x00FF00
#define COLOR_BLUE 0xFF0000
#define COLOR_BLACK 0x000000
#endif
static const char *TAG = "app_common";
/**
* @brief Draw detection result on RGB565 image.