ss928_framework/libapi/LibApi.cpp
2024-12-16 13:31:45 +08:00

1166 lines
35 KiB
C++
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ncnn include
#include "wrapperncnn.h"
#include "layer.h"
#include "net.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <float.h>
#include <fstream>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <chrono>
#include <thread>
extern "C" {
/*
Copyright (c), 2001-2022, Shenshu Tech. Co., Ltd.
*/
#include "LibApi.h"
#include "securec.h"
#include "libapi_common_svp.h"
#include "libapi_npu_process.h"
#include "libapi_npu_model.h"
#define SAMPLE_SVP_NPU_ARG_MAX_NUM 3
#define SAMPLE_SVP_NPU_IDX_TWO 2
#define SAMPLE_SVP_NPU_CMP_STR_NUM 2
static char **g_npu_cmd_argv = TD_NULL;
static td_u32 g_npu_dev_id = 0;
static td_void *g_data_buf = TD_NULL;
static size_t g_buf_size;
// // #define YOLOV5_V60 1 //YOLOv5 v6.0
// #define YOLOV5_V62 \
// 1 // YOLOv5 v6.2 export onnx model method
// // https://github.com/shaoshengsong/yolov5_62_export_ncnn
// #if YOLOV5_V60 || YOLOV5_V62
// #define MAX_STRIDE 64
// #else
// #define MAX_STRIDE 32
// #endif
#define MAX_STRIDE 64
/*
* function : to process abnormal case
*/
#ifndef __LITEOS__
static td_void libapi_svp_npu_handle_sig(td_s32 signo)
{
if (signo == SIGINT || signo == SIGTERM) {
switch (*g_npu_cmd_argv[1]) {
case '0':
libapi_svp_npu_acl_resnet50_handle_sig();
break;
case '1':
libapi_svp_npu_acl_resnet50_handle_sig();
break;
default:
break;
}
}
return ;
}
#endif
/*
* function : show usage
*/
static td_void libapi_svp_npu_usage(const td_char *prg_name)
{
printf("Usage : %s <index>\n", prg_name);
printf("index:\n");
printf("\t 0) acl_resnet50.\n");
printf("\t 1) acl_resnet50_multithread.\n");
printf("\t 2) acl_mobilenet_v3_dynamicbatch_with_mmzcache.\n");
return ;
}
static td_s32 libapi_svp_npu_case(int argc, char *argv[])
{
td_s32 ret = TD_SUCCESS;
switch (*argv[1]) {
case '0':
if (argc != SAMPLE_SVP_NPU_ARG_MAX_NUM - 1) {
return TD_FAILURE;
}
libapi_svp_npu_acl_resnet50();
break;
case '1':
if (argc != SAMPLE_SVP_NPU_ARG_MAX_NUM - 1) {
return TD_FAILURE;
}
libapi_svp_npu_acl_resnet50_multithread();
break;
case '2':
if (argc != SAMPLE_SVP_NPU_ARG_MAX_NUM - 1) {
return TD_FAILURE;
}
libapi_svp_npu_acl_mobilenet_v3_dynamicbatch();
break;
default:
ret = TD_FAILURE;
break;
}
return ret;
}
static td_s32 libapi_svp_npu_init_resource(td_void)
{
/* ACL init */
const char *acl_config_path = "";
aclrtRunMode run_mode;
td_s32 ret;
ret = aclInit(acl_config_path);
if (ret != ACL_ERROR_NONE) {
macro_svp_trace_err("acl init fail.\n");
return TD_FAILURE;
}
macro_svp_trace_info("acl init success.\n");
/* open device */
ret = aclrtSetDevice(g_npu_dev_id);
if (ret != ACL_ERROR_NONE) {
macro_svp_trace_err("acl open device %d fail.\n", g_npu_dev_id);
return TD_FAILURE;
}
macro_svp_trace_info("open device %d success.\n", g_npu_dev_id);
/* get run mode */
ret = aclrtGetRunMode(&run_mode);
if ((ret != ACL_ERROR_NONE) || (run_mode != ACL_DEVICE)) {
macro_svp_trace_err("acl get run mode fail.\n");
return TD_FAILURE;
}
macro_svp_trace_info("get run mode success\n");
return TD_SUCCESS;
}
static td_void libapi_svp_npu_destroy_resource(td_void)
{
aclError ret;
ret = aclrtResetDevice(g_npu_dev_id);
if (ret != ACL_ERROR_NONE) {
macro_svp_trace_err("reset device fail\n");
}
macro_svp_trace_info("end to reset device is %d\n", g_npu_dev_id);
ret = aclFinalize();
if (ret != ACL_ERROR_NONE) {
macro_svp_trace_err("finalize acl fail\n");
}
macro_svp_trace_info("end to finalize acl\n");
return ;
}
static td_s32 libapi_svp_npu_acl_prepare_init()
{
td_s32 ret;
ret = libapi_svp_npu_init_resource();
if (ret != TD_SUCCESS) {
libapi_svp_npu_destroy_resource();
}
return ret;
}
static td_void libapi_svp_npu_acl_prepare_exit(td_u32 thread_num)
{
for (td_u32 model_index = 0; model_index < thread_num; model_index++) {
libapi_npu_destroy_desc(model_index);
libapi_npu_unload_model(model_index);
}
libapi_svp_npu_destroy_resource();
return ;
}
static td_s32 libapi_svp_npu_fill_input_data(td_void *dev_buf, size_t buf_size)
{
td_s32 ret;
td_char path[PATH_MAX] = { 0 };
size_t file_size;
if (realpath("testyuv.sp420", path) == TD_NULL) {
macro_svp_trace_err("Invalid file!.\n");
return TD_FAILURE;
}
FILE *fp = fopen(path, "rb");
macro_svp_check_exps_return(fp == TD_NULL, TD_FAILURE, ENUM_SVP_ERR_LEVEL_ERROR,
"open image file failed!\n");
ret = fseek(fp, 0L, SEEK_END);
macro_svp_check_exps_goto(ret == -1, end, ENUM_SVP_ERR_LEVEL_ERROR, "Fseek failed!\n");
file_size = ftell(fp);
macro_svp_check_exps_goto(file_size == 0, end, ENUM_SVP_ERR_LEVEL_ERROR, "Ftell failed!\n");
ret = fseek(fp, 0L, SEEK_SET);
macro_svp_check_exps_goto(ret == -1, end, ENUM_SVP_ERR_LEVEL_ERROR, "Fseek failed!\n");
file_size = (file_size > buf_size) ? buf_size : file_size;
ret = fread(dev_buf, file_size, 1, fp);
macro_svp_check_exps_goto(ret != 1, end, ENUM_SVP_ERR_LEVEL_ERROR, "Read file failed!\n");
if (fp != TD_NULL) {
fclose(fp);
}
return TD_SUCCESS;
end:
if (fp != TD_NULL) {
fclose(fp);
}
return TD_FAILURE;
}
static td_s32 libapi_svp_npu_get_input_data(td_void **data_buf, size_t *data_len, td_u32 model_index,
td_void *yuv_data_buf, size_t yuv_data_len)
{
size_t buf_size;
td_s32 ret;
ret = libapi_npu_get_input_size_by_index(0, &buf_size, model_index);
if (ret != TD_SUCCESS) {
macro_svp_trace_err("execute get input size fail.\n");
return TD_FAILURE;
}
ret = aclrtMalloc(data_buf, buf_size, ACL_MEM_MALLOC_NORMAL_ONLY);
if (ret != ACL_ERROR_NONE) {
macro_svp_trace_err("malloc device buffer fail. size is %zu, errorCode is %d.\n", buf_size, ret);
return TD_FAILURE;
}
// for new fill function
size_t real_size = (yuv_data_len > buf_size) ? buf_size : yuv_data_len;
std::cout << "yuv_data_len : " + std::to_string(yuv_data_len) << std::endl;
std::cout << "buf_size : " + std::to_string(buf_size) << std::endl;
std::cout << "real_size : " + std::to_string(real_size) << std::endl;
memcpy(*data_buf, yuv_data_buf, real_size);
// ret = libapi_svp_npu_fill_input_data(*data_buf, buf_size);
// if (ret != TD_SUCCESS) {
// macro_svp_trace_err("memcpy_s device buffer fail.\n");
// (td_void)aclrtFree(data_buf);
// return TD_FAILURE;
// }
*data_len = real_size;
macro_svp_trace_info("get input data success\n");
return TD_SUCCESS;
}
static td_void libapi_svp_npu_release_input_data(td_void **data_buf, size_t *data_len, td_u32 thread_num)
{
for (td_u32 model_index = 0; model_index < thread_num; model_index++) {
ot_unused(data_len[model_index]);
(td_void)aclrtFree(data_buf[model_index]);
}
return ;
}
static td_s32 libapi_svp_npu_dataset_prepare_init(td_u32 model_index)
{
td_s32 ret;
ret = libapi_npu_create_input_dataset(model_index);
if (ret != TD_SUCCESS) {
macro_svp_trace_err("execute create input fail.\n");
return TD_FAILURE;
}
ret = libapi_npu_create_output(model_index);
if (ret != TD_SUCCESS) {
libapi_npu_destroy_input_dataset(model_index);
macro_svp_trace_err("execute create output fail.\n");
return TD_FAILURE;
}
return TD_SUCCESS;
}
static td_void libapi_svp_npu_dataset_prepare_exit(td_u32 thread_num)
{
for (td_u32 model_index = 0; model_index < thread_num; model_index++) {
libapi_npu_destroy_output(model_index);
libapi_npu_destroy_input_dataset(model_index);
}
return ;
}
static td_s32 libapi_svp_npu_create_input_databuf(td_void *data_buf, size_t data_len, td_u32 model_index)
{
return libapi_npu_create_input_databuf(data_buf, data_len, model_index);
}
static td_void libapi_svp_npu_destroy_input_databuf(td_u32 thread_num)
{
for (td_u32 model_index = 0; model_index < thread_num; model_index++) {
libapi_npu_destroy_input_databuf(model_index);
}
return ;
}
// static void bgr2yuv420sp(const unsigned char *bgrdata, int width, int height,
// unsigned char *yptr, unsigned char *uvptr,
// int stride) {
// #if __ARM_NEON
// uint8x8_t _v38 = vdup_n_u8(38);
// uint8x8_t _v75 = vdup_n_u8(75);
// uint8x8_t _v15 = vdup_n_u8(15);
// uint8x8_t _v127 = vdup_n_u8(127);
// uint8x8_t _v84_107 = vzip_u8(vdup_n_u8(84), vdup_n_u8(107)).val[0];
// uint8x8_t _v43_20 = vzip_u8(vdup_n_u8(43), vdup_n_u8(20)).val[0];
// uint16x8_t _v128 = vdupq_n_u16((128 << 8) + 128);
// #endif // __ARM_NEON
// for (int y = 0; y + 1 < height; y += 2) {
// const unsigned char *p0 = bgrdata + y * width * 3;
// const unsigned char *p1 = bgrdata + (y + 1) * width * 3;
// unsigned char *yptr0 = yptr + y * stride;
// unsigned char *yptr1 = yptr + (y + 1) * stride;
// unsigned char *uvptr0 = uvptr + (y / 2) * stride;
// int x = 0;
// #if __ARM_NEON
// for (; x + 7 < width; x += 8) {
// uint8x8x3_t _bgr0 = vld3_u8(p0);
// uint8x8x3_t _bgr1 = vld3_u8(p1);
// uint16x8_t _y0 = vmull_u8(_bgr0.val[0], _v15);
// uint16x8_t _y1 = vmull_u8(_bgr1.val[0], _v15);
// _y0 = vmlal_u8(_y0, _bgr0.val[1], _v75);
// _y1 = vmlal_u8(_y1, _bgr1.val[1], _v75);
// _y0 = vmlal_u8(_y0, _bgr0.val[2], _v38);
// _y1 = vmlal_u8(_y1, _bgr1.val[2], _v38);
// uint8x8_t _y0_u8 = vqrshrun_n_s16(vreinterpretq_s16_u16(_y0), 7);
// uint8x8_t _y1_u8 = vqrshrun_n_s16(vreinterpretq_s16_u16(_y1), 7);
// uint16x4_t _b4 = vpaddl_u8(_bgr0.val[0]);
// uint16x4_t _g4 = vpaddl_u8(_bgr0.val[1]);
// uint16x4_t _r4 = vpaddl_u8(_bgr0.val[2]);
// _b4 = vpadal_u8(_b4, _bgr1.val[0]);
// _g4 = vpadal_u8(_g4, _bgr1.val[1]);
// _r4 = vpadal_u8(_r4, _bgr1.val[2]);
// uint16x4x2_t _brbr = vzip_u16(_b4, _r4);
// uint16x4x2_t _gggg = vzip_u16(_g4, _g4);
// uint16x4x2_t _rbrb = vzip_u16(_r4, _b4);
// uint8x8_t _br = vshrn_n_u16(vcombine_u16(_brbr.val[0], _brbr.val[1]), 2);
// uint8x8_t _gg = vshrn_n_u16(vcombine_u16(_gggg.val[0], _gggg.val[1]), 2);
// uint8x8_t _rb = vshrn_n_u16(vcombine_u16(_rbrb.val[0], _rbrb.val[1]), 2);
// // uint8x8_t _br = vtrn_u8(_bgr0.val[0], _bgr0.val[2]).val[0];
// // uint8x8_t _gg = vtrn_u8(_bgr0.val[1], _bgr0.val[1]).val[0];
// // uint8x8_t _rb = vtrn_u8(_bgr0.val[2], _bgr0.val[0]).val[0];
// uint16x8_t _uv = vmlal_u8(_v128, _br, _v127);
// _uv = vmlsl_u8(_uv, _gg, _v84_107);
// _uv = vmlsl_u8(_uv, _rb, _v43_20);
// uint8x8_t _uv_u8 = vqshrn_n_u16(_uv, 8);
// vst1_u8(yptr0, _y0_u8);
// vst1_u8(yptr1, _y1_u8);
// vst1_u8(uvptr0, _uv_u8);
// p0 += 24;
// p1 += 24;
// yptr0 += 8;
// yptr1 += 8;
// uvptr0 += 8;
// }
// #endif
// for (; x + 1 < width; x += 2) {
// unsigned char b00 = p0[0];
// unsigned char g00 = p0[1];
// unsigned char r00 = p0[2];
// unsigned char b01 = p0[3];
// unsigned char g01 = p0[4];
// unsigned char r01 = p0[5];
// unsigned char b10 = p1[0];
// unsigned char g10 = p1[1];
// unsigned char r10 = p1[2];
// unsigned char b11 = p1[3];
// unsigned char g11 = p1[4];
// unsigned char r11 = p1[5];
// // y = 0.29900 * r + 0.58700 * g + 0.11400 * b
// // u = -0.16874 * r - 0.33126 * g + 0.50000 * b + 128
// // v = 0.50000 * r - 0.41869 * g - 0.08131 * b + 128
// #define SATURATE_CAST_UCHAR(X) \
// (unsigned char)::std::min(::std::max((int)(X), 0), 255);
// unsigned char y00 =
// SATURATE_CAST_UCHAR((38 * r00 + 75 * g00 + 15 * b00 + 64) >> 7);
// unsigned char y01 =
// SATURATE_CAST_UCHAR((38 * r01 + 75 * g01 + 15 * b01 + 64) >> 7);
// unsigned char y10 =
// SATURATE_CAST_UCHAR((38 * r10 + 75 * g10 + 15 * b10 + 64) >> 7);
// unsigned char y11 =
// SATURATE_CAST_UCHAR((38 * r11 + 75 * g11 + 15 * b11 + 64) >> 7);
// unsigned char b4 = (b00 + b01 + b10 + b11) / 4;
// unsigned char g4 = (g00 + g01 + g10 + g11) / 4;
// unsigned char r4 = (r00 + r01 + r10 + r11) / 4;
// // unsigned char b4 = b00;
// // unsigned char g4 = g00;
// // unsigned char r4 = r00;
// unsigned char u = SATURATE_CAST_UCHAR(
// ((-43 * r4 - 84 * g4 + 127 * b4 + 128) >> 8) + 128);
// unsigned char v = SATURATE_CAST_UCHAR(
// ((127 * r4 - 107 * g4 - 20 * b4 + 128) >> 8) + 128);
// #undef SATURATE_CAST_UCHAR
// yptr0[0] = y00;
// yptr0[1] = y01;
// yptr1[0] = y10;
// yptr1[1] = y11;
// uvptr0[0] = u;
// uvptr0[1] = v;
// p0 += 6;
// p1 += 6;
// yptr0 += 2;
// yptr1 += 2;
// uvptr0 += 2;
// }
// }
// }
// struct YuvStuct {
// ncnn::Mat im;
// int size;
// };
// YuvStuct g_syuv;
// static std::vector<Object> objects;
// static int img_w;
// static int img_h;
// static float scale = 1.f;
// static int wpad;
// static int hpad;
// static std::vector<Object> proposals;
// static ncnn::Mat in_pad;
// static YuvStuct libapi_ncnn_convertimg_yolov5s(cv::Mat bgr)
// {
// // bgr = cv::imread(jpg, 1);
// if (bgr.empty()) {
// fprintf(stderr, "bgr.empty()\n");
// YuvStuct syuv;
// return syuv;
// }
// const int target_size = 640;
// const float prob_threshold = 0.25f;
// const float nms_threshold = 0.45f;
// img_w = bgr.cols;
// img_h = bgr.rows;
// // letterbox pad to multiple of MAX_STRIDE
// int w = img_w;
// int h = img_h;
// // float scale = 1.f;
// if (w > h) {
// scale = (float)target_size / w;
// w = target_size;
// h = h * scale;
// } else {
// scale = (float)target_size / h;
// h = target_size;
// w = w * scale;
// }
// ncnn::Mat in = ncnn::Mat::from_pixels_resize(
// bgr.data, ncnn::Mat::PIXEL_BGR2RGB, img_w, img_h, w, h);
// // pad to target_size rectangle
// // yolov5/utils/datasets.py letterbox
// wpad = (w + MAX_STRIDE - 1) / MAX_STRIDE * MAX_STRIDE - w;
// hpad = (h + MAX_STRIDE - 1) / MAX_STRIDE * MAX_STRIDE - h;
// printf("w:%d,h:%d,MAX_STRIDE:%d,wpad:%d,hpad:%dscale:%f\n", w, h, MAX_STRIDE, wpad,
// hpad,scale);
// const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
// in_pad.substract_mean_normalize(0, norm_vals);
// ncnn::copy_make_border(in, in_pad, hpad / 2, hpad - hpad / 2, wpad / 2,
// wpad - wpad / 2, ncnn::BORDER_CONSTANT, 114.f);
// int imgin_w = 640, imgin_h = 640;
// cv::Mat a(imgin_w, imgin_h, CV_8UC3);
// memset(a.data, 0xFF, imgin_w * imgin_h * 3);
// in.to_pixels(a.data, ncnn::Mat::PIXEL_RGB2BGR);
// // for test
// cv::imwrite("in_image.png", a);
// // cv::imshow("in_image", a);
// // yuv420sp
// ncnn::Mat yuv(imgin_w, imgin_h / 2 * 3, 1);
// unsigned char *puv = (unsigned char *)yuv + imgin_w * imgin_h;
// bgr2yuv420sp(a.data, imgin_w, imgin_h, yuv, puv, imgin_w);
// //for test
// FILE *fp = fopen("testyuv.sp420", "wb");
// if (fp) {
// fwrite(yuv, imgin_w * imgin_h * 3 / 2, 1, fp);
// fclose(fp);
// }
// g_syuv.im = yuv;
// g_syuv.size = imgin_w * imgin_h / 2 * 3;
// return g_syuv;
// }
static cv::Mat read_img(const std::string path)
{
cv::Mat img;
std::ifstream file(path, std::ios::binary);
if (!file) {
macro_svp_trace_err("无法打开文件!\n");
return img;
}
std::vector<char> data((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
file.close();
img = cv::imdecode(cv::Mat(data), cv::IMREAD_COLOR);
if (img.empty()) {
macro_svp_trace_err("图像解码失败!\n");
return img;
}
macro_svp_trace_info("图像解码成功!\n");
return img;
}
/* function : show the sample of npu resnet50 */
// td_void libapi_svp_npu_acl_resnet50(td_void)
UmError LibapiSvpNpuHandleSig(void)
{
struct sigaction sa;
(td_void)memset_s(&sa, sizeof(struct sigaction), 0, sizeof(struct sigaction));
sa.sa_handler = libapi_svp_npu_handle_sig;
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
return UM_OK;
}
// svp step 1
UmError LibapiSvpNpuAclPrepareInit(void)
{
td_s32 ret;
ret = libapi_svp_npu_acl_prepare_init();
return ret;
}
UmError LibApiSvpNpuAclPrepareExit(void)
{
libapi_svp_npu_acl_prepare_exit(1);
return UM_OK;
}
// svp step 2
UmError LibApiSvpNpuLoadModel(char* om_model_path, int model_index, bool is_cached)
{
td_char path[PATH_MAX] = { 0 };
td_s32 ret;
if (sizeof(om_model_path) > PATH_MAX) {
macro_svp_trace_err("pathname too long!.\n");
return TD_NULL;
}
if (realpath(om_model_path, path) == TD_NULL) {
std::cout << om_model_path << std::endl;
std::cout << path << std::endl;
macro_svp_trace_err("invalid file!.\n");
return TD_NULL;
}
if (is_cached == TD_TRUE) {
ret = libapi_npu_load_model_with_mem_cached(path, model_index);
} else {
ret = libapi_npu_load_model_with_mem(path, model_index);
}
if (ret != TD_SUCCESS) {
macro_svp_trace_err("execute load model fail, model_index is:%d.\n", model_index);
goto acl_prepare_end1;
}
ret = libapi_npu_create_desc(model_index);
if (ret != TD_SUCCESS) {
macro_svp_trace_err("execute create desc fail.\n");
goto acl_prepare_end2;
}
return TD_SUCCESS;
acl_prepare_end2:
libapi_npu_destroy_desc(model_index);
acl_prepare_end1:
libapi_npu_unload_model(model_index);
return ret;
}
UmError LibApiSvpNpuUnLoadModel(int model_index)
{
libapi_npu_destroy_desc(model_index);
libapi_npu_unload_model(model_index);
return UM_OK;
}
// struct YuvStuct {
// ncnn::Mat im;
// int size;
// };
// for test
struct YuvStuct {
// ncnn::Mat im;
cv::Mat im;
int size;
YuvStuct()
{
im = cv::Mat::zeros(640, 640, CV_8UC3);
size = 0;
}
};
// svp step 3
UmError _LibApiSvpNpuLoadDataset(void **data_buf, size_t *data_len, int model_index, void *yuv_data_buf, size_t yuv_data_len)
{
td_s32 ret;
ret = libapi_svp_npu_dataset_prepare_init(model_index);
if (ret != TD_SUCCESS) {
// libapi_svp_npu_acl_prepare_exit(1);
return ret;
}
// if (g_data_buf==NULL)
// {
ret = libapi_svp_npu_get_input_data(data_buf, data_len, model_index, yuv_data_buf, yuv_data_len);
if (ret != TD_SUCCESS) {
macro_svp_trace_err("execute create input fail.\n");
libapi_svp_npu_dataset_prepare_exit(1);
// libapi_svp_npu_acl_prepare_exit(1);
return ret;
}
// }
// else
// {
// // for new fill function
// // std::cout << "yuv_data_len : " + std::to_string(yuv_data_len) << std::endl;
// // std::cout << "buf_size : " + std::to_string(buf_size) << std::endl;
// // std::cout << "real_size : " + std::to_string(real_size) << std::endl;
// memcpy(data_buf, yuv_data_buf, data_len);
// }
std::cout << "libapi_svp_npu_create_input_databuf data_len:" + std::to_string(*data_len) << std::endl;
std::cout << "libapi_svp_npu_create_input_databuf model_index:" + std::to_string(model_index) << std::endl;
ret = libapi_svp_npu_create_input_databuf(*data_buf, *data_len, model_index);
if (ret != TD_SUCCESS) {
macro_svp_trace_err("memcpy_s device buffer fail.\n");
libapi_svp_npu_release_input_data(data_buf, data_len, 1);
libapi_svp_npu_dataset_prepare_exit(1);
// libapi_svp_npu_acl_prepare_exit(1);
return ret;
}
return ret;
}
UmError LibApiSvpNpuLoadDataset(int model_index, void *src, size_t data_len, size_t imgin_w, size_t imgin_h, int flag)
{
UmError ret;
cv::Mat im(imgin_h, imgin_w, flag);
memcpy(im.data, src, data_len);
// cv::imwrite("tmp.png", im);
ncnn_convertimg_yolov5s_by_cvim(&im);
YuvStuct* psyuv = (YuvStuct*)ncnn_get_syuv_param();
ret = _LibApiSvpNpuLoadDataset(&g_data_buf, &g_buf_size, model_index, psyuv->im.data, psyuv->size);
if (ret != UM_OK) {
macro_svp_trace_err("_LibApiSvpNpuLoadDataset fail.\n");
return ret;
}
return ret;
}
UmError LibApiSvpNpuUnloadDataset()
{
libapi_svp_npu_destroy_input_databuf(1);
libapi_svp_npu_release_input_data(&g_data_buf, &g_buf_size, 1);
libapi_svp_npu_dataset_prepare_exit(1);
ncnn_clear_objects_param();
ncnn_clear_proposals_param();
return UM_OK;
}
UmError LibApiSvpNpuModelExecute(int model_index)
{
td_s32 ret;
auto start = std::chrono::high_resolution_clock::now();
ret = libapi_npu_model_execute(model_index);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "libapi_npu_model_execute 程序运行时间: " << elapsed.count() << "" << std::endl;
if (ret != TD_SUCCESS) {
macro_svp_trace_err("execute inference fail.\n");
return ret;
}
start = std::chrono::high_resolution_clock::now();
libapi_npu_output_model_result(model_index);
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "libapi_npu_output_model_result 程序运行时间: " << elapsed.count() << "" << std::endl;
return UM_OK;
}
struct Object {
cv::Rect_<float> rect;
int label;
float prob;
};
struct TarObject {
int x;
int y;
int width;
int height;
int label;
float prob;
char text[256];
};
struct RetrunObject {
int count;
TarObject objects[128];
};
UmError LibApiSvpNpuGetModelExecuteResult(int model_index, void* objs /*RetrunObject*/ )
{
UmError ret;
printf("0\n");
std::vector<Object> _objs = (*(std::vector<Object>*)ncnn_get_objects_param());
std::cout << "_objs.size()" + std::to_string(_objs.size()) << std::endl;
const char** class_names = ncnn_get_class_name();
char text[256];
int baseLine = 0;
RetrunObject* src_objs = (RetrunObject*)objs;
int src_idx = 0;
for (int i=0; i<_objs.size(); i++)
{
TarObject* tar_obj = &src_objs->objects[src_idx];
const Object src_obj = _objs.at(i);
// printf("1\n");
// 这里要改,模型参数变化后,要调整
// for test
if (src_obj.label > 1) continue;
if (src_obj.prob < 0.0) continue;
src_idx++;
sprintf(text, "%s %.1f%%", class_names[src_obj.label], src_obj.prob * 100);
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
int x = src_obj.rect.x;
int y = src_obj.rect.y - label_size.height - baseLine;
if (y < 0) y = 0;
if (x + label_size.width > ((cv::Mat*)ncnn_get_cur_bgr())->cols)
x = ((cv::Mat*)ncnn_get_cur_bgr())->cols - label_size.width;
// printf("2\n");
tar_obj->x = x;
tar_obj->y = y;
tar_obj->width = src_obj.rect.width;
tar_obj->height = src_obj.rect.height;
tar_obj->label = src_obj.label;
tar_obj->prob = src_obj.prob;
memcpy(tar_obj->text, class_names[src_obj.label], strlen(class_names[src_obj.label]));
// printf("3\n");
// printf("tar_obj->x:%d tar_obj->y:%d tar_obj->width:%d tar_obj->height:%d \
// tar_obj->label:%d tar_obj->prob:%f tar_obj->text:%s " \
// tar_obj->x, tar_obj->y, tar_obj->width, tar_obj->height, \
// tar_obj->label, tar_obj->prob, tar_obj->text);
printf("tar_obj->x:%d tar_obj->y:%d tar_obj->width:%d tar_obj->height:%d tar_obj->label:%d tar_obj->text:%f \n",
tar_obj->x, tar_obj->y, tar_obj->width, tar_obj->height, tar_obj->label, tar_obj->prob);
// printf("4\n");
src_objs->count = src_idx;
// printf("5\n");
if (src_idx==128) break;
}
// printf("=========================\n");
ncnn_clear_objects_param();
ncnn_clear_proposals_param();
return ret;
}
#include "libapi_ive_main.h"
#include "libapi_common_ive.h"
// 检查 MPI 初始化
UmError LibapiCommonIveCheckMpiInit()
{
return libapi_common_ive_check_mpi_init();
}
// MPI 初始化
UmError LibapiCommonIveMpiInit()
{
return libapi_common_ive_mpi_init();
}
// 推出 MPI
UmError LibapiCommonIveMpiExit()
{
libapi_common_ive_mpi_exit();
return TD_SUCCESS;
}
UmError LibApiIveCreateMMZImage(void *mmz_img, int img_type, int width, int height)
{
return libapi_common_ive_create_image((ot_svp_img*)mmz_img, (ot_svp_img_type)img_type, width, height);
}
UmError LibApiIveDestroyMMZImage(void *mmz_img)
{
return libapi_common_ive_destroy_image((ot_svp_img*)mmz_img);
}
UmError LibApiIveReadMMZFromOsMem(void *mmz_img, void *cv_src, int width, int height, int img_type)
{
return libapi_common_ive_read_os_mem((ot_svp_img*)mmz_img, cv_src, width, height, (ot_svp_img_type)img_type);
}
UmError LibApiIveWriteMMZToOsMem(void *mmz_img, void *cv_tar)
{
return libapi_common_ive_write_os_mem((ot_svp_img*)mmz_img, cv_tar);
}
UmError LibApiIveWriteMMZToFile(void *mmz_img, FILE *fp)
{
return libapi_common_ive_write_file((ot_svp_img*)mmz_img, fp);
}
UmError LibApiCanny(void* src, int width, int height, int img_type,
void* dst, char canny_complete)
{
return libapi_ive_canny(src, width, height, (ot_svp_img_type)img_type, dst, canny_complete);
}
UmError LibapiSobelHandleSig()
{
libapi_ive_sobel_handle_sig();
return TD_SUCCESS;
}
UmError LibApiSobel()
{
libapi_ive_sobel();
return TD_SUCCESS;
}
UmError LibapiKcfHandleSig()
{
return TD_SUCCESS;
}
UmError LibApiKcf()
{
libapi_ive_kcf();
return TD_SUCCESS;
}
// int main(int argc, char* argv[])
// {
// remove("image.png");
// LibapiSvpNpuHandleSig();
// LibapiSvpNpuAclPrepareInit();
// const char *om_model_path = "./yolov5s_v6.2.om";
// UmError ret = LibApiSvpNpuLoadModel(om_model_path, 0, false);
// if (ret != TD_SUCCESS) {
// macro_svp_trace_err("LibApiSvpNpuLoadModel fail.\n");
// exit(0);
// }
// td_char path[PATH_MAX] = { 0 };
// if (realpath("./test.jpg", path) == TD_NULL) {
// macro_svp_trace_err("Invalid file!.\n");
// exit(0);
// }
// const std::string spath = path;
// std::cout << spath << std::endl;
// cv::Mat im = read_img(spath);
// // cv::Mat im = cv::imread("test.jpg", 1);
// if (im.empty())
// {
// std::cout << "im is empty" << std::endl;
// exit(0);
// }
// for (int i=0; i<30; i++)
// {
// std::cout << std::to_string(im.total()*im.channels()) << std::endl;
// std::cout << std::to_string(im.size().width) << std::endl;
// std::cout << std::to_string(im.size().height) << std::endl;
// LibApiSvpNpuLoadDataset(0, im.data, im.total()*im.channels(), im.size().width , im.size().height, CV_8UC3);
// // ncnn_convertimg_yolov5s_by_cvim(&im);
// // YuvStuct* psyuv = (YuvStuct*)get_syuv_param();
// // ret = _LibApiSvpNpuLoadDataset(g_data_buf, g_buf_size, 0, psyuv->im.data, psyuv->size);
// // if (ret != UM_OK) {
// // macro_svp_trace_err("LibApiSvpNpuLoadDataset fail.\n");
// // exit(0);
// // }
// ret = LibApiSvpNpuModelExecute(0);
// if (ret != UM_OK) {
// macro_svp_trace_err("LibApiSvpNpuModelExecute fail.\n");
// exit(0);
// }
// }
// LibApiSvpNpuUnloadDataset();
// return 0;
// }
// #include <dlfcn.h> // Linux for so
// int main(int argc, char* argv[])
// {
// remove("image.png");
// const char* libPath = "./libLibApi.so";
// void* handle = dlopen(libPath, RTLD_LAZY);
// if (!handle) {
// std::cerr << "无法加载动态库: " << dlerror() << std::endl; // Linux
// // std::cerr << "无法加载动态库" << std::endl; // Windows
// return 1;
// }
// // 获取函数指针
// // LibapiSvpNpuHandleSig();
// typedef int (*pFucLibapiSvpNpuHandleSig)();
// pFucLibapiSvpNpuHandleSig pfucLibapiSvpNpuHandleSig =
// (pFucLibapiSvpNpuHandleSig)dlsym(handle, "LibapiSvpNpuHandleSig");
// pfucLibapiSvpNpuHandleSig();
// // LibapiSvpNpuAclPrepareInit();
// typedef int (*pFucLibapiSvpNpuAclPrepareInit)();
// pFucLibapiSvpNpuAclPrepareInit pfucLibapiSvpNpuAclPrepareInit =
// (pFucLibapiSvpNpuAclPrepareInit)dlsym(handle, "LibapiSvpNpuAclPrepareInit");
// pfucLibapiSvpNpuAclPrepareInit();
// const char *om_model_path = "./yolov5s_v6.2.om";
// // UmError ret = LibApiSvpNpuLoadModel(om_model_path, 0, false);
// typedef int (*pFucLibApiSvpNpuLoadModel)(const char* om_model_path, int model_index, bool is_cached);
// pFucLibApiSvpNpuLoadModel pfucLibApiSvpNpuLoadModel =
// (pFucLibApiSvpNpuLoadModel)dlsym(handle, "LibApiSvpNpuLoadModel");
// UmError ret = pfucLibApiSvpNpuLoadModel(om_model_path, 0, false);
// if (ret != TD_SUCCESS) {
// macro_svp_trace_err("LibApiSvpNpuLoadModel fail.\n");
// exit(0);
// }
// td_char path[PATH_MAX] = { 0 };
// if (realpath("./test.jpg", path) == TD_NULL) {
// macro_svp_trace_err("Invalid file!.\n");
// exit(0);
// }
// const std::string spath = path;
// cv::Mat im = read_img(spath);
// if (im.empty())
// {
// std::cout << "im is empty" << std::endl;
// exit(0);
// }
// typedef int (*pFucLibApiSvpNpuLoadDataset)(int model_index, void *src, size_t data_len,
// size_t imgin_w, size_t imgin_h, int flag);
// typedef int (*pFucLibApiSvpNpuModelExecute)(int model_index);
// typedef int (*pFucLibApiSvpNpuGetModelExecuteResult)(int model_index, void* retrunObject);
// typedef int (*pFucLibApiSvpNpuUnloadDataset)();
// typedef int (*pFucLibApiSvpNpuUnLoadModel)(int model_index);
// for (int i=0; i<2; i++)
// {
// //LibApiSvpNpuLoadDataset(0, im.data, im.total()*im.channels(), im.size().width , im.size().height , CV_8UC3);
// pFucLibApiSvpNpuLoadDataset pfucLibApiSvpNpuLoadDataset =
// (pFucLibApiSvpNpuLoadDataset)dlsym(handle, "LibApiSvpNpuLoadDataset");
// UmError ret = pfucLibApiSvpNpuLoadDataset(0, im.data, im.total()*im.channels(),
// im.size().width , im.size().height , CV_8UC3);
// //ret = LibApiSvpNpuModelExecute(0);
// pFucLibApiSvpNpuModelExecute pfucLibApiSvpNpuModelExecute =
// (pFucLibApiSvpNpuModelExecute)dlsym(handle, "LibApiSvpNpuModelExecute");
// pfucLibApiSvpNpuModelExecute(0);
// if (ret != UM_OK) {
// macro_svp_trace_err("LibApiSvpNpuModelExecute fail.\n");
// exit(0);
// }
// RetrunObject objs;
// pFucLibApiSvpNpuGetModelExecuteResult pfucLibApiSvpNpuGetModelExecuteResult =
// (pFucLibApiSvpNpuGetModelExecuteResult)dlsym(handle, "LibApiSvpNpuGetModelExecuteResult");
// pfucLibApiSvpNpuGetModelExecuteResult(0, &objs);
// //LibApiSvpNpuUnloadDataset();
// pFucLibApiSvpNpuUnloadDataset pfucLibApiSvpNpuUnloadDataset =
// (pFucLibApiSvpNpuUnloadDataset)dlsym(handle, "LibApiSvpNpuUnloadDataset");
// pfucLibApiSvpNpuUnloadDataset();
// }
// pFucLibApiSvpNpuUnLoadModel pfucLibApiSvpNpuUnLoadModel =
// (pFucLibApiSvpNpuUnLoadModel)dlsym(handle, "LibApiSvpNpuUnLoadModel");
// pfucLibApiSvpNpuUnLoadModel(0);
// return 0;
// }
static void myThreadFunction(int arg) {
while (true)
{
std::cout << "1" << std::endl;
sleep(1);
}
}
int main(int argc, char* argv[])
{
std::thread t(myThreadFunction, 100); // 启动一个新线程来执行myThreadFunction并传递参数100
t.join(); // 等待线程完成执行。这将会阻塞当前线程,直到指定线程完成执行。
return 0;
// // LibapiSobelHandleSig();
// // LibApiSobel();
// // remove("image.png");
// td_char path[PATH_MAX] = { 0 };
// if (realpath("./test.jpg", path) == TD_NULL) {
// macro_svp_trace_err("Invalid file!.\n");
// exit(0);
// }
// LibapiCommonIveMpiInit();
// const std::string spath = path;
// std::cout << spath << std::endl;
// cv::Mat im = read_img(spath);
// int width = im.size().width;
// int height = im.size().height;
// std::cout << std::to_string(im.size().width) << std::endl;
// std::cout << std::to_string(im.size().height) << std::endl;
// cv::Mat gray;
// cv::cvtColor(im, gray, cv::COLOR_BGR2GRAY);
// ot_svp_img img;
// char img_type = 0x0;
// cv::Mat blackImage = cv::Mat::zeros(cv::Size(640, 480), CV_8UC1);
// LibApiIveCreateMMZImage(&img, img_type, width, height);
// LibApiIveReadMMZFromOsMem(&img, gray.data, width, height, img_type);
// auto start = std::chrono::high_resolution_clock::now();
// LibApiIveWriteMMZToOsMem(&img, blackImage.data);
// auto end = std::chrono::high_resolution_clock::now();
// LibApiIveDestroyMMZImage(&img);
// cv::imwrite("dst1111.jpg", blackImage);
// // LibApiCanny(gray.data, width, height, img_type, blackImage.data, '1');
// // libapi_ive_gmm2();
// std::chrono::duration<double> elapsed = end - start;
// std::cout << "canny 程序运行时间: " << elapsed.count() << " 秒" << std::endl;
// cv::imwrite("dst1111.jpg", blackImage);
// LibapiCommonIveMpiExit();
}
}