Blame view

src/helpers/gen_json.hpp 13.6 KB
0b4cd5d5   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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  /*
   * @Author: yangzilong
   * @Last Modified by: yangzilong
   * @Date: 2021-11-24 18:25:14
   * @Email: yangzilong@objecteye.com
   * @Description:
   */
  
  #pragma once
  
  #include <string>
  #include "json/json.h"
  #include "time_helper.hpp"
  #include "../common/logger.hpp"
  #include "../ai_platform/header.h"
  #include "../ai_platform/common_header.h"
  
  namespace helpers
  {
      namespace gen_json
      {
  
          static std::string ORI_IMAGE_PATH_PLACEHOLDER = "___ORI_IMAGE_PATH_PLACEHOLDER___";
          static std::string ROI_IMAGE_PATH_PLACEHOLDER = "___ROI_IMAGE_PATH_PLACEHOLDER___";
  
          static auto get_builder(const int float_precision = 5) -> Json::StreamWriterBuilder
          {
              Json::StreamWriterBuilder builder;
              builder["indentation"  ] = "";
              builder["emitUTF8"     ] = true;
              builder["precision"] = float_precision;
              builder["precisionType"] = "decimal";
              return builder;
          }
  
          static std::string gen_delete_task_json(const std::string &taskid, int error_code)
          {
              Json::Value root;
              root["task_id"] = taskid;
              root["code"] = std::to_string(error_code);
              return Json::writeString(get_builder(), root);
          }
  
          static std::string gen_task_status_json(const std::vector<std::string> &taskids, const std::vector<int> &statues)
          {
              if (taskids.size() != statues.size())
              {
                  LOG_ERROR("number of task_id must equal number of statues\n");
                  return "";
              }
  
              Json::Value root;
              for (int i = 0; i < taskids.size(); ++i)
              {
                  Json::Value item;
                  item["task_id"] = taskids[i];
                  item["status"] = std::to_string(statues[i]);
                  root.append(item);
              }
  
              return Json::writeString(get_builder(), root);
          };
  
          static std::string gen_office_task_heart_beat_json(const std::vector<std::string> &taskids)
          {
              Json::Value root;
              for (int i = 0; i < taskids.size(); ++i)
              {
                  Json::Value item;
                  item["task_id"] = taskids[i];
                  root.append(item);
              }
  
              return Json::writeString(get_builder(), root);
          };
  
  
          static std::string gen_face_detection_json(const std::string &taskid,
                                                     const int &objid, const std::string &roi_fpath, const std::string &ori_fpath,
                                                     const sy_rect &box, const float &score, const sy_point *kpts,
                                                     const int &n_kpts = 25)
          {
              Json::Value root;
              root["task_id"] = taskid;
              root["object_id"] = objid;
              root["algor_type"] = (int)algorithm_type_t::FACE_SNAPSHOT;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
              {
                  Json::Value data;
                  data["snapshot_image_path"] = roi_fpath;
                  data["video_image_path"] = ori_fpath;
  
  
                  {
                      Json::Value boxes;
                      {
                          Json::Value boxNode;
                          boxNode["top"] = box.top_;
                          boxNode["left"] = box.left_;
                          boxNode["right"] = box.left_ + box.width_;
                          boxNode["bottom"] = box.top_ + box.height_;
                          boxNode["score"] = score;
                          {
  
                              Json::Value kptsNode;
                              for (int i = 0; i < n_kpts; ++i)
                              {
                                  Json::Value kp;
                                  kp.append(kpts[i].x_);
                                  kp.append(kpts[i].y_);
                                  kptsNode.append(kp);
                              }
                              boxNode["ldmk"] = kptsNode;
                          }
                          boxes.append(boxNode);
                      }
                      data["box"] = boxes;
                  }
                  root["data"] = data;
              }
  
              return Json::writeString(get_builder(), root);
          };
  
          static std::string gen_snapshot_json(const algorithm_type_t &algor_type, video_object_snapshot const &algo_result)
          {
              Json::Value root;
              root["task_id"] = algo_result.task_id;
              root["algor_type"] = (int)algor_type;
              root["object_id"] = algo_result.object_id;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
  
              {
                  Json::Value dataNode;
                  Json::Value boxNode;
                  {
                      Json::Value boxNodeItem;
                      boxNodeItem["top"] = algo_result.obj_info.res_top;
                      boxNodeItem["left"] = algo_result.obj_info.res_left;
                      boxNodeItem["right"] = algo_result.obj_info.res_right;
                      boxNodeItem["bottom"] = algo_result.obj_info.res_bottom;
                      boxNodeItem["score"] = algo_result.obj_info.res_prob;
                      boxNodeItem["index"] = algo_result.obj_info.res_index; //221212byzsh
  
                      boxNode.append(boxNodeItem);
                      dataNode["box"] = boxNode;
                  }
                  dataNode["video_image_path"] = algo_result.video_image_path;
                  dataNode["snapshot_image_path"] = algo_result.snapshot_image_path;
                  root["data"] = dataNode;
  
              }
              return Json::writeString(get_builder(), root);
          };
  
          static std::string gen_multi_obj_json(const algorithm_type_t &algor_type, video_object_snapshot const &algo_result)
          {
              Json::Value root;
              root["task_id"] = algo_result.task_id;
              root["algor_type"] = (int)algor_type;
              root["object_id"] = algo_result.object_id;
              root["bfinished"] = algo_result.nFinished;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
  
              if(algo_result.nFinished == 0){
                  Json::Value dataNode;
                  Json::Value boxNode;
  
                  Json::Value boxNodeItem;
                  boxNodeItem["top"] = algo_result.obj_info.res_top;
                  boxNodeItem["left"] = algo_result.obj_info.res_left;
                  boxNodeItem["right"] = algo_result.obj_info.res_right;
                  boxNodeItem["bottom"] = algo_result.obj_info.res_bottom;
                  boxNodeItem["score"] = algo_result.obj_info.res_prob;
                  boxNodeItem["index"] = algo_result.obj_info.res_index; //221212byzsh
                  boxNode.append(boxNodeItem);
  
                  dataNode["box"] = boxNode;
  
                  dataNode["video_image_path"] = algo_result.video_image_path;
                  dataNode["snapshot_image_path"] = algo_result.snapshot_image_path;
                  root["data"] = dataNode;
              }
  
              return Json::writeString(get_builder(), root);
          };
  
          static std::string gen_generic_json(const std::string& taskId, long object_id,
                                              const box_t &box,
                                              const algorithm_type_t &algor_type,
                                              const std::string &image_path)
          {
              Json::Value root;
              root["task_id"] = taskId;
              root["object_id"] = int(object_id);
              root["algor_type"] = static_cast<int>(algor_type);
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
              {
                  Json::Value dataNode;
                  Json::Value boxNode;
                  {
                      Json::Value boxNodeItem;
                      boxNodeItem["top"] = box.top;
                      boxNodeItem["left"] = box.left;
                      boxNodeItem["right"] = box.right;
                      boxNodeItem["bottom"] = box.bottom;
                      boxNodeItem["score"] = box.score;
  
                      boxNode.append(boxNodeItem);
                      dataNode["box"] = boxNode;
                  }
  
                  dataNode["video_image_path"] = ORI_IMAGE_PATH_PLACEHOLDER;
                  dataNode["snapshot_image_path"] = ("" == image_path) ? ROI_IMAGE_PATH_PLACEHOLDER : image_path;
                  root["data"] = dataNode;
              }
  
  
              return Json::writeString(get_builder(), root);
          }
  
  
          static std::string gen_retrograde_json(const std::string& taskId, int object_id,
                                                 const box_t &box,
                                                 const algorithm_type_t &algor_type,
                                                 const std::string &image_path = "")
          {
              return gen_generic_json(taskId, object_id, box, algor_type, image_path);
          }
  
          static std::string gen_pedestrian_safety_json(const std::string& taskId, int object_id,
                                                        const box_t &box,
                                                        const algorithm_type_t &algor_type,
                                                        const std::string &image_path = "")
          {
              return gen_generic_json(taskId, object_id, box, algor_type, image_path);
          }
  
  
          static std::string gen_takeaway_member_cls_json(const std::string& taskId, int object_id,
                                                          const box_t &box, const int &category,
                                                          const std::string &image_path = "")
          {
              return gen_generic_json(taskId, object_id, box, algorithm_type_t::TAKEAWAY_MEMBER_CLASSIFICATION, image_path);
          }
  
          static std::string gen_human_gather_json(const std::string& taskId, std::vector<box_t> boxes, const std::string &image_path)
          {
              Json::Value root;
              root["task_id"] = taskId;
              root["algor_type"] = (int)algorithm_type_t::HUMAN_GATHER;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
              {
                  Json::Value dataNode;
                  dataNode["snapshot_image_path"] = "";
                  dataNode["video_image_path"] = ("" == image_path) ? ORI_IMAGE_PATH_PLACEHOLDER : image_path;
  
                  {
                      Json::Value boxesNode;
                      for (auto &box: boxes)
                      {
                          Json::Value boxNode;
                          boxNode["top"] = box.top;
                          boxNode["left"] = box.left;
                          boxNode["right"] = box.right;
                          boxNode["bottom"] = box.bottom;
                          boxNode["score"] = box.score;
                          boxesNode.append(boxNode);
                      }
                      dataNode["box"] = boxesNode;
                  }
                  root["data"] = dataNode;
              }
              return Json::writeString(get_builder(), root);
          }
  
          // added by zsh 220802 用于视频快照接口
          static std::string gen_screen_json(const std::string &taskid, const std::string &ori_fpath)
          {
              Json::Value root;
              root["task_id"] = taskid;
              root["algor_type"] = (int)algorithm_type_t::VIDEO_SNAPSHOT;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
              root["video_snapshot_path"] = ori_fpath;
              return Json::writeString(get_builder(), root);
          };
  
          static std::string gen_boxes_json(const std::string& taskId, int algorithm_type,  std::vector<box_t> boxes, const std::string &image_path){
              Json::Value root;
              root["task_id"] = taskId;
              root["algor_type"] = algorithm_type;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
              {
                  Json::Value dataNode;
                  dataNode["snapshot_image_path"] = "";
                  dataNode["video_image_path"] = ("" == image_path) ? ORI_IMAGE_PATH_PLACEHOLDER : image_path;
  
                  {
                      Json::Value boxesNode;
                      for (auto &box: boxes)
                      {
                          Json::Value boxNode;
                          boxNode["top"] = box.top;
                          boxNode["left"] = box.left;
                          boxNode["right"] = box.right;
                          boxNode["bottom"] = box.bottom;
                          boxNode["score"] = box.score;
                          boxesNode.append(boxNode);
                      }
                      dataNode["box"] = boxesNode;
                  }
                  root["data"] = dataNode;
              }
              return Json::writeString(get_builder(), root);
          }
  
          // 221026byzsh 施工占道
          static std::string gen_road_work_json(const std::string& taskId, std::vector<box_t> boxes, const std::string &image_path)
          {
              return gen_boxes_json(taskId, (int)algorithm_type_t::ROAD_WORK_DET, boxes, image_path);
          }
  
          // added by zsh 230220 用于视频定时抓拍
          static std::string gen_vtsnapshot_json(const std::string &taskid, const std::string &ori_fpath)
          {
              Json::Value root;
              root["task_id"] = taskid;
              root["algor_type"] = (int)algorithm_type_t::VIDEO_TIMING_SNAPSHOT;
              root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());
              root["video_snapshot_path"] = ori_fpath;
              return Json::writeString(get_builder(), root);
          };
  
      }  // namespace gen_json
  
  }  // namespace helpers