- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <fstream>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <sys/time.h>
- #include "rknn_api.h"
- #include "opencv2/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- using namespace std;
- int main(int argc, char** argv)
- {
- const char *img_path = "../tmp/stand-06_13_02-01374.jpg";
- const char *model_path = "../tmp/rensnet50.rknn";
- const int output_elems = 2;
- const int img_width = 224;
- const int img_height = 224;
- const int img_channels = 3;
- const int input_index = 0; // node name "input" 输入输出节点的索引??
- const int output_index = 0; // node name "MobilenetV1/Predictions/Reshape_1"
- // Load image
- cv::Mat img = cv::imread(img_path, 1);
- if(!img.data) {
- printf("cv::imread %s fail!\n", img_path);
- return -1;
- }
- if(img.cols != img_width || img.rows != img_height)
- cv::resize(img, img, cv::Size(img_width, img_height), (0, 0), (0, 0), cv::INTER_LINEAR);
- //BGR->RGB
- //cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
- // Load model
- FILE *fp = fopen(model_path, "rb");
- if(fp == NULL) {
- printf("fopen %s fail!\n", model_path);
- return -1;
- }
- fseek(fp, 0, SEEK_END); //重定位流(数据流/文件)上的文件内部位置指针
- int model_len = ftell(fp); //函数 ftell() 用于得到文件位置指针当前位置相对于文件首的偏移字节数
- void *model = malloc(model_len);
- fseek(fp, 0, SEEK_SET); //SEEK_SET开头-0,SEEK_CUR当前位置-1,SEEK_END结束-2
- if(model_len != fread(model, 1, model_len, fp)) { //权重读到model中
- printf("fread %s fail!\n", model_path);
- free(model);
- return -1;
- }
- // Start Inference
- rknn_input inputs[1]; //输入结构体,index,buf:指向数据的指针,size,pass_through:数据是否需要转换,type:数据的类型,fmt:数据的格式
- rknn_output outputs[1]; //want_float,is_prealloc,index,buf,size
- rknn_tensor_attr output0_attr; //index,n_dims,dims,name,n_elems,size,fmt:NCHW-NHWC,type,qnt_type,f1,zp,scale
- int ret = 0;
- rknn_context ctx = 0; //模型的对象?
- ret = rknn_init(&ctx, model, model_len, RKNN_FLAG_PRIOR_MEDIUM | RKNN_FLAG_COLLECT_PERF_MASK); //中优先级,打开测试开关
- if(ret < 0) {
- printf("rknn_init fail! ret=%d\n", ret);
- goto Error;
- }
- output0_attr.index = 0; //设置
- ret = rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, &output0_attr, sizeof(output0_attr)); //查询ctx对象输出属性
- if(ret < 0) {
- printf("rknn_query fail! ret=%d\n", ret);
- goto Error;
- }
- inputs[0].index = input_index; //这个索引搞得不是很清楚
- inputs[0].buf = img.data; //装载数据
- inputs[0].size = img_width * img_height * img_channels; //占内存大小
- 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);
- goto Error;
- }
- ret = rknn_run(ctx, nullptr);
- if(ret < 0) {
- printf("rknn_run fail! ret=%d\n", ret);
- goto Error;
- }
- outputs[0].want_float = true;
- outputs[0].is_prealloc = false;
- ret = rknn_outputs_get(ctx, 1, outputs, nullptr); //从ctx中将output取出
- if(ret < 0) {
- printf("rknn_outputs_get fail! ret=%d\n", ret);
- goto Error;
- }
- // Process output
- if(outputs[0].size == output0_attr.n_elems * sizeof(float))
- {
- for(int i = 0;i <output0_attr.n_elems;i++){
- std::cout << "outputs " << i << " : " << ((float*)outputs[0].buf)[i] << std::endl;
- }
- std::cout << "outputs[0].size : " << outputs[0].size << std::endl;
- std::cout << "outputs0_attr.n_elems : " << output0_attr.n_elems << std::endl;
- }
- else
- {
- printf("rknn_outputs_get fail! get output_size = [%d], but expect %u!\n",
- outputs[0].size, (uint32_t)(output0_attr.n_elems * sizeof(float)));
- }
- rknn_outputs_release(ctx, 1, outputs);
- Error:
- if(ctx > 0) rknn_destroy(ctx);
- if(model) free(model);
- if(fp) fclose(fp);
- return 0;
- }
复制代码
报错欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |