1
1
import logging
2
+ import threading
2
3
import time
3
4
from typing import Any , Dict , Optional , List
4
5
@@ -27,6 +28,7 @@ def __init__(self, device: str, settings: Optional[Dict[str, Any]] = None, **kwa
27
28
self ._device_name = device
28
29
self ._camera : Optional [aravis .Camera ] = None
29
30
self ._settings : Dict [str , Any ] = {} if settings is None else settings
31
+ self ._camera_lock = threading .Lock ()
30
32
31
33
# thread
32
34
if device is not None :
@@ -36,11 +38,25 @@ def __init__(self, device: str, settings: Optional[Dict[str, Any]] = None, **kwa
36
38
37
39
def open (self ) -> None :
38
40
"""Open module."""
41
+ BaseVideo .open (self )
42
+
39
43
# list devices
40
44
ids = aravis .get_device_ids ()
41
45
if self ._device_name not in ids :
42
46
raise ValueError ('Could not find given device name in list of available cameras.' )
43
47
48
+ # open camera
49
+ self .activate_camera ()
50
+
51
+ def close (self ) -> None :
52
+ """Close the module."""
53
+ BaseVideo .close (self )
54
+ with self ._camera_lock :
55
+ self ._close_camera ()
56
+
57
+ def _open_camera (self ) -> None :
58
+ """Open camera."""
59
+
44
60
# open camera
45
61
log .info ('Connecting to camera %s...' , self ._device_name )
46
62
self ._camera = aravis .Camera (self ._device_name )
@@ -51,32 +67,47 @@ def open(self) -> None:
51
67
log .info (f'Setting value { key } ={ value } ...' )
52
68
self ._camera .set_feature (key , value )
53
69
54
- # open base
55
- BaseVideo .open (self )
56
-
57
- def close (self ) -> None :
58
- """Close the module."""
59
- BaseVideo .close (self )
70
+ # start acquisition
71
+ self ._camera .start_acquisition_continuous (nb_buffers = 5 )
60
72
73
+ def _close_camera (self ) -> None :
74
+ """Close camera."""
61
75
# stop camera
62
76
if self ._camera is not None :
77
+ log .info ('Closing camera...' )
63
78
self ._camera .stop_acquisition ()
64
79
self ._camera .shutdown ()
80
+ self ._camera = None
81
+
82
+ def _activate_camera (self ) -> None :
83
+ """Can be overridden by derived class to implement inactivity sleep"""
84
+ with self ._camera_lock :
85
+ self ._open_camera ()
86
+
87
+ def _deactivate_camera (self ) -> None :
88
+ """Can be overridden by derived class to implement inactivity sleep"""
89
+ with self ._camera_lock :
90
+ self ._close_camera ()
65
91
66
92
def _capture (self ) -> None :
67
- # start acquisition
68
- self ._camera .start_acquisition_continuous (nb_buffers = 5 )
93
+ """Take new images in loop."""
69
94
70
95
# loop until closing
71
96
last = time .time ()
72
97
while not self .closing .is_set ():
73
- # read frame
74
- frame = self ._camera .pop_frame ()
98
+ # no camera or not active?
99
+ if self ._camera is None or not self .camera_active :
100
+ # wait a little
101
+ self .closing .wait (0.1 )
102
+ continue
75
103
76
104
# if time since last image is too short, wait a little
77
105
if time .time () - last < self ._interval :
78
106
self .closing .wait (0.01 )
79
107
continue
108
+
109
+ # read frame
110
+ frame = self ._camera .pop_frame ()
80
111
last = time .time ()
81
112
82
113
# process it
@@ -91,6 +122,7 @@ def set_exposure_time(self, exposure_time: float, **kwargs: Any) -> None:
91
122
Raises:
92
123
ValueError: If exposure time could not be set.
93
124
"""
125
+ self .activate_camera ()
94
126
self ._camera .set_exposure_time (exposure_time * 1e6 )
95
127
96
128
def get_exposure_time (self , ** kwargs : Any ) -> float :
@@ -99,6 +131,7 @@ def get_exposure_time(self, **kwargs: Any) -> float:
99
131
Returns:
100
132
Exposure time in seconds.
101
133
"""
134
+ self .activate_camera ()
102
135
return self ._camera .get_exposure_time () / 1e6
103
136
104
137
0 commit comments