3
3
Adapted from simple_pyspin: https://github.com/klecknerlab/simple_pyspin
4
4
"""
5
5
6
+ from __future__ import annotations
7
+
8
+ import collections
9
+ import logging
6
10
import time
7
- from collections import deque
8
- from typing import Tuple , Union
11
+ from types import TracebackType
12
+ from typing import Any
9
13
10
14
import numpy as np
11
15
import PySpin
63
67
"SerialReceiveQueueClear" : "Clear Serial Port" ,
64
68
}
65
69
66
- _DEBUG = __name__ == "__main__"
67
-
68
- _SYSTEM = None
70
+ _SYSTEM : PySpin .System | None = None
69
71
70
72
71
73
def list_cameras () -> PySpin .CameraList :
@@ -178,7 +180,7 @@ class FlirCamera:
178
180
PySpin .intfICommand : "command" ,
179
181
}
180
182
181
- def __init__ (self , src : Union [ int , str ] = 0 , lock : bool = False ):
183
+ def __init__ (self , src : int | str = 0 , lock : bool = False ):
182
184
"""
183
185
Parameters
184
186
----------
@@ -196,9 +198,7 @@ def __init__(self, src: Union[int, str] = 0, lock: bool = False):
196
198
super ().__setattr__ ("lock" , lock )
197
199
198
200
cam_list = list_cameras ()
199
-
200
- if _DEBUG :
201
- print (f"Found { cam_list .GetSize ()} FLIR camera(s)" )
201
+ logging .debug ("Found %s FLIR camera(s)" , cam_list .GetSize ())
202
202
203
203
self ._src_type = type (src )
204
204
self ._src = src
@@ -216,10 +216,10 @@ def __init__(self, src: Union[int, str] = 0, lock: bool = False):
216
216
217
217
# Other attributes which may be accessed later
218
218
self ._running = False
219
- self ._frame_times = deque ()
219
+ self ._frame_times : collections . deque [ float ] = collections . deque ()
220
220
self ._incomplete_image_count = 0
221
221
222
- def __getattr__ (self , attr : str ) -> object :
222
+ def __getattr__ (self , attr : str ) -> Any :
223
223
# Add this in so @property decorator works as expected
224
224
if attr in self .__dict__ :
225
225
return self .__dict__ [attr ]
@@ -265,11 +265,16 @@ def __setattr__(self, attr: str, val: object) -> None:
265
265
else :
266
266
super ().__setattr__ (attr , val )
267
267
268
- def __enter__ (self ) -> " FlirCamera" :
268
+ def __enter__ (self ) -> FlirCamera :
269
269
self .init ()
270
270
return self
271
271
272
- def __exit__ (self , type , value , traceback ) -> None :
272
+ def __exit__ (
273
+ self ,
274
+ exc_type : type [BaseException ] | None ,
275
+ exc : BaseException | None ,
276
+ traceback : TracebackType | None ,
277
+ ) -> None :
273
278
self .close ()
274
279
275
280
def __del__ (self ) -> None :
@@ -388,7 +393,7 @@ def height(self) -> int:
388
393
return int (height )
389
394
390
395
@property
391
- def shape (self ) -> Tuple [int , int ]:
396
+ def shape (self ) -> tuple [int , int ]:
392
397
"""Get the camera array dimensions (Height x Width)"""
393
398
if not self .initialized :
394
399
self .init ()
@@ -405,10 +410,11 @@ def init(self) -> None:
405
410
Initializes the camera. Automatically called if the camera is opened
406
411
using a 'with' clause.
407
412
"""
408
-
409
413
self .cam .Init ()
410
414
411
- for node in self .cam .GetNodeMap ().GetNodes ():
415
+ node_map : PySpin .INodeMap = self .cam .GetNodeMap ()
416
+ nodes : list [PySpin .INode ] = node_map .GetNodes ()
417
+ for node in nodes :
412
418
pit = node .GetPrincipalInterfaceType ()
413
419
name = node .GetName ()
414
420
self .camera_node_types [name ] = self ._attr_type_names .get (pit , pit )
@@ -456,7 +462,7 @@ def stop(self) -> None:
456
462
457
463
if self .running :
458
464
self .cam .EndAcquisition ()
459
- self ._frame_times = deque ()
465
+ self ._frame_times . clear ()
460
466
self ._incomplete_image_count = 0
461
467
self ._running = False
462
468
@@ -474,11 +480,10 @@ def close(self) -> None:
474
480
pass
475
481
476
482
# Reset attributes
477
- self .camera_attributes = {}
478
- self .camera_methods = {}
479
- self .camera_node_types = {}
483
+ self .camera_attributes : dict [ str , Any ] = {}
484
+ self .camera_methods : dict [ str , PySpin . CCommandPtr ] = {}
485
+ self .camera_node_types : dict [ str , str ] = {}
480
486
self ._initialized = False
481
- # self.system.ReleaseInstance()
482
487
483
488
def get_image (self , wait : bool = True ) -> PySpin .ImagePtr :
484
489
"""
@@ -513,7 +518,7 @@ def get_image(self, wait: bool = True) -> PySpin.ImagePtr:
513
518
514
519
def get_array (
515
520
self , wait : bool = True , get_chunk : bool = False , complete_frames_only : bool = False
516
- ) -> Union [ np .ndarray , Tuple [np .ndarray , PySpin .PySpin .ChunkData ] ]:
521
+ ) -> np .ndarray | tuple [np .ndarray , PySpin .PySpin .ChunkData ]:
517
522
"""
518
523
Get an image from the camera, and convert it to a numpy array.
519
524
@@ -547,12 +552,14 @@ def get_array(
547
552
if len (self ._frame_times ) > 3600 :
548
553
self ._frame_times .popleft ()
549
554
555
+ arr : np .ndarray = img .GetNDArray ()
550
556
if get_chunk :
551
- return img .GetNDArray (), img .GetChunkData ()
557
+ chunk : PySpin .PySpin .ChunkData = img .GetChunkData ()
558
+ return (arr , chunk )
552
559
else :
553
- return img . GetNDArray ()
560
+ return arr
554
561
555
- def get_info (self , name : str ) -> dict :
562
+ def get_info (self , name : str ) -> dict [ str , Any ] :
556
563
"""
557
564
Get information on a camera node (attribute or method).
558
565
@@ -570,7 +577,7 @@ def get_info(self, name: str) -> dict:
570
577
- "unit": the unit of the value (as a string).
571
578
- "min" and "max": the min/max value.
572
579
"""
573
- info = {"name" : name }
580
+ info : dict [ str , Any ] = {"name" : name }
574
581
575
582
if name in self .camera_attributes :
576
583
node = self .camera_attributes [name ]
@@ -716,25 +723,3 @@ def document(self, verbose: bool = True) -> str:
716
723
lines .append ("" )
717
724
718
725
return "\n " .join (lines )
719
-
720
-
721
- if __name__ == "__main__" :
722
-
723
- def test ():
724
- with FlirCamera () as cam :
725
- print (cam .document (verbose = False ))
726
- print (cam )
727
- cam .start ()
728
- while True :
729
- try :
730
- global image
731
- image = cam .get_array ()
732
- # print(cam.incomplete_image_count, cam.real_fps)
733
- print (cam .DeviceTemperature )
734
-
735
- except KeyboardInterrupt :
736
- break
737
-
738
- # test()
739
-
740
- print (list_cameras ())
0 commit comments