BioImager  4.9.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
microscope.cameras.picam.PiCamera Class Reference
Inheritance diagram for microscope.cameras.picam.PiCamera:
microscope.abc.Camera microscope.abc.TriggerTargetMixin microscope.abc.DataDevice microscope.abc.Device

Public Member Functions

 __init__ (self, *args, **kwargs)
 
 get_awb_mode (self)
 
 set_awb_mode (self, val)
 
 set_iso_mode (self, val)
 
 HW_trigger (self, channel)
 
 initialize (self)
 
 make_safe (self)
 
 abort (self)
 
None set_trigger (self, TriggerType ttype, TriggerMode tmode)
 
TriggerMode trigger_mode (self)
 
TriggerType trigger_type (self)
 
 setLED (self, state=False)
 
 set_exposure_time (self, value)
 
 get_exposure_time (self)
 
 get_cycle_time (self)
 
 soft_trigger (self)
 
- Public Member Functions inherited from microscope.abc.Camera
 set_readout_mode (self, description)
 
 get_transform (self)
 
 set_transform (self, transform)
 
typing.Tuple[int, int] get_sensor_shape (self)
 
microscope.Binning get_binning (self)
 
None set_binning (self, microscope.Binning binning)
 
microscope.ROI get_roi (self)
 
None set_roi (self, microscope.ROI roi)
 
- Public Member Functions inherited from microscope.abc.TriggerTargetMixin
None trigger (self)
 
- Public Member Functions inherited from microscope.abc.DataDevice
 __del__ (self)
 
None enable (self)
 
None disable (self)
 
None set_client (self, new_client)
 
None update_settings (self, settings, bool init=False)
 
None receiveClient (self, str client_uri)
 
 grab_next_data (self, bool soft_trigger=True)
 
None receiveData (self, data, timestamp)
 
- Public Member Functions inherited from microscope.abc.Device
bool get_is_enabled (self)
 
None shutdown (self)
 
None add_setting (self, name, dtype, get_func, set_func, values, typing.Optional[typing.Callable[[], bool]] readonly=None)
 
 get_setting (self, str name)
 
 get_all_settings (self)
 
None set_setting (self, str name, value)
 
 describe_setting (self, str name)
 
 describe_settings (self)
 

Public Attributes

 camera = None
 
 roi = ROI(None, None, None, None)
 
float exposure_time = 0.001
 
float cycle_time = self.exposure_time
 
 trigger_mode
 
str camversion = self.camera.revision
 
tuple camversion = (2592, 1944)
 
- Public Attributes inherited from microscope.abc.Camera
 set_readout_mode
 
 get_roi
 
 set_roi
 
- Public Attributes inherited from microscope.abc.DataDevice
bool enabled = False
 
bool _fetch_thread = True
 
bool _dispatch_thread = True
 
None _fetch_thread_run = self._fetch_data()
 
- Public Attributes inherited from microscope.abc.Device
bool enabled = False
 

Protected Member Functions

 _fetch_data (self)
 
 _do_disable (self)
 
 _do_shutdown (self)
 
 _do_enable (self)
 
 _get_roi (self)
 
 _set_binning (self, h_bin, v_bin)
 
 _get_binning (self)
 
 _set_roi (self, left, top, width, height)
 
 _get_sensor_shape (self)
 
 _do_trigger (self)
 
- Protected Member Functions inherited from microscope.abc.Camera
 _process_data (self, data)
 
 _set_readout_transform (self, new_transform)
 
- Protected Member Functions inherited from microscope.abc.TriggerTargetMixin
- Protected Member Functions inherited from microscope.abc.DataDevice
 _send_data (self, client, data, timestamp)
 
None _dispatch_loop (self)
 
None _fetch_loop (self)
 
 _client (self)
 
 _client (self, val)
 
None _put (self, data, timestamp)
 
- Protected Member Functions inherited from microscope.abc.Device

Protected Attributes

bool _acquiring = False
 
float _exposure_time = 0.1
 
bool _triggered = False
 
 _trigger_type = TriggerType.SOFTWARE
 
 _queue = queue.Queue()
 
 _awb_modes = picamera.PiCamera.AWB_MODES
 
list _iso_modes = [0, 100, 200, 320, 400, 500, 640, 800]
 
- Protected Attributes inherited from microscope.abc.Camera
list _readout_modes = ["default"]
 
int _readout_mode = 0
 
tuple _readout_transform = (False, False, False)
 
tuple _client_transform = (False, False, False)
 
tuple _transform = (False, False, False)
 
- Protected Attributes inherited from microscope.abc.DataDevice
 _fetch_thread = None
 
bool _fetch_thread_run = False
 
bool _using_callback = False
 
list _clientStack = []
 
 _liveClients = set()
 
 _dispatch_thread = None
 
 _dispatch_buffer = queue.Queue(maxsize=buffer_length)
 
bool _acquiring = False
 
 _new_data_condition = threading.Condition()
 
 _client = None
 
tuple _new_data = (data, timestamp)
 
- Protected Attributes inherited from microscope.abc.Device
dict _settings = {}
 

Additional Inherited Members

- Static Public Attributes inherited from microscope.abc.Camera
list ALLOWED_TRANSFORMS = [p for p in itertools.product(*3 * [[False, True]])]
 
- Static Public Attributes inherited from microscope.abc.DataDevice
 set_setting = keep_acquiring(Device.set_setting)
 

Detailed Description

Definition at line 50 of file picam.py.

Constructor & Destructor Documentation

◆ __init__()

microscope.cameras.picam.PiCamera.__init__ ( self,
* buffer_length,
** kwargs )
Derived.__init__ must call this at some point.

Reimplemented from microscope.abc.Camera.

Definition at line 51 of file picam.py.

51 def __init__(self, *args, **kwargs):
52 super(PiCamera, self).__init__(**kwargs)
53 # example parameter to allow setting.
54 # self.add_setting('_error_percent', 'int',
55 # lambda: self._error_percent,
56 # self._set_error_percent,
57 # lambda: (0, 100))
58 self._acquiring = False
59 self._exposure_time = 0.1
60 self._triggered = False
61 self.camera = None
62 # Region of interest.
63 self.roi = ROI(None, None, None, None)
64 # Cycle time
65 self.exposure_time = 0.001 # in seconds
66 self.cycle_time = self.exposure_time
67 # initialise in soft trigger mode
68 self._trigger_type = TriggerType.SOFTWARE
69 # setup hardware triggerline
70 GPIO.setmode(GPIO.BCM)
71 # GPIO trigger line is an input
72 GPIO.setup(GPIO_Trigger, GPIO.IN)
73 # GPIO control over camera LED is an output
74 GPIO.setup(GPIO_CAMLED, GPIO.OUT)
75 # add trigger to settings
76 trg_source_names = [x.name for x in TrgSourceMap]
77 # set up queue to store images as they are acquired
78 self._queue = queue.Queue()
79 self._awb_modes = picamera.PiCamera.AWB_MODES
80 self._iso_modes = [0, 100, 200, 320, 400, 500, 640, 800]
81
82 def _trigger_source_setter(index: int) -> None:
83 trigger_type = TrgSourceMap[trg_source_names[index]].value
84 self.set_trigger(trigger_type, self.trigger_mode)
85
86 self.add_setting(
87 "trig source",
88 "enum",
89 lambda: TrgSourceMap(self._trigger_type).name,
90 _trigger_source_setter,
91 trg_source_names,
92 )
93 self.add_setting(
94 "AWB",
95 "enum",
96 lambda: self._awb_modes[self.get_awb_mode()],
97 lambda awb: self.set_awb_mode(awb),
98 values=(list(self._awb_modes.keys())),
99 )
100
101 self.add_setting(
102 "ISO",
103 "enum",
104 lambda: self._iso_modes.index(self.camera.iso),
105 lambda iso: self.set_iso_mode(iso),
106 values=(self._iso_modes),
107 )
108
109 # self.add_setting(
110 # "pixel size",
111 # "float",
112 # lambda: self._pixel_size,
113 # lambda pxsz: setattr(self, "_pixel_size", pxsz),
114 # # technically should be: (nextafter(0.0, inf), nextafter(inf, 0.0))
115 # values=(0.0, float("inf")),
116 # )
117 self.initialize()
118

Member Function Documentation

◆ _do_disable()

microscope.cameras.picam.PiCamera._do_disable ( self)
protected
Do any device-specific work on disable.

Subclasses should override this method rather than modify
`disable`.

Reimplemented from microscope.abc.Device.

Definition at line 168 of file picam.py.

168 def _do_disable(self):
169 self.abort()
170

◆ _do_enable()

microscope.cameras.picam.PiCamera._do_enable ( self)
protected
Do any device specific work on enable.

Subclasses should override this method, rather than modify
`enable`.

Reimplemented from microscope.abc.Device.

Definition at line 175 of file picam.py.

175 def _do_enable(self):
176 _logger.info("Preparing for acquisition.")
177 if self._acquiring:
178 self.abort()
179 # actually start camera
180 if not self.camera:
181 self.initialize()
182 self._acquiring = True
183 _logger.info("Acquisition enabled.")
184 return True
185

◆ _do_shutdown()

microscope.cameras.picam.PiCamera._do_shutdown ( self)
protected
Private method - actual shutdown of the device.

Users should be calling :meth:`shutdown` and not this method.
Concrete implementations should implement this method instead
of `shutdown`.

Reimplemented from microscope.abc.Device.

Definition at line 171 of file picam.py.

171 def _do_shutdown(self):
172 self._do_disable()
173 self.camera.close()
174

◆ _do_trigger()

microscope.cameras.picam.PiCamera._do_trigger ( self)
protected
Actual trigger of the device.

Classes implementing this interface should implement this
method instead of `trigger`.

Reimplemented from microscope.abc.TriggerTargetMixin.

Definition at line 260 of file picam.py.

260 def _do_trigger(self):
261 self.soft_trigger()
262

◆ _fetch_data()

microscope.cameras.picam.PiCamera._fetch_data ( self)
protected
Poll for data and return it, with minimal processing.

If the device uses buffering in software, this function should
copy the data from the buffer, release or recycle the buffer,
then return a reference to the copy.  Otherwise, if the SDK
returns a data object that will not be written to again, this
function can just return a reference to the object.  If no
data is available, return `None`.

Reimplemented from microscope.abc.DataDevice.

Definition at line 136 of file picam.py.

136 def _fetch_data(self):
137 if self._queue.qsize() is not 0:
138 data = self._queue.get()
139 _logger.info("Sending image")
140 return data
141 else:
142 return None
143

◆ _get_binning()

microscope.cameras.picam.PiCamera._get_binning ( self)
protected
Return the current binning.

Reimplemented from microscope.abc.Camera.

Definition at line 227 of file picam.py.

227 def _get_binning(self):
228 return Binning(1, 1)
229

◆ _get_roi()

microscope.cameras.picam.PiCamera._get_roi ( self)
protected
Return the current ROI (left, top, width, height).

Reimplemented from microscope.abc.Camera.

Definition at line 220 of file picam.py.

220 def _get_roi(self):
221 """Return the current ROI (left, top, width, height)."""
222 return self.roi
223

◆ _get_sensor_shape()

microscope.cameras.picam.PiCamera._get_sensor_shape ( self)
protected
Return a tuple of `(width, height)` indicating shape in pixels.

Reimplemented from microscope.abc.Camera.

Definition at line 252 of file picam.py.

252 def _get_sensor_shape(self):
253 if self.camversion == "ov5647": # picam version 1
254 self.camera.resolution = (2592, 1944)
255 # faqll back to defualt if not set above.
256 res = self.camera.resolution
257 self._set_roi(0, 0, res[0], res[1])
258 return res
259

◆ _set_binning()

microscope.cameras.picam.PiCamera._set_binning ( self,
binning,
v_bin )
protected
Set binning along both axes.  Return `True` if successful.

Reimplemented from microscope.abc.Camera.

Definition at line 224 of file picam.py.

224 def _set_binning(self, h_bin, v_bin):
225 return True
226

◆ _set_roi()

microscope.cameras.picam.PiCamera._set_roi ( self,
left,
top,
width,
height )
protected
Set the ROI to (left, tip, width, height).

Reimplemented from microscope.abc.Camera.

Definition at line 231 of file picam.py.

231 def _set_roi(self, left, top, width, height):
232 """Set the ROI to (left, tip, width, height)."""
233 self.roi = ROI(left, top, width, height)
234

◆ abort()

microscope.cameras.picam.PiCamera.abort ( self)
Stop acquisition as soon as possible.

Reimplemented from microscope.abc.DataDevice.

Definition at line 186 of file picam.py.

186 def abort(self):
187 _logger.info("Disabling acquisition.")
188 if self._acquiring:
189 self._acquiring = False
190

◆ get_awb_mode()

microscope.cameras.picam.PiCamera.get_awb_mode ( self)

Definition at line 119 of file picam.py.

119 def get_awb_mode(self):
120 return self.camera.awb_mode
121

◆ get_cycle_time()

microscope.cameras.picam.PiCamera.get_cycle_time ( self)
Return the cycle time in seconds.

Reimplemented from microscope.abc.Camera.

Definition at line 247 of file picam.py.

247 def get_cycle_time(self):
248 # fudge to make it work initially
249 # exposure times are in us, so multiple by 1E-6 to get seconds.
250 return self.camera.exposure_speed * 1.0e-6 + 0.1
251

◆ get_exposure_time()

microscope.cameras.picam.PiCamera.get_exposure_time ( self)
Return the current exposure time in seconds.

Reimplemented from microscope.abc.Camera.

Definition at line 243 of file picam.py.

243 def get_exposure_time(self):
244 # exposure times are in us, so multiple by 1E-6 to get seconds.
245 return self.camera.exposure_speed * 1.0e-6
246

◆ HW_trigger()

microscope.cameras.picam.PiCamera.HW_trigger ( self,
channel )
Function called by GPIO interupt, needs to trigger image capture

Definition at line 130 of file picam.py.

130 def HW_trigger(self, channel):
131 """Function called by GPIO interupt, needs to trigger image capture"""
132 with picamera.array.PiYUVArray(self.camera) as output:
133 self.camera.capture(output, format="yuv", use_video_port=False)
134 self._queue.put(output.array[:, :, 0])
135

◆ initialize()

microscope.cameras.picam.PiCamera.initialize ( self)
Initialise the Pi Camera camera.
Open the connection, connect properties and populate settings dict.

Reimplemented from microscope.abc.Device.

Definition at line 144 of file picam.py.

144 def initialize(self):
145 """Initialise the Pi Camera camera.
146 Open the connection, connect properties and populate settings dict.
147 """
148 if not self.camera:
149 try:
150 # initialise camera in still image mode.
151 self.camera = picamera.PiCamera(sensor_mode=2)
152 except:
153 raise Exception("Problem opening camera.")
154 _logger.info("Initializing camera.")
155 self.camversion = self.camera.revision
156 _logger.info("cam version " + self.camversion)
157
158 # create img buffer to hold images.
159 # disable camera LED by default
160 self.setLED(False)
161 self.set_awb_mode(0) # set auto white balance to off
162 self._get_sensor_shape()
163

◆ make_safe()

microscope.cameras.picam.PiCamera.make_safe ( self)

Definition at line 164 of file picam.py.

164 def make_safe(self):
165 if self._acquiring:
166 self.abort()
167

◆ set_awb_mode()

microscope.cameras.picam.PiCamera.set_awb_mode ( self,
val )

Definition at line 122 of file picam.py.

122 def set_awb_mode(self, val):
123 for key, value in self._awb_modes.items():
124 if value == val:
125 self.camera.awb_mode = key
126

◆ set_exposure_time()

microscope.cameras.picam.PiCamera.set_exposure_time ( self,
value )
Set the exposure time on the device in seconds.

Reimplemented from microscope.abc.Camera.

Definition at line 239 of file picam.py.

239 def set_exposure_time(self, value):
240 # exposure times are set in us.
241 self.camera.shutter_speed = int(value * 1.0e6)
242

◆ set_iso_mode()

microscope.cameras.picam.PiCamera.set_iso_mode ( self,
val )

Definition at line 127 of file picam.py.

127 def set_iso_mode(self, val):
128 self.camera.iso = self._iso_modes[val]
129

◆ set_trigger()

None microscope.cameras.picam.PiCamera.set_trigger ( self,
TriggerType ttype,
TriggerMode tmode )
Set device for a specific trigger.

Reimplemented from microscope.abc.TriggerTargetMixin.

Definition at line 191 of file picam.py.

193 ) -> None:
194 if ttype == self._trigger_type:
195 return
196 elif ttype == TriggerType.SOFTWARE:
197 GPIO.remove_event_detect(GPIO_Trigger)
198 self._trigger_type = TriggerType.SOFTWARE
199 elif ttype == TriggerType.RISING_EDGE:
200 GPIO.add_event_detect(
201 GPIO_Trigger,
202 GPIO.RISING,
203 callback=self.HW_trigger,
204 bouncetime=10,
205 )
206 self._trigger_type = TriggerType.RISING_EDGE
207

◆ setLED()

microscope.cameras.picam.PiCamera.setLED ( self,
state = False )

Definition at line 236 of file picam.py.

236 def setLED(self, state=False):
237 GPIO.output(GPIO_CAMLED, state)
238

◆ soft_trigger()

microscope.cameras.picam.PiCamera.soft_trigger ( self)

Definition at line 263 of file picam.py.

263 def soft_trigger(self):
264 _logger.info(
265 "Trigger received; self._acquiring is %s." % self._acquiring
266 )
267 if self._acquiring:
268 with picamera.array.PiYUVArray(self.camera) as output:
269 self.camera.capture(output, format="yuv", use_video_port=False)
270 self._queue.put(output.array[:, :, 0])
271
272
273# ongoing implemetation notes
274
275# should be able to use rotation and hflip to set specific output image
276# rotations
277
278# roi's can be set with the zoom function, default is (0,0,1,1) meaning all the data.
279
280# Need to setup a buffer for harware triggered data aquisition so we can
281# call the acquisition and then download the data at our leasure

◆ trigger_mode()

TriggerMode microscope.cameras.picam.PiCamera.trigger_mode ( self)

Reimplemented from microscope.abc.TriggerTargetMixin.

Definition at line 209 of file picam.py.

209 def trigger_mode(self) -> TriggerMode:
210 # if self._trigger_type==devices.TRIGGER_BEFORE:
211 return TriggerMode.ONCE
212

◆ trigger_type()

TriggerType microscope.cameras.picam.PiCamera.trigger_type ( self)

Reimplemented from microscope.abc.TriggerTargetMixin.

Definition at line 217 of file picam.py.

217 def trigger_type(self) -> TriggerType:
218 return self._trigger_type
219

Member Data Documentation

◆ _acquiring

bool microscope.cameras.picam.PiCamera._acquiring = False
protected

Definition at line 58 of file picam.py.

◆ _awb_modes

microscope.cameras.picam.PiCamera._awb_modes = picamera.PiCamera.AWB_MODES
protected

Definition at line 79 of file picam.py.

◆ _exposure_time

float microscope.cameras.picam.PiCamera._exposure_time = 0.1
protected

Definition at line 59 of file picam.py.

◆ _iso_modes

list microscope.cameras.picam.PiCamera._iso_modes = [0, 100, 200, 320, 400, 500, 640, 800]
protected

Definition at line 80 of file picam.py.

◆ _queue

microscope.cameras.picam.PiCamera._queue = queue.Queue()
protected

Definition at line 78 of file picam.py.

◆ _trigger_type

microscope.cameras.picam.PiCamera._trigger_type = TriggerType.SOFTWARE
protected

Definition at line 68 of file picam.py.

◆ _triggered

bool microscope.cameras.picam.PiCamera._triggered = False
protected

Definition at line 60 of file picam.py.

◆ camera

microscope.cameras.picam.PiCamera.camera = None

Definition at line 61 of file picam.py.

◆ camversion [1/2]

str microscope.cameras.picam.PiCamera.camversion = self.camera.revision

Definition at line 155 of file picam.py.

◆ camversion [2/2]

tuple microscope.cameras.picam.PiCamera.camversion = (2592, 1944)

Definition at line 253 of file picam.py.

◆ cycle_time

float microscope.cameras.picam.PiCamera.cycle_time = self.exposure_time

Definition at line 66 of file picam.py.

◆ exposure_time

float microscope.cameras.picam.PiCamera.exposure_time = 0.001

Definition at line 65 of file picam.py.

◆ roi

microscope.cameras.picam.PiCamera.roi = ROI(None, None, None, None)

Definition at line 63 of file picam.py.

◆ trigger_mode

microscope.cameras.picam.PiCamera.trigger_mode

Definition at line 84 of file picam.py.


The documentation for this class was generated from the following file: