-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcamera.py
282 lines (211 loc) · 8.09 KB
/
camera.py
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
# ===============================================================================
# Copyright 2015 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
# ============= enthought library imports =======================
# ============= standard library imports ========================
import ctypes
import os
from PIL import Image
from numpy import zeros, uint8, uint32
from cStringIO import StringIO
# ============= local library imports ==========================
from core import lib, TOUPCAM_EVENT_IMAGE, TOUPCAM_EVENT_STILLIMAGE, success, HToupCam
class ToupCamCamera(object):
_data = None
_frame_fn = None
_temptint_cb = None
_save_path = None
resolution = None
size = None
def __init__(self, resolution=None, bits=32, size=None):
if resolution is None and size is None:
resolution = 2
if bits not in (32,):
raise ValueError('Bits needs to by 8 or 32')
if size is None:
self.resolution = resolution
else:
self.size = size
self.cam = self.get_camera()
self.bits = bits
# icamera interface
def save(self, p, extension='JPEG', *args, **kw):
image = self.get_pil_image()
image.save(p, extension, *args, **kw)
def save_jpeg(self, p, quality=100):
im = self.get_pil_image()
im.save(p, 'JPEG', quality=quality)
def save_tiff(self, p):
im = self.get_pil_image()
im.save(p, 'TIFF')
def get_jpeg_data(self, data=None, quality=75):
im = self.get_pil_image(data)
s = StringIO()
im.save(s, 'JPEG', quality=quality)
s.seek(0, os.SEEK_END)
return s.getvalue()
def get_pil_image(self, data=None):
# im = self._data
if data is None:
data = self._data
raw = data.view(uint8).reshape(data.shape + (-1,))
bgr = raw[..., :3]
image = Image.fromarray(bgr, 'RGB')
b, g, r = image.split()
return Image.merge('RGB', (r, g, b))
def get_image_data(self, *args, **kw):
d = self._data
return d
def close(self):
if self.cam:
lib.Toupcam_Close(self.cam)
def open(self):
if self.resolution:
self.set_esize(self.resolution)
else:
self.set_size(*self.size)
args = self.get_size()
if not args:
return
h, w = args[1].value, args[0].value
shape = (h, w)
if self.bits == 8:
dtype = uint8
else:
dtype = uint32
self._data = zeros(shape, dtype=dtype)
bits = ctypes.c_int(self.bits)
def get_frame(nEvent, ctx):
if nEvent == TOUPCAM_EVENT_IMAGE:
w, h = ctypes.c_uint(), ctypes.c_uint()
lib.Toupcam_PullImage(self.cam, ctypes.c_void_p(self._data.ctypes.data), bits,
ctypes.byref(w),
ctypes.byref(h))
elif nEvent == TOUPCAM_EVENT_STILLIMAGE:
w, h = self.get_size()
h, w = h.value, w.value
still = zeros((h, w), dtype=uint32)
lib.Toupcam_PullStillImage(self.cam, ctypes.c_void_p(still.ctypes.data), bits, None, None)
self._do_save(still)
callback = ctypes.CFUNCTYPE(None, ctypes.c_uint, ctypes.c_void_p)
self._frame_fn = callback(get_frame)
result = lib.Toupcam_StartPullModeWithCallback(self.cam, self._frame_fn)
return success(result)
# private
def _do_save(self, im):
image = self.get_pil_image(im)
image.save(self._save_path)
# ToupCam interface
def _lib_func(self, func, *args, **kw):
ff = getattr(lib, 'Toupcam_{}'.format(func))
result = ff(self.cam, *args, **kw)
return success(result)
def _lib_get_func(self, func):
v = ctypes.c_int()
if self._lib_func('get_{}'.format(func), ctypes.byref(v)):
return v.value
# setters
def set_gamma(self, v):
self._lib_func('put_Gamma', ctypes.c_int(v))
def set_contrast(self, v):
self._lib_func('put_Contrast', ctypes.c_int(v))
def set_brightness(self, v):
self._lib_func('put_Brightness', ctypes.c_int(v))
def set_saturation(self, v):
self._lib_func('put_Saturation', ctypes.c_int(v))
def set_hue(self, v):
self._lib_func('put_Hue', ctypes.c_int(v))
def set_exposure_time(self, v):
self._lib_func('put_ExpoTime', ctypes.c_ulong(v))
# getters
def get_gamma(self):
return self._lib_get_func('Gamma')
def get_contrast(self):
return self._lib_get_func('Contrast')
def get_brightness(self):
return self._lib_get_func('Brightness')
def get_saturation(self):
return self._lib_get_func('Saturation')
def get_hue(self):
return self._lib_get_func('Hue')
def get_exposure_time(self):
return self._lib_get_func('ExpoTime')
def do_awb(self, callback=None):
"""
Toupcam_AwbOnePush(HToupCam h, PITOUPCAM_TEMPTINT_CALLBACK fnTTProc, void* pTTCtx);
:return:
"""
def temptint_cb(temp, tint):
if callback:
callback((temp, tint))
callback = ctypes.CFUNCTYPE(None, ctypes.c_uint, ctypes.c_void_p)
self._temptint_cb = callback(temptint_cb)
return self._lib_func('AwbOnePush', self._temptint_cb)
def set_temperature_tint(self, temp, tint):
lib.Toupcam_put_TempTint(self.cam, temp, tint)
def get_temperature_tint(self):
temp = ctypes.c_int()
tint = ctypes.c_int()
if self._lib_func('get_TempTint', ctypes.byref(temp), ctypes.byref(tint)):
return temp.value, tint.value
def get_auto_exposure(self):
expo_enabled = ctypes.c_bool()
result = lib.Toupcam_get_AutoExpoEnable(self.cam, ctypes.byref(expo_enabled))
if success(result):
return expo_enabled.value
def set_auto_exposure(self, expo_enabled):
lib.Toupcam_put_AutoExpoEnable(self.cam, expo_enabled)
def get_camera(self, cid=None):
func = lib.Toupcam_Open
func.restype = ctypes.POINTER(HToupCam)
cam = func(cid)
return cam
def get_serial(self):
sn = ctypes.create_string_buffer(32)
result = lib.Toupcam_get_SerialNumber(self.cam, sn)
if success(result):
sn = sn.value
return sn
def get_firmware_version(self):
fw = ctypes.create_string_buffer(16)
result = lib.Toupcam_get_FwVersion(self.cam, fw)
if success(result):
return fw.value
def get_hardware_version(self):
hw = ctypes.create_string_buffer(16)
result = lib.Toupcam_get_HwVersion(self.cam, hw)
if success(result):
return hw.value
def get_size(self):
w, h = ctypes.c_long(), ctypes.c_long()
result = lib.Toupcam_get_Size(self.cam, ctypes.byref(w), ctypes.byref(h))
if success(result):
return w, h
def get_esize(self):
res = ctypes.c_long()
result = lib.Toupcam_get_eSize(self.cam, ctypes.byref(res))
if success(result):
return res
def set_esize(self, nres):
lib.Toupcam_put_eSize(self.cam, ctypes.c_ulong(nres))
def set_size(self, w, h):
self._lib_func('put_Size', ctypes.c_long(w), ctypes.c_long(h))
if __name__ == '__main__':
import time
cam = ToupCamCamera()
cam.open()
time.sleep(1)
cam.save('foo.jpg')
# ============= EOF =============================================