CNStreamInferData.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <rapidjson/document.h>
  5. #include <rapidjson/rapidjson.h>
  6. #include <rapidjson/stringbuffer.h>
  7. #include <rapidjson/writer.h>
  8. using namespace std;
  9. typedef struct {
  10. double x, y, w, h;
  11. } InferBoundingBox;
  12. class InferInfo
  13. {
  14. public:
  15. std::string Label;
  16. double Score;
  17. InferBoundingBox BBox;
  18. public:
  19. InferInfo() {}
  20. ~InferInfo() {}
  21. bool jsonToObject(const rapidjson::Value& object){
  22. const auto end = object.MemberEnd();
  23. if(end == object.FindMember("Label") || !object["Label"].IsString()){
  24. return false;
  25. }
  26. else{
  27. Label = object["Label"].GetString();
  28. }
  29. if(end == object.FindMember("Score") || !object["Score"].IsDouble()){
  30. return false;
  31. }
  32. else{
  33. Score = object["Score"].GetDouble();
  34. }
  35. if(end == object.FindMember("BBox") || !object["BBox"].IsArray()){
  36. return false;
  37. }
  38. else{
  39. const rapidjson::Value& objs = object["BBox"];
  40. BBox.x = objs[0].GetDouble();
  41. BBox.y = objs[1].GetDouble();
  42. BBox.w = objs[2].GetDouble();
  43. BBox.h = objs[3].GetDouble();
  44. }
  45. return true;
  46. }
  47. };
  48. class CNStreamInferData
  49. {
  50. public:
  51. using Ptr = std::shared_ptr<CNStreamInferData>;
  52. std::string StreamName;
  53. int FrameCount;
  54. int width;
  55. int height;
  56. vector<InferInfo> Objects;
  57. std::string ImageBase64;
  58. std::string videoPath;
  59. public:
  60. CNStreamInferData() {}
  61. ~CNStreamInferData() {}
  62. bool jsonToObject(const std::string& json){
  63. rapidjson::Document doc;
  64. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  65. return false;
  66. }
  67. // get members
  68. const auto end = doc.MemberEnd();
  69. // json_type
  70. if (end == doc.FindMember("StreamName") || !doc["StreamName"].IsString()) {
  71. return false;
  72. } else {
  73. StreamName = doc["StreamName"].GetString();
  74. }
  75. if (end == doc.FindMember("width") || !doc["width"].IsInt()) {
  76. return false;
  77. } else {
  78. width = doc["width"].GetInt();
  79. }
  80. if (end == doc.FindMember("height") || !doc["height"].IsInt()) {
  81. return false;
  82. } else {
  83. height = doc["height"].GetInt();
  84. }
  85. if (end == doc.FindMember("FrameCount") || !doc["FrameCount"].IsInt()) {
  86. return false;
  87. } else {
  88. FrameCount = doc["FrameCount"].GetInt();
  89. }
  90. if (end == doc.FindMember("Objects") || !doc["Objects"].IsArray()) {
  91. return false;
  92. } else {
  93. const rapidjson::Value& objects = doc["Objects"];
  94. for (size_t i = 0; i < objects.Size(); ++i) {
  95. const rapidjson::Value& obj = objects[i];
  96. InferInfo data;
  97. if(!data.jsonToObject(obj)) return false;
  98. this->Objects.push_back(data);
  99. }
  100. }
  101. if(end != doc.FindMember("ImageBase64") && doc["ImageBase64"].IsString())
  102. {
  103. ImageBase64 = doc["ImageBase64"].GetString();
  104. }
  105. if (end != doc.FindMember("videoPath") && doc["videoPath"].IsString()) {
  106. videoPath = doc["videoPath"].GetString();
  107. }
  108. return true;
  109. }
  110. };