nx_metadata_sdk 1.0 Metadata SDK

device_agent.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <thread>
6 #include <atomic>
7 #include <memory>
8 #include <condition_variable>
9 #include <vector>
10 #include <string>
11 #include <deque>
12
13 #include <nx/sdk/analytics/helpers/consuming_device_agent.h>
14 #include <nx/sdk/analytics/helpers/pixel_format.h>
15 #include <nx/sdk/analytics/helpers/object_metadata_packet.h>
16
17 #include "objects/random.h"
18 #include "modules/standard_taxonomy/module.h"
19
20 #include "engine.h"
21 #include "stub_analytics_plugin_deprecated_object_detection_ini.h"
22
23 namespace nx {
24 namespace vms_server_plugins {
25 namespace analytics {
26 namespace stub {
27 namespace deprecated_object_detection {
28
29 class DeviceAgent: public nx::sdk::analytics::ConsumingDeviceAgent
30 {
31 public:
32 DeviceAgent(Engine* engine, const nx::sdk::IDeviceInfo* deviceInfo);
33 virtual ~DeviceAgent() override;
34
35 protected:
36 virtual void doSetNeededMetadataTypes(
37 nx::sdk::Result<void>* outValue,
38 const nx::sdk::analytics::IMetadataTypes* neededMetadataTypes) override;
39
40 virtual std::string manifestString() const override;
41
42 virtual nx::sdk::Result<const nx::sdk::ISettingsResponse*> settingsReceived() override;
43
44 virtual bool pushCompressedVideoFrame(
45 const nx::sdk::analytics::ICompressedVideoPacket* videoFrame) override;
46
47 virtual bool pullMetadataPackets(
48 std::vector<nx::sdk::analytics::IMetadataPacket*>* metadataPackets) override;
49
50 private:
51 std::vector<nx::sdk::analytics::IMetadataPacket*> cookSomeObjects();
52
53 nx::sdk::Ptr<nx::sdk::analytics::IObjectMetadata> cookBlinkingObjectIfNeeded(
54 int64_t metadataTimestampUs);
55
56 void addBlinkingObjectIfNeeded(
57 int64_t metadataTimestampUs,
58 std::vector<nx::sdk::analytics::IMetadataPacket*>* metadataPackets,
59 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
60
61 void addFixedObjectIfNeeded(
62 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
63
64 void addCounterIfNeeded(
65 nx::sdk::Ptr<nx::sdk::analytics::ObjectMetadataPacket> objectMetadataPacket);
66
67 void processVideoFrame(const nx::sdk::analytics::IDataPacket* videoFrame, const char* func);
68
69 void setObjectCount(int objectCount);
70
71 void cleanUpTimestampQueue();
72
73 void parseSettings();
74
75 template<typename ObjectType>
76 void setIsObjectTypeGenerationNeeded(bool isObjectTypeGenerationNeeded)
77 {
78 if (isObjectTypeGenerationNeeded)
79 {
80 m_objectGenerator.registerObjectFactory<ObjectType>(
81 []() { return std::make_unique<ObjectType>(); });
82 }
83 else
84 {
85 m_objectGenerator.unregisterObjectFactory<ObjectType>();
86 }
87 }
88
89 void updateObjectGenerationParameters();
90
91 private:
92 Engine* const m_engine;
93
94 int m_frameCounter = 0;
95
96 std::deque<int64_t> m_frameTimestampUsQueue;
97 int64_t m_lastVideoFrameTimestampUs = 0;
98 int64_t m_lastBlinkingObjectTimestampUs = 0;
99
100 struct DeviceAgentSettings
101 {
102 bool needToGenerateObjects() const
103 {
104 return generateCars
105 || generateTrucks
106 || generatePedestrians
107 || generateHumanFaces
108 || generateBicycles
109 || generateStones;
110 }
111
112 std::atomic<bool> generateCars{true};
113 std::atomic<bool> generateTrucks{true};
114 std::atomic<bool> generatePedestrians{true};
115 std::atomic<bool> generateHumanFaces{true};
116 std::atomic<bool> generateBicycles{true};
117 std::atomic<bool> generateStones{false};
118 std::atomic<bool> generateFixedObject{false};
119
120 std::mutex fixedObjectColorMutex;
121 std::string fixedObjectColor;
122
123 std::atomic<bool> generateCounter{false};
124
125 std::atomic<std::chrono::milliseconds> blinkingObjectPeriodMs{
126 std::chrono::milliseconds::zero()};
127
128 std::atomic<bool> blinkingObjectInDedicatedPacket{false};
129
130 std::atomic<int> numberOfObjectsToGenerate{1};
131 std::atomic<int> generateObjectsEveryNFrames{1};
132
133 std::atomic<bool> generatePreviews{true};
134
135 std::atomic<std::chrono::milliseconds> additionalFrameProcessingDelayMs{
136 std::chrono::milliseconds::zero()};
137
138 std::atomic<std::chrono::milliseconds> overallMetadataDelayMs{
139 std::chrono::milliseconds::zero()};
140
141 std::atomic<int> numberOfFramesBeforePreviewGeneration{30};
142
143 std::atomic<float> counterBoundingBoxSideSize{0};
144 std::atomic<float> counterBoundingBoxXOffset{0};
145 std::atomic<float> counterBoundingBoxYOffset{0};
146
147 std::vector<char> previewImage;
148 std::string previewImageFormat;
149 std::string previewImageUrl;
150 };
151
152 DeviceAgentSettings m_deviceAgentSettings;
153
154 struct ObjectContext
155 {
156 ObjectContext() = default;
157 ObjectContext(std::unique_ptr<AbstractObject> object): object(std::move(object)) {}
158
159 ObjectContext& operator=(std::unique_ptr<AbstractObject>&& otherObject)
160 {
161 reset();
162 object = std::move(otherObject);
163 return *this;
164 }
165
166 void reset()
167 {
168 object.reset();
169 isPreviewGenerated = false;
170 frameCounter = 0;
171 }
172
173 bool operator!() const { return !object; }
174
175 std::unique_ptr<AbstractObject> object;
176 bool isPreviewGenerated = false;
177 int frameCounter = 0;
178 };
179
180 std::mutex m_objectGenerationMutex;
181 RandomObjectGenerator m_objectGenerator;
182 std::vector<ObjectContext> m_objectContexts;
183 int m_counterObjectAttributeValue = 0;
184
185 modules::standard_taxonomy::Module m_standardTaxonomyModule;
186 };
187
188 const std::string kBlinkingObjectType = "nx.stub.blinkingObject";
189 const std::string kFixedObjectType = "nx.stub.fixedObject";
190 const std::string kCounterObjectType = "nx.stub.counter";
191
192 } // namespace deprecated_object_detection
193 } // namespace stub
194 } // namespace analytics
195 } // namespace vms_server_plugins
196 } // namespace nx
nx::vms_server_plugins::analytics::stub::RandomObjectGenerator
Definition: random.h:25
nx::sdk::analytics::ICompressedVideoPacket
Definition: i_compressed_video_packet.h:34
nx::sdk::Ptr
Definition: ptr.h:18
nx::vms_server_plugins::analytics::stub::deprecated_object_detection::DeviceAgent::pushCompressedVideoFrame
virtual bool pushCompressedVideoFrame(const nx::sdk::analytics::ICompressedVideoPacket *videoFrame) override
Definition: device_agent.cpp:160
nx::vms_server_plugins::analytics::stub::deprecated_object_detection::DeviceAgent
Definition: device_agent.h:29
nx::vms_server_plugins::analytics::stub::deprecated_object_detection::DeviceAgent::settingsReceived
virtual nx::sdk::Result< const nx::sdk::ISettingsResponse * > settingsReceived() override
Definition: device_agent.cpp:129
nx::sdk::analytics::ConsumingDeviceAgent
Definition: consuming_device_agent.h:38
nx::vms_server_plugins::analytics::stub::deprecated_object_detection::DeviceAgent::manifestString
virtual std::string manifestString() const override
Definition: device_agent.cpp:65
nx::sdk::analytics::IMetadataTypes
Definition: i_metadata_types.h:13
nx::sdk::Result< void >
Definition: result.h:81
nx::sdk::IDeviceInfo
Definition: i_device_info.h:14
nx::sdk::Result
Definition: result.h:47
nx
Definition: apple_utils.h:6
nx::vms_server_plugins::analytics::stub::deprecated_object_detection::Engine
Definition: engine.h:18
nx::vms_server_plugins::analytics::stub::modules::standard_taxonomy::Module
Definition: module.h:22
nx::sdk::analytics::IDataPacket
Definition: i_data_packet.h:17