-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkcameramodule.c
397 lines (338 loc) · 12.1 KB
/
kcameramodule.c
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
#include "kcamera.h"
#include "streamer.h"
#include <Python.h>
#include <structmember.h>
typedef struct
{
PyObject_HEAD
// parameters
KcParams m_params;
PyObject *m_resObject;
PyObject *m_modeObject;
PyObject *m_streamerObject;
// list of frames
} Camera;
Camera *g_camera = NULL;
void updateResObject(Camera *self)
{
Py_XDECREF(self->m_resObject);
self->m_resObject = Py_BuildValue("(II)", self->m_params.m_width, self->m_params.m_height);
}
static int parseArgs(Camera *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"resolution", "framerate", "duration", "mode", "brightness", "autoshutter", "awb", "awb_red", "awb_blue", "shutter_speed", "saturation", "max_latency", "mem_reserve",
"hflip", "vflip", "start_shift", NULL};
PyObject *resObject = NULL, *modeObject = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OIfOIppfffIIIppI", kwlist,
&resObject, &self->m_params.m_framerate, &self->m_params.m_duration, &modeObject, &self->m_params.m_brightness,
&self->m_params.m_autoShutter, &self->m_params.m_awb, &self->m_params.m_awbRed, &self->m_params.m_awbBlue,
&self->m_params.m_shutterSpeed, &self->m_params.m_saturation, &self->m_params.m_maxLatency, &self->m_params.m_memReserve,
&self->m_params.m_hflip, &self->m_params.m_vflip, &self->m_params.m_startShift))
return -1;
if (resObject)
{
if (!PyArg_ParseTuple(resObject, "II", &self->m_params.m_width, &self->m_params.m_height))
return -1;
else
{
// decrement old object
Py_XDECREF(self->m_resObject);
// reuse object
Py_XINCREF(resObject);
self->m_resObject = resObject;
}
}
else
updateResObject(self);
if (modeObject)
{
char *mode;
if (!PyArg_Parse(modeObject, "s", &mode))
return -1;
else
{
strcpy(self->m_params.m_mode, mode);
// decrement old object
Py_XDECREF(self->m_modeObject);
// reuse object
Py_XINCREF(modeObject);
self->m_modeObject = modeObject;
}
}
else
{
Py_XDECREF(self->m_modeObject);
self->m_modeObject = Py_BuildValue("s", self->m_params.m_mode);
}
return 0;
}
static PyObject *createStream(const char *filename)
{
PyObject *args, *res;
args = Py_BuildValue("(s)", filename);
res = PyObject_CallObject((PyObject *)&streamerType, args);
Py_XDECREF(args); // no longer needed
return res;
}
static PyObject *camera_stream(Camera *self, PyObject *args, PyObject *kwds)
{
if (parseArgs(self, args, kwds)<0)
{
PyErr_BadArgument();
return NULL;
}
// create streamer object
if (self->m_streamerObject==NULL)
self->m_streamerObject = createStream("");
else
Py_INCREF(self->m_streamerObject);
return self->m_streamerObject;
}
static PyObject *camera_record(Camera *self, PyObject *args, PyObject *kwds)
{
PyObject *streamer;
if (parseArgs(self, args, kwds)<0)
{
PyErr_BadArgument();
return NULL;
}
// if there's already a recording, close it down and start a new one (can't have more than 1)
if (kcGetRecord())
kcStopRecord();
// create record object
if (kcStartRecord()<0)
{
PyErr_SetString(PyExc_AttributeError, "recorder is already recording");
return NULL;
}
// start
kcStart();
// create streamer object
streamer = createStream("");
return streamer;
}
static PyObject *camera_getModes(Camera *self, PyObject *args)
{
unsigned i, n;
PyObject *tuple, *mode;
const char **modes;
modes = kcGetModes();
// count the number of modes
for (n=0; modes[n]; n++);
tuple = PyTuple_New(n);
for (i=0; i<n; i++)
{
mode = PyUnicode_FromString(modes[i]);
PyTuple_SetItem(tuple, i, mode);
}
return tuple;
}
static PyObject *camera_load(Camera *self, PyObject *args)
{
const char *filename;
PyObject *streamer;
if (!PyArg_ParseTuple(args, "s", &filename))
{
PyErr_SetString(PyExc_Exception, "load needs a filename (string)\n");
return NULL;
}
streamer = createStream(filename);
return streamer;
}
static void camera_dealloc(Camera *self)
{
kcExit();
Py_XDECREF(self->m_resObject);
Py_XDECREF(self->m_modeObject);
Py_TYPE(self)->tp_free((PyObject *)self);
g_camera = NULL;
}
static PyObject *camera_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Camera *self;
if (g_camera) // only one object allowed
{
PyErr_SetString(PyExc_Exception, "only one Camera object allowed");
return NULL;
}
self = (Camera *)type->tp_alloc(type, 0);
if (!self)
{
PyErr_SetString(PyExc_Exception, "memory allocation");
return NULL;
}
g_camera = self;
return (PyObject *)self;
}
void streamCallback(Streamer *streamer)
{
if (g_camera && streamer==(Streamer *)g_camera->m_streamerObject)
g_camera->m_streamerObject = NULL;
}
static int camera_init(Camera *self, PyObject *args, PyObject *kwds)
{
int res;
unsigned width, height;
self->m_streamerObject = NULL;
stSetCallback(streamCallback);
kcInit(&self->m_params);
width = self->m_params.m_width;
height = self->m_params.m_height;
res = parseArgs(self, args, kwds);
kcUpdateParams();
// If the width or height was changed, we need to update the resolution object
if (width!=self->m_params.m_width || height!=self->m_params.m_height)
updateResObject(self);
return res;
}
static int camera_setAttr(Camera *self, PyObject *attr_name, PyObject *v)
{
unsigned long val;
unsigned width=self->m_params.m_width, height=self->m_params.m_height;
int res;
PyObject *str = PyUnicode_AsEncodedString(attr_name, "utf-8", "~E~");
const char *cstr = PyBytes_AS_STRING(str);
if (v==NULL)
return PyObject_GenericSetAttr((PyObject *)self, attr_name, v);
if (strcmp(cstr, "brightness")==0)
{
// limit brightness value between 0 and 100
val = PyLong_AsUnsignedLong(v);
if (val>100)
{
Py_XDECREF(v);
v = PyLong_FromUnsignedLong(100);
}
}
else if (strcmp(cstr, "mode")==0)
{
int i;
char *mode = (char *)PyUnicode_1BYTE_DATA(v);
const char **modes;
modes = kcGetModes();
for (i=0; modes[i]; i++)
if (strcmp(mode, modes[i])==0)
break;
if (modes[i]==NULL)
{
PyErr_SetString(PyExc_AttributeError, "invalid mode");
return -1;
}
strcpy(self->m_params.m_mode, modes[i]);
}
else if (strcmp(cstr, "framerate")==0)
{
// limit framerate value
val = PyLong_AsUnsignedLong(v);
if (val < self->m_params.m_minFps)
{
Py_XDECREF(v);
v = PyLong_FromUnsignedLong(self->m_params.m_minFps);
}
else if (val > self->m_params.m_maxFps)
{
Py_XDECREF(v);
v = PyLong_FromUnsignedLong(self->m_params.m_maxFps);
}
}
// update param value
res = PyObject_GenericSetAttr((PyObject *)self, attr_name, v);
// handle side-effects from parameter change
kcUpdateParams();
// If resolution has changed, update m_resObject
if (width!=self->m_params.m_width || height!=self->m_params.m_height)
updateResObject(self);
return res;
}
static PyMemberDef camera_members[] =
{
{"resolution", T_OBJECT, offsetof(Camera, m_resObject), READONLY, "frame resolution (width, height)"},
{"framerate", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_framerate), 0, "framerate (frames/second)"},
{"duration", T_FLOAT, offsetof(Camera, m_params) + offsetof(KcParams, m_duration), 0, "record duration (milliseconds)"},
{"mode", T_OBJECT, offsetof(Camera,m_modeObject), 0, "video mode, use getmodes to get possible modes"},
{"brightness", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_brightness), 0, "brightness setting (0 to 100)"},
{"autoshutter", T_BOOL, offsetof(Camera, m_params) + offsetof(KcParams, m_autoShutter), 0, "auto shutter enable"},
{"awb", T_BOOL, offsetof(Camera, m_params) + offsetof(KcParams, m_awb), 0, "auto white balance enable"},
{"awb_red", T_FLOAT, offsetof(Camera, m_params) + offsetof(KcParams, m_awbRed), 0, "auto white balance red gain (awb must be False)"},
{"awb_blue", T_FLOAT, offsetof(Camera, m_params) + offsetof(KcParams, m_awbBlue), 0, "auto white balance blue gain (awb must be False)"},
{"shutter_speed", T_FLOAT, offsetof(Camera, m_params) + offsetof(KcParams, m_shutterSpeed), 0, "shutter speed in microseconds"},
{"saturation", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_saturation), 0, "saturation (0 to 255)"},
{"max_latency", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_maxLatency), 0, "maximum latency allowed in grab mode"},
{"mem_reserve", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_memReserve), 0, "amount of memory to reserve when grabbing frames (megabytes)"},
{"hflip", T_BOOL, offsetof(Camera, m_params) + offsetof(KcParams, m_hflip), 0, "flip horizontal orientation"},
{"vflip", T_BOOL, offsetof(Camera, m_params) + offsetof(KcParams, m_vflip), 0, "flip vertical orientation"},
{"start_shift", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_startShift), 0, "start recording shifted in microseconds"},
{"measured_framerate", T_FLOAT, offsetof(Camera, m_params) + offsetof(KcParams, m_fps), READONLY, "current frames per second"},
{"max_framerate", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_maxFps), READONLY, "maximum allowed frames per second"},
{"min_framerate", T_UINT, offsetof(Camera, m_params) + offsetof(KcParams, m_minFps), READONLY, "minimum allowed frames per second"},
{NULL} // Sentinel
};
static PyMethodDef camera_methods[] = {
{"stream", (PyCFunction)camera_stream, METH_VARARGS|METH_KEYWORDS, "get live streamer object"},
{"record", (PyCFunction)camera_record, METH_VARARGS|METH_KEYWORDS, "record frames"},
{"getmodes", (PyCFunction)camera_getModes, METH_NOARGS, "get video modes"},
{"load", (PyCFunction)camera_load, METH_VARARGS, "load stream from file"},
{NULL} // Sentinel
};
static PyTypeObject cameraType =
{
PyVarObject_HEAD_INIT(NULL, 0)
"kcamera.Camera", // tp_name
sizeof(Camera), // tp_basicsize
0, // tp_itemsize
(destructor)camera_dealloc, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_reserved
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
camera_setAttr, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
"kcamera objects", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
camera_methods, // tp_methods
camera_members, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
(initproc)camera_init, // tp_init
0, // tp_alloc
camera_new // tp_new
};
static struct PyModuleDef kcameraModule = {
PyModuleDef_HEAD_INIT,
"kcamera", // name of module
"kcamera module",
-1, // size of per-interpreter state of the module,
// or -1 if the module keeps state in global variables.
NULL, NULL, NULL, NULL, NULL
};
PyMODINIT_FUNC PyInit_kcamera(void)
{
PyObject *m;
if (PyType_Ready(&cameraType)<0)
return NULL;
streamerInit();
m = PyModule_Create(&kcameraModule);
if (m == NULL)
return NULL;
Py_INCREF(&cameraType);
PyModule_AddObject(m, "Camera", (PyObject *)&cameraType);
return m;
}