demo.cpp 11.5 KB
#include "../ai_platform/stl_aiplatform.h"
#include <chrono>
#include <thread>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>

#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include "opencv2/imgproc/types_c.h"

#include "demo_util.hpp"

using namespace std;
using namespace cv;


static long long get_cur_time(){
    chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
        = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
    return tpMicro.time_since_epoch().count();
}

string get_ipc_url(int gi){
  string ipc_url = "/data/share/data/Street.uvf";
  switch(gi){
    case 0:
      ipc_url = "rtsp://admin:admin@123456@192.168.60.176:554/cam/realmonitor?channel=1&subtype=0";
      break;
    case 1:
      ipc_url = "rtsp://122.97.218.170:8604/openUrl/V5nXRHa?params=eyJwcm90b2NhbCI6InJ0c3AiLCJjbGllbnRUeXBlIjoib3Blbl9hcGkiLCJleHByaWVUaW1lIjotMSwicHJvdG9jb2wiOiJydHNwIiwiZXhwaXJlVGltZSI6MzAwLCJlbmFibGVNR0MiOnRydWUsImV4cGFuZCI6InN0YW5kYXJkPXJ0c3Amc3RyZWFtZm9ybT1ydHAiLCJhIjoiMTBjZjM4N2JjY2Y5NDg3YzhjNWYzNjE2M2ViMWUyNTJ8MXwwfDEiLCJ0IjoxfQ==";
      break;
    case 2:
      ipc_url = "rtsp://admin:ad123456@192.168.10.166:554/cam/realmonitor?channel=1&subtype=0";
      break;
    case 3:
      ipc_url = "rtsp://122.97.218.170:8604/openUrl/LBBYTra?params=eyJwcm90b2NhbCI6InJ0c3AiLCJjbGllbnRUeXBlIjoib3Blbl9hcGkiLCJleHByaWVUaW1lIjotMSwicHJvdG9jb2wiOiJydHNwIiwiZXhwaXJlVGltZSI6MzAwLCJlbmFibGVNR0MiOnRydWUsImV4cGFuZCI6InN0YW5kYXJkPXJ0c3Amc3RyZWFtZm9ybT1ydHAiLCJhIjoiOTgzYjRjMmUxMThlNGU1OTlkYThmMTI3NTkyMGViODV8MXwwfDEiLCJ0IjoxfQ==";
      // ipc_url = "rtsp://122.97.218.170:8604/openUrl/V5nXRHa?params=eyJwcm90b2NhbCI6InJ0c3AiLCJjbGllbnRUeXBlIjoib3Blbl9hcGkiLCJleHByaWVUaW1lIjotMSwicHJvdG9jb2wiOiJydHNwIiwiZXhwaXJlVGltZSI6MzAwLCJlbmFibGVNR0MiOnRydWUsImV4cGFuZCI6InN0YW5kYXJkPXJ0c3Amc3RyZWFtZm9ybT1ydHAiLCJhIjoiMTBjZjM4N2JjY2Y5NDg3YzhjNWYzNjE2M2ViMWUyNTJ8MXwwfDEiLCJ0IjoxfQ==";
      break;
    case 4:
      ipc_url = "/data/share/data/Street.uvf";
      break;
    case 5:
      ipc_url = "/data/share/data/公安局老桥头_CVR15F89410_1465819864_1B.mp4";
      break;
    case 6:
      ipc_url = "/data/share/data/不带头盔2.mp4";
      break;
    case 7:
      ipc_url = "/data/share/data/hczr1.mp4";
      break;
    case 8:
      ipc_url = "/data/share/data/燕高路口高点_CVR_2015-12-30_09-00-00_2015-12-30.mp4";
      break;
    case 9:
      // ipc_url = "/opt/share/data/1-00000002d55_h265.mp4";
      ipc_url = "/data/share/data/duan1.mp4";
      break;
    case 10:
      ipc_url = "/data/share/data/gs1-00000001.mp4";
      break;
    case 11:
      ipc_url = "/data/share/data/98.mp4";
      break;
    case 12:
      ipc_url = "/data/share/data/AV_12345_1_20230108110849847_D_1673147329847729.mp4";
      break;
    case 13:
      ipc_url = "/data/share/data/nx_1-00000002.mp4";
      break;
    case 14:
      ipc_url = "/data/share/data/nx1-0000001.mp4";
      break;
    case 15:
      ipc_url = "/data/share/data/nx1-00000002.mp4";
      break;
    case 16:
      ipc_url = "/data/share/data/Street.uvf";
      break;
    case 17:
      ipc_url = "/data/share/data/xiancun.mp4";
      break;
    case 18:
      ipc_url = "rtsp://192.168.60.146:8554/w_1_out";
      break;
    case 19:
      ipc_url = "/data/share/data/42_44.avi";
      break;
    case 20:
      ipc_url = "/data/share/data/37_70.avi";
      break;
    case 21:
      ipc_url = "/data/share/data/192_25.avi";
      break;
    case 22:
      ipc_url = "/data/share/data/31_4.avi";
      break;
    case 23:
      ipc_url = "/data/share/data/146_192.avi";
      break;
    case 24:
      ipc_url = "/data/share/data/146_187.mp4";
      break;
    case 25:
      // 公安内网
      ipc_url = "rtsp://192.168.51.200:8554/320202202308231001001002";
      break;
    case 26:
      // FFmpeg生成的解码错误数据流
      ipc_url = "rtsp://192.168.10.4:8554/street";
      break;
    case 27:
      ipc_url = "/data/share/data/Street_4k_265.mp4";
      break;
    case 28:
      ipc_url = "http://192.168.60.179:10016/公安局老桥头_CVR15F89410_1465819864_1B.mp4";
      break;
    case 29:
      ipc_url = "http://192.168.60.179:10016/不带头盔2.mp4";
      break;
    case 30:
      ipc_url = "/data/share/data/149_45.avi";
      break;
    case 31:
      ipc_url = "/data/share/data/146_88.avi";
      break;
    case 32:
      ipc_url = "/data/share/data/141_74.avi";
      break;
    default:
      ipc_url = "/data/share/data/Street.uvf";
      break;
  }

  return ipc_url;
}

static int nTaskId = 0;

string createTask(void *handle, int gi, bool bFlag = true){
  task_param tparam;

  tparam.ipc_url = get_ipc_url(gi);

  if (bFlag){
    nTaskId = gi;
  }
  std::string task_id_str = "test_task_id_" + std::to_string(nTaskId);
  tparam.task_id = task_id_str;
  nTaskId++;

  tparam.result_folder_little = "./res";         //目标快照抠图保存地址
	tparam.result_folder = "./res";                //目标快照大图保存地址

  const int result_code = add_task(handle, tparam);
  if (result_code != 0)
    printf("[Error]: ");
  printf("--- task_id: %s result code: %d\n", tparam.task_id.c_str(), result_code);

  return task_id_str;
}

void show_result(ObjectData obj)
{
  string str_dir = "./res/result/";
  string file_name = obj.task_id + "_" + to_string(obj.object_id) + "_" + obj.str_ts_ms;
  string data_file_name =  str_dir + file_name + ".txt";
  std::ofstream res_file(data_file_name);

  res_file << obj.task_id << "  " << obj.task_frame_count << endl;
  res_file << obj.index << " " << obj.object_id << " " << obj.left << " " << obj.top << " " << obj.right << " " << obj.bottom << " " << obj.confidence << endl;

  cv::Mat image = cv::imread(obj.ori_pic_path);

  cv::Rect rc(obj.left, obj.top,obj.right - obj.left, obj.bottom - obj.top);
  const cv::Scalar color(0, 0, 255);
  cv::rectangle(image, rc, color);

  if (obj.index == 0)
  {// person
    res_file << "person:" << endl;
    res_file << "hair: " << hair[obj.hp_cls[0].res_index] << " " << obj.hp_cls[0].res_prob << endl;
    res_file << "hair_color: " << hair_color[obj.hp_cls[1].res_index] << " " << obj.hp_cls[1].res_prob << endl;
    res_file << "eye_glass: " << eye_glass[obj.hp_cls[2].res_index] << " " << obj.hp_cls[2].res_prob << endl;
    res_file << "mask: " << mask[obj.hp_cls[3].res_index] << " " << obj.hp_cls[3].res_prob << endl;
    res_file << "up cloth: " << up_cloth[obj.hp_cls[4].res_index] << " " << obj.hp_cls[4].res_prob << endl;
    res_file << "up_cloth_color: " << up_cloth_color[obj.hp_cls[5].res_index] << " " << obj.hp_cls[5].res_prob << endl;
    res_file << "clothing context: " << clothing_text[obj.hp_cls[6].res_index] << " " << obj.hp_cls[6].res_prob << endl;
    res_file << "down cloth: " << down_cloth[obj.hp_cls[7].res_index] << " " << obj.hp_cls[7].res_prob << endl;
    res_file << "down cloth color: " << down_cloth_color[obj.hp_cls[8].res_index] << " " << obj.hp_cls[8].res_prob << endl;
    res_file << "bag: " << bag[obj.hp_cls[9].res_index] << " " << obj.hp_cls[9].res_prob << endl;
    res_file << "sex: " << sex[obj.hp_cls[10].res_index] << " " << obj.hp_cls[10].res_prob << endl;
    res_file << "age: " << age[obj.hp_cls[11].res_index] << " " << obj.hp_cls[11].res_prob << endl;
    res_file << "viewpoint: " << viewpoint[obj.hp_cls[12].res_index] << " " << obj.hp_cls[12].res_prob << endl;
    res_file << "umbrella: " << umbrella[obj.hp_cls[13].res_index] << " " << obj.hp_cls[13].res_prob << endl;
    res_file << "hold_baby: " << hold_baby[obj.hp_cls[14].res_index] << " " << obj.hp_cls[14].res_prob << endl;
    res_file << "person state: " << personstate[obj.hp_cls[15].res_index] << " " << obj.hp_cls[15].res_prob << endl;

    for (size_t i = 0; i < HUMANREID_FEATURE_SIZE; i++)
    {
      auto hp_cls = obj.hp_feature[i];
      res_file << obj.hp_feature[i] << " ";
    }
    res_file << endl;
  } 
  else if (obj.index >= 4 && obj.index <= 8)
  {// vehicle
    res_file << "vehicle:" << endl;
    if (obj.vehicle_color_index >= 0)
    {
      res_file << "color:" << vehicle_color[obj.vehicle_color_index] << " " << obj.vehicle_color_prob << endl;
    }
    
    for (size_t i = 0; i < obj.vec_vpd_cls.size(); i++)
    {
      auto info = obj.vec_vpd_cls[i];
      if (info.index >= 0)
      {
        res_file << vechicle_pendant[info.index] << " " << info.confidence << " " << info.left_ << " " << info.top_ << " " << info.width_ << " " << info.height_ << endl;
      }

      cv::Rect rc(info.left_, info.top_, info.width_, info.height_);
      const cv::Scalar color(0, 255, 255);
      cv::rectangle(image, rc, color);
    }

    auto& plate_info = obj.vehicle_plate;
    res_file << "plate:" << plate_info.left_ << " " << plate_info.top_ << " " << plate_info.width_ << " " << plate_info.height_ << " " << plate_info.detect_score << endl;
    cv::Rect rc(plate_info.left_, plate_info.top_, plate_info.width_, plate_info.height_);
    const cv::Scalar color(0, 255, 0);
    cv::rectangle(image, rc, color);
    res_file << "plate number:" ;
    for (size_t i = 0; i < 8; i++)
    {
      res_file << plate_info.recg[i].character << " " << plate_info.recg[i].maxprob << " ";
    }
    res_file << endl;
    res_file << "num_score:" << plate_info.num_score << endl;
    res_file << "type:" << plate_type[plate_info.type] << endl;
    res_file << "state:" << plate_state[plate_info.state] << " " << plate_info.state_score << endl;
  }

  res_file.close();
  
  string result_path = str_dir + file_name + ".jpg";
  cv::imwrite(result_path, image);
  image.release();
}

void test_gpu(int gpuID){
    tsl_aiplatform_param vptParam;
    vptParam.gpuid = gpuID;
    vptParam.models_dir = ".";

    vptParam.log_days = 1;
    vptParam.log_level = AI_LOG_LEVEL_TRACE;
    // vptParam.log_level = AI_LOG_LEVEL_DEBUG;
    vptParam.log_mem = 64 * 1024 * 1024; // 64MB.
    vptParam.log_path = "logs/main.log";

    vptParam.result_cbk = show_result;

    void *handle;
    int flag = tsl_aiplatform_init(&handle, vptParam);
    if (0 != flag) {
        printf("Init Failed! Error Code: %d\n", flag);
        return;
    } else {
        printf("Init Success\n");
    }

    CreateResultFolder("./res/result", "");

  // createTask(handle, algor_vec2, 19, false);
  // createTask(handle, algor_vec2, 20, false);
  // createTask(handle, algor_vec2, 21, false);
  // createTask(handle, algor_vec2, 22, false);
  // createTask(handle, algor_vec2, 23, false);
  // createTask(handle, algor_vec2, 24, false);
  // createTask(handle, algor_vec2, 30, false);
  // createTask(handle, algor_vec2, 31, false);
  string task_id = createTask(handle, 4, false);

  bool bFlag = true;
  while (bFlag){
    char op = getchar();
    switch (op)
    {
      case 'a':
        task_id = createTask(handle, 4, false);
        break;
      case 'c':
        finish_task(handle, task_id, 0);
        break;
      case 'g':
        close_all_task(handle);
        break;
      case 'q':
        close_all_task(handle);
        bFlag = false;
        break;
      default:
        break;
    }
  }

  tsl_aiplatform_release(&handle);
}

int main(int argc, char *argv[]) {
    printf("new test\n");

    // if (argc < 4) {
    //   fprintf(stderr, "./xxx 0 2 10 1 ## [start_ai_id, end_ai_id) repeat_num gpu_id\n");
    //   return -1;
    // }

    // //! load params.
    // int start_id = atoi(argv[1]);
    // int end_id = atoi(argv[2]);
    // int repeat_num = atoi(argv[3]);
    // int gpuID = atoi(argv[4]);

    test_gpu(0);
    // test_gpu(1);
    // test_gpu(2);
    // test_gpu(3);


    printf("Done.\n"); 

    return 0;
}