123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- #ifndef CNSTREAM_FRAME_VA_HPP_
- #define CNSTREAM_FRAME_VA_HPP_
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #if (CV_MAJOR_VERSION >= 3)
- #include <opencv2/imgcodecs/imgcodecs.hpp>
- #endif
- #include <memory>
- #include <mutex>
- #include <string>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- #include "cnstream_common.hpp"
- #include "cnstream_frame.hpp"
- #include "cnstream_syncmem.hpp"
- #include "private/cnstream_allocator.hpp"
- #include "util/cnstream_any.hpp"
- constexpr int CN_MAX_PLANES = 6;
- namespace cnstream {
- enum class CNDataFormat {
- CN_INVALID = -1,
- CN_PIXEL_FORMAT_YUV420_NV21 = 0,
- CN_PIXEL_FORMAT_YUV420_NV12,
- CN_PIXEL_FORMAT_BGR24,
- CN_PIXEL_FORMAT_RGB24,
- CN_PIXEL_FORMAT_ARGB32,
- CN_PIXEL_FORMAT_ABGR32,
- CN_PIXEL_FORMAT_RGBA32,
- CN_PIXEL_FORMAT_BGRA32
- };
- struct DevContext {
- enum class DevType {
- INVALID = -1,
- CPU = 0,
- MLU = 1,
- } dev_type = DevType::INVALID;
- int dev_id = 0;
- int ddr_channel = 0;
- };
- inline int CNGetPlanes(CNDataFormat fmt) {
- switch (fmt) {
- case CNDataFormat::CN_PIXEL_FORMAT_BGR24:
- case CNDataFormat::CN_PIXEL_FORMAT_RGB24:
- return 1;
- case CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV12:
- case CNDataFormat::CN_PIXEL_FORMAT_YUV420_NV21:
- return 2;
- default:
- return 0;
- }
- return 0;
- }
- class CNDataFrame : public NonCopyable {
- public:
-
- CNDataFrame() = default;
-
- ~CNDataFrame() = default;
-
- int GetPlanes() const { return CNGetPlanes(fmt); }
-
- size_t GetPlaneBytes(int plane_idx) const;
-
- size_t GetBytes() const;
- public:
-
- void CopyToSyncMem(void** ptr_src, bool dst_mlu);
-
- cv::Mat ImageBGR();
-
- cv::Mat ImageBGR_NO_OSD();
-
- bool HasBGRImage() {
- std::lock_guard<std::mutex> lk(mtx);
- if (bgr_mat.empty()) return false;
- return true;
- }
-
- void CopyToSyncMemOnDevice(int device_id);
- std::shared_ptr<void> cpu_data = nullptr;
- std::shared_ptr<void> mlu_data = nullptr;
- std::unique_ptr<CNSyncedMemory> data[CN_MAX_PLANES];
- uint64_t frame_id = -1;
- CNDataFormat fmt;
- int width;
- int height;
- int stride[CN_MAX_PLANES];
- DevContext ctx;
- std::unique_ptr<IDataDeallocator> deAllocator_ = nullptr;
- std::atomic<int> dst_device_id{-1};
- private:
- std::mutex mtx;
- cv::Mat bgr_mat;
- cv::Mat bgr_mat_ON;
- };
- struct CNInferBoundingBox {
- float x;
- float y;
- float w;
- float h;
- };
- typedef struct {
- int id = -1;
- int value = -1;
- float score = 0;
- } CNInferAttr;
- using CNInferFeature = std::vector<float>;
- using CNInferFeatures = std::vector<std::pair<std::string, CNInferFeature>>;
- using StringPairs = std::vector<std::pair<std::string, std::string>>;
- class CNInferObject {
- public:
- CNS_IGNORE_DEPRECATED_PUSH
-
- CNInferObject() = default;
-
- ~CNInferObject() = default;
- CNS_IGNORE_DEPRECATED_POP
- std::string id;
- std::string track_id;
- float score;
- CNInferBoundingBox bbox;
- CNS_DEPRECATED std::unordered_map<int, any> datas;
- Collection collection;
-
- bool AddAttribute(const std::string& key, const CNInferAttr& value);
-
- bool AddAttribute(const std::pair<std::string, CNInferAttr>& attribute);
-
- CNInferAttr GetAttribute(const std::string& key);
-
- bool AddExtraAttribute(const std::string& key, const std::string& value);
-
- bool AddExtraAttributes(const std::vector<std::pair<std::string, std::string>>& attributes);
-
- std::string GetExtraAttribute(const std::string& key);
-
- bool RemoveExtraAttribute(const std::string& key);
-
- StringPairs GetExtraAttributes();
-
- bool AddFeature(const std::string &key, const CNInferFeature &feature);
-
- CNInferFeature GetFeature(const std::string &key);
-
- CNInferFeatures GetFeatures();
- private:
- std::unordered_map<std::string, CNInferAttr> attributes_;
- std::unordered_map<std::string, std::string> extra_attributes_;
- std::unordered_map<std::string, CNInferFeature> features_;
- std::mutex attribute_mutex_;
- std::mutex feature_mutex_;
- };
- using CNInferObjectPtr = std::shared_ptr<CNInferObject>;
- struct CNInferObjs : public NonCopyable {
- std::vector<std::shared_ptr<CNInferObject>> objs_;
- std::mutex mutex_;
- };
- struct InferData {
-
- CNDataFormat input_fmt_;
- int input_width_;
- int input_height_;
- std::shared_ptr<void> input_cpu_addr_;
- size_t input_size_;
-
- std::vector<std::shared_ptr<void>> output_cpu_addr_;
- std::vector<size_t> output_sizes_;
- size_t output_num_;
- };
- struct CNInferData : public NonCopyable {
- std::unordered_map<std::string, std::vector<std::shared_ptr<InferData>>> datas_map_;
-
- std::mutex mutex_;
- };
- using CNDataFramePtr = std::shared_ptr<CNDataFrame>;
- using CNInferObjsPtr = std::shared_ptr<CNInferObjs>;
- using CNObjsVec = std::vector<std::shared_ptr<CNInferObject>>;
- using CNInferDataPtr = std::shared_ptr<CNInferData>;
- CNS_DEPRECATED static constexpr int CNDataFramePtrKey = 0;
- CNS_DEPRECATED static constexpr int CNInferObjsPtrKey = 1;
- CNS_DEPRECATED static constexpr int CNInferDataPtrKey = 2;
- CNS_IGNORE_DEPRECATED_PUSH
- CNS_DEPRECATED static inline
- CNDataFramePtr GetCNDataFramePtr(std::shared_ptr<CNFrameInfo> frameInfo) {
- std::lock_guard<std::mutex> guard(frameInfo->datas_lock_);
- return cnstream::any_cast<CNDataFramePtr>(frameInfo->datas[CNDataFramePtrKey]);
- }
- CNS_DEPRECATED static inline
- CNInferObjsPtr GetCNInferObjsPtr(std::shared_ptr<CNFrameInfo> frameInfo) {
- std::lock_guard<std::mutex> guard(frameInfo->datas_lock_);
- return cnstream::any_cast<CNInferObjsPtr>(frameInfo->datas[CNInferObjsPtrKey]);
- }
- CNS_DEPRECATED static inline
- CNInferDataPtr GetCNInferDataPtr(std::shared_ptr<CNFrameInfo> frameInfo) {
- std::lock_guard<std::mutex> guard(frameInfo->datas_lock_);
- return cnstream::any_cast<CNInferDataPtr>(frameInfo->datas[CNInferDataPtrKey]);
- }
- CNS_IGNORE_DEPRECATED_POP
- static constexpr char kCNDataFrameTag[] = "CNDataFrame";
- static constexpr char kCNInferObjsTag[] = "CNInferObjs";
- static constexpr char kCNInferDataTag[] = "CNInferData";
- }
- #endif
|