-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFOCV_Object.cpp
More file actions
475 lines (403 loc) · 20.2 KB
/
FOCV_Object.cpp
File metadata and controls
475 lines (403 loc) · 20.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//
// FOCV_ObjectCreation.cpp
// Pods
//
// Created by Łukasz Kurant on 06/08/2024.
//
#include "FOCV_Object.hpp"
#include "FOCV_Storage.hpp"
#include "FOCV_JsiObject.hpp"
#include "jsi/TypedArray.h"
#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
#include "ConvertImage.hpp"
#include "jsi/TypedArray.h"
using namespace mrousavy;
// General idea and this function for hashing is from
// https://mrousavy.com/blog/Hashing-String-Ifs
constexpr uint64_t hashString(const char* str, size_t length) {
uint64_t hash = 14695981039346656037ull;
const uint64_t fnv_prime = 1099511628211ull;
for (size_t i = 0; i < length; ++i) {
hash ^= static_cast<uint64_t>(str[i]);
hash *= fnv_prime;
}
return hash;
}
jsi::Object FOCV_Object::create(jsi::Runtime& runtime, const jsi::Value* arguments, size_t count) {
std::string id;
std::string objectType = arguments[0].asString(runtime).utf8(runtime);
switch(hashString(objectType.c_str(), objectType.size())) {
case hashString("mat", 3): {
auto rows = static_cast<int>(arguments[1].asNumber());
auto cols = static_cast<int>(arguments[2].asNumber());
auto type = static_cast<int>(arguments[3].asNumber());
auto channels = CV_MAT_CN(type);
auto depth = CV_MAT_DEPTH(type);
std::vector<int> size {rows, cols};
if (count > 4 && arguments[4].isObject()) {
auto array = arguments[4].asObject(runtime).asArray(runtime);
auto length = array.length(runtime);
std::vector<double> vec;
vec.reserve(length);
for(size_t i = 0; i < length; i++) {
vec.push_back(array.getValueAtIndex(runtime, i).asNumber());
}
cv::Mat object(vec, true);
object = object.reshape(channels, size);
object.convertTo(object, depth);
id = FOCV_Storage::save(object);
} else {
cv::Mat object(size, type);
id = FOCV_Storage::save(object);
}
} break;
case hashString("mat_vector", 10): {
std::vector<cv::Mat> vec;
id = FOCV_Storage::save(vec);
} break;
case hashString("rect", 4): {
auto x = static_cast<int>(arguments[1].asNumber());
auto y = static_cast<int>(arguments[2].asNumber());
auto width = static_cast<int>(arguments[3].asNumber());
auto height = static_cast<int>(arguments[4].asNumber());
cv::Rect object(x, y, width, height);
id = FOCV_Storage::save(object);
} break;
case hashString("rect_vector", 11): {
std::vector<cv::Rect> vec;
id = FOCV_Storage::save(vec);
} break;
case hashString("point", 5): {
auto x = static_cast<int>(arguments[1].asNumber());
auto y = static_cast<int>(arguments[2].asNumber());
cv::Point object(x, y);
id = FOCV_Storage::save(object);
} break;
case hashString("point_vector", 12): {
std::vector<cv::Point> vec;
id = FOCV_Storage::save(vec);
} break;
case hashString("point2f", 7): {
auto x = static_cast<float>(arguments[1].asNumber());
auto y = static_cast<float>(arguments[2].asNumber());
cv::Point2f object(x, y);
id = FOCV_Storage::save(object);
} break;
case hashString("point2f_vector", 14): {
std::vector<cv::Point2f> vec;
if (count > 1 && arguments[1].isObject()) {
auto array = arguments[1].asObject(runtime).asArray(runtime);
auto length = array.length(runtime);
for(size_t i = 0; i < length; i++) {
auto value = array.getValueAtIndex(runtime, i);
auto value_id = FOCV_JsiObject::id_from_wrap(runtime, value);
auto point2fPtr = FOCV_Storage::get<cv::Point2f>(value_id);
vec.push_back(*point2fPtr);
}
}
id = FOCV_Storage::save(vec);
} break;
case hashString("point_vector_vector", 19): {
std::vector<std::vector<cv::Point>> vec;
id = FOCV_Storage::save(vec);
} break;
case hashString("size", 4): {
auto width = static_cast<int>(arguments[1].asNumber());
auto height = static_cast<int>(arguments[2].asNumber());
cv::Size object(width, height);
id = FOCV_Storage::save(object);
} break;
case hashString("vec3b", 5): {
auto a = static_cast<uchar>(arguments[1].asNumber());
auto b = static_cast<uchar>(arguments[2].asNumber());
auto c = static_cast<uchar>(arguments[3].asNumber());
cv::Vec3b object(a, b, c);
id = FOCV_Storage::save(object);
} break;
case hashString("scalar", 6): {
if (count > 4 && arguments[4].isNumber()) {
auto a = arguments[1].asNumber();
auto b = arguments[2].asNumber();
auto c = arguments[3].asNumber();
auto d = arguments[4].asNumber();
cv::Scalar object(a, b, c, d);
id = FOCV_Storage::save(object);
} else if(count > 3 && arguments[3].isNumber()) {
auto a = arguments[1].asNumber();
auto b = arguments[2].asNumber();
auto c = arguments[3].asNumber();
cv::Scalar object(a, b, c);
id = FOCV_Storage::save(object);
} else {
auto a = arguments[1].asNumber();
cv::Scalar object(a);
id = FOCV_Storage::save(object);
}
} break;
case hashString("rotated_rect", 12): {
auto x = static_cast<int>(arguments[1].asNumber());
auto y = static_cast<int>(arguments[2].asNumber());
auto width = static_cast<int>(arguments[3].asNumber());
auto height = static_cast<int>(arguments[4].asNumber());
auto angle = static_cast<float>(arguments[5].asNumber());
cv::RotatedRect object(cv::Point(x,y), cv::Size(width, height), angle);
id = FOCV_Storage::save(object);
} break;
case hashString("term_criteria", 13): {
auto termCriteriaType = static_cast<int>(arguments[1].asNumber());
auto maxCount = static_cast<int>(arguments[2].asNumber());
auto epsilon = arguments[3].asNumber();
cv::TermCriteria object(termCriteriaType, maxCount, epsilon);
if (!object.isValid()) {
throw std::runtime_error("Fast OpenCV Error: Invalid TermCriteria object parameters!");
}
id = FOCV_Storage::save(object);
} break;
case hashString("keypoint_vector", 15): {
std::vector<cv::KeyPoint> vec;
id = FOCV_Storage::save(vec);
} break;
case hashString("dmatch_vector", 13): {
std::vector<cv::DMatch> vec;
id = FOCV_Storage::save(vec);
} break;
case hashString("dmatch_vector_vector", 20): {
std::vector<std::vector<cv::DMatch>> vec;
id = FOCV_Storage::save(vec);
} break;
}
return FOCV_JsiObject::wrap(runtime, objectType, id);
}
jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* arguments, size_t count) {
jsi::Object value(runtime);
std::string objectType = FOCV_JsiObject::type_from_wrap(runtime, arguments[0]);
std::string id = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
switch(hashString(objectType.c_str(), objectType.size())) {
case hashString("mat", 3): {
auto mat = *FOCV_Storage::get<cv::Mat>(id);
std::string format = "jpeg";
if(arguments[1].isString()) {
format = arguments[1].asString(runtime).utf8(runtime);
}
mat.convertTo(mat, CV_8U);
value.setProperty(runtime, "base64", jsi::String::createFromUtf8(runtime, ImageConverter::mat2str(mat, format)));
value.setProperty(runtime, "size", jsi::Value(mat.size));
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
value.setProperty(runtime, "type", jsi::Value(mat.type()));
} break;
case hashString("mat_vector", 10): {
auto mats = *FOCV_Storage::get<std::vector<cv::Mat>>(id);
auto array = jsi::Array(runtime, mats.size());
for (size_t i = 0; i < mats.size(); i++) {
jsi::Object item(runtime);
item.setProperty(runtime, "size", jsi::Value(mats.at(i).size));
item.setProperty(runtime, "cols", jsi::Value(mats.at(i).cols));
item.setProperty(runtime, "rows", jsi::Value(mats.at(i).rows));
array.setValueAtIndex(runtime, i, item);
}
value.setProperty(runtime, "array", array);
} break;
case hashString("rect", 4): {
auto rect = *FOCV_Storage::get<cv::Rect>(id);
value.setProperty(runtime, "x", jsi::Value(rect.x));
value.setProperty(runtime, "y", jsi::Value(rect.y));
value.setProperty(runtime, "width", jsi::Value(rect.width));
value.setProperty(runtime, "height", jsi::Value(rect.height));
} break;
case hashString("rect_vector", 11): {
auto rects = *FOCV_Storage::get<std::vector<cv::Rect>>(id);
auto array = jsi::Array(runtime, rects.size());
for (size_t i = 0; i < rects.size(); i++) {
jsi::Object item(runtime);
item.setProperty(runtime, "x", jsi::Value(rects.at(i).x));
item.setProperty(runtime, "y", jsi::Value(rects.at(i).y));
item.setProperty(runtime, "width", jsi::Value(rects.at(i).width));
item.setProperty(runtime, "height", jsi::Value(rects.at(i).height));
array.setValueAtIndex(runtime, i, item);
}
value.setProperty(runtime, "array", array);
} break;
case hashString("point", 5): {
auto point = *FOCV_Storage::get<cv::Point>(id);
value.setProperty(runtime, "x", jsi::Value(point.x));
value.setProperty(runtime, "y", jsi::Value(point.y));
} break;
case hashString("point2f", 7): {
auto point = *FOCV_Storage::get<cv::Point2f>(id);
value.setProperty(runtime, "x", jsi::Value(point.x));
value.setProperty(runtime, "y", jsi::Value(point.y));
} break;
case hashString("point_vector", 12): {
auto points = *FOCV_Storage::get<std::vector<cv::Point>>(id);
auto array = jsi::Array(runtime, points.size());
for (size_t i = 0; i < points.size(); i++) {
jsi::Object item(runtime);
item.setProperty(runtime, "x", jsi::Value(points.at(i).x));
item.setProperty(runtime, "y", jsi::Value(points.at(i).y));
array.setValueAtIndex(runtime, i, item);
}
value.setProperty(runtime, "array", array);
} break;
case hashString("point_vector_vector", 19): {
auto points_vector = *FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(id);
auto vectors_array = jsi::Array(runtime, points_vector.size());
for (size_t i = 0; i < points_vector.size(); i++) {
const auto& points = points_vector.at(i);
auto array = jsi::Array(runtime, points.size());
for (size_t j = 0; j < points.size(); j++) {
jsi::Object item(runtime);
item.setProperty(runtime, "x", jsi::Value(points.at(j).x));
item.setProperty(runtime, "y", jsi::Value(points.at(j).y));
array.setValueAtIndex(runtime, j, item);
}
vectors_array.setValueAtIndex(runtime, i, array);
}
value.setProperty(runtime, "array", vectors_array);
} break;
case hashString("size", 4): {
auto size = *FOCV_Storage::get<cv::Size>(id);
value.setProperty(runtime, "width", jsi::Value(size.width));
value.setProperty(runtime, "height", jsi::Value(size.height));
} break;
case hashString("vec3b", 5): {
auto vec = *FOCV_Storage::get<cv::Vec3b>(id);
value.setProperty(runtime, "a", jsi::Value(vec.val[0]));
value.setProperty(runtime, "b", jsi::Value(vec.val[1]));
value.setProperty(runtime, "c", jsi::Value(vec.val[2]));
} break;
case hashString("scalar", 6): {
auto scalar = *FOCV_Storage::get<cv::Scalar>(id);
value.setProperty(runtime, "a", jsi::Value(scalar.val[0]));
value.setProperty(runtime, "b", jsi::Value(scalar.val[1]));
value.setProperty(runtime, "c", jsi::Value(scalar.val[2]));
value.setProperty(runtime, "d", jsi::Value(scalar.val[3]));
} break;
case hashString("rotated_rect", 12): {
auto rect = *FOCV_Storage::get<cv::RotatedRect>(id);
value.setProperty(runtime, "centerX", jsi::Value(rect.center.x));
value.setProperty(runtime, "centerY", jsi::Value(rect.center.y));
value.setProperty(runtime, "width", jsi::Value(rect.size.width));
value.setProperty(runtime, "height", jsi::Value(rect.size.height));
value.setProperty(runtime, "angle", jsi::Value(rect.angle));
} break;
case hashString("term_criteria", 13): {
auto termCriteria = *FOCV_Storage::get<cv::TermCriteria>(id);
value.setProperty(runtime, "type", jsi::Value(termCriteria.type));
value.setProperty(runtime, "maxCount", jsi::Value(termCriteria.maxCount));
value.setProperty(runtime, "epsilon", jsi::Value(termCriteria.epsilon));
} break;
case hashString("keypoint_vector", 15): {
auto keypoints = *FOCV_Storage::get<std::vector<cv::KeyPoint>>(id);
auto array = jsi::Array(runtime, keypoints.size());
for (size_t i = 0; i < keypoints.size(); i++) {
jsi::Object item(runtime);
item.setProperty(runtime, "x", jsi::Value(keypoints.at(i).pt.x));
item.setProperty(runtime, "y", jsi::Value(keypoints.at(i).pt.y));
item.setProperty(runtime, "size", jsi::Value(keypoints.at(i).size));
item.setProperty(runtime, "angle", jsi::Value(keypoints.at(i).angle));
item.setProperty(runtime, "response", jsi::Value(keypoints.at(i).response));
item.setProperty(runtime, "octave", jsi::Value(keypoints.at(i).octave));
item.setProperty(runtime, "classId", jsi::Value(keypoints.at(i).class_id));
array.setValueAtIndex(runtime, i, item);
}
value.setProperty(runtime, "array", array);
} break;
case hashString("dmatch_vector", 13): {
auto matches = *FOCV_Storage::get<std::vector<cv::DMatch>>(id);
auto array = jsi::Array(runtime, matches.size());
for (size_t i = 0; i < matches.size(); i++) {
jsi::Object item(runtime);
item.setProperty(runtime, "queryIdx", jsi::Value(matches.at(i).queryIdx));
item.setProperty(runtime, "trainIdx", jsi::Value(matches.at(i).trainIdx));
item.setProperty(runtime, "imgIdx", jsi::Value(matches.at(i).imgIdx));
item.setProperty(runtime, "distance", jsi::Value(matches.at(i).distance));
array.setValueAtIndex(runtime, i, item);
}
value.setProperty(runtime, "array", array);
} break;
case hashString("dmatch_vector_vector", 20): {
auto matchVectors = *FOCV_Storage::get<std::vector<std::vector<cv::DMatch>>>(id);
auto outerArray = jsi::Array(runtime, matchVectors.size());
for (size_t i = 0; i < matchVectors.size(); i++) {
const auto& matches = matchVectors.at(i);
auto innerArray = jsi::Array(runtime, matches.size());
for (size_t j = 0; j < matches.size(); j++) {
jsi::Object item(runtime);
item.setProperty(runtime, "queryIdx", jsi::Value(matches.at(j).queryIdx));
item.setProperty(runtime, "trainIdx", jsi::Value(matches.at(j).trainIdx));
item.setProperty(runtime, "imgIdx", jsi::Value(matches.at(j).imgIdx));
item.setProperty(runtime, "distance", jsi::Value(matches.at(j).distance));
innerArray.setValueAtIndex(runtime, j, item);
}
outerArray.setValueAtIndex(runtime, i, innerArray);
}
value.setProperty(runtime, "array", outerArray);
} break;
}
return value;
}
jsi::Object FOCV_Object::copyObjectFromVector(jsi::Runtime& runtime, const jsi::Value* arguments, size_t count) {
std::string createdId;
jsi::Object value(runtime);
std::string objectType = FOCV_JsiObject::type_from_wrap(runtime, arguments[0]);
std::string vectorId = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
auto index = static_cast<int>(arguments[1].asNumber());
switch(hashString(objectType.c_str(), objectType.size())) {
case hashString("mat_vector", 10): {
auto array = *FOCV_Storage::get<std::vector<cv::Mat>>(vectorId);
auto mat = array.at(index);
createdId = FOCV_Storage::save(mat);
return FOCV_JsiObject::wrap(runtime, "mat", createdId);
} break;
case hashString("rect_vector", 11): {
auto array = *FOCV_Storage::get<std::vector<cv::Rect>>(vectorId);
auto rect = array.at(index);
createdId = FOCV_Storage::save(rect);
return FOCV_JsiObject::wrap(runtime, "rect", createdId);
} break;
case hashString("point_vector", 12): {
auto array = *FOCV_Storage::get<std::vector<cv::Point>>(vectorId);
auto point = array.at(index);
createdId = FOCV_Storage::save(point);
return FOCV_JsiObject::wrap(runtime, "point", createdId);
} break;
case hashString("point_vector_vector", 19): {
auto array = *FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(vectorId);
auto point = array.at(index);
createdId = FOCV_Storage::save(point);
return FOCV_JsiObject::wrap(runtime, "point_vector", createdId);
} break;
}
return value;
}
void FOCV_Object::addObjectToVector(jsi::Runtime& runtime, const jsi::Value* arguments, size_t count) {
std::string createdId;
jsi::Object value(runtime);
std::string objectType = FOCV_JsiObject::type_from_wrap(runtime, arguments[0]);
std::string vectorId = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
std::string objectId = FOCV_JsiObject::id_from_wrap(runtime, arguments[1]);
switch(hashString(objectType.c_str(), objectType.size())) {
case hashString("mat_vector", 10): {
auto& array = *FOCV_Storage::get<std::vector<cv::Mat>>(vectorId);
auto& object = *FOCV_Storage::get<cv::Mat>(objectId);
array.push_back(std::move(object));
} break;
case hashString("rect_vector", 11): {
auto& array = *FOCV_Storage::get<std::vector<cv::Rect>>(vectorId);
auto& object = *FOCV_Storage::get<cv::Rect>(objectId);
array.push_back(object);
} break;
case hashString("point_vector", 12): {
auto& array = *FOCV_Storage::get<std::vector<cv::Point>>(vectorId);
auto& object = *FOCV_Storage::get<cv::Point>(objectId);
array.push_back(object);
} break;
case hashString("point_vector_vector", 19): {
auto& array = *FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(vectorId);
auto& object = *FOCV_Storage::get<std::vector<cv::Point>>(objectId);
array.push_back(object);
} break;
}
}