Blame view

src/ai_engine_module/HumanFeature.cpp 2.31 KB
6fdcb6a5   Hu Chunming   初次提交,代码大体完成编写,完善中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  #include "HumanFeature.h"
  #include "humanreid.h"
  
  
  HumanFeature::HumanFeature(/* args */)
  {
  }
  
  HumanFeature::~HumanFeature()
  {
      release();
  }
  
  int HumanFeature::init(int dev_id, string model_dir){
  
      string model_path = model_dir + "/models/human_reid2/humanreid_310p.om" ;
  
      LOG_INFO("hp 版本:{}  模型路径:{}", humanreid_get_version(), model_path);
  
      humanreid_param param;
      char modelNames[100];
      strcpy(modelNames, model_path.c_str());
      param.modelNames = modelNames;
      param.devId = dev_id;
  
      m_devId = param.devId;
      ACL_CALL(aclrtSetDevice(m_devId), ACL_SUCCESS, -1);
      ACL_CALL(aclrtCreateContext(&m_algorthim_ctx, m_devId), ACL_SUCCESS, -1);
  
      int ret = humanreid_init(&m_handle, param);
      if(ret != 0){
          LOG_ERROR("humanreid_init error.");
          return -1;
      }
  
      return 0;
  }
  
  vector<HFResult> HumanFeature::process(vector<DeviceMemory*> vec_gpuMem) {
      int batchsize = vec_gpuMem.size();
  
      vector<sy_img> batch_img;
      for (int i = 0; i < vec_gpuMem.size(); i++) {
          DeviceMemory* mem = vec_gpuMem[i];
  
          sy_img img;
          img.w_ = mem->getWidth();     
          img.h_ = mem->getHeight();
          img.c_ = mem->getChannel();   
          img.data_ = mem->getMem();
          batch_img.push_back(img);
      }
  
      float ** feature=new float*[batchsize];
      for(int b=0;b<batchsize;b++)
      {
          feature[b]=new float[HUMANREID_FEATURESIZE];
      }
  
      int ret = SY_SUCCESS;
      do
      {
          ret = humanreid_batch(m_handle, batch_img.data(), batchsize, feature);
          if (SY_SUCCESS != ret) {
              printf("humanFeature process failed!");
              ret = SY_FAILED;
              break;
          }
      } while (0);
  
      vector<HFResult> vec_hf_res;
      
      for(int batchIdx = 0;batchIdx<batchsize;batchIdx++){ 
          //debug
          HFResult res;
          memcpy(res.feature, feature[batchIdx], sizeof(float) * HUMANREID_FEATURESIZE);
          vec_hf_res.push_back(res);
      
          if (feature[batchIdx]!=NULL)
              delete[] feature[batchIdx];
      }
  
      if (feature!=NULL) {
          delete feature;
          feature = nullptr;
      }
  
      return vec_hf_res;
  }
  
  void HumanFeature::release(){
      if (m_handle){
          humanreid_release(&m_handle);
          m_handle = NULL;
      }
      if(m_algorthim_ctx){
          aclrtDestroyContext(m_algorthim_ctx);
      }
  }