1#ifndef YODAU_BACKEND_OPENCV_CLIENT_HPP
2#define YODAU_BACKEND_OPENCV_CLIENT_HPP
9#include "stream_manager.hpp"
11#include <opencv2/opencv.hpp>
20#include <unordered_map>
23namespace yodau::backend {
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
48
49
50
51
52 opencv_client() =
default;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
72 const stream& s,
const std::function<
void(frame&&)>& on_frame,
73 const std::stop_token& st
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 std::vector<event> motion_processor(
const stream& s,
const frame& f);
101
102
103
104
105
106
107
108 stream_manager::daemon_start_fn daemon_start_fn();
111
112
113
114
115
116
117
118 stream_manager::frame_processor_fn frame_processor_fn();
122
123
124
125
126
127
128
129 int local_index_from_path(
const std::string& path)
const;
132
133
134
135
136
137
138
139 frame mat_to_frame(
const cv::Mat& m)
const;
142
143
144
145
146 float cross_z(
const point& a,
const point& b,
const point& c)
const;
149
150
151
152
153
154 int orient(
const point& a,
const point& b,
const point& c)
const;
157
158
159 bool between(
float a,
float b,
float c)
const;
162
163
164 bool on_segment(
const point& a,
const point& b,
const point& c)
const;
167
168
169
170
171 bool segments_intersect(
172 const point& p1,
const point& p2,
const point& q1,
const point& q2
176
177
178
179
180
181
182 std::optional<point> segment_intersection(
183 const point& p1,
const point& p2,
const point& q1,
const point& q2
187
188
189
190
191
192
193
194 void add_motion_event(
195 std::vector<event>& out,
const std::string& stream_name,
196 const std::chrono::steady_clock::time_point ts,
const point& pos_pct
200
201
202
203
205 bool& hit,
float& best_dist2, point& best_a, point& best_b,
206 point& best_pos,
const point& cur_pos_pct,
const point& a,
207 const point& b,
const point& pos
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 void test_line_segment_against_contour(
226 bool& hit,
float& best_dist2, point& best_a, point& best_b,
227 point& best_pos,
const point& cur_pos_pct,
228 const std::vector<point>& contour_pct,
const point& a,
const point& b
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 void process_tripwire_for_line(
247 std::vector<event>& out,
const stream& s,
const line& l,
248 const point& prev_pos,
const point& cur_pos_pct,
249 const std::vector<point>& contour_pct,
250 const std::chrono::steady_clock::time_point now
254
255
256
257
258
259 std::optional<size_t> find_largest_contour_index(
260 const std::vector<std::vector<cv::Point>>& contours
265 mutable std::mutex mtx;
268
269
270
271
272 std::unordered_map<std::string, cv::Mat> prev_gray_by_stream;
275
276
277
278
279 std::unordered_map<std::string, std::chrono::steady_clock::time_point>
283
284
285
286
287 std::unordered_map<std::string, point> last_pos_by_stream;
290
291
292
293
294 std::unordered_map<std::string, std::chrono::steady_clock::time_point>
295 last_tripwire_by_key;
299
300
301
302
303
304
305
306
307
308void opencv_daemon_start(
309 const stream& s,
const std::function<
void(frame&&)>& on_frame,
310 const std::stop_token& st
314
315
316
317
318
319
320
321
322
323
324std::vector<event> opencv_motion_processor(
const stream& s,
const frame& f);
Simple interactive CLI (REPL) for controlling a stream_manager.
void cmd_start_stream(const std::vector< std::string > &args) const
Handler for start-stream.
void dispatch_command(const std::string &cmd, const std::vector< std::string > &args) const
Dispatch a command to its handler.
void cmd_set_line(const std::vector< std::string > &args) const
Handler for set-line.
int run() const
Run the interactive command loop.
void cmd_add_line(const std::vector< std::string > &args) const
Handler for add-line.
static cxxopts::ParseResult parse_with_cxxopts(const std::string &cmd, const std::vector< std::string > &args, cxxopts::Options &options)
Parse command arguments using cxxopts.
backend::stream_manager & stream_mgr
Stream manager controlled by this CLI.
void cmd_stop_stream(const std::vector< std::string > &args) const
Handler for stop-stream.
void cmd_list_streams(const std::vector< std::string > &args) const
Handler for list-streams.
void cmd_add_stream(const std::vector< std::string > &args) const
Handler for add-stream.
void cmd_list_lines(const std::vector< std::string > &args) const
Handler for list-lines.
cli_client(backend::stream_manager &mgr)
Construct a CLI client operating on an existing manager.
static std::vector< std::string > tokenize(const std::string &line)
Split a line into whitespace-separated tokens.
Central coordinator for streams, geometry, frame processing and events.
std::function< void(const std::string &stream_name, frame &&f)> manual_push_fn
Hook for manual frame pushing.
Represents a single video stream and its analytic connections.
std::string get_name() const
Get logical stream name.
stream & operator=(stream &&other) noexcept
Move-assign a stream.
std::string name
Logical stream name.
std::vector< line_ptr > lines_snapshot() const
Get a snapshot of all connected lines.
void activate(stream_pipeline pipeline=stream_pipeline::automatic)
Activate the stream in a pipeline.
stream(std::string path, std::string name, const std::string &type_str={}, bool loop=true)
Construct a stream description.
stream & operator=(const stream &)=delete
Non-copyable (streams manage shared connections / mutex).
stream(stream &&other) noexcept
Move-construct a stream.
bool loop
Looping behavior for file streams.
void dump(std::ostream &out, bool connections=false) const
Dump stream metadata to an output stream.
std::vector< std::string > line_names() const
Get a list of names of all connected lines.
bool is_looping() const
Whether the stream is configured to loop on exhaustion.
void connect_line(line_ptr line)
Connect a geometric line to this stream.
stream_pipeline pipeline() const
Get current pipeline activity of the stream.
stream(const stream &)=delete
Non-copyable (streams manage shared connections / mutex).
std::unordered_map< std::string, line_ptr > lines
Connected lines keyed by their logical names.
void deactivate()
Deactivate the stream.
std::string path
Path or URL to the stream source.
static std::string type_name(const stream_type type)
Convert a stream type to a canonical textual name.
static stream_type identify(const std::string &path)
Identify stream type from a path/URL.
std::string get_path() const
Get stream path or URL.
stream_type get_type() const
Get the stream transport/source type.
stream_type type
Detected or user-specified stream type.
std::mutex lines_mtx
Mutex guarding lines.
static std::string pipeline_name(const stream_pipeline pipeline)
Convert a pipeline mode to its textual name.
stream_pipeline active
Currently active pipeline mode.
static yodau::backend::tripwire_dir parse_tripwire_dir(const std::string &s)
line_ptr make_line(std::vector< point > points, std::string name, bool closed=false)
Create and normalize a line.
std::string normalize_str(std::string_view str)
Remove whitespace and parentheses from a string.
std::vector< point > parse_points(const std::string &points_str)
Parse points from a textual representation.
float parse_float(std::string_view num_str)
Parse a float from a string view.
stream_pipeline
Processing pipeline mode for a stream.
std::shared_ptr< line const > line_ptr
Shared, immutable line pointer.
event_kind
High-level classification of backend events.
tripwire_dir
Allowed crossing direction for a tripwire.
pixel_format
Pixel format of a frame buffer.
stream_type
Source/transport type of a video stream.
Generic event produced by the backend.
std::chrono::steady_clock::time_point ts
Monotonic timestamp when the event was generated.
event_kind kind
Type of the event.
std::string stream_name
Name/identifier of the stream that produced the event.
std::optional< point > pos_pct
Optional position associated with the event in percentage coordinates.
std::string line_name
Name of the line / ROI / rule responsible for this event.
std::string message
Human-readable event description or payload.
int width
Frame width in pixels.
int stride
Number of bytes per row.
int height
Frame height in pixels.
std::vector< std::uint8_t > data
Raw pixel bytes.
std::chrono::steady_clock::time_point ts
Monotonic timestamp when the frame was captured/produced.
pixel_format format
Pixel format of the buffer.
Polyline / polygon described in percentage coordinates.
std::string name
Logical name of the line (e.g., "entrance_tripwire").
tripwire_dir dir
Optional tripwire direction constraint.
bool closed
Whether the chain is closed.
void dump(std::ostream &out) const
Print a human-readable representation of the line.
std::vector< point > points
Vertex list in percentage coordinates.
void normalize()
Canonicalize point order.
bool operator==(const line &other) const
Equality check using canonical point comparison.
Point in percentage-based image coordinates.
float distance_to(const point &other) const
Compute Euclidean distance to another point.
float x
Horizontal coordinate (percentage of width).
float y
Vertical coordinate (percentage of height).
static constexpr float epsilon
Tolerance used for fuzzy point comparisons.
bool compare(const point &other) const
Compare two points with tolerance epsilon.