|
@@ -0,0 +1,82 @@
|
|
|
+#include "yolo_seg_mqtt_async.h"
|
|
|
+
|
|
|
+void YoloSegmentMqttAsyncClient::init(const std::string &file) {
|
|
|
+ m_message_arrived_callback_ = CloudDataArrived;
|
|
|
+
|
|
|
+ MqttAsyncClient::init_from_proto(file);
|
|
|
+
|
|
|
+#ifdef ENABLE_TENSORRT_DETECT
|
|
|
+ detector = new TensorrtWheelDetector(ETC_PATH PROJECT_NAME "/model.engine", ETC_PATH PROJECT_NAME "/class.txt");
|
|
|
+#else
|
|
|
+ detector = new TensorrtWheelDetector(ETC_PATH PROJECT_NAME "/model.onnx", ETC_PATH PROJECT_NAME "/class.txt");
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+// 识别结果数据包含识别结果、时间序号、具体识别信息
|
|
|
+int YoloSegmentMqttAsyncClient::CloudDataArrived(void *client, char *topicName, int topicLen,
|
|
|
+ MQTTAsync_message *message) {
|
|
|
+ auto *tof_client = (YoloSegmentMqttAsyncClient *)client;
|
|
|
+ // TODO:测试
|
|
|
+ std::string topic = topicName;
|
|
|
+ if (topic == "tof/ir") {
|
|
|
+ // 数据反序列化
|
|
|
+ int lable;
|
|
|
+ cv::Mat merge_mat;
|
|
|
+ JetStream::LabelImage testimage2;
|
|
|
+ testimage2.ParseFromArray(message->payload, message->payloadlen);
|
|
|
+ NetMessageTrans::Proto2lableMat(testimage2, lable, merge_mat);
|
|
|
+
|
|
|
+ // 模型识别
|
|
|
+ std::vector<Object> objs;
|
|
|
+ cv::cvtColor(merge_mat, merge_mat, cv::COLOR_GRAY2RGB);
|
|
|
+ tof_client->detector->detect(merge_mat, objs, merge_mat);
|
|
|
+
|
|
|
+ // 分离识别结果
|
|
|
+ auto t = std::chrono::steady_clock::now();
|
|
|
+ JetStream::LabelYolo seg_results;
|
|
|
+ seg_results.set_label(lable);
|
|
|
+ seg_results.add_boxes();
|
|
|
+ seg_results.add_boxes();
|
|
|
+ seg_results.add_boxes();
|
|
|
+ seg_results.add_boxes();
|
|
|
+ for (auto iter = objs.begin(); iter != objs.end(); iter++) {
|
|
|
+ auto seg_points = tof_client->detector->getPointsFromObj(*iter);
|
|
|
+ int device_index = (int(iter->rect.x / 640) * 0x01) | (int(iter->rect.y / 480) << 1);
|
|
|
+ // 校验识别矩形框是否越界
|
|
|
+ int device_index_check = (int((iter->rect.x + iter->rect.width) / 640) * 0x01) | (int((iter->rect.y + iter->rect.height) / 480) << 1);
|
|
|
+ if (device_index != device_index_check) {
|
|
|
+ // TODO:存图
|
|
|
+ objs.erase(iter);
|
|
|
+ iter--;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (iter->prob > 0.9) {
|
|
|
+ int box_contour[480][2] = {0};
|
|
|
+ int x_alpha = (device_index & 0x1);
|
|
|
+ int y_alpha = ((device_index & 0x2) >> 1);
|
|
|
+ for (auto &pt: seg_points) { // use 7.5~9ms
|
|
|
+ pt.x -= x_alpha * merge_mat.cols / 2;
|
|
|
+ pt.y -= y_alpha * merge_mat.rows / 2;
|
|
|
+ if (box_contour[pt.y][0] == 0 && box_contour[pt.y][1] == 0) {
|
|
|
+ box_contour[pt.y][0] = pt.x;
|
|
|
+ box_contour[pt.y][1] = pt.x;
|
|
|
+ } else {
|
|
|
+ box_contour[pt.y][0] = MIN(pt.x, box_contour[pt.y][0]);
|
|
|
+ box_contour[pt.y][1] = MAX(pt.x, box_contour[pt.y][1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (auto & ends : box_contour) {
|
|
|
+ auto line = seg_results.mutable_boxes(device_index)->add_lines();
|
|
|
+ line->set_begin(ends[0]);
|
|
|
+ line->set_end(ends[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ char data[seg_results.ByteSizeLong()];
|
|
|
+ seg_results.SerializeToArray((void *)data, seg_results.ByteSizeLong());
|
|
|
+ tof_client->SendMessage("tof/seg", data, seg_results.ByteSizeLong());
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+}
|