Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ checkAndAddElement(element/algorithm/lprnet)
checkAndAddElement(element/algorithm/ppocr)
checkAndAddElement(element/algorithm/yolov8)
checkAndAddElement(element/algorithm/lightstereo)
checkAndAddElement(element/algorithm/ppyoloe_plus)

checkAndAddElement(element/multimedia/decode)
checkAndAddElement(element/multimedia/osd)
Expand Down
87 changes: 87 additions & 0 deletions element/algorithm/ppyoloe_plus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
cmake_minimum_required(VERSION 3.10)
project(algorithmAPI)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -g")

if (NOT DEFINED TARGET_ARCH)
set(TARGET_ARCH pcie)
endif()

if (${TARGET_ARCH} STREQUAL "pcie")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -pthread")

set(FFMPEG_DIR /opt/sophon/sophon-ffmpeg-latest/lib/cmake)
find_package(FFMPEG REQUIRED)
include_directories(${FFMPEG_INCLUDE_DIRS})
link_directories(${FFMPEG_LIB_DIRS})

set(OpenCV_DIR /opt/sophon/sophon-opencv-latest/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIRS})

set(LIBSOPHON_DIR /opt/sophon/libsophon-current/data/libsophon-config.cmake)
find_package(LIBSOPHON REQUIRED)
include_directories(${LIBSOPHON_INCLUDE_DIRS})
link_directories(${LIBSOPHON_LIB_DIRS})

set(BM_LIBS bmlib bmrt bmcv yuv)
find_library(BMJPU bmjpuapi)
if(BMJPU)
set(JPU_LIBS bmjpuapi bmjpulite)
endif()

include_directories(../)
include_directories(../../../framework)
include_directories(../../../framework/include)

include_directories(../../../3rdparty/spdlog/include)
include_directories(../../../3rdparty/nlohmann-json/include)
include_directories(../../../3rdparty/httplib)

include_directories(include)
add_library(ppyoloe_plus SHARED
src/ppyoloe_plus_pre_process.cc
src/ppyoloe_plus_post_process.cc
src/ppyoloe_plus_inference.cc
src/ppyoloe_plus.cc
)

target_link_libraries(ppyoloe_plus ${FFMPEG_LIBS} ${OpenCV_LIBS} ${BM_LIBS} ${JPU_LIBS} -lpthread)

elseif (${TARGET_ARCH} STREQUAL "soc")
add_compile_options(-fPIC)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage -g -rdynamic")
# set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage -rdynamic")
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_ASM_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)

include_directories("${SOPHON_SDK_SOC}/include/")
include_directories("${SOPHON_SDK_SOC}/include/opencv4")
link_directories("${SOPHON_SDK_SOC}/lib/")
set(BM_LIBS bmlib bmrt bmcv yuv)
find_library(BMJPU bmjpuapi)
if(BMJPU)
set(JPU_LIBS bmjpuapi bmjpulite)
endif()

include_directories(../)
include_directories(../../../framework)
include_directories(../../../framework/include)

include_directories(../../../3rdparty/spdlog/include)
include_directories(../../../3rdparty/nlohmann-json/include)
include_directories(../../../3rdparty/httplib)

include_directories(include)
add_library(ppyoloe_plus SHARED
src/ppyoloe_plus_pre_process.cc
src/ppyoloe_plus_post_process.cc
src/ppyoloe_plus_inference.cc
src/ppyoloe_plus.cc
)
target_link_libraries(ppyoloe_plus ${FFMPEG_LIBS} ${OpenCV_LIBS} ${BM_LIBS} ${JPU_LIBS} -fprofile-arcs -lgcov -lpthread)
endif()
62 changes: 62 additions & 0 deletions element/algorithm/ppyoloe_plus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# sophon-stream ppyoloe_plus element

[English](README_EN.md) | 简体中文

sophon-stream ppyoloe_plus element是sophon-stream框架中的一个插件,是一个简单、快速、强大的检测模型。本项目已提供此插件例程

## 1. 特性
* 支持多路视频流
* 支持多线程处理

## 2. 配置参数
sophon-stream ppyoloe_plus插件具有一些可配置的参数,可以根据需求进行设置。以下是一些常用的参数:

```json
{
"configure": {
"model_path": "../ppyoloe_bytetrack_ppyoloe_plus/data/models/tray_det_1209_1b.bmodel",
"threshold_conf": 0.65,
"threshold_nms": 0.45,
"bgr2rgb": true,
"mean": [
0,
0,
0
],
"std": [
1,
1,
1
],
"stage": [
"post"
],
"use_tpu_kernel": false,
"class_names_file": "../ppyoloe_bytetrack_ppyoloe_plus/data/model.name"
},
"shared_object": "../../build/lib/libppyoloe_plus.so",
"name": "ppyoloe_plus_post",
"side": "sophgo",
"thread_number": 1
}
```

| 参数名 | 类型 | 默认值 | 说明 |
|:-------------:| :-------: | :------------------:| :------------------------:|
| model_path | 字符串 | "../ppyoloe_bytetrack_ppyoloe_plus/data/models/tray_det_1209_1b.bmodel" | yolov8模型路径 |
| threshold_conf | 浮点数或map | 0.65 | 目标检测物体置信度阈值,设置为浮点数时,所有类别共用同一个阈值;设置为map时,不同类别可以使用不同阈值,此时还需要正确设置class_names_file |
| threshold_nms | 浮点数 | 0.45 | 目标检测NMS IOU阈值 |
| bgr2rgb | bool | true | 解码器解出来的图像默认是bgr格式,是否需要将图像转换成rgb格式 |
| mean | 浮点数组 | 无 | 图像前处理均值,长度为3;计算方式为: y=(x-mean)/std;若bgr2rgb=true,数组中数组顺序需为r、g、b,否则需为b、g、r |
| std | 浮点数组 | 无 | 图像前处理方差,长度为3;计算方式同上;若bgr2rgb=true数组中数组顺序需为r、g、b,否则需为b、g、r |
| stage | 列表 | ["pre"] | 标志前处理、推理、后处理三个阶段 |
| use_tpu_kernel | 布尔值 | true | 是否启用tpu_kernel后处理 |
| class_names_file | 字符串 | 无 | threshold_conf为浮点数时不生效,可以不设置;当threshold_conf为map时启用,class name文件的路径 |
| shared_object | 字符串 | "../../build/lib/libppyoloe_plus.so" | libyolov8 动态库路径 |
| name | 字符串 | "ppyoloe_plus_post" | element 名称 |
| side | 字符串 | "sophgo"| 设备类型 |
| thread_number | 整数 | 1 | 启动线程数 |

> **注意**:
1. stage参数,需要设置为"pre","infer","post" 其中之一或相邻项的组合,并且按前处理-推理-后处理的顺序连接element。将三个阶段分配在三个element上的目的是充分利用各项资源,提高检测效率。

108 changes: 108 additions & 0 deletions element/algorithm/ppyoloe_plus/include/ppyoloe_plus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2022 Sophgo Technologies Inc. All rights reserved.
//
// SOPHON-STREAM is licensed under the 2-Clause BSD License except for the
// third-party components.
//
//===----------------------------------------------------------------------===//

#ifndef SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_H_
#define SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_H_

#include "element_factory.h"
#include "group.h"
#include "ppyoloe_plus_context.h"
#include "ppyoloe_plus_inference.h"
#include "ppyoloe_plus_post_process.h"
#include "ppyoloe_plus_pre_process.h"

namespace sophon_stream {
namespace element {
namespace ppyoloe_plus {

class Ppyoloe_plus : public ::sophon_stream::framework::Element {
public:
Ppyoloe_plus();
~Ppyoloe_plus() override;

const static std::string elementName;

/**
* @brief
* 解析configure,初始化派生element的特有属性;调用initContext初始化算法相关参数
* @param json json格式的配置文件
* @return common::ErrorCode
* 成功返回common::ErrorCode::SUCCESS,失败返回common::ErrorCode::PARSE_CONFIGURE_FAIL
*/
common::ErrorCode initInternal(const std::string& json) override;

/**
* @brief
* element的功能在这里实现。例如,算法模块需要实现组batch、调用算法、发送数据等功能
* @param dataPipeId pop数据时对应的dataPipeId
* @return common::ErrorCode 成功返回common::ErrorCode::SUCCESS
*/
common::ErrorCode doWork(int dataPipeId) override;

void setContext(std::shared_ptr<::sophon_stream::element::Context> context);
void setPreprocess(
std::shared_ptr<::sophon_stream::element::PreProcess> pre);
void setInference(
std::shared_ptr<::sophon_stream::element::Inference> infer);
void setPostprocess(
std::shared_ptr<::sophon_stream::element::PostProcess> post);
void setStage(bool pre, bool infer, bool post);
void initProfiler(std::string name, int interval);
std::shared_ptr<::sophon_stream::element::Context> getContext() {
return mContext;
}
std::shared_ptr<::sophon_stream::element::PreProcess> getPreProcess() {
return mPreProcess;
}
std::shared_ptr<::sophon_stream::element::Inference> getInference() {
return mInference;
}
std::shared_ptr<::sophon_stream::element::PostProcess> getPostProcess() {
return mPostProcess;
}

/**
* @brief 从json文件读取的配置项
*/
static constexpr const char* CONFIG_INTERNAL_STAGE_NAME_FIELD = "stage";
static constexpr const char* CONFIG_INTERNAL_MODEL_PATH_FIELD =
"model_path";
static constexpr const char* CONFIG_INTERNAL_THRESHOLD_CONF_FIELD =
"threshold_conf";
static constexpr const char* CONFIG_INTERNAL_THRESHOLD_NMS_FIELD =
"threshold_nms";
static constexpr const char* CONFIG_INTERNAL_THRESHOLD_BGR2RGB_FIELD =
"bgr2rgb";
static constexpr const char* CONFIG_INTERNAL_THRESHOLD_MEAN_FIELD = "mean";
static constexpr const char* CONFIG_INTERNAL_THRESHOLD_STD_FIELD = "std";
static constexpr const char* CONFIG_INTERNAL_CLASS_NAMES_FILE_FILED =
"class_names_file";

private:
std::shared_ptr<Ppyoloe_plusContext> mContext; // context对象
std::shared_ptr<Ppyoloe_plusPreProcess> mPreProcess; // 预处理对象
std::shared_ptr<Ppyoloe_plusInference> mInference; // 推理对象
std::shared_ptr<Ppyoloe_plusPostProcess> mPostProcess; // 后处理对象

bool use_pre = false;
bool use_infer = false;
bool use_post = false;

std::string mFpsProfilerName;
::sophon_stream::common::FpsProfiler mFpsProfiler;

common::ErrorCode initContext(const std::string& json);
void process(common::ObjectMetadatas& objectMetadatas);
};

} // namespace ppyoloe_plus
} // namespace element
} // namespace sophon_stream

#endif // SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_H_
70 changes: 70 additions & 0 deletions element/algorithm/ppyoloe_plus/include/ppyoloe_plus_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2022 Sophgo Technologies Inc. All rights reserved.
//
// SOPHON-STREAM is licensed under the 2-Clause BSD License except for the
// third-party components.
//
//===----------------------------------------------------------------------===//

#ifndef SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_CONTEXT_H_
#define SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_CONTEXT_H_

#include "algorithmApi/context.h"

namespace sophon_stream {
namespace element {
namespace ppyoloe_plus {

#define USE_ASPECT_RATIO 1

class Ppyoloe_plusContext : public ::sophon_stream::element::Context {
public:
int deviceId; // 设备ID

std::shared_ptr<BMNNContext> bmContext;
std::shared_ptr<BMNNNetwork> bmNetwork;
bm_handle_t handle;

std::vector<float> mean; // 前处理均值, 长度为3,顺序为rgb
std::vector<float> stdd; // 前处理方差, 长度为3,顺序为rgb
bool bgr2rgb; // 是否将bgr图像转成rgb推理
bmcv_convert_to_attr converto_attr; // 图片做归一化,标准化预处理使用

/**
* @brief 最小的置信度阈值。详细说明请参考README
*/
float thresh_conf_min = 0.5;
/**
* @brief 置信度阈值,key:类名,value:阈值
* 该参数支持对不同的类别设置不同的阈值
*/
std::unordered_map<std::string, float> thresh_conf;
/**
* @brief NMS IOU阈值
*/
float thresh_nms;
std::vector<std::string> class_names;
/**
* @brief 决定是否启用类别阈值
*/
bool class_thresh_valid = false;

/**
* @brief
* 类别数量,从model中读取。需要和thresh_conf、class_names的长度做校验
*/
int class_num = 1;
int m_frame_h, m_frame_w;
int net_h, net_w, m_net_channel;
int max_batch;
int input_num;
int output_num;
int min_dim;

};
} // namespace ppyoloe_plus
} // namespace element
} // namespace sophon_stream

#endif // SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_CONTEXT_H_
40 changes: 40 additions & 0 deletions element/algorithm/ppyoloe_plus/include/ppyoloe_plus_inference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2022 Sophgo Technologies Inc. All rights reserved.
//
// SOPHON-STREAM is licensed under the 2-Clause BSD License except for the
// third-party components.
//
//===----------------------------------------------------------------------===//

#ifndef SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_INFERENCE_H_
#define SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_INFERENCE_H_

#include "algorithmApi/inference.h"
#include "ppyoloe_plus_context.h"

namespace sophon_stream {
namespace element {
namespace ppyoloe_plus {

class Ppyoloe_plusInference : public ::sophon_stream::element::Inference {
public:
~Ppyoloe_plusInference() override;
/**
* @brief init device and engine
* @param[in] context: model path,inputs and outputs name...
*/
void init(std::shared_ptr<Ppyoloe_plusContext> context);
/**
* @brief network predict output
* @param[in] context: inputData and outputData
*/
common::ErrorCode predict(std::shared_ptr<Ppyoloe_plusContext> context,
common::ObjectMetadatas& objectMetadatas);
};

} // namespace ppyoloe_plus
} // namespace element
} // namespace sophon_stream

#endif // SOPHON_STREAM_ELEMENT_PPYOLOE_PLUS_INFERENCE_H_
Loading