本文是对cnstream代码的学习笔记
git代码 :https://github.com/Cambricon/CNStream
寒武纪开发者论坛: http://forum.cambricon.com/list-47-1.html
1 简介osd模块
这个模块主要是将检测和追踪结果叠加在原图上的。
2 对外接口
Osd类继承了Module和ModuleCreate类,它的公有接口有四个。
bool Open(cnstream::ModuleParamSet paramSet) override; void Close() override; int Process(std::shared_ptr<CNFrameInfo> data) override; bool CheckParamSet(const ModuleParamSet& paramSet) const override;
3 与pipeline的关系
创建了一个pipeline对象后,我们调用 bool Pipeline::Start() 会调用到接口 bool Open(cnstream::ModuleParamSet paramSet) override; ,主要作用是更新osd的一些私有变量,比如变量labels_它保存了标签的名称;secondary_labels_保存了次要标签的名称;chinese_label_flag_设置是否有中文标签;attr_keys_设置?;label_size_设置标签的大小;还有text_scale_、 text_thickness_、 box_thickness_ 、logo_;
最后pipeline关闭会调用到 void Close() override; 接口。
4 Process接口
Osd引入了CnoSd这个类,在osd内部有一个叫osd_ctxs_的map来存储cnosd类,也就是一个osd模块对象为每一个线程分配一个cnosd。
cnosd初始化主要是根据labels_的大小设置了colors_, std::vector<cv::Scalar> colors; 这是一个保存随机rgb颜色的数组。具体生成随机颜色的方法[https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/],也就是为每个labels设置一个不同的颜色。同时通过这个类可以设置文档的颜色、标签框大小、标签的层数和标签框层数等等。
最重要的是这个类实现了在图像上画标签、画logo的功能:
void DrawLabel(cv::Mat *image, const CNObjsVec& objects, std::vector<std::string> attr_keys = {}) const; void DrawLogo(cv::Mat *image, std::string logo) const;
5 一些数据结构
- stream_count_map_
一个全局的map std::unordered_map<std::string, int> CNFrameInfo::stream_count_map_; 用来标识某路数据流生成了几个CNFrameInfo。
- CNFrameInfo
这是Module之间传递的数据结构,主要记录了数据属于哪一路视频流,数据帧的时间戳和保存自定义数据的map(命名为datas)等。
public:
std::string stream_id; ///< The data stream aliases where this frame is located to. int64_t timestamp = -1; ///< The time stamp of this frame. size_t flags = 0; ///< The mask for this frame, ``CNFrameFlag``. // user-defined DataFrame,InferResult etc... std::unordered_map<int, any> datas; mutable uint32_t channel_idx = INVALID_STREAM_IDX; ///< The index of the channel, stream_index
private: SpinLock mask_lock_; /*The mask map of the module. It identifies which modules the data can already be processed by.*/ std::unordered_map<unsigned int, uint64_t> module_mask_map_; SpinLock eos_lock_; uint64_t eos_mask = 0;
- CNDataFrame
这是上面自定义数据结构支持的一种,用于存放视频帧数据。
- CNInferObject
也是上面自定义数据结构支持的一种,用于存放神经网络推理结果。
浙公网安备 33010602011771号