gen_json.hpp 13.6 KB
/*
 * @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