-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpyueye_utils.py
147 lines (117 loc) · 4.43 KB
/
pyueye_utils.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
#!/usr/bin/env python
from pyueye import ueye
from threading import Thread
import timeit
def get_bits_per_pixel(color_mode):
"""
returns the number of bits per pixel for the given color mode
raises exception if color mode is not is not in dict
"""
return {
ueye.IS_CM_SENSOR_RAW8: 8,
ueye.IS_CM_SENSOR_RAW10: 16,
ueye.IS_CM_SENSOR_RAW12: 16,
ueye.IS_CM_SENSOR_RAW16: 16,
ueye.IS_CM_MONO8: 8,
ueye.IS_CM_RGB8_PACKED: 24,
ueye.IS_CM_BGR8_PACKED: 24,
ueye.IS_CM_RGBA8_PACKED: 32,
ueye.IS_CM_BGRA8_PACKED: 32,
ueye.IS_CM_BGR10_PACKED: 32,
ueye.IS_CM_RGB10_PACKED: 32,
ueye.IS_CM_BGRA12_UNPACKED: 64,
ueye.IS_CM_BGR12_UNPACKED: 48,
ueye.IS_CM_BGRY8_PACKED: 32,
ueye.IS_CM_BGR565_PACKED: 16,
ueye.IS_CM_BGR5_PACKED: 16,
ueye.IS_CM_UYVY_PACKED: 16,
ueye.IS_CM_UYVY_MONO_PACKED: 16,
ueye.IS_CM_UYVY_BAYER_PACKED: 16,
ueye.IS_CM_CBYCRY_PACKED: 16,
} [color_mode]
class uEyeException(Exception):
def __init__(self, error_code):
self.error_code = error_code
def __str__(self):
return "Err: " + str(self.error_code)
def check(ret):
if ret != ueye.IS_SUCCESS:
raise uEyeException(ret)
class ImageBuffer:
def __init__(self):
self.mem_ptr = ueye.c_mem_p()
self.mem_id = ueye.int()
class MemoryInfo:
def __init__(self, h_cam, img_buff):
self.x = ueye.int()
self.y = ueye.int()
self.bits = ueye.int()
self.pitch = ueye.int()
self.img_buff = img_buff
rect_aoi = ueye.IS_RECT()
check(ueye.is_AOI(h_cam,
ueye.IS_AOI_IMAGE_GET_AOI, rect_aoi, ueye.sizeof(rect_aoi)))
self.width = rect_aoi.s32Width.value
self.height = rect_aoi.s32Height.value
check(ueye.is_InquireImageMem(h_cam,
self.img_buff.mem_ptr,
self.img_buff.mem_id, self.x, self.y, self.bits, self.pitch))
class ImageData:
def __init__(self, h_cam, img_buff):
self.h_cam = h_cam
self.img_buff = img_buff
self.mem_info = MemoryInfo(h_cam, img_buff)
self.color_mode = ueye.is_SetColorMode(h_cam, ueye.IS_GET_COLOR_MODE)
self.bits_per_pixel = get_bits_per_pixel(self.color_mode)
self.array = ueye.get_data(self.img_buff.mem_ptr,
self.mem_info.width,
self.mem_info.height,
self.mem_info.bits,
self.mem_info.pitch,
True)
def as_1d_image(self):
channels = int((7 + self.bits_per_pixel) / 8)
import numpy
if channels > 1:
return numpy.reshape(self.array, (self.mem_info.height, self.mem_info.width, channels))
else:
return numpy.reshape(self.array, (self.mem_info.height, self.mem_info.width))
def unlock(self):
check(ueye.is_UnlockSeqBuf(self.h_cam, self.img_buff.mem_id, self.img_buff.mem_ptr))
class Rect:
def __init__(self, x=0, y=0, width=0, height=0):
self.x = x
self.y = y
self.width = width
self.height = height
class FrameThread(Thread):
def __init__(self, cam, views=None, copy=True):
super(FrameThread, self).__init__()
self.timeout = 1000
self.cam = cam
self.running = True
self.views = views
self.copy = copy
self.t_old = timeit.default_timer()
cam.set_full_auto()
def run(self):
while self.running:
img_buffer = ImageBuffer()
ret = ueye.is_WaitForNextImage(self.cam.handle(),
self.timeout,
img_buffer.mem_ptr,
img_buffer.mem_id)
if ret == ueye.IS_SUCCESS:
self.notify(ImageData(self.cam.handle(), img_buffer))
t = timeit.default_timer()
fps = 1/(t - self.t_old)
print(fps, ret)
self.t_old = t
#break
def notify(self, image_data):
print(image_data.as_1d_image()[0:10,1,1])
image_data.unlock()
# do things with image_data
def stop(self):
self.cam.stop_video()
self.running = False