forked from fehlfarbe/python-aruco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathokapi-typemaps.i
599 lines (536 loc) · 16.3 KB
/
okapi-typemaps.i
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/************************************
NumPy <--> OpenCV typemaps
Source: https://github.com/neingeist/python-exercises/blob/master/okapi-typemaps.i
************************************/
%{
#define SWIG_FILE_WITH_INIT
#if ((PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION > 6) || \
(PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 0) || \
(PY_MAJOR_VERSION > 3))
#define CAPSULES_SUPPORTED
#endif
#ifndef NPY_ARRAY_C_CONTIGUOUS
#define NPY_ARRAY_C_CONTIGUOUS NPY_C_CONTIGUOUS
#define PyArray_SetBaseObject(arr, x) (PyArray_BASE(arr) = (x))
#endif
%}
%include "numpy_old.i"
%init %{
import_array();
%}
%fragment("OKAPI_Fragments", "header",
fragment="NumPy_Fragments")
{
// convert NumPy type to OpenCV type
int numpy_type_to_mat_type(PyArrayObject *array, int channels)
{
switch (array_type(array)) {
case NPY_BYTE: return CV_MAKETYPE(CV_8S, channels);
case NPY_UBYTE: return CV_MAKETYPE(CV_8U, channels);
case NPY_SHORT: return CV_MAKETYPE(CV_16S, channels);
case NPY_USHORT: return CV_MAKETYPE(CV_16U, channels);
case NPY_INT: return CV_MAKETYPE(CV_32S, channels);
//case NPY_UINT: // does not exist
//case NPY_LONG: // does not exist
//case NPY_ULONG: // does not exist
case NPY_FLOAT: return CV_MAKETYPE(CV_32F, channels);
case NPY_DOUBLE: return CV_MAKETYPE(CV_64F, channels);
default:
PyErr_Format(PyExc_TypeError,
"Unsupported datatype: %s.",
typecode_string(array_type(array)));
}
return -1;
}
// convert OpenCV type to NumPy type
int mat_type_to_numpy_type(int type)
{
// convert numpy type to CV type
switch (CV_MAT_DEPTH(type)) {
case CV_8S: return NPY_BYTE;
case CV_8U: return NPY_UBYTE;
case CV_16S: return NPY_SHORT;
case CV_16U: return NPY_USHORT;
case CV_32S: return NPY_INT;
//case NPY_UINT: // does not exist
//case NPY_LONG: // does not exist
//case NPY_ULONG: // does not exist
case CV_32F: return NPY_FLOAT;
case CV_64F: return NPY_DOUBLE;
default:
PyErr_Format(PyExc_TypeError,
"Unsupported OpenCV datatype: %d (depth: %d).",
type, CV_MAT_DEPTH(type));
}
return -1;
}
void delete_mat(void *ptr)
{
// printf("deleting underlying mat at %p\n", ptr);
cv::Mat *m = static_cast< cv::Mat* >(ptr);
assert (m != NULL);
delete m;
}
%#ifdef CAPSULES_SUPPORTED
void delete_mat_capsule(PyObject *obj)
{
// printf("deleting underlying mat at %p\n", ptr);
cv::Mat *m = static_cast< cv::Mat* >(PyCapsule_GetPointer(obj, NULL));
assert (m != NULL);
delete m;
}
%#endif
cv::Scalar* array_to_scalar(PyObject* input)
{
PyArrayObject* array = obj_to_array_no_conversion(input, NPY_NOTYPE);
if (array == NULL) {
// error message is set by obj_to_array_no_conversion
return NULL;
}
// creates 4-element integer array ToDo: diff. convert data types
size_t size = array_size(array, 0);
size_t step = array->strides[0];
int data[4] = {0, 0, 0, 0};
for(size_t i=0, j=0; i<size*step; i+=step,j++){
data[j] = (unsigned char)array_data(array)[i];
}
return new cv::Scalar(data[0], data[1], data[2], data[3]);
}
cv::Size* array_to_size(PyObject* input)
{
PyArrayObject* array = obj_to_array_no_conversion(input, NPY_NOTYPE);
if (array == NULL) {
// error message is set by obj_to_array_no_conversion
return NULL;
}
// creates 4-element integer array ToDo: diff. convert data types
size_t size = array_size(array, 0);
size_t step = array->strides[0];
int data[4] = {0, 0, 0, 0};
for(size_t i=0, j=0; i<size*step; i+=step,j++){
data[j] = (unsigned char)array_data(array)[i];
}
return new cv::Size(data[0], data[1]);
}
cv::Mat* array_to_mat(PyObject* input)
{
PyArrayObject* array = obj_to_array_no_conversion(input, NPY_NOTYPE);
if (array == NULL) {
// error message is set by obj_to_array_no_conversion
return NULL;
}
// check if array has 2 or 3 dimensions
int ndims = array_numdims(array);
int channels, type;
if (ndims == 2)
channels = 1;
else if (ndims == 3) {
channels = array_size(array, 2);
// check number of channels
if (channels > 4) {
PyErr_Format(PyExc_TypeError,
"Array can have a maximum of 4 channels. Input as %d.", channels);
return NULL;
}
}
else {
PyErr_Format(PyExc_TypeError,
"Array must be 2- or 3-dimensional. Input is %d-dimensional.", ndims);
return NULL;
}
// convert NumPy type to OpenCV type
if ((type = numpy_type_to_mat_type(array, channels)) == -1)
return NULL;
// check that array is in C order
for (int i = 0; i < ndims-1; ++i) {
if (PyArray_STRIDE(array, i) < PyArray_STRIDE(array, i+1)) {
PyErr_Format(PyExc_TypeError,
"Array is not in C order: stride of dim %d: %ld < stride of dim %d: %ld",
i, PyArray_STRIDE(array, i), i+1, PyArray_STRIDE(array, i+1));
return NULL;
}
}
// use stride of first dimension as stepsize
// and check strides of other dimensions
int step = ((PyArrayObject *)array)->strides[0];
if (PyArray_STRIDE(array, 1) != channels * PyArray_ITEMSIZE(array)) {
PyErr_Format(PyExc_TypeError,
"Array cannot have strides in other than 1st dimension (rows). Dim 2: %ld vs %d",
PyArray_STRIDE(array, 1), channels * PyArray_ITEMSIZE(array));
return NULL;
}
// create new view onto the data
return new cv::Mat(array_size(array, 0), array_size(array, 1), type, array_data(array), step);
}
PyObject* mat_to_array(const cv::Mat& mat)
{
// handle empty mat
if (mat.empty()) {
npy_intp dims[1] = { 0 };
int type = mat_type_to_numpy_type(mat.type());
PyObject* array = PyArray_EMPTY(1, dims, type, 0);
return array;
}
npy_intp dims[3] = { mat.rows, mat.cols, mat.channels() };
npy_intp strides[3] = { (npy_intp)mat.step, (npy_intp)mat.elemSize(), (npy_intp)mat.elemSize1()};
int ndims = (mat.channels() > 1) ? 3 : 2;
int type = mat_type_to_numpy_type(mat.type());
int flags = NPY_WRITEABLE;
if (mat.isContinuous())
flags |= NPY_C_CONTIGUOUS;
PyObject* array = PyArray_New(&PyArray_Type, ndims, dims, type,
strides, mat.data, 0, flags, NULL);
return array;
}
PyObject* point2f_to_array(cv::Point2f* point)
{
npy_intp dims = 2;
npy_intp strides[1] = { 1 };
int flags = NPY_WRITEABLE;
float *data = new float[2];
data[0] = point->x;
data[1] = point->y;
PyObject* array = PyArray_SimpleNewFromData(1, &dims, NPY_FLOAT, data);
return array;
}
}
///////////////////////////////////////
/// const cv::Mat&
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
const cv::Mat&
{
$1 = is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
const cv::Mat&
{
$1 = array_to_mat($input);
if ($1 == NULL)
SWIG_fail;
}
%typemap(freearg,
fragment="OKAPI_Fragments")
const cv::Mat&
{
if ($1 != NULL)
delete $1;
}
%typemap(argout,
fragment="OKAPI_Fragments")
const cv::Mat&
{
}
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
cv::Mat const &
{
$1 = true;//is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
cv::Mat const &
{
$1 = array_to_mat($input);
if ($1 == NULL)
SWIG_fail;
}
///////////////////////////////////////
/// cv::Mat
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
cv::Mat
{
$1 = is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
cv::Mat
(cv::Mat* mat)
{
mat = array_to_mat($input);
if (mat == NULL)
SWIG_fail;
$1 = *mat;
}
%typemap(freearg,
fragment="OKAPI_Fragments")
cv::Mat
{
if (mat$argnum != NULL)
delete mat$argnum;
}
%typemap(argout,
fragment="OKAPI_Fragments")
cv::Mat
{
}
///////////////////////////////////////
/// cv::Mat*
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
cv::Mat*
{
$1 = is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
cv::Mat*
(uchar* data_ptr)
{
$1 = array_to_mat($input);
if ($1 == NULL)
SWIG_fail;
// save dataptr to check whether it changed
data_ptr = $1->data;
}
%typemap(freearg,
fragment="OKAPI_Fragments")
cv::Mat*
{
if ($1 != NULL)
delete $1;
}
%typemap(argout,
fragment="OKAPI_Fragments")
cv::Mat*
{
// check if the underlying data changed
if (data_ptr$argnum != $1->data) {
// TODO don't fail if we can reallocate the underlying memory
// (not always possible due to the way NumPy does reference counting)
PyErr_Format(PyExc_RuntimeError,
"Underlying data changed. Please make sure that the "
"input data is of correct size and type: %d %d %d",
$1->rows, $1->cols, $1->type());
SWIG_fail;
}
}
///////////////////////////////////////
/// cv::Mat&
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
cv::Mat&
{
$1 = is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
cv::Mat &
(uchar* data_ptr)
{
$1 = array_to_mat($input);
if ($1 == NULL)
SWIG_fail;
// save dataptr to check whether it changed
data_ptr = $1->data;
}
%typemap(freearg,
fragment="OKAPI_Fragments")
cv::Mat&
{
if ($1 != NULL)
delete $1;
}
%typemap(argout,
fragment="OKAPI_Fragments")
cv::Mat&
{
// check if the underlying data changed
if (data_ptr$argnum != $1->data) {
// TODO don't fail if we can reallocate the underlying memory
// (not always possible due to the way NumPy does reference counting)
PyErr_Format(PyExc_RuntimeError,
"Underlying data changed. Please make sure that the "
"input data is of correct size and type: %d %d %d",
$1->rows, $1->cols, $1->type());
SWIG_fail;
}
}
///////////////////////////////////////
/// return cv::Mat
///////////////////////////////////////
%typemap(out,
fragment="NumPy_Fragments")
cv::Mat
{
PyObject* array = mat_to_array($1);
if (array == NULL)
SWIG_fail;
// add a reference to the underlying cv::Mat so
// that the memory is not freed before the NumPy array
// is released or reassigned
cv::Mat *m = new cv::Mat($1);
// printf("created new mat object at %p\n", m);
%#ifdef CAPSULES_SUPPORTED
PyArray_BASE(array) = PyCapsule_New(m, NULL, delete_mat_capsule);
%#else
PyArray_BASE(array) = PyCObject_FromVoidPtr(m, delete_mat);
%#endif
$result = array;
}
%typemap(out,
fragment="NumPy_Fragments")
const cv::Mat&
{
PyObject* array = mat_to_array(*$1);
if (array == NULL)
SWIG_fail;
// add a reference to the underlying cv::Mat so
// that the memory is not freed before the NumPy array
// is released or reassigned
cv::Mat *m = new cv::Mat(*$1);
// printf("created new mat object at %p\n", m);
%#ifdef CAPSULES_SUPPORTED
PyArray_BASE(array) = PyCapsule_New(m, NULL, delete_mat_capsule);
%#else
PyArray_BASE(array) = PyCObject_FromVoidPtr(m, delete_mat);
%#endif
$result = array;
}
///////////////////////////////////////
// vector<cv::Mat>
///////////////////////////////////////
%define %vector_typemap(T)
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
const std::vector<T> &
{
$1 = PySequence_Check($input);
}
%typemap(in,
fragment="OKAPI_Fragments")
const std::vector<T> &
(std::vector<T*> relvec)
{
if (!PySequence_Check($input)) {
printf("NO SEQUENCE\n");
SWIG_fail;
}
Py_ssize_t length = PySequence_Size($input);
$1 = new std::vector< cv::Mat >(length);
for (Py_ssize_t ii = 0; ii < length; ++ii) {
cv::Mat *tmp = array_to_mat(PySequence_GetItem($input, ii));
if (tmp == NULL) {
printf("%ld no array\n", ii);
SWIG_fail;
}
$1->at(ii) = *tmp;
relvec.push_back(tmp);
}
}
%typemap(freearg,
fragment="OKAPI_Fragments")
const std::vector<T> &
{
if ($1 != NULL)
delete $1;
for (size_t ii = 0; ii < relvec$argnum.size(); ++ii)
delete relvec$argnum[ii];
}
%enddef
///////////////////////////////////////
// okapi::bstring
///////////////////////////////////////
%typemap(out) okapi::bstring
{
$result = PyByteArray_FromStringAndSize((const char*) &$1[0], $1.size());
}
///////////////////////////////////////
// buffers
///////////////////////////////////////
%typemap(in) (const void *buffer, std::size_t buffer_size) {
if (PyByteArray_Check($input)) {
$1 = (void *) PyByteArray_AsString($input);
$2 = PyByteArray_Size($input);
}
%#if PY_MAJOR_VERSION < 3
else if (PyString_Check($input)) {
$1 = (void *) PyString_AsString($input);
$2 = PyString_Size($input);
}
%#endif
else {
PyErr_SetString(PyExc_ValueError, "Expecting a bytearray");
return NULL;
}
}
%typemap(typecheck) (const void *buffer, std::size_t buffer_size) {
$1 = (PyByteArray_Check($input) || PyString_Check($input)) ? 1 : 0;
}
///////////////////////////////////////
// boost::filesystem::path
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER)
const boost::filesystem::path &
{
$1 = PyUnicode_Check($input) || PyBytes_Check($input);
}
%typemap(in) const boost::filesystem::path &
{
if (PyUnicode_Check($input))
{
PyObject *bytes = PyUnicode_AsEncodedString($input, Py_FileSystemDefaultEncoding, "surrogateescape");
$1 = new boost::filesystem::path(PyBytes_AsString(bytes));
Py_CLEAR(bytes);
}
else if (PyBytes_Check($input))
{
$1 = new boost::filesystem::path(PyBytes_AsString($input));
}
}
%typemap(freearg) const boost::filesystem::path
{
delete $1;
}
///////////////////////////////////////
/// const cv::Scalar
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
cv::Scalar
{
$1 = is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
cv::Scalar
{
$1 = *array_to_scalar($input);
if (&$1 == NULL)
SWIG_fail;
}
///////////////////////////////////////
/// const cv::Size
///////////////////////////////////////
%typecheck(SWIG_TYPECHECK_POINTER,
fragment="OKAPI_Fragments")
cv::Size
{
$1 = is_array($input) || PySequence_Check($input);
}
%typemap(in, numinputs=1,
fragment="OKAPI_Fragments")
cv::Size
{
$1 = *array_to_size($input);
if (&$1 == NULL)
SWIG_fail;
}
///////////////////////////////////////
/// return cv::Point2f *
///////////////////////////////////////
%typemap(out,
fragment="OKAPI_Fragments")
cv::Point2f
{
PyObject* array = point2f_to_array(&$1);
if (array == NULL)
SWIG_fail;
$result = array;
}