#include "rknn_api.h"
#include
#include
#include
#include
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
void YOLO(string model_path)
{
FILE *fp_ = fopen(model_path.c_str(), "rb");
if (fp_ == NULL)
{
printf("fopen %s fail!\n", model_path.c_str());
return;
}
fseek(fp_, 0, SEEK_END);
int model_len = ftell(fp_);
void *model_ = malloc(model_len);
fseek(fp_, 0, SEEK_SET);
if (model_len != fread(model_, 1, model_len, fp_))
{
printf("fread %s fail!\n", model_path.c_str());
free(model_);
return;
}
rknn_context ctx = 0;
int ret = rknn_init(&ctx, model_, model_len, RKNN_FLAG_PRIOR_MEDIUM);
if (ret < 0)
{
printf("rknn_init fail! ret=%d\n", ret);
if (ctx)
rknn_destroy(ctx);
if (model_)
free(model_);
if (fp_)
fclose(fp_);
return;
}
rknn_input_output_num io_num;
ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
cv::Mat img = cv::imread("/home/toybrick/a.jpg");
int cols = img.cols, rows = img.rows;
cv::resize(img, img, cv::Size(224, 224), (0, 0), (0, 0), cv::INTER_LINEAR);
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
while (1)
{
rknn_input inputs[1];
memset(inputs, 0, sizeof(inputs));
inputs[0].index = 0;
inputs[0].buf = img.data;
inputs[0].size = 224 * 224 * 3;
inputs[0].pass_through = false;
inputs[0].type = RKNN_TENSOR_UINT8;
inputs[0].fmt = RKNN_TENSOR_NHWC;
ret = rknn_inputs_set(ctx, 1, inputs);
if (ret < 0)
{
printf("rknn_input_set fail! ret=%d\n", ret);
if (ctx)
rknn_destroy(ctx);
if (model_)
free(model_);
if (fp_)
fclose(fp_);
return;
}
ret = rknn_run(ctx, nullptr);
rknn_output outputs[io_num.n_output];
memset(outputs, 0, sizeof(outputs));
for (int i = 0; i < io_num.n_output; ++i)
{
outputs[i].want_float = true;
outputs[i].is_prealloc = false;
}
ret = rknn_outputs_get(ctx, io_num.n_output, outputs, nullptr);
if (ret < 0)
{
printf("rknn_outputs_get fail! ret=%d\n", ret);
if (ctx)
rknn_destroy(ctx);
if (model_)
free(model_);
if (fp_)
fclose(fp_);
return;
}
cout << outputs[0].size << endl;
}
}
void ap(string model_path) {
FILE *fp_ = fopen(model_path.c_str(), "rb");
if (fp_ == NULL)
{
printf("fopen %s fail!\n", model_path.c_str());
return;
}
fseek(fp_, 0, SEEK_END);
int model_len = ftell(fp_);
void *model_ = malloc(model_len);
fseek(fp_, 0, SEEK_SET);
if (model_len != fread(model_, 1, model_len, fp_))
{
printf("fread %s fail!\n", model_path.c_str());
free(model_);
return;
}
rknn_context ctx = 0;
int ret = rknn_init(&ctx, model_, model_len, RKNN_FLAG_PRIOR_MEDIUM);
if (ret < 0)
{
printf("rknn_init fail! ret=%d\n", ret);
if (ctx)
rknn_destroy(ctx);
if (model_)
free(model_);
if (fp_)
fclose(fp_);
return;
}
rknn_input_output_num io_num;
ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
uchar data[640*640*8];
while (1)
{
rknn_input inputs[1];
memset(inputs, 0, sizeof(inputs));
inputs[0].index = 0;
inputs[0].buf = data;
inputs[0].size = 640*640*8;
inputs[0].pass_through = false;
inputs[0].type = RKNN_TENSOR_UINT8;
inputs[0].fmt = RKNN_TENSOR_NHWC;
ret = rknn_inputs_set(ctx, 1, inputs);
if (ret < 0)
{
printf("rknn_input_set fail! ret=%d\n", ret);
if (ctx)
rknn_destroy(ctx);
if (model_)
free(model_);
if (fp_)
fclose(fp_);
return;
}
ret = rknn_run(ctx, nullptr);
rknn_output outputs[io_num.n_output];
memset(outputs, 0, sizeof(outputs));
for (int i = 0; i < io_num.n_output; ++i)
{
outputs[i].want_float = true;
outputs[i].is_prealloc = false;
}
ret = rknn_outputs_get(ctx, io_num.n_output, outputs, nullptr);
if (ret < 0)
{
printf("rknn_outputs_get fail! ret=%d\n", ret);
if (ctx)
rknn_destroy(ctx);
if (model_)
free(model_);
if (fp_)
fclose(fp_);
return;
}
cout << outputs[0].size << endl;
}
}
int main()
{
string model_path("/home/toybrick/coco.rknn");
thread t(YOLO, model_path);
string modelpath("/home/toybrick/d.rknn");
thread t1(ap, modelpath);
t.join();
t1.join();
return 0;
}
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(OpenCV REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
include
)
add_executable(duo src/duo.cc)
target_link_libraries(duo
${PROJECT_SOURCE_DIR}/librknn_api.so
${OpenCV_LIBRARIES}
pthread
)