gen_json.hpp 12.4 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 "include.h"
#include "../ai_engine_module/ai_engine_module.h"
#include "./timer.hpp"
#include "logger.hpp"

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;

                    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 ai_engine_module::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 ai_engine_module::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 ai_engine_module::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 ai_engine_module::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);
        }


#ifdef WITH_SECOND_PROCESS
        static std::string gen_pedestrian_fight_json(const ai_engine_module::fight_fall_cls::result_fight_data_t &data, const std::string &image_path = "")
        {
            Json::Value root;
            root["task_id"] = data.taskid;
            root["algor_type"] = (int)algorithm_type_t::PEDESTRIAN_FIGHT;
            root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());


            {
                Json::Value objectIdNode;
                for (auto &objId: data.objectids)
                    objectIdNode.append((int)objId);
                root["object_ids"] = objectIdNode;
            }

            {
                Json::Value dataNode;
                dataNode["snapshot_image_path"] = ("" == image_path) ? ROI_IMAGE_PATH_PLACEHOLDER : image_path;
                dataNode["video_image_path"] = ORI_IMAGE_PATH_PLACEHOLDER;

                Json::Value boxNode;
                {
                    Json::Value boxNodeItem;
                    boxNodeItem["top"] = data.box.top;
                    boxNodeItem["left"] = data.box.left;
                    boxNodeItem["right"] = data.box.right;
                    boxNodeItem["bottom"] = data.box.bottom;
                    boxNodeItem["score"] = data.box.score;

                    boxNode.append(boxNodeItem);
                    dataNode["box"] = boxNode;
                }
                root["data"] = dataNode;
            }
            return Json::writeString(get_builder(), root);
        }

        static std::string gen_pedestrian_fall_json(const ai_engine_module::fight_fall_cls::result_fall_data_t &data, const std::string &image_path = "")
        {
            Json::Value root;
            root["task_id"] = data.taskid;
            root["algor_type"] = (int)algorithm_type_t::PEDESTRIAN_FALL;
            root["object_id"] = (int)data.objectid;
            root["timestamp_ms"] = std::to_string(timer::get_timestamp<std::chrono::milliseconds>());

            {
                Json::Value dataNode;
                dataNode["snapshot_image_path"] = ("" == image_path) ? ROI_IMAGE_PATH_PLACEHOLDER : image_path;
                dataNode["video_image_path"] = ORI_IMAGE_PATH_PLACEHOLDER;

                Json::Value boxNode;
                {
                    Json::Value boxNodeItem;
                    boxNodeItem["top"] = data.box.top;
                    boxNodeItem["left"] = data.box.left;
                    boxNodeItem["right"] = data.box.right;
                    boxNodeItem["bottom"] = data.box.bottom;
                    boxNodeItem["score"] = data.box.score;

                    boxNode.append(boxNodeItem);
                    dataNode["box"] = boxNode;
                }
                root["data"] = dataNode;
            }
            return Json::writeString(get_builder(), root);
        }
#endif
        static std::string gen_human_gather_json(const std::string& taskId, std::vector<ai_engine_module::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);
        }

    }  // namespace gen_json

}  // namespace helpers