-
Notifications
You must be signed in to change notification settings - Fork 0
/
ueye_camera.py
147 lines (113 loc) · 4.81 KB
/
ueye_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
"""
uEye CCD base setup
e.g. colormode, capture image, open ccd, stop ccd
@fcn_header:
class Camera:
@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 ueye_utils import (uEyeException, Rect, get_bits_per_pixel,
ImageBuffer, check)
class Camera:
def __init__(self, device_id=0):
self.h_cam = ueye.HIDS(device_id)
self.img_buffers = []
def __enter__(self):
self.init()
return self
def __exit__(self, _type, value, traceback):
self.exit()
def handle(self):
return self.h_cam
def alloc(self, buffer_count=3):
rect = self.get_aoi()
bpp = get_bits_per_pixel(self.get_colormode())
for buff in self.img_buffers:
check(ueye.is_FreeImageMem(self.h_cam, buff.mem_ptr, buff.mem_id))
for i in range(buffer_count):
buff = ImageBuffer()
ueye.is_AllocImageMem(self.h_cam,
rect.width, rect.height, bpp,
buff.mem_ptr, buff.mem_id)
check(ueye.is_AddToSequence(self.h_cam, buff.mem_ptr, buff.mem_id))
self.img_buffers.append(buff)
ueye.is_InitImageQueue(self.h_cam, 0)
def init(self):
ret = ueye.is_InitCamera(self.h_cam, None)
if ret != ueye.IS_SUCCESS:
self.h_cam = None
raise uEyeException(ret)
return ret
def exit(self):
ret = None
if self.h_cam is not None:
ret = ueye.is_ExitCamera(self.h_cam)
if ret == ueye.IS_SUCCESS:
self.h_cam = None
def get_aoi(self):
rect_aoi = ueye.IS_RECT()
ueye.is_AOI(self.h_cam, ueye.IS_AOI_IMAGE_GET_AOI, rect_aoi, ueye.sizeof(rect_aoi))
return Rect(rect_aoi.s32X.value,
rect_aoi.s32Y.value,
rect_aoi.s32Width.value,
rect_aoi.s32Height.value)
def set_aoi(self, x, y, width, height):
rect_aoi = ueye.IS_RECT()
rect_aoi.s32X = ueye.int(x)
rect_aoi.s32Y = ueye.int(y)
rect_aoi.s32Width = ueye.int(width)
rect_aoi.s32Height = ueye.int(height)
return ueye.is_AOI(self.h_cam, ueye.IS_AOI_IMAGE_SET_AOI, rect_aoi, ueye.sizeof(rect_aoi))
def capture_video(self, wait=False):
wait_param = ueye.IS_WAIT if wait else ueye.IS_DONT_WAIT
return ueye.is_CaptureVideo(self.h_cam, wait_param)
def stop_video(self):
return ueye.is_StopLiveVideo(self.h_cam, ueye.IS_FORCE_VIDEO_STOP)
def freeze_video(self, wait=False):
wait_param = ueye.IS_WAIT if wait else ueye.IS_DONT_WAIT
return ueye.is_FreezeVideo(self.h_cam, wait_param)
def set_colormode(self, colormode):
check(ueye.is_SetColorMode(self.h_cam, colormode))
def get_colormode(self):
ret = ueye.is_SetColorMode(self.h_cam, ueye.IS_GET_COLOR_MODE)
return ret
def set_FrameRate(self, rate):
rate = ueye.DOUBLE(rate)
newrate = ueye.DOUBLE()
check(ueye.is_SetFrameRate(self.h_cam, rate, newrate))
print('FR:',newrate)
def set_Exposure(self, val):
ms = ueye.DOUBLE(val)
check(ueye.is_Exposure(self.h_cam, ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, ms, ueye.sizeof(ms)))
print('EXP:',ms)
def set_full_auto(self):
enable = ueye.DOUBLE(1)
zero = ueye.DOUBLE(0)
ms = ueye.DOUBLE(20)
rate = ueye.DOUBLE(50)
newrate = ueye.DOUBLE()
ret = ueye.is_SetAutoParameter(self.h_cam, ueye.IS_SET_ENABLE_AUTO_GAIN, enable, zero)
ret = ueye.is_SetAutoParameter(self.h_cam, ueye.IS_SET_ENABLE_AUTO_SHUTTER, enable, zero)
ret = ueye.is_SetFrameRate(self.h_cam, rate, newrate)
ret = ueye.is_Exposure(self.h_cam, ueye.IS_EXPOSURE_CMD_GET_EXPOSURE, ms, ueye.sizeof(ms))
print("Auto mode")
print('A_GAIN:',ret)
print('A_SHUTTER:',ret)
print('FR:',ret,newrate)
print('EXP:',ret,ms)
def get_format_list(self):
count = ueye.UINT()
check(ueye.is_ImageFormat(self.h_cam, ueye.IMGFRMT_CMD_GET_NUM_ENTRIES, count, ueye.sizeof(count)))
format_list = ueye.IMAGE_FORMAT_LIST(ueye.IMAGE_FORMAT_INFO * count.value)
format_list.nSizeOfListEntry = ueye.sizeof(ueye.IMAGE_FORMAT_INFO)
format_list.nNumListElements = count.value
check(ueye.is_ImageFormat(self.h_cam, ueye.IMGFRMT_CMD_GET_LIST,
format_list, ueye.sizeof(format_list)))
return format_list