-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemscripten_binding.cpp
More file actions
290 lines (265 loc) · 10.2 KB
/
Copy pathemscripten_binding.cpp
File metadata and controls
290 lines (265 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* @file emscripten_binding.cpp
* @brief WebAssembly bindings for the Subvision CV library via Emscripten.
*
* Exposes the core Subvision functionality to JavaScript through Emscripten's
* embind system. This file handles:
* - RGBA → BGR colour space conversion (browser images are RGBA)
* - Typed array marshalling between JavaScript and C++
* - Result packaging as JavaScript-friendly objects
*
* ## Exported JavaScript API
*
* | C++ Function | JavaScript Function |
* Description |
* |-------------------------|----------------------------------------|---------------------------------------|
* | `processTargetImage<T>` | `Module.processTargetImage(w, h, arr)` | Detect
* and score all impacts | | `getSheetCoordinates<T>`|
* `Module.getSheetCoordinates(w, h, arr)`| Detect sheet corner coordinates | |
* `setLoggingEnabled` | `Module.setLoggingEnabled(bool)` |
* Enable/disable console logging |
*
* ## JavaScript Usage Example
*
* @code{.js}
* import SubvisionCV from './subvision_core_es6.js';
*
* const module = await SubvisionCV();
*
* // Get image data from a canvas
* const canvas = document.getElementById('myCanvas');
* const ctx = canvas.getContext('2d');
* const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
*
* // Process the target image
* const results = module.processTargetImage(canvas.width, canvas.height,
* imageData.data); console.log('Number of impacts:', results.impacts.size());
*
* for (let i = 0; i < results.impacts.size(); i++) {
* const impact = results.impacts.get(i);
* console.log(`Impact: score=${impact.score},
* distance=${impact.distance}`);
* }
*
* // Get sheet coordinates
* const coords = module.getSheetCoordinates(canvas.width, canvas.height,
* imageData.data); for (let i = 0; i < coords.size(); i++) { const pt =
* coords.get(i); console.log(`Corner: (${pt.x}, ${pt.y})`);
* }
* @endcode
*/
#include "include/impact_detection.h"
#include "include/logging.h"
#include "include/sheet_detection.h"
#include "include/types.h"
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <string>
using namespace emscripten;
/**
* @brief JavaScript-friendly representation of an impact result.
*
* Mirrors the native subvision::Impact struct but is designed for
* use with Emscripten's value_object binding. Exported to JavaScript
* as the `Impact` type.
*
* @note In JavaScript, access fields directly: `impact.distance`,
* `impact.score`, etc.
*/
struct JSImpact {
int distance; ///< Distance from center in millimeters.
int score; ///< Computed score (0–570).
int zone; ///< Target zone identifier.
float angle; ///< Angular position in degrees.
int count; ///< Number of impacts at this location.
/**
* @brief Convert a native C++ Impact to a JSImpact.
*
* @param impact The native Impact to convert.
* @return A JSImpact with identical field values.
*/
static JSImpact fromImpact(const subvision::Impact &impact) {
JSImpact jsImpact;
jsImpact.distance = impact.distance;
jsImpact.score = impact.score;
jsImpact.zone = impact.zone;
jsImpact.angle = impact.angle;
jsImpact.count = impact.count;
return jsImpact;
}
};
/**
* @brief Container for impact processing results returned to JavaScript.
*
* Exported as the `ImpactResults` type in JavaScript. Contains the
* annotated image as a cv::Mat and an array of Impact objects.
*
* JavaScript usage:
* @code{.js}
* const results = module.processTargetImage(width, height, data);
* const mat = results.annotatedImage;
* const imageBytes = mat.data; // Uint8Array of RGBA pixels
* const impacts = results.impacts; // ImpactVector
* @endcode
*/
struct JSImpactResults {
cv::Mat annotatedImage; ///< Annotated image with targets and impacts drawn
///< (RGBA format).
val impacts = val::array(); ///< JavaScript array of JSImpact objects.
};
/**
* @brief Detect sheet corner coordinates from a JavaScript image buffer.
*
* Converts the incoming RGBA typed array to a BGR cv::Mat, calls the
* native getSheetCoordinates(), and returns the result as a JavaScript
* array of {x, y} point objects.
*
* @tparam T Pixel data type (typically `unsigned char`).
* @param width Image width in pixels.
* @param height Image height in pixels.
* @param typedArray JavaScript Uint8Array containing RGBA pixel data.
* @return JavaScript array of point objects with `x` and `y` properties
* (normalised percentage coordinates in [0, 1]).
*
* JavaScript usage:
* @code{.js}
* const coords = Module.getSheetCoordinates(width, height, imageData.data);
* @endcode
*/
template <typename T>
val getSheetCoordinates(int width, int height, const val &typedArray) {
subvision::log("Start processing getSheetCoordinates with width: " +
std::to_string(width) + ", height: " + std::to_string(height));
std::vector<T> vec = convertJSArrayToNumberVector<T>(typedArray);
subvision::log("Vector size: " + std::to_string(vec.size()));
cv::Mat mat(height, width, CV_8UC4, vec.data());
cv::cvtColor(mat, mat, cv::COLOR_RGBA2BGR);
subvision::log("Processing getSheetCoordinates with width: " +
std::to_string(width) + ", height: " + std::to_string(height));
auto points = subvision::getSheetCoordinates(mat);
val jsArray = val::array();
for (const auto &pt : points) {
val jsPoint = val::object();
jsPoint.set("x", pt.x);
jsPoint.set("y", pt.y);
jsArray.call<void>("push", jsPoint);
}
return jsArray;
}
/**
* @brief Process a target image from JavaScript and return impact results.
*
* Main entry point for WebAssembly impact detection. Converts the incoming
* RGBA typed array to BGR, runs the full detection pipeline, and packages
* results as JavaScript-friendly objects.
*
* @tparam T Pixel data type (typically `unsigned char`).
* @param width Image width in pixels.
* @param height Image height in pixels.
* @param typedArray JavaScript Uint8Array containing RGBA pixel data.
* @return JSImpactResults containing the annotated image (RGBA) and impact
* array.
*
* @note The annotated image is converted back to RGBA before returning
* so it can be directly drawn to a canvas.
*
* JavaScript usage:
* @code{.js}
* const results = Module.processTargetImage(canvas.width, canvas.height,
* imageData.data);
* @endcode
*/
template <typename T>
JSImpactResults processTargetImage(int width, int height,
const val &typedArray) {
subvision::ImpactResults results;
auto start = std::chrono::high_resolution_clock::now();
std::vector<T> vec = convertJSArrayToNumberVector<T>(typedArray);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
subvision::log("Temps écoulé pour vecFromJSArray: " +
std::to_string(elapsed.count()) + " secondes");
cv::Mat mat(height, width, CV_8UC4, vec.data());
cv::cvtColor(mat, mat, cv::COLOR_RGBA2BGR);
bool success = subvision::retrieveImpacts(mat, results);
JSImpactResults jsResults;
if (success) {
auto annotated_image = results.annotatedImage;
cv::cvtColor(annotated_image, annotated_image, cv::COLOR_BGR2RGBA);
jsResults.annotatedImage = annotated_image;
val impactArray = val::array();
for (const auto &impact : results.impacts) {
impactArray.call<void>("push", JSImpact::fromImpact(impact));
}
jsResults.impacts = impactArray;
}
return jsResults;
}
/**
* @brief Extract raw pixel data from a cv::Mat as a JavaScript typed array
* view.
*
* Creates a memory_view over the Mat's data buffer, allowing JavaScript
* to read the pixel data directly without copying.
*
* @tparam T Element type for the memory view (typically `unsigned char`).
* @param mat The cv::Mat to expose.
* @return An Emscripten val wrapping a typed array view of the Mat's data.
*
* @warning The returned view becomes invalid if the Mat is deallocated or
* reallocated.
*/
template <typename T> val matData(const cv::Mat &mat) {
return val(memory_view<T>((mat.total() * mat.elemSize()) / sizeof(T),
(T *)mat.data));
}
/**
* @defgroup emscripten_bindings Emscripten WASM Bindings
* @brief JavaScript API bindings for the Subvision WebAssembly module.
*
* These bindings export the following types and functions to JavaScript:
*
* **Types:**
* - `Point2f` — 2D point with `x`, `y` float properties
* - `Mat` — OpenCV matrix with `rows`, `columns`, `data` properties
* - `Impact` — Impact result with `distance`, `score`, `zone`, `angle`, `count`
* - `ImpactResults` — Contains `annotatedImage` (Mat) and `impacts` (array)
*
* **Functions:**
* - `processTargetImage(width, height, typedArray)` — Full impact detection
* - `getSheetCoordinates(width, height, typedArray)` — Sheet corner detection
* - `setLoggingEnabled(bool)` — Toggle console logging
*
* **Vectors:**
* - `vector_uchar` — std::vector<unsigned char>
* - `vector_point2f` — std::vector<cv::Point2f>
* - `ImpactVector` — std::vector<JSImpact>
* @{
*/
// Définition des liaisons Emscripten
EMSCRIPTEN_BINDINGS(subvision_module) {
register_vector<uchar>("vector_uchar");
register_vector<cv::Point2f>("vector_point2f");
class_<cv::Point2f>("Point2f")
.constructor<float, float>()
.property("x", &cv::Point2f::x)
.property("y", &cv::Point2f::y);
class_<cv::Mat>("Mat")
.property("rows", &cv::Mat::rows)
.property("columns", &cv::Mat::cols)
.property("data", &matData<unsigned char>);
value_object<JSImpact>("Impact")
.field("distance", &JSImpact::distance)
.field("score", &JSImpact::score)
.field("zone", &JSImpact::zone)
.field("angle", &JSImpact::angle)
.field("count", &JSImpact::count);
register_vector<JSImpact>("ImpactVector");
value_object<JSImpactResults>("ImpactResults")
.field("annotatedImage", &JSImpactResults::annotatedImage)
.field("impacts", &JSImpactResults::impacts);
function("processTargetImage", &processTargetImage<unsigned char>);
function("getSheetCoordinates", &getSheetCoordinates<unsigned char>);
function("setLoggingEnabled", &subvision::setLoggingEnabled);
}
/** @} */ // end of emscripten_bindings group