-
Notifications
You must be signed in to change notification settings - Fork 0
/
ueye_utils.py
192 lines (156 loc) · 5.82 KB
/
ueye_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
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
"""
Provide utils to ueye_camera.py
@fcn_header:
def get_bits_per_pixel(color_mode):
class uEyeException(Exception):
def check(ret):
class ImageBuffer:
class MemoryInfo:
class ImageData:
class Rect:
class FrameThread(Thread):
@refer: pyueye_example, source code from
https://en.ids-imaging.com/techtipps-detail/en_techtip-embedded-vision-kit.html
Copyright (c) 2017 by IDS Imaging Development Systems GmbH.
All rights reserved.
@create data: 2019.11.04
@update data: 2019.11.05
@author: Yang-Jie Gao
@e-mail: 60777001h@ntnu.edu.tw
"""
from pyueye import ueye
from threading import Thread
from ctypes import byref
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
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))
#break
def notify(self, image_data):
if self.views:
if type(self.views) is not list:
self.views = [self.views]
for view in self.views:
view.handle(image_data)
def stop(self):
self.cam.stop_video()
self.running = False
# class RecThread(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
# 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))
# break
# def notify(self, image_data):
# if self.views:
# if type(self.views) is not list:
# self.views = [self.views]
# for view in self.views:
# view.handle(image_data)