HepuTarget2.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include <iostream>
  3. #include <rapidjson/document.h>
  4. #include <rapidjson/rapidjson.h>
  5. #include <rapidjson/stringbuffer.h>
  6. #include <rapidjson/writer.h>
  7. using namespace std;
  8. class HepuTarget2 //doc 4.7.31
  9. {
  10. private:
  11. public:
  12. using Ptr = std::shared_ptr<HepuTarget2>;
  13. public:
  14. const string cmd = "ivpClientTracking";
  15. string token;
  16. int channelid;//0:红外 1:可见光
  17. int trackingStatus;//跟踪状态,0:非跟踪状态;1:跟踪正常;2:跟踪不稳定;3:跟踪目标失锁;4:跟踪目标丢失;10:跟踪通信异常;其他:跟踪失败;
  18. typedef struct Size{
  19. int width;
  20. int height;
  21. };
  22. typedef struct Rect{
  23. int x;
  24. int y;
  25. int width;
  26. int height;
  27. };
  28. Size size;//跟踪视频尺寸
  29. Rect rect;//跟踪目标在图像上的位置坐标,在跟踪视频尺寸坐标系下
  30. double response;//跟踪响应率
  31. HepuTarget2(){};
  32. ~HepuTarget2(){};
  33. void objectToJson(string& str){
  34. rapidjson::StringBuffer strBuf;
  35. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  36. this->objectToJson(writer);
  37. str = strBuf.GetString();
  38. }
  39. void objectToJson(rapidjson::Writer<rapidjson::StringBuffer>& writer){
  40. writer.StartObject();
  41. writer.Key("cmd");
  42. writer.String(cmd.c_str());
  43. writer.Key("param");
  44. writer.StartObject();
  45. writer.Key("token");
  46. writer.String(token.c_str());
  47. writer.Key("channelid");
  48. writer.Int(channelid);
  49. writer.Key("trackingStatus");
  50. writer.Int(trackingStatus);
  51. writer.Key("size");
  52. writer.StartArray();
  53. writer.Int(size.width);
  54. writer.Int(size.height);
  55. writer.EndArray();
  56. writer.Key("rect");
  57. writer.StartArray();
  58. writer.Int(rect.x);
  59. writer.Int(rect.y);
  60. writer.Int(rect.width);
  61. writer.Int(rect.height);
  62. writer.EndArray();
  63. writer.Key("response");
  64. writer.Double(response);
  65. writer.EndObject();
  66. writer.EndObject();
  67. }
  68. bool jsonToObject(std::string& json){
  69. rapidjson::Document doc;
  70. doc.Parse(json.c_str());
  71. if(!doc.IsObject()){
  72. return false;
  73. }
  74. const auto end = doc.MemberEnd();
  75. if(end == doc.FindMember("cmd") || !doc["cmd"].IsString()){
  76. return false;
  77. }else{
  78. if(cmd != doc["cmd"].GetString())
  79. return false;
  80. }
  81. if(end == doc.FindMember("param") || !doc["param"].IsObject()) {
  82. return false;
  83. }else{
  84. if(end == doc.FindMember("token") || !doc["token"].IsString()) {
  85. return false;
  86. }else{
  87. token = doc["token"].GetString();
  88. }
  89. if(end == doc.FindMember("channelid") || !doc["channelid"].IsInt()) {
  90. return false;
  91. }else{
  92. channelid = doc["channelid"].GetInt();
  93. }
  94. if(end == doc.FindMember("trackingStatus") || !doc["trackingStatus"].IsInt()) {
  95. return false;
  96. }else{
  97. trackingStatus = doc["trackingStatus"].GetInt();
  98. }
  99. if(end == doc.FindMember("size") || !doc["size"].IsArray()) {
  100. return false;
  101. }else{
  102. const auto temp = doc["size"].GetArray();
  103. if(temp.Size() < 2)
  104. return false;
  105. size.width = temp[0].GetInt();
  106. size.height = temp[1].GetInt();;
  107. }
  108. if(end == doc.FindMember("rect") || !doc["rect"].IsArray()) {
  109. return false;
  110. }else{
  111. const auto temp = doc["rect"].GetArray();
  112. if(temp.Size() < 4)
  113. return false;
  114. rect.x = temp[0].GetInt();
  115. rect.y = temp[1].GetInt();
  116. rect.width = temp[2].GetInt();
  117. rect.height = temp[3].GetInt();;
  118. }
  119. if(end == doc.FindMember("response") || !doc["response"].IsDouble()) {
  120. return false;
  121. }else{
  122. response = doc["response"].GetDouble();
  123. }
  124. }
  125. return true;
  126. }
  127. };