Blame view

test/main.cpp 9.08 KB
85cc8cb9   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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  /*
   * @Author: yangzilong
   * @LastEdit: yangzilong
   * @Date: 2021-11-24 16:42:38
   * @Email: yangzilong@objecteye.com
   * @Description:
   */
  
  #ifdef _MSC_VER
  
  #include <windows.h>
  #endif
  #include "header.h"
  #include "opencv2/highgui/highgui.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include "opencv2/opencv.hpp"
  #include "stl_aiplatform.h"
  #include "sy_common.h"
  #include "time.h"
  #include <chrono>
  #include <iostream>
  #include <memory>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string>
  #include <vector>
  #ifdef PRINT_LOG
  #include "spdlog/spdlog.h"
  #endif
  #ifdef _MSC_VER
  #include <direct.h>
  #include <io.h>
  #define ACCESS _access
  #define MKDIR(a) _mkdir((a))
  #else
  #include <stdarg.h>
  #include <sys/stat.h>
  #include <unistd.h>
  #define ACCESS access
  #define MKDIR(a) mkdir((a), 0755)
  #define TRUE 1
  #define FALSE 0
  #define Sleep(a) usleep((a)*1000)
  typedef int BOOL;
  typedef unsigned int DWORD;
  typedef void *LPVOID;
  typedef char _TCHAR;
  #endif
  #ifdef _DEBUG
  
  #else
  #pragma comment(lib, "mvpt.lib") //???
  #endif
  
  using namespace std;
  
  #include "./utils.hpp"
  #include <chrono>
  #include <random>
  #include <thread>
  
  
  
  algorithm_type_t algor_index_to_algor_type(const int &idx) {
    switch (idx) {
    case 0:
      return algorithm_type_t::FACE_SNAPSHOT;
    case 1:
      return algorithm_type_t::HUMAN_SNAPSHOT;
    case 2:
      return algorithm_type_t::VEHICLE_SNAPSHOT;
    case 3:
      return algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT;
    default:
      return algorithm_type_t::UNKNOWN;
    }
  }
  
  void init_mq_conn(void *handle) {
    for (auto key : {mq_type_t::ALARM_MQ, mq_type_t::GET_TASK_MQ, mq_type_t::HEART_BEAT_MQ}) {
      rabbitmq_conn_params_t mq_conn_params;
      
      mq_conn_params.port = 5673;
      strcpy(mq_conn_params.ip, "192.168.10.8");
      strcpy(mq_conn_params.uname, "guest");
      strcpy(mq_conn_params.passwd, "guest");
      strcpy(mq_conn_params.vhost, "/");
      strcpy(mq_conn_params.exchange, "tsl.test.topic");
      strcpy(mq_conn_params.exchange_type, "topic");
  
      switch (key) {
  
      case mq_type_t::ALARM_MQ: {
        strcpy(mq_conn_params.queue, "tsl.test.queue.alarm");
        strcpy(mq_conn_params.routing_key, "tsl.test.queue.alarm.key");
      } break;
      case mq_type_t::GET_TASK_MQ: {
        strcpy(mq_conn_params.queue, "tsl.test.queue.get");
        strcpy(mq_conn_params.routing_key, "tsl.test.queue.get.key");
      } break;
      case mq_type_t::HEART_BEAT_MQ: {
        strcpy(mq_conn_params.queue, "tsl.test.queue.hb");
        strcpy(mq_conn_params.routing_key, "tsl.test.queue.hb.key");
      } break;
      }
  
      mq_conn_params.durable_exchange = false;
      mq_conn_params.durable_queue = false;
  
      if (0 != add_mq_conn(handle, key, mq_conn_params))
        fprintf(stderr, "ip is %s port is %d\n", mq_conn_params.ip, mq_conn_params.port);
    }
  }
  
78676b64   Hu Chunming   删除无用代码
115
116
  const char* ipc_url = "/home/cmhu/tongtu/tsl_aiplatform_project_with_screenshot/data/duan1.avi";
  
85cc8cb9   Hu Chunming   原版代码
117
118
119
120
  void set_ipc_url(task_param &tparam, const algorithm_type_t &algor_type) {
  
  #if 1
    if (algor_type == algorithm_type_t::FACE_SNAPSHOT) {
78676b64   Hu Chunming   删除无用代码
121
      tparam.ipc_url = ipc_url;
85cc8cb9   Hu Chunming   原版代码
122
    } else if (algor_type == algorithm_type_t::HUMAN_SNAPSHOT) {
78676b64   Hu Chunming   删除无用代码
123
      tparam.ipc_url = ipc_url;
85cc8cb9   Hu Chunming   原版代码
124
125
    } 
    else if (algor_type == algorithm_type_t::VEHICLE_SNAPSHOT)
78676b64   Hu Chunming   删除无用代码
126
      tparam.ipc_url = ipc_url;
85cc8cb9   Hu Chunming   原版代码
127
    else if (algor_type == algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT)
78676b64   Hu Chunming   删除无用代码
128
      tparam.ipc_url = ipc_url;
85cc8cb9   Hu Chunming   原版代码
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
   
  #else
    tparam.ipc_url = "rtmp://192.168.10.56:1935/objecteye/1";
  #endif
  }
  
  void set_task_params(task_param &tparam, const unsigned &idx, const algorithm_type_t &algor_type) {
    auto algor_init_params = new algor_init_config_param_t;
    switch (algor_type) {
    case algorithm_type_t::FACE_SNAPSHOT: {
      auto basic_params = new algor_basic_config_param_t;
      {
        basic_params->algor_valid_rect.top_ = 0;
        basic_params->algor_valid_rect.left_ = 0;
        basic_params->algor_valid_rect.width_ = 1920;
        basic_params->algor_valid_rect.height_ = 1080;
  
        basic_params->result_folder = "res/face";
        ;
        basic_params->result_folder_little = "res/face_little";
        ;
      }
      auto algor_params = new algor_config_param_snapshot;
      { algor_params->threshold = 0.5f; }
  
      algor_init_params->algor_param = algor_params;
      algor_init_params->basic_param = basic_params;
    } break;
  
    case algorithm_type_t::HUMAN_SNAPSHOT: {
      auto basic_params = new algor_basic_config_param_t;
      {
        basic_params->algor_valid_rect.top_ = 0;
        basic_params->algor_valid_rect.left_ = 0;
        basic_params->algor_valid_rect.width_ = 1920;
        basic_params->algor_valid_rect.height_ = 1080;
  
        basic_params->result_folder = "res/human";
        basic_params->result_folder_little = "res/human_little";
      }
      auto algor_params = new algor_config_param_snapshot;
      { algor_params->threshold = 0.5f; }
  
      algor_init_params->algor_param = algor_params;
      algor_init_params->basic_param = basic_params;
    } break;
  
    case algorithm_type_t::HUMAN_GATHER: {
  
      auto algor_params = new algor_config_param_human_gather;
      {
        algor_params->frame_stride = 1;
        // algor_params->human_count_threshold = 3;
        algor_params->human_count_threshold = 1;
      }
  
      auto basic_params = new algor_basic_config_param_t;
      {
        basic_params->algor_valid_rect.top_ = 0;
        basic_params->algor_valid_rect.left_ = 0;
        basic_params->algor_valid_rect.width_ = 1920;
        basic_params->algor_valid_rect.height_ = 1080;
        basic_params->result_folder = "res/gather";
        basic_params->result_folder_little = "res/gather_little";
      }
  
      algor_init_params->algor_param = algor_params;
      algor_init_params->basic_param = basic_params;
    } break;
  
    case algorithm_type_t::VEHICLE_SNAPSHOT: {
  
      auto basic_params = new algor_basic_config_param_t;
      {
        basic_params->algor_valid_rect.top_ = 0;
        basic_params->algor_valid_rect.left_ = 0;
        basic_params->algor_valid_rect.width_ = 1920;
        basic_params->algor_valid_rect.height_ = 1080;
        basic_params->result_folder = "res/vehicle";
        basic_params->result_folder_little = "res/vehicle_little";
      }
  
      auto algor_params = new algor_config_param_snapshot;
      { algor_params->threshold = 0.5f; }
  
      algor_init_params->algor_param = algor_params;
      algor_init_params->basic_param = basic_params;
    } break;
  
    case algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT: {
  
      auto basic_params = new algor_basic_config_param_t;
      {
        basic_params->algor_valid_rect.top_ = 0;
        basic_params->algor_valid_rect.left_ = 0;
        basic_params->algor_valid_rect.width_ = 1920;
        basic_params->algor_valid_rect.height_ = 1080;
        basic_params->result_folder = "res/nonmotor";
        basic_params->result_folder_little = "res/nonmotor_little";
      }
  
      auto algor_params = new algor_config_param_snapshot;
      { algor_params->threshold = 0.5f; }
  
      algor_init_params->algor_param = algor_params;
      algor_init_params->basic_param = basic_params;
    } break;
  
    default: {
      if (algor_init_params != nullptr) {
        delete algor_init_params;
        algor_init_params = nullptr;
      }
      return;
    } break;
    }
  
    tparam.algor_config_params[idx].algor_type = algor_type;
    tparam.algor_config_params[idx].algor_init_config_param = algor_init_params;
  }
  
  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]);
  
    void *handle;
  
    tsl_aiplatform_param vptParam;
    vptParam.gpuid = gpuID;
    vptParam.trt_serialize_file = "";
  
    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";
  
  #ifdef PRINT_LOG
    spdlog::set_level(spdlog::level::debug);
    spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
  #endif
  
    int flag = tsl_aiplatform_init(&handle, vptParam);
  
    if (0 != flag) {
      printf("Init Failed! Error Code: %d\n", flag);
      system("pause");
      return 0;
    } else {
      printf("Init Success\n");
    }
  
  #ifdef POST_USE_RABBITMQ
  
  #if 1
    init_mq_conn(handle);
  #endif
  
  #endif // #ifdef POST_USE_RABBITMQ
  
    printf("Finish init mq\n");
    srand(time(nullptr));
    printf("begin add task\n");
    unsigned g_idx = 0;
  
85cc8cb9   Hu Chunming   原版代码
303
304
    for (int i = start_id; i < end_id; i++) {
      for (int j = 0; j < repeat_num; ++j) {
85cc8cb9   Hu Chunming   原版代码
305
306
307
308
309
310
311
312
  
        task_param tparam;
  
        const auto algor_type = algor_index_to_algor_type(i);
        set_ipc_url(tparam, algor_type);
  
        tparam.algor_counts = 1;
  
78676b64   Hu Chunming   删除无用代码
313
        std::string task_id_str = "test_task_id_" + to_string(g_idx++);
85cc8cb9   Hu Chunming   原版代码
314
315
316
317
318
        tparam.task_id = task_id_str.c_str();
        tparam.algor_config_params = new algor_config_param[tparam.algor_counts];
  
        for (int idx = 0; idx < tparam.algor_counts; ++idx) {
          set_task_params(tparam, idx, algor_type);
85cc8cb9   Hu Chunming   原版代码
319
320
321
322
323
324
        }
  
        const int result_code = add_task(handle, tparam);
        if (result_code != 0)
          printf("[Error]: ");
        printf("task_id: %s algort type %d result code: %d\n", tparam.task_id, int(algor_type), result_code);
85cc8cb9   Hu Chunming   原版代码
325
326
327
328
      }
    }
  
  
78676b64   Hu Chunming   删除无用代码
329
    while (getchar() != 'q');
85cc8cb9   Hu Chunming   原版代码
330
331
332
333
334
335
  
    tsl_aiplatform_release(&handle);
    printf("Done.\n");
  
    return 0;
  }