Add example files for YOLOV8's human pose recognition and image segmentation

This commit is contained in:
hqy1433
2024-08-19 11:57:53 +08:00
committed by Neucrack
parent 83f1b0479d
commit 24629722e6
13 changed files with 418 additions and 4 deletions

View File

@@ -1,11 +1,9 @@
nn_yolov8 examples based on MaixCDK
====
## only run yolov8 model
- Use `maixcdk build` to compile binary files.
- Move files and runtime libraries to device.
- Use SSH command with parameters to run programs.
` "Usage: " + std::string(argv[0]) + " mud_model_path <image_path>"; `
To use "yolov8n_pose" you can add [draw_pose](https://wiki.sipeed.com/maixpy/api/maix/nn.html#draw_pose)
To use "yolov8n_seg" you can add [draw_seg_mask](https://wiki.sipeed.com/maixpy/api/maix/nn.html#draw_seg_mask)

9
examples/nn_yolov8_pose/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
build
dist
.config.mk
.flash.conf.json
data
/CMakeLists.txt
__pycache__

View File

@@ -0,0 +1,9 @@
nn_yolov8_pose examples based on MaixCDK
====
## only run yolov8_pose model
- Use `maixcdk build` to compile binary files.
- Move files and runtime libraries to device.
- Use SSH command with parameters to run programs.
` "Usage: " + std::string(argv[0]) + " mud_model_path <image_path>"; `

View File

@@ -0,0 +1,74 @@
############### Add include ###################
list(APPEND ADD_INCLUDE "include"
)
list(APPEND ADD_PRIVATE_INCLUDE "")
###############################################
############ Add source files #################
# list(APPEND ADD_SRCS "src/main.c"
# "src/test.c"
# )
append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test2.c")
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
# FILE(GLOB EXTRA_SRC "src/*.c")
# list(APPEND ADD_SRCS ${EXTRA_SRC})
# aux_source_directory(src ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
# set(ADD_ASM_SRCS "src/asm.S")
# list(APPEND ADD_SRCS ${ADD_ASM_SRCS})
# SET_PROPERTY(SOURCE ${ADD_ASM_SRCS} PROPERTY LANGUAGE C) # set .S ASM file as C language
# SET_SOURCE_FILES_PROPERTIES(${ADD_ASM_SRCS} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D BBBBB")
###############################################
###### Add required/dependent components ######
list(APPEND ADD_REQUIREMENTS basic nn vision)
###############################################
###### Add link search path for requirements/libs ######
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
# list(APPEND ADD_REQUIREMENTS pthread m) # add system libs, pthread and math lib for example here
# set (OpenCV_DIR opencv/lib/cmake/opencv4)
# find_package(OpenCV REQUIRED)
###############################################
############ Add static libs ##################
# list(APPEND ADD_STATIC_LIB "lib/libtest.a")
###############################################
#### Add compile option for this component ####
#### Just for this component, won't affect other
#### modules, including component that depend
#### on this component
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAAA=1)
#### Add compile option for this component
#### and components depend on this component
# list(APPEND ADD_DEFINITIONS -DAAAAA222=1
# -DAAAAA333=1)
###############################################
############ Add static libs ##################
#### Update parent's variables like CMAKE_C_LINK_FLAGS
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
###############################################
######### Add files need to download #########
# list(APPEND ADD_FILE_DOWNLOADS "{
# 'url': 'https://*****/abcde.tar.xz',
# 'urls': [], # backup urls, if url failed, will try urls
# 'sites': [], # download site, user can manually download file and put it into dl_path
# 'sha256sum': '',
# 'filename': 'abcde.tar.xz',
# 'path': 'toolchains/xxxxx',
# 'check_files': []
# }"
# )
#
# then extracted file in ${DL_EXTRACTED_PATH}/toolchains/xxxxx,
# you can directly use then, for example use it in add_custom_command
##############################################
# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
register_component()

View File

View File

@@ -0,0 +1,3 @@
#pragma once

View File

@@ -0,0 +1,113 @@
#include "maix_basic.hpp"
#include "maix_vision.hpp"
#include "maix_nn_yolov8.hpp"
#include "main.h"
using namespace maix;
int _main(int argc, char *argv[])
{
log::info("Program start");
std::string model_type = "unknown";
int ret = 0;
err::Err e;
maix::image::Format img_fmt = maix::image::FMT_RGB888;
std::vector<float> mean = {};
std::vector<float> scale = {};
char tmp_chars[64] = {0};
std::string help = "Usage: " + std::string(argv[0]) + " mud_model_path <image_path>";
if (argc < 2)
{
log::info(help.c_str());
return -1;
}
const char *model_path = argv[1];
float conf_threshold = 0.5;
float iou_threshold = 0.45;
bool dual_buff = true;
nn::YOLOv8 detector("", dual_buff);
e = detector.load(model_path);
err::check_raise(e, "load model failed");
log::info("load yolov8 model %s success", model_path);
if (argc >= 3)
{
const char *img_path = argv[2];
log::info("load image now");
maix::image::Image *img = maix::image::load(img_path, img_fmt);
err::check_null_raise(img, "load image " + std::string(img_path) + " failed");
log::info("load image %s success: %s", img_path, img->to_str().c_str());
if (img->width() != detector.input_size().width() || img->height() != detector.input_size().height())
{
log::warn("image size not match model input size, will auto resize from %dx%d to %dx%d", img->width(), img->height(), detector.input_size().width(), detector.input_size().height());
}
log::info("detect now");
nn::Objects *result = detector.detect(*img, conf_threshold, iou_threshold);
if(result->size() == 0)
{
log::info("no object detected !");
}
for (auto &r : *result)
{
log::info("result: %s", r->to_str().c_str());
img->draw_rect(r->x, r->y, r->w, r->h, maix::image::Color::from_rgb(255, 0, 0));
snprintf(tmp_chars, sizeof(tmp_chars), "%s: %.2f", detector.labels[r->class_id].c_str(), r->score);
img->draw_string(r->x, r->y, detector.labels[r->class_id], maix::image::Color::from_rgb(255, 0, 0));
detector.draw_pose(*img, r->points, 4, image::COLOR_RED);
}
img->save("result.jpg");
delete result;
delete img;
}
else
{
log::info("open camera now");
maix::image::Size input_size = detector.input_size();
camera::Camera cam = camera::Camera(input_size.width(), input_size.height(), detector.input_format());
log::info("open camera success");
display::Display disp = display::Display();
while (!app::need_exit())
{
uint64_t t = time::ticks_ms();
maix::image::Image *img = cam.read();
err::check_null_raise(img, "read camera failed");
uint64_t t2 = time::ticks_ms();
nn::Objects *result = detector.detect(*img);
uint64_t t3 = time::ticks_ms();
for (auto &r : *result)
{
log::info("result: %s", r->to_str().c_str());
img->draw_rect(r->x, r->y, r->w, r->h, maix::image::Color::from_rgb(255, 0, 0));
img->draw_string(r->x, r->y, detector.labels[r->class_id], maix::image::Color::from_rgb(255, 0, 0));
detector.draw_pose(*img, r->points, 4, image::COLOR_RED);
}
disp.show(*img);
delete result;
delete img;
log::info("time: all %d ms, detect %d ms", time::ticks_ms() - t, t3 - t2);
}
}
log::info("Program exit");
return ret;
}
int main(int argc, char *argv[])
{
// Catch SIGINT signal(e.g. Ctrl + C), and set exit flag to true.
signal(SIGINT, [](int sig)
{ app::set_exit_flag(true); });
// Use CATCH_EXCEPTION_RUN_RETURN to catch exception,
// if we don't catch exception, when program throw exception, the objects will not be destructed.
// So we catch exception here to let resources be released(call objects' destructor) before exit.
CATCH_EXCEPTION_RUN_RETURN(_main, -1, argc, argv);
}

9
examples/nn_yolov8_seg/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
build
dist
.config.mk
.flash.conf.json
data
/CMakeLists.txt
__pycache__

View File

@@ -0,0 +1,9 @@
nn_yolov8_seg examples based on MaixCDK
====
## only run yolov8_seg model
- Use `maixcdk build` to compile binary files.
- Move files and runtime libraries to device.
- Use SSH command with parameters to run programs.
` "Usage: " + std::string(argv[0]) + " mud_model_path <image_path>"; `

View File

@@ -0,0 +1,74 @@
############### Add include ###################
list(APPEND ADD_INCLUDE "include"
)
list(APPEND ADD_PRIVATE_INCLUDE "")
###############################################
############ Add source files #################
# list(APPEND ADD_SRCS "src/main.c"
# "src/test.c"
# )
append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test2.c")
# FILE(GLOB_RECURSE EXTRA_SRC "src/*.c")
# FILE(GLOB EXTRA_SRC "src/*.c")
# list(APPEND ADD_SRCS ${EXTRA_SRC})
# aux_source_directory(src ADD_SRCS) # collect all source file in src dir, will set var ADD_SRCS
# append_srcs_dir(ADD_SRCS "src") # append source file in src dir to var ADD_SRCS
# list(REMOVE_ITEM COMPONENT_SRCS "src/test.c")
# set(ADD_ASM_SRCS "src/asm.S")
# list(APPEND ADD_SRCS ${ADD_ASM_SRCS})
# SET_PROPERTY(SOURCE ${ADD_ASM_SRCS} PROPERTY LANGUAGE C) # set .S ASM file as C language
# SET_SOURCE_FILES_PROPERTIES(${ADD_ASM_SRCS} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D BBBBB")
###############################################
###### Add required/dependent components ######
list(APPEND ADD_REQUIREMENTS basic nn vision)
###############################################
###### Add link search path for requirements/libs ######
# list(APPEND ADD_LINK_SEARCH_PATH "${CONFIG_TOOLCHAIN_PATH}/lib")
# list(APPEND ADD_REQUIREMENTS pthread m) # add system libs, pthread and math lib for example here
# set (OpenCV_DIR opencv/lib/cmake/opencv4)
# find_package(OpenCV REQUIRED)
###############################################
############ Add static libs ##################
# list(APPEND ADD_STATIC_LIB "lib/libtest.a")
###############################################
#### Add compile option for this component ####
#### Just for this component, won't affect other
#### modules, including component that depend
#### on this component
# list(APPEND ADD_DEFINITIONS_PRIVATE -DAAAAA=1)
#### Add compile option for this component
#### and components depend on this component
# list(APPEND ADD_DEFINITIONS -DAAAAA222=1
# -DAAAAA333=1)
###############################################
############ Add static libs ##################
#### Update parent's variables like CMAKE_C_LINK_FLAGS
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group libmaix/libtest.a -ltest2 -Wl,--end-group" PARENT_SCOPE)
###############################################
######### Add files need to download #########
# list(APPEND ADD_FILE_DOWNLOADS "{
# 'url': 'https://*****/abcde.tar.xz',
# 'urls': [], # backup urls, if url failed, will try urls
# 'sites': [], # download site, user can manually download file and put it into dl_path
# 'sha256sum': '',
# 'filename': 'abcde.tar.xz',
# 'path': 'toolchains/xxxxx',
# 'check_files': []
# }"
# )
#
# then extracted file in ${DL_EXTRACTED_PATH}/toolchains/xxxxx,
# you can directly use then, for example use it in add_custom_command
##############################################
# register component, DYNAMIC or SHARED flags will make component compiled to dynamic(shared) lib
register_component()

View File

View File

@@ -0,0 +1,3 @@
#pragma once

View File

@@ -0,0 +1,113 @@
#include "maix_basic.hpp"
#include "maix_vision.hpp"
#include "maix_nn_yolov8.hpp"
#include "main.h"
using namespace maix;
int _main(int argc, char *argv[])
{
log::info("Program start");
std::string model_type = "unknown";
int ret = 0;
err::Err e;
maix::image::Format img_fmt = maix::image::FMT_RGB888;
std::vector<float> mean = {};
std::vector<float> scale = {};
char tmp_chars[64] = {0};
std::string help = "Usage: " + std::string(argv[0]) + " mud_model_path <image_path>";
if (argc < 2)
{
log::info(help.c_str());
return -1;
}
const char *model_path = argv[1];
float conf_threshold = 0.5;
float iou_threshold = 0.45;
bool dual_buff = true;
nn::YOLOv8 detector("", dual_buff);
e = detector.load(model_path);
err::check_raise(e, "load model failed");
log::info("load yolov8 model %s success", model_path);
if (argc >= 3)
{
const char *img_path = argv[2];
log::info("load image now");
maix::image::Image *img = maix::image::load(img_path, img_fmt);
err::check_null_raise(img, "load image " + std::string(img_path) + " failed");
log::info("load image %s success: %s", img_path, img->to_str().c_str());
if (img->width() != detector.input_size().width() || img->height() != detector.input_size().height())
{
log::warn("image size not match model input size, will auto resize from %dx%d to %dx%d", img->width(), img->height(), detector.input_size().width(), detector.input_size().height());
}
log::info("detect now");
nn::Objects *result = detector.detect(*img, conf_threshold, iou_threshold);
if(result->size() == 0)
{
log::info("no object detected !");
}
for (auto &r : *result)
{
log::info("result: %s", r->to_str().c_str());
img->draw_rect(r->x, r->y, r->w, r->h, maix::image::Color::from_rgb(255, 0, 0));
snprintf(tmp_chars, sizeof(tmp_chars), "%s: %.2f", detector.labels[r->class_id].c_str(), r->score);
img->draw_string(r->x, r->y, detector.labels[r->class_id], maix::image::Color::from_rgb(255, 0, 0));
detector.draw_seg_mask(*img, r->x, r->y, *r->seg_mask);
}
img->save("result.jpg");
delete result;
delete img;
}
else
{
log::info("open camera now");
maix::image::Size input_size = detector.input_size();
camera::Camera cam = camera::Camera(input_size.width(), input_size.height(), detector.input_format());
log::info("open camera success");
display::Display disp = display::Display();
while (!app::need_exit())
{
uint64_t t = time::ticks_ms();
maix::image::Image *img = cam.read();
err::check_null_raise(img, "read camera failed");
uint64_t t2 = time::ticks_ms();
nn::Objects *result = detector.detect(*img);
uint64_t t3 = time::ticks_ms();
for (auto &r : *result)
{
log::info("result: %s", r->to_str().c_str());
img->draw_rect(r->x, r->y, r->w, r->h, maix::image::Color::from_rgb(255, 0, 0));
img->draw_string(r->x, r->y, detector.labels[r->class_id], maix::image::Color::from_rgb(255, 0, 0));
detector.draw_seg_mask(*img, r->x, r->y, *r->seg_mask);
}
disp.show(*img);
delete result;
delete img;
log::info("time: all %d ms, detect %d ms", time::ticks_ms() - t, t3 - t2);
}
}
log::info("Program exit");
return ret;
}
int main(int argc, char *argv[])
{
// Catch SIGINT signal(e.g. Ctrl + C), and set exit flag to true.
signal(SIGINT, [](int sig)
{ app::set_exit_flag(true); });
// Use CATCH_EXCEPTION_RUN_RETURN to catch exception,
// if we don't catch exception, when program throw exception, the objects will not be destructed.
// So we catch exception here to let resources be released(call objects' destructor) before exit.
CATCH_EXCEPTION_RUN_RETURN(_main, -1, argc, argv);
}