|
| 1 | +/*************************** |
| 2 | +@Author: Chunel |
| 3 | + |
| 4 | +@File: E01-AutoPilot.cpp |
| 5 | +@Time: 2023/9/15 20:03 |
| 6 | +@Desc: 本example主要展示,Camera 每隔1000ms,采集一张图片 |
| 7 | + LaneDetector 和 CarDetector同时消费这张图片,计算对应的内容 |
| 8 | + 并且最终在Show中展示对应的结果信息 |
| 9 | +***************************/ |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <algorithm> |
| 13 | + |
| 14 | +#include "CGraph.h" |
| 15 | + |
| 16 | +using namespace CGraph; |
| 17 | + |
| 18 | +static const int DEFAULT_IMAGE_SIZE = 64; |
| 19 | +static const int DEFAULT_MESSAGE_BUF_SIZE = 16; |
| 20 | +static const char* EXAMPLE_IMAGE_TOPIC = "/example/image/topic"; |
| 21 | +static const char* EXAMPLE_PARAM_KEY = "example-param-key"; |
| 22 | + |
| 23 | + |
| 24 | +struct ImageMParam : public GMessageParam { |
| 25 | + int frame_id_ = 0; |
| 26 | + char image_buf_[DEFAULT_IMAGE_SIZE] = {0}; |
| 27 | + |
| 28 | + ImageMParam& operator=(const ImageMParam& param) { |
| 29 | + if (this == ¶m) { |
| 30 | + return *this; |
| 31 | + } |
| 32 | + |
| 33 | + this->frame_id_ = param.frame_id_; |
| 34 | + memset(image_buf_, 0, DEFAULT_IMAGE_SIZE); |
| 35 | + memcpy(image_buf_, param.image_buf_, DEFAULT_IMAGE_SIZE); |
| 36 | + return *this; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | + |
| 41 | +class CameraGDaemon : public GDaemon { |
| 42 | +public: |
| 43 | + CVoid daemonTask(GDaemonParamPtr param) override { |
| 44 | + ImageMParam image; |
| 45 | + image.frame_id_ = cur_index_; |
| 46 | + std::string info = "this is " + std::to_string(cur_index_) + " image"; |
| 47 | + memcpy(image.image_buf_, info.c_str(), info.length()); |
| 48 | + cur_index_++; |
| 49 | + |
| 50 | + CGRAPH_PUB_MPARAM(ImageMParam, EXAMPLE_IMAGE_TOPIC, image); |
| 51 | + } |
| 52 | + |
| 53 | +private: |
| 54 | + int cur_index_ = 0; |
| 55 | +}; |
| 56 | + |
| 57 | + |
| 58 | +struct DetectResultGParam : public GParam { |
| 59 | + int lane_num_ = 0; |
| 60 | + int car_num_ = 0; |
| 61 | + int frame_id_ = 0; |
| 62 | + |
| 63 | + CStatus setup() override { |
| 64 | + lane_num_ = 0; |
| 65 | + car_num_ = 0; |
| 66 | + frame_id_ = 0; |
| 67 | + return CStatus(); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +class LaneDetectorGNode : public GNode { |
| 73 | +public: |
| 74 | + CStatus init() override { |
| 75 | + // 订阅 EXAMPLE_IMAGE_TOPIC 消息。每次 bind的返回值,是不一样的,用于区分 |
| 76 | + conn_id_ = CGRAPH_BIND_MESSAGE_TOPIC(ImageMParam, EXAMPLE_IMAGE_TOPIC, DEFAULT_MESSAGE_BUF_SIZE) |
| 77 | + return CStatus(); |
| 78 | + } |
| 79 | + |
| 80 | + CStatus run() override { |
| 81 | + ImageMParam image; |
| 82 | + auto status = CGRAPH_SUB_MPARAM(ImageMParam, conn_id_, image); |
| 83 | + if (status.isErr()) { |
| 84 | + return status; |
| 85 | + } |
| 86 | + |
| 87 | + auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(DetectResultGParam, EXAMPLE_PARAM_KEY) |
| 88 | + // detector lane in image.image_buf_ ... |
| 89 | + CGRAPH_ECHO("detecting lane in frame [%d], info is [%s]", image.frame_id_, image.image_buf_); |
| 90 | + param->frame_id_ = image.frame_id_; |
| 91 | + param->lane_num_ = std::abs((int)std::random_device{}()) % 10; |
| 92 | + return CStatus(); |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + int conn_id_ = 0; |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | +class CarDetectorGNode : public GNode { |
| 101 | +public: |
| 102 | + CStatus init() override { |
| 103 | + conn_id_ = CGRAPH_BIND_MESSAGE_TOPIC(ImageMParam, EXAMPLE_IMAGE_TOPIC, DEFAULT_MESSAGE_BUF_SIZE) |
| 104 | + return CStatus(); |
| 105 | + } |
| 106 | + |
| 107 | + CStatus run() override { |
| 108 | + ImageMParam image; |
| 109 | + auto status = CGRAPH_SUB_MPARAM(ImageMParam, conn_id_, image); |
| 110 | + if (status.isErr()) { |
| 111 | + return status; |
| 112 | + } |
| 113 | + |
| 114 | + auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(DetectResultGParam, EXAMPLE_PARAM_KEY); |
| 115 | + CGRAPH_ECHO("finding car in frame [%d], info is [%s]", image.frame_id_, image.image_buf_); |
| 116 | + // find car in image.image_buf_ ... |
| 117 | + param->car_num_ = std::abs((int)std::random_device{}()) % 5; |
| 118 | + return CStatus(); |
| 119 | + } |
| 120 | + |
| 121 | +private: |
| 122 | + int conn_id_ = 0; |
| 123 | +}; |
| 124 | + |
| 125 | + |
| 126 | +class ShowGNode : public GNode { |
| 127 | + CStatus run() override { |
| 128 | + auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(DetectResultGParam, EXAMPLE_PARAM_KEY); |
| 129 | + CGRAPH_ECHO("find [%d] car and [%d] lane in frame [%d] \n", param->car_num_, param->lane_num_, param->frame_id_); |
| 130 | + return CStatus(); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | + |
| 135 | +void example_auto_pilot() { |
| 136 | + GElementPtr lane, car, show = nullptr; |
| 137 | + auto pipeline = GPipelineFactory::create(); |
| 138 | + |
| 139 | + pipeline->registerGElement<LaneDetectorGNode>(&lane, {}, "lane"); |
| 140 | + pipeline->registerGElement<CarDetectorGNode>(&car, {}, "car"); |
| 141 | + pipeline->registerGElement<ShowGNode>(&show, {lane, car}, "show"); |
| 142 | + |
| 143 | + pipeline->createGParam<DetectResultGParam>(EXAMPLE_PARAM_KEY); |
| 144 | + pipeline->addGDaemon<CameraGDaemon>(1000); // 模拟相机,每间隔1000ms,生成一张图片 |
| 145 | + |
| 146 | + pipeline->process(10); |
| 147 | + GPipelineFactory::clear(); |
| 148 | + CGRAPH_CLEAR_MESSAGES() |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +int main() { |
| 153 | + example_auto_pilot(); |
| 154 | + return 0; |
| 155 | +} |
0 commit comments