完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#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=%dn", 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); // get result 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=%dn", 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.want_float = true; outputs.is_prealloc = false; } ret = rknn_outputs_get(ctx, io_num.n_output, outputs, nullptr); if (ret < 0) { printf("rknn_outputs_get fail! ret=%dn", 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=%dn", 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]; // get result 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=%dn", 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.want_float = true; outputs.is_prealloc = false; } ret = rknn_outputs_get(ctx, io_num.n_output, outputs, nullptr); if (ret < 0) { printf("rknn_outputs_get fail! ret=%dn", 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; }
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 ) |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
基于米尔瑞芯微RK3576核心板/开发板的人脸疲劳检测应用方案
626 浏览 0 评论
887 浏览 1 评论
784 浏览 1 评论
1997 浏览 1 评论
3242 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 16:15 , Processed in 0.653986 second(s), Total 73, Slave 56 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号