Смотри вот код, мне надо его разделить так чтобы была иициализация rknn 
и потом запускать инферес по разным картинкам
Переменные не переименовывать, цикл не менять местами


#include "rknn_api.h"

#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include "rknn_postprocess_fun.h"

#define PERF_WITH_POST 1

/*-------------------------------------------
                  Functions
-------------------------------------------*/
static inline int64_t getCurrentTimeUs()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec * 1000000 + tv.tv_usec;
}

static void dump_tensor_attr(rknn_tensor_attr *attr)
{
  char dims[128] = {0};
  int i;
  for (i = 0; i < attr->n_dims; ++i)
  {
    int idx = (int)strlen(dims);
    sprintf(&dims[idx], "%d%s", attr->dims[i], (i == attr->n_dims - 1) ? "" : ", ");
  }
  printf("  index=%d, name=%s, n_dims=%d, dims=[%s], n_elems=%d, size=%d, fmt=%s, type=%s, qnt_type=%s, "
         "zp=%d, scale=%f\n",
         attr->index, attr->name, attr->n_dims, dims, attr->n_elems, attr->size, get_format_string(attr->fmt),
         get_type_string(attr->type), get_qnt_type_string(attr->qnt_type), attr->zp, attr->scale);
}


/*-------------------------------------------
                  Main Functions
-------------------------------------------*/
int rknn_context ctx = 0;

int rknn(unsigned char *input_data)
{

  char *model_path = MODEL_PATH;

  const float nms_threshold = NMS_THRESH;
  const float box_conf_threshold = BOX_THRESH;

  int img_width = 0;
  int img_height = 0;


		int ret = rknn_init(&ctx, model_path, 0, 0, NULL);

		if (ret < 0)
		{
			printf("rknn_init fail! ret=%d\n", ret);
			return -1;
		}

		// Get sdk and driver version
		rknn_sdk_version sdk_ver;
		ret = rknn_query(ctx, RKNN_QUERY_SDK_VERSION, &sdk_ver, sizeof(sdk_ver));
		if (ret != RKNN_SUCC)
		{
			printf("rknn_query fail! ret=%d\n", ret);
			return -1;
		}
		printf("rknn_api/rknnrt version: %s, driver version: %s\n", sdk_ver.api_version, sdk_ver.drv_version);

		// Get Model Input Output Info
		rknn_input_output_num io_num;
		ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
		if (ret != RKNN_SUCC)
		{
			printf("rknn_query fail! ret=%d\n", ret);
			return -1;
		}
		printf("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output);

		printf("input tensors:\n");
		rknn_tensor_attr *input_attrs = (rknn_tensor_attr *)malloc(io_num.n_input * sizeof(rknn_tensor_attr));
		if (!input_attrs) {
			printf("malloc failed for input_attrs\n");
			rknn_destroy(ctx);
			return -1;
		}
		memset(input_attrs, 0, io_num.n_input * sizeof(rknn_tensor_attr));
		uint32_t i;
		for (i = 0; i < io_num.n_input; i++)
		{
			input_attrs[i].index = i;
			// query info
			ret = rknn_query(ctx, RKNN_QUERY_INPUT_ATTR, &(input_attrs[i]), sizeof(rknn_tensor_attr));
			if (ret < 0)
			{
				printf("rknn_init error! ret=%d\n", ret);
				free(input_attrs);
				rknn_destroy(ctx);
				return -1;
			}
			dump_tensor_attr(&(input_attrs[i]));
		}

		printf("output tensors:\n");
		rknn_tensor_attr *output_attrs = (rknn_tensor_attr *)malloc(io_num.n_output * sizeof(rknn_tensor_attr));
		if (!output_attrs) {
			printf("malloc failed for output_attrs\n");
			free(input_attrs);
			rknn_destroy(ctx);
			return -1;
		}
		memset(output_attrs, 0, io_num.n_output * sizeof(rknn_tensor_attr));
		for (i = 0; i < io_num.n_output; i++)
		{
			output_attrs[i].index = i;
			// query info
			ret = rknn_query(ctx, RKNN_QUERY_NATIVE_NHWC_OUTPUT_ATTR, &(output_attrs[i]), sizeof(rknn_tensor_attr));
			if (ret != RKNN_SUCC)
			{
				printf("rknn_query fail! ret=%d\n", ret);
				free(input_attrs);
				free(output_attrs);
				rknn_destroy(ctx);
				return -1;
			}
			dump_tensor_attr(&(output_attrs[i]));
		}

		// Get custom string
		rknn_custom_string custom_string;
		ret = rknn_query(ctx, RKNN_QUERY_CUSTOM_STRING, &custom_string, sizeof(custom_string));
		if (ret != RKNN_SUCC)
		{
			printf("rknn_query fail! ret=%d\n", ret);
			free(input_attrs);
			free(output_attrs);
			rknn_destroy(ctx);
			return -1;
		}
		printf("custom string: %s\n", custom_string.string);

		//rknn_tensor_type input_type = RKNN_TENSOR_UINT8;
		rknn_tensor_type input_type = RKNN_TENSOR_INT8;
		rknn_tensor_format input_layout = RKNN_TENSOR_NHWC;

	
		// Create input tensor memory
		rknn_tensor_mem *input_mems[1];
		// default input type is int8 (normalize and quantize need compute in outside)
		// if set uint8, will fuse normalize and quantize to npu
		input_attrs[0].type = input_type;
		// default fmt is NHWC, npu only support NHWC in zero copy mode
		input_attrs[0].fmt = input_layout;

		input_mems[0] = rknn_create_mem(ctx, input_attrs[0].size_with_stride);

  // Copy input data to input tensor memory
  int width = input_attrs[0].dims[2];
  int stride = input_attrs[0].w_stride;

  if (width == stride)
  {
    memcpy(input_mems[0]->virt_addr, input_data, width * input_attrs[0].dims[1] * input_attrs[0].dims[3]);
  }
  else
  {
    int height = input_attrs[0].dims[1];
    int channel = input_attrs[0].dims[3];
    // copy from src to dst with stride
    uint8_t *src_ptr = input_data;
    uint8_t *dst_ptr = (uint8_t *)input_mems[0]->virt_addr;
    int src_wc_elems = width * channel;
    int dst_wc_elems = stride * channel;
    int h;
    for (h = 0; h < height; ++h)
    {
      memcpy(dst_ptr, src_ptr, src_wc_elems);
      src_ptr += src_wc_elems;
      dst_ptr += dst_wc_elems;
    }
  }
	
	
  // Create output tensor memory
  rknn_tensor_mem **output_mems = (rknn_tensor_mem **)malloc(io_num.n_output * sizeof(rknn_tensor_mem *));
  if (!output_mems) {
    printf("malloc failed for output_mems\n");
    free(input_attrs);
    free(output_attrs);
    rknn_destroy_mem(ctx, input_mems[0]);
    rknn_destroy(ctx);
 
    return -1;
  }
  for (i = 0; i < io_num.n_output; ++i)
  {
    output_mems[i] = rknn_create_mem(ctx, output_attrs[i].size_with_stride);
  }

  // Set input tensor memory
  ret = rknn_set_io_mem(ctx, input_mems[0], &(input_attrs[0]));
  if (ret < 0)
  {
    printf("rknn_set_io_mem fail! ret=%d\n", ret);
    free(input_attrs);
    free(output_attrs);
    for (i = 0; i < io_num.n_output; ++i)
    {
      rknn_destroy_mem(ctx, output_mems[i]);
    }
    free(output_mems);
    rknn_destroy_mem(ctx, input_mems[0]);
    rknn_destroy(ctx);

    return -1;
  }

  // Set output tensor memory
  for (i = 0; i < io_num.n_output; ++i)
  {
    ret = rknn_set_io_mem(ctx, output_mems[i], &(output_attrs[i]));
    if (ret < 0)
    {
      printf("rknn_set_io_mem fail! ret=%d\n", ret);
      free(input_attrs);
      free(output_attrs);
      for (uint32_t j = 0; j < io_num.n_output; ++j) {
        rknn_destroy_mem(ctx, output_mems[j]);
      }
      free(output_mems);
      rknn_destroy_mem(ctx, input_mems[0]);
      rknn_destroy(ctx);

      return -1;
    }
  }

  // Run
  printf("Begin perf ...\n");

	int64_t start_us = getCurrentTimeUs();
	ret = rknn_run(ctx, NULL);
	if (ret < 0)
	{
		printf("rknn run error %d\n", ret);
		free(input_attrs);
		free(output_attrs);
		for (uint32_t j = 0; j < io_num.n_output; ++j)
			rknn_destroy_mem(ctx, output_mems[j]);
		free(output_mems);
		rknn_destroy_mem(ctx, input_mems[0]);
		rknn_destroy(ctx);

		return -1;
	}
	int64_t elapse_us = getCurrentTimeUs() - start_us;
	printf("%4d: Elapse Time = %.2fms, FPS = %.2f\n", i, elapse_us / 1000.f, 1000.f * 1000.f / elapse_us);

  int model_width = 0;
  int model_height = 0;
  if (input_attrs[0].fmt == RKNN_TENSOR_NCHW)
  {
    printf("model is NCHW input fmt\n");
    model_width = input_attrs[0].dims[2];
    model_height = input_attrs[0].dims[3];
  }
  else
  {
    printf("model is NHWC input fmt\n");
    model_width = input_attrs[0].dims[1];
    model_height = input_attrs[0].dims[2];
  }
  // post process
  float scale_w = (float)model_width / img_width;
  float scale_h = (float)model_height / img_height;

  detect_result_group_t detect_result_group;
  memset(&detect_result_group, 0, sizeof(detect_result_group));
  float *out_scales = (float *)malloc(io_num.n_output * sizeof(float));
  int32_t *out_zps = (int32_t *)malloc(io_num.n_output * sizeof(int32_t));
  if (!out_scales || !out_zps) {
    printf("malloc failed for out_scales or out_zps\n");
    free(input_attrs);
    free(output_attrs);
    for (i = 0; i < io_num.n_output; ++i)
      rknn_destroy_mem(ctx, output_mems[i]);
    free(output_mems);
    rknn_destroy_mem(ctx, input_mems[0]);
    rknn_destroy(ctx);

    if (out_scales) free(out_scales);
    if (out_zps) free(out_zps);
    return -1;
  }
  for (i = 0; i < io_num.n_output; ++i)
  {
    out_scales[i] = output_attrs[i].scale;
    out_zps[i] = output_attrs[i].zp;
  }

  post_process((int8_t *)output_mems[0]->virt_addr,
               (int8_t *)output_mems[1]->virt_addr,
               (int8_t *)output_mems[2]->virt_addr,
               640, 640,
               box_conf_threshold, nms_threshold,
               scale_w, scale_h,
               out_zps, out_scales,
               &detect_result_group);

  {
    char text[256];
    int j;
    for (j = 0; j < detect_result_group.count; j++)
    {
      detect_result_t *det_result = &(detect_result_group.results[j]);
      sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100);
      printf("%s @ (%d %d %d %d) %f\n",
             det_result->name,
             det_result->box.left, det_result->box.top, det_result->box.right, det_result->box.bottom,
             det_result->prop);
    }
  }

  // Destroy rknn memory
  rknn_destroy_mem(ctx, input_mems[0]);
  for (i = 0; i < io_num.n_output; ++i)
  {
    rknn_destroy_mem(ctx, output_mems[i]);
  }
  free(output_mems);

  free(input_attrs);
  free(output_attrs);
  free(out_scales);
  free(out_zps);

  // destroy
  rknn_destroy(ctx);


  return 0;
}