MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VisionMatrix.hpp
Go to the documentation of this file.
1#pragma once
2
4
7
9
10/**
11 * @class VisionMatrix
12 * @brief Compute matrix holding one VisionAnalyzer for a query and, when an
13 * extraction mode is given, one VisionExtractor.
14 *
15 * An intent set in the query without its parameters runs with the defaults.
16 * Keep the matrix for as long as frames arrive: its analyzer reuses its GPU
17 * images on every call, and tracking and motion compare each frame with the
18 * previous one. Configure the extraction through extractor(). Build it with
19 * create(): the matrix must be held by a shared_ptr to run.
20 *
21 * An analysis can be extracted from any frame, not only the one it was taken
22 * on. Boxes, contours, tracks, keypoints and appearance statistics are
23 * normalized values held on the CPU: they stay valid after later calls and
24 * apply at any size. The edge and flow images, and the labels mask() reads,
25 * belong to the analyzer and are valid only until its next call.
26 */
27class MAYAFLUX_API VisionMatrix : public GrammarAwareComputeMatrix {
28public:
29 [[nodiscard]] static std::shared_ptr<VisionMatrix> create(
31 std::optional<VisionExtractMode> extract = {},
33
34 explicit VisionMatrix(
36 std::optional<VisionExtractMode> extract = {},
38
39 using GrammarAwareComputeMatrix::execute;
40
41 /**
42 * @brief Analyze @p image and keep it with its analysis until the next
43 * call.
44 *
45 * analysis() and frame() return what this call produced. Extract from
46 * them through extractor(); mask() reads this call's labels through
47 * analyzer()->get_executor().
48 */
49 void execute(const std::shared_ptr<Core::VKImage>& image);
50
51 /**
52 * @brief execute() on frame @p index of @p source: a texture container's
53 * layer, or a video stream's or window's current frame.
54 *
55 * frame() is the image the frame was resolved to. For a video stream or
56 * window that image is reused, so it holds the next resolved frame after
57 * the next call. A window ignores @p index and gives the frame its
58 * default processor last read back. A video stream ignores it too and
59 * gives the frame in its first slot, or none when it streams through a
60 * ring, which clears like a null image.
61 */
62 void execute(const std::shared_ptr<Kakshya::SignalSourceContainer>& source, size_t index);
63
64 /**
65 * @brief Extract from @p image with @p analysis and keep all three until
66 * the next call.
67 *
68 * analysis() and frame() return @p analysis and @p image, extraction()
69 * what the extractor produced. The analysis may come from another frame.
70 */
71 void extract(const Kinesis::Vision::VisionAnalysis& analysis, const std::shared_ptr<Core::VKImage>& image);
72
73 /** @brief The analysis of the last execute() or extract(), empty before the first. */
74 [[nodiscard]] const Kinesis::Vision::VisionAnalysis& analysis() const { return m_analysis; }
75
76 /** @brief The image the last execute() analyzed or extract() extracted from, null before the first. */
77 [[nodiscard]] const std::shared_ptr<Core::VKImage>& frame() const { return m_frame; }
78
79 /**
80 * @brief The image the last extract() produced. Null before the first,
81 * without an extractor, or when the mode gave no image.
82 *
83 * The extractor rewrites one image per output size, so the same image
84 * carries each later extraction of that size.
85 */
86 [[nodiscard]] const std::shared_ptr<Core::VKImage>& extraction() const { return m_extraction; }
87
88 /**
89 * @brief Analyses per second arriving through execute() and extract(),
90 * smoothed over recent calls. Zero before the second call.
91 *
92 * The rate a collection recorded once per analysis is read at, for
93 * ChimeraBuilder::from(collection, frame_rate) or Chimera::set() when the
94 * work sets the pace. Setting a layer again moves a freely playing layer
95 * to the newest frame; a lagged layer keeps its lag.
96 */
97 [[nodiscard]] double analysis_rate() const { return m_rate; }
98
99 /**
100 * @brief The VisionResult of every sequence the analyzer's last completed
101 * analysis ran, with each step's image named by its op. Valid until
102 * the next analysis.
103 */
104 [[nodiscard]] const std::vector<Kinesis::Vision::VisionResult>& results() { return analyzer()->get_results(); }
105
106 /**
107 * @brief The VisionResult of the sequence that served @p intent in the
108 * last completed analysis, or null when the query has no such
109 * sequence or nothing has completed yet.
110 *
111 * TrackObjects and EstimateMotion share one sequence. Images that hold
112 * their own op's output at the end of each sequence:
113 * - FindElements: gray or rgba_to_hsv, the threshold image.
114 * - DetectEdges: canny. Its gray holds the edges too.
115 * - MeasureAppearance: gray, sobel.
116 * - DetectFeatures, TrackObjects: harris_response. Their gray holds
117 * blurred gradients.
118 * - EstimateMotion: flow, flow_visualization, and gray when TrackObjects
119 * is not set.
120 *
121 * The result stays valid however long it is held. Its images are rewritten
122 * in place when that sequence runs again.
123 */
124 [[nodiscard]] std::shared_ptr<const Kinesis::Vision::VisionResult> result(Kinesis::Vision::VisionIntent intent);
125
126 /** @brief Analyze @p image with the matrix's query. */
127 [[nodiscard]] Kinesis::Vision::VisionAnalysis analyze(const std::shared_ptr<Core::VKImage>& image);
128
129 /** @brief Analyze frame @p index of @p source with the matrix's query. */
130 [[nodiscard]] Kinesis::Vision::VisionAnalysis analyze(
131 const std::shared_ptr<Kakshya::SignalSourceContainer>& source, size_t index = 0);
132
133 /** @brief The matrix's "analyze" operation. */
134 [[nodiscard]] std::shared_ptr<VisionAnalyzer> analyzer() { return get_operation<VisionAnalyzer>("analyze"); }
135
136 /** @brief The matrix's "extract" operation, or null when the matrix was built without a mode. */
137 [[nodiscard]] std::shared_ptr<VisionExtractor> extractor() { return get_operation<VisionExtractor>("extract"); }
138
139private:
141 std::shared_ptr<Core::VKImage> m_frame;
142 std::shared_ptr<Core::VKImage> m_extraction;
143 std::optional<std::chrono::steady_clock::time_point> m_last_call;
144 double m_rate {};
145
146 void count_call();
147};
148
149} // namespace MayaFlux::Yantra::Vision
IO::ImageData image
Definition Decoder.cpp:47
uint32_t index
Definition VKDevice.cpp:122
FunctionalOperation<shared_ptr<SignalSourceContainer>, vector<DataVariant>> whose real API matches En...
GPU getter for what VisionAnalyzer found.
ComputeMatrix extension that integrates grammar-based operation selection.
std::shared_ptr< VisionExtractor > extractor()
The matrix's "extract" operation, or null when the matrix was built without a mode.
const std::vector< Kinesis::Vision::VisionResult > & results()
The VisionResult of every sequence the analyzer's last completed analysis ran, with each step's image...
double analysis_rate() const
Analyses per second arriving through execute() and extract(), smoothed over recent calls.
std::optional< std::chrono::steady_clock::time_point > m_last_call
Kinesis::Vision::VisionAnalysis m_analysis
std::shared_ptr< Core::VKImage > m_extraction
const std::shared_ptr< Core::VKImage > & extraction() const
The image the last extract() produced.
std::shared_ptr< VisionAnalyzer > analyzer()
The matrix's "analyze" operation.
std::shared_ptr< Core::VKImage > m_frame
const std::shared_ptr< Core::VKImage > & frame() const
The image the last execute() analyzed or extract() extracted from, null before the first.
const Kinesis::Vision::VisionAnalysis & analysis() const
The analysis of the last execute() or extract(), empty before the first.
Compute matrix holding one VisionAnalyzer for a query and, when an extraction mode is given,...
VisionIntent
Composable flags naming what a vision analysis should produce.
Composed context: caller-supplied data for whichever intents need it, consulted after a VisionQuery's...
Composed analysis output: one populated field per active intent that produced a result this call.
Composed query: which VisionIntents are active, and each active one's parameters.