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
| #ifndef MEDIAPIPE_CALCULATORS_OUTPUT_JSON_OUTPUT_CALCULATOR_H_ #define MEDIAPIPE_CALCULATORS_OUTPUT_JSON_OUTPUT_CALCULATOR_H_
#include "mediapipe/framework/calculator_framework.h" #include "ims_output.pb.h" #include <sstream> #include <iomanip>
namespace mediapipe {
class JSONOutputCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc) { cc->Inputs().Tag("IMS_OUTPUT").Set<ims::IMSOutput>(); cc->Outputs().Tag("JSON").Set<std::string>(); cc->Options<JSONOutputOptions>(); return absl::OkStatus(); }
absl::Status Open(CalculatorContext* cc) override { const auto& options = cc->Options<JSONOutputOptions>(); pretty_print_ = options.pretty_print(); include_system_status_ = options.include_system_status(); precision_ = options.precision(); return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override { if (cc->Inputs().Tag("IMS_OUTPUT").IsEmpty()) { return absl::OkStatus(); }
const ims::IMSOutput& output = cc->Inputs().Tag("IMS_OUTPUT").Get<ims::IMSOutput>(); std::string json = IMSOutputToJSON(output); cc->Outputs().Tag("JSON").AddPacket( MakePacket<std::string>(json).At(cc->InputTimestamp())); return absl::OkStatus(); }
private: bool pretty_print_ = false; bool include_system_status_ = false; int precision_ = 2; std::string IMSOutputToJSON(const ims::IMSOutput& output) { std::ostringstream ss; ss << std::fixed << std::setprecision(precision_); ss << "{"; ss << "\"timestamp_ms\":" << output.timestamp_ms() << ","; ss << "\"frame_id\":" << output.frame_id() << ","; ss << "\"dms\":"; DMSResultToJSON(ss, output.dms()); ss << ","; ss << "\"oms\":"; OMSResultToJSON(ss, output.oms()); ss << ","; ss << "\"vehicle\":"; VehicleStateToJSON(ss, output.vehicle()); if (include_system_status_) { ss << ","; ss << "\"system\":"; SystemStatusToJSON(ss, output.system()); } ss << "}"; return ss.str(); } void DMSResultToJSON(std::ostringstream& ss, const ims::DMSResult& dms) { ss << "{"; ss << "\"fatigue_score\":" << dms.fatigue_score() << ","; ss << "\"fatigue_level\":" << dms.fatigue_level() << ","; ss << "\"perclos\":" << dms.perclos() << ","; ss << "\"blink_rate\":" << dms.blink_rate() << ","; ss << "\"distraction_score\":" << dms.distraction_score() << ","; ss << "\"gaze_zone\":" << dms.gaze_zone() << ","; ss << "\"gaze_direction\":" << static_cast<int>(dms.gaze_direction()) << ","; ss << "\"head_pose\":{"; ss << "\"yaw\":" << dms.head_pose().yaw() << ","; ss << "\"pitch\":" << dms.head_pose().pitch() << ","; ss << "\"roll\":" << dms.head_pose().roll(); ss << "},"; ss << "\"eye_state\":{"; ss << "\"left_eye_open\":" << dms.eye_state().left_eye_open() << ","; ss << "\"right_eye_open\":" << dms.eye_state().right_eye_open(); ss << "},"; ss << "\"is_yawning\":" << (dms.is_yawning() ? "true" : "false") << ","; ss << "\"alert_fatigue\":" << (dms.alert_fatigue() ? "true" : "false") << ","; ss << "\"alert_distraction\":" << (dms.alert_distraction() ? "true" : "false") << ","; ss << "\"alert_no_driver\":" << (dms.alert_no_driver() ? "true" : "false") << ","; ss << "\"alert_level\":" << dms.alert_level() << ","; ss << "\"alert_message\":\"" << EscapeJSON(dms.alert_message()) << "\""; ss << "}"; } void OMSResultToJSON(std::ostringstream& ss, const ims::OMSResult& oms) { ss << "{"; ss << "\"num_occupants\":" << oms.num_occupants() << ","; ss << "\"num_unbuckled\":" << oms.num_unbuckled() << ","; ss << "\"child_presence_detected\":" << (oms.child_presence_detected() ? "true" : "false") << ","; ss << "\"child_confidence\":" << oms.child_confidence(); ss << "}"; } void VehicleStateToJSON(std::ostringstream& ss, const ims::VehicleState& vehicle) { ss << "{"; ss << "\"speed\":" << vehicle.speed() << ","; ss << "\"steering_angle\":" << vehicle.steering_angle() << ","; ss << "\"turn_signal_left\":" << (vehicle.turn_signal_left() ? "true" : "false") << ","; ss << "\"turn_signal_right\":" << (vehicle.turn_signal_right() ? "true" : "false") << ","; ss << "\"gear\":" << vehicle.gear(); ss << "}"; } void SystemStatusToJSON(std::ostringstream& ss, const ims::SystemStatus& system) { ss << "{"; ss << "\"cpu_usage\":" << system.cpu_usage() << ","; ss << "\"memory_usage\":" << system.memory_usage() << ","; ss << "\"fps\":" << system.fps() << ","; ss << "\"device_id\":\"" << system.device_id() << "\""; ss << "}"; } std::string EscapeJSON(const std::string& s) { std::string result; for (char c : s) { switch (c) { case '"': result += "\\\""; break; case '\\': result += "\\\\"; break; case '\n': result += "\\n"; break; case '\r': result += "\\r"; break; case '\t': result += "\\t"; break; default: if (c >= 32 && c < 127) { result += c; } else { char buf[8]; snprintf(buf, sizeof(buf), "\\u%04x", c); result += buf; } } } return result; } };
REGISTER_CALCULATOR(JSONOutputCalculator);
}
#endif
|