forked from pupil-labs/pupil-picoflexx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroypycy.pyx
164 lines (133 loc) · 5.79 KB
/
roypycy.pyx
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
# cython: language_level=3, language=c++
# distutils: language=c++
import cython
import numpy as np
from libc.stdint cimport uint16_t, uint32_t, uint8_t
# From https://stackoverflow.com/a/29343772
cdef extern from "swigpyobject.h":
ctypedef struct SwigPyObject:
void *ptr
cdef extern from "royale/Vector.hpp" namespace "royale":
cdef cppclass Vector[T]:
cppclass iterator:
T operator*()
iterator operator++()
bint operator==(iterator)
bint operator!=(iterator)
Vector()
void push_back(T&)
T& operator[](int)
T& at(int)
const T* data()
iterator begin()
iterator end()
size_t count()
cdef extern from "royale/Pair.hpp" namespace "royale":
cdef cppclass Pair[T, U]:
Pair()
T first
U second
cdef extern from "royale/LensParameters.hpp" namespace "royale":
ctypedef struct LensParameters:
Pair[float, float] principalPoint; #!< cx/cy
Pair[float, float] focalLength; #!< fx/fy
Pair[float, float] distortionTangential; #!< p1/p2
Vector[float] distortionRadial; #!< k1/k2/k3
cdef extern from "royale/DepthData.hpp" namespace "royale":
ctypedef struct DepthData:
int version; # !< version number of the data format
# std::chrono::microseconds timeStamp; # !< timestamp in microseconds precision (time since epoch 1970)
# StreamId streamId; # !< stream which produced the data
uint16_t width; # !< width of depth image
uint16_t height; # !< height of depth image
Vector[uint32_t] exposureTimes; # !< exposureTimes retrieved from CapturedUseCase
Vector[DepthPoint] points; # !< array of points
ctypedef struct DepthPoint:
float x; # !< X coordinate [meters]
float y; # !< Y coordinate [meters]
float z; # !< Z coordinate [meters]
float noise; # !< noise value [meters]
uint16_t grayValue; # !< 16-bit gray value
uint8_t depthConfidence; # !< value from 0 (invalid) to 255 (full confidence)
cdef extern from "royale/ExposureMode.hpp" namespace "royale":
ctypedef enum ExposureMode:
MANUAL 'royale::ExposureMode::MANUAL', # !< Camera exposure mode set to manual
AUTOMATIC 'royale::ExposureMode::AUTOMATIC' # !< Camera exposure mode set to automatic
cdef extern from "royale/ICameraDevice.hpp" namespace "royale":
cdef cppclass ICameraDevice:
int getExposureMode(ExposureMode &exposureMode, uint16_t streamId)
int setExposureMode(ExposureMode exposureMode, uint16_t streamId)
int getLensParameters(LensParameters ¶ms)
def get_depth_data(depthdata):
cdef SwigPyObject *swig_obj = <SwigPyObject*>depthdata.this
cdef DepthData *mycpp_ptr = <DepthData*?>swig_obj.ptr
cdef DepthData my_instance = mycpp_ptr[0]
result = np.zeros((my_instance.points.count(), ), dtype=np.float64)
cdef double[:] result_view = result
cdef int i = 0;
for pt in my_instance.points:
result_view[i] = pt.z
i += 1
return result
def get_lens_parameters(camera):
cdef SwigPyObject *swig_obj = <SwigPyObject*>camera.this
cdef ICameraDevice **mycpp_ptr = <ICameraDevice**?>swig_obj.ptr
cdef LensParameters params;
mycpp_ptr[0][0].getLensParameters(params)
return {
'principalPoint': (params.principalPoint.first, params.principalPoint.second),
'focalLength': (params.focalLength.first, params.focalLength.second),
'distortionTangential': (params.distortionTangential.first, params.distortionTangential.second),
'distortionRadial': tuple(x for x in params.distortionRadial),
}
def get_exposure_mode(camera):
cdef SwigPyObject *swig_obj = <SwigPyObject*>camera.this
cdef ICameraDevice **mycpp_ptr = <ICameraDevice**?>swig_obj.ptr
cdef ExposureMode exp;
mycpp_ptr[0][0].getExposureMode(exp, 0)
return <int>exp
def set_exposure_mode(camera, mode):
cdef SwigPyObject *swig_obj = <SwigPyObject*>camera.this
cdef ICameraDevice **mycpp_ptr = <ICameraDevice**?>swig_obj.ptr
cdef ExposureMode exp_mode;
if mode == 0:
exp_mode = ExposureMode.MANUAL
elif mode == 1:
exp_mode = ExposureMode.AUTOMATIC
else:
raise ValueError('Invalid mode')
mycpp_ptr[0][0].setExposureMode(exp_mode, 0)
@cython.boundscheck(False) # Deactivate bounds checking
@cython.wraparound(False) # Deactivate negative indexing.
def get_backend_data(depthdata):
# Obtain the raw DepthData instance from the Swig object
cdef SwigPyObject *swig_obj = <SwigPyObject*>depthdata.this
cdef DepthData *mycpp_ptr = <DepthData*?>swig_obj.ptr
cdef DepthData my_instance = mycpp_ptr[0]
data = np.zeros((my_instance.points.count(), ),
dtype=[
("x", np.float32),
("y", np.float32),
("z", np.float32),
("noise", np.float32),
("grayValue", np.uint16),
("depthConfidence", np.uint8),
],
).view(np.recarray)
# Setup memoryviews
cdef float[:] r_x = data.x
cdef float[:] r_y = data.y
cdef float[:] r_z = data.z
cdef float[:] r_noise = data.noise
cdef uint16_t[:] r_grayValue = data.grayValue
cdef uint8_t[:] r_depthConfidence = data.depthConfidence
cdef int i = 0;
for pt in my_instance.points:
r_x[i] = pt.x
r_y[i] = pt.y
r_z[i] = pt.z
r_noise[i] = pt.noise
r_grayValue[i] = pt.grayValue
r_depthConfidence[i] = pt.depthConfidence
i+=1
return data