Skip to content

Commit

Permalink
add yolo11 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Oct 10, 2024
1 parent 330a6db commit e80fd15
Show file tree
Hide file tree
Showing 21 changed files with 1,568 additions and 18 deletions.
925 changes: 925 additions & 0 deletions components/nn/include/maix_nn_yolo11.hpp

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions components/nn/include/maix_nn_yolov8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

namespace maix::nn
{
enum YOLOv8_Type
enum class YOLOv8_Type
{
TYPE_DETECT = 0,
TYPE_POSE = 1,
TYPE_SEG = 2
DETECT = 0,
POSE = 1,
SEG = 2
};

class _KpInfo
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace maix::nn
YOLOv8(const string &model = "", bool dual_buff = true)
{
_model = nullptr;
_type = TYPE_DETECT;
_type = YOLOv8_Type::DETECT;
_dual_buff = dual_buff;
if (!model.empty())
{
Expand Down Expand Up @@ -101,9 +101,9 @@ namespace maix::nn
_extra_info = _model->extra_info();
if (_extra_info.find("model_type") != _extra_info.end())
{
if (_extra_info["model_type"] != "yolov8")
if (_extra_info["model_type"] != this->type_str)
{
log::error("model_type not match, expect 'yolov8', but got '%s'", _extra_info["model_type"].c_str());
log::error("model_type not match, expect '%s', but got '%s'", this->type_str.c_str(), _extra_info["model_type"].c_str());
return err::ERR_ARGS;
}
}
Expand All @@ -112,7 +112,7 @@ namespace maix::nn
log::error("model_type key not found");
return err::ERR_ARGS;
}
log::info("model info:\n\ttype: yolov8");
log::info("model info:\n\ttype: %d", this->type_str.c_str());
if (_extra_info.find("input_type") != _extra_info.end())
{
std::string input_type = _extra_info["input_type"];
Expand Down Expand Up @@ -221,11 +221,11 @@ namespace maix::nn
{
if (_extra_info["type"] == "pose")
{
_type = TYPE_POSE;
_type = YOLOv8_Type::POSE;
}
else if (_extra_info["type"] == "seg")
{
_type = TYPE_SEG;
_type = YOLOv8_Type::SEG;
}
else if (_extra_info["type"] != "detector")
{
Expand All @@ -235,7 +235,7 @@ namespace maix::nn
}
else
{
_type = TYPE_DETECT;
_type = YOLOv8_Type::DETECT;
}
std::vector<nn::LayerInfo> inputs = _model->inputs_info();
_input_size = image::Size(inputs[0].shape[3], inputs[0].shape[2]);
Expand Down Expand Up @@ -425,6 +425,9 @@ namespace maix::nn
*/
std::vector<float> scale;

protected:
std::string type_str = "yolov8";

private:
image::Size _input_size;
image::Format _input_img_fmt;
Expand Down Expand Up @@ -480,11 +483,11 @@ namespace maix::nn
delete objects_total;
}
// decode keypoints
if (_type == TYPE_POSE)
if (_type == YOLOv8_Type::POSE)
{
_decode_keypoints(*objects, kp_out);
}
else if (_type == TYPE_SEG)
else if (_type == YOLOv8_Type::SEG)
{
_decode_seg_points(*objects, kp_out, mask_out);
}
Expand Down Expand Up @@ -739,7 +742,7 @@ namespace maix::nn
}
if (img_w == _input_size.width() && img_h == _input_size.height())
{
if (_type == TYPE_SEG)
if (_type == YOLOv8_Type::SEG)
{
for (nn::Object *obj : objs)
{
Expand Down Expand Up @@ -769,7 +772,7 @@ namespace maix::nn
obj->points.at(i * 2 + 1) *= *scale_h;
}
CORRECT_BBOX_RANGE_YOLOV8(obj);
if (_type == TYPE_SEG)
if (_type == YOLOv8_Type::SEG)
{
if (obj->w != obj->seg_mask->width() || obj->h != obj->seg_mask->height())
{
Expand Down Expand Up @@ -802,7 +805,7 @@ namespace maix::nn
obj->points.at(i * 2 + 1) = (obj->points.at(i * 2 + 1) - pad_h) * scale_reverse;
}
CORRECT_BBOX_RANGE_YOLOV8(obj);
if (_type == TYPE_SEG)
if (_type == YOLOv8_Type::SEG)
{
if (obj->w != obj->seg_mask->width() || obj->h != obj->seg_mask->height())
{
Expand Down Expand Up @@ -835,7 +838,7 @@ namespace maix::nn
obj->points.at(i * 2 + 1) = (obj->points.at(i * 2 + 1) - pad_h) * scale_reverse;
}
CORRECT_BBOX_RANGE_YOLOV8(obj);
if (_type == TYPE_SEG)
if (_type == YOLOv8_Type::SEG)
{
if (obj->w != obj->seg_mask->width() || obj->h != obj->seg_mask->height())
{
Expand Down
9 changes: 9 additions & 0 deletions examples/nn_yolo11_detect/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build
dist
.config.mk
.flash.conf.json
data

/CMakeLists.txt

__pycache__
9 changes: 9 additions & 0 deletions examples/nn_yolo11_detect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +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>"; `
74 changes: 74 additions & 0 deletions examples/nn_yolo11_detect/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Empty file.
3 changes: 3 additions & 0 deletions examples/nn_yolo11_detect/main/include/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once


111 changes: 111 additions & 0 deletions examples/nn_yolo11_detect/main/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

#include "maix_basic.hpp"
#include "maix_vision.hpp"
#include "maix_nn_yolo11.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::YOLO11 detector("", dual_buff);
e = detector.load(model_path);
err::check_raise(e, "load model failed");
log::info("load yolo11 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));
}
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));
}
disp.show(*img);
delete result;
delete img;
log::info("time: all %d ms, detect %d ms, fps: %.1f", time::ticks_ms() - t, t3 - t2, time::fps());
}
}

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 changes: 9 additions & 0 deletions examples/nn_yolo11_pose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build
dist
.config.mk
.flash.conf.json
data

/CMakeLists.txt

__pycache__
9 changes: 9 additions & 0 deletions examples/nn_yolo11_pose/README.md
Original file line number Diff line number Diff line change
@@ -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>"; `
Loading

0 comments on commit e80fd15

Please sign in to comment.