fir — thermal sensor driver (fir == far infrared)¶
The fir module is used for controlling the thermal sensors.
Example usage:
import sensor, fir
# Setup camera.
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames()
fir.init()
# Show image.
while(True):
img = sensor.snapshot()
ta, ir, to_min, to_max = fir.read_ir()
fir.draw_ir(image, ir)
print("====================")
print("Ambient temperature: %0.2f" % ta)
print("Min temperature seen: %0.2f" % to_min)
print("Max temperature seen: %0.2f" % to_max)
Functions¶
- fir.init(type=-1, refresh: int | None = None, resolution: int | None = None) None¶
Initializes an attached thermopile shield using I/O pins P4 and P5 (and P0, P1, P2, P3 for
fir.FIR_LEPTON)typeindicates the type of thermopile shield:fir.FIR_NONE: 0 pixels.fir.FIR_SHIELD: 16x4 pixels.fir.FIR_MLX90621: 16x4 pixels.fir.FIR_MLX90640: 32x24 pixels.fir.FIR_MLX90641: 16x12 pixels.fir.FIR_AMG8833: 8x8 pixels.fir.FIR_LEPTON: 80x60 pixels (FLIR Lepton 1.x/2.x) or 160x120 pixels (FLIR Lepton 3.x)
By default type is
-1which will causefir.init()to automatically scan and initialize an attached thermal sensor based on the I2C address. Note thatfir.FIR_MLX90640andfir.FIR_MLX90641have the same I2C address so you must passfir.FIR_MLX90641to type to initialize it specifically.fir.FIR_LEPTONon the OpenMV Cam Pure Thermal this uses internal I/O pins and does not use P0-P5.refreshis the thermopile sensor power-of-2 refresh rate in Hz:fir.FIR_NONE: N/Afir.FIR_SHIELD: Defaults to 64 Hz. Can be 1 Hz, 2 Hz, 4 Hz, 8 Hz, 16 Hz, 32 Hz, 64 Hz, 128 Hz, 256 Hz, or 512 Hz. Note that a higher refresh rate lowers the accuracy and vice-versa.fir.FIR_MLX90621: Defaults to 64 Hz. Can be 1 Hz, 2 Hz, 4 Hz, 8 Hz, 16 Hz, 32 Hz, 64 Hz, 128 Hz, 256 Hz, or 512 Hz. Note that a higher refresh rate lowers the accuracy and vice-versa.fir.FIR_MLX90640: Defaults to 32 Hz. Can be 1 Hz, 2 Hz, 4 Hz, 8 Hz, 16 Hz, 32 Hz, or 64 Hz. Note that a higher refresh rate lowers the accuracy and vice-versa.fir.FIR_MLX90641: Defaults to 32 Hz. Can be 1 Hz, 2 Hz, 4 Hz, 8 Hz, 16 Hz, 32 Hz, or 64 Hz. Note that a higher refresh rate lowers the accuracy and vice-versa.fir.FIR_AMG8833: 10 Hzfir.FIR_LEPTON: 9 Hz (really 8.7 Hz).
resolutionis the thermopile sensor measurement resolution:fir.FIR_NONE: N/Afir.FIR_SHIELD: Defaults to 18-bits. Can be 15-bits, 16-bits, 17-bits, or 18-bits. Note that a higher resolution lowers the maximum temperature range and vice-versa.fir.FIR_MLX90621: Defaults to 18-bits. Can be 15-bits, 16-bits, 17-bits, or 18-bits. Note that a higher resolution lowers the maximum temperature range and vice-versa.fir.FIR_MLX90640: Defaults to 19-bits. Can be 16-bits, 17-bits, 18-bits, or 19-bits. Note that a higher resolution lowers the maximum temperature range and vice-versa.fir.FIR_MLX90641: Defaults to 19-bits. Can be 16-bits, 17-bits, 18-bits, or 19-bits. Note that a higher resolution lowers the maximum temperature range and vice-versa.fir.FIR_AMG8833: 12-bits.fir.FIR_LEPTON: 14-bits.
For the
fir.FIR_SHIELDandfir.FIR_MLX90621:15-bits -> Max of ~950C.
16-bits -> Max of ~750C.
17-bits -> Max of ~600C.
18-bits -> Max of ~450C.
For the
fir.FIR_MLX90640andfir.FIR_MLX90641:16-bits -> Max of ~750C.
17-bits -> Max of ~600C.
18-bits -> Max of ~450C.
19-bits -> Max of ~300C.
For the
fir.FIR_AMG8833:Max of ~80C.
For the
fir.FIR_LEPTON:Max of ~140C (can be up to 400C-450C in low-gain mode).
Note
For
fir.FIR_LEPTONmode this driver implements triple buffering to receive the FLIR Lepton image. This uses 28.125 KB of RAM for the FLIR Lepton 1.x/2.x and 112.5 KB of RAM for the FLIR Lepton 3.x. Triple buffering ensures that reading an image withfir.read_ir()andfir.snapshot()never block. For all other sensors the I2C bus is accessed to read the image.
- fir.width() int¶
Returns the width (horizontal resolution) of the thermal sensor in-use:
fir.FIR_NONE: 0 pixels.fir.FIR_SHIELD: 16 pixels.fir.FIR_MLX90621: 16 pixels.fir.FIR_MLX90640: 32 pixels.fir.FIR_MLX90641: 16 pixels.fir.FIR_AMG8833: 8 pixels.fir.FIR_LEPTON: 80 pixels (FLIR Lepton 1.x/2.x) or 160 pixels (FLIR Lepton 3.x).
- fir.height() int¶
Returns the height (vertical resolution) of the thermal sensor in-use:
fir.FIR_NONE: 0 pixels.fir.FIR_SHIELD: 4 pixels.fir.FIR_MLX90621: 4 pixels.fir.FIR_MLX90640: 24 pixels.fir.FIR_MLX90641: 12 pixels.fir.FIR_AMG8833: 8 pixels.fir.FIR_LEPTON: 60 pixels (FLIR Lepton 1.x/2.x) or 120 pixels (FLIR Lepton 3.x).
- fir.refresh() int¶
Returns the current refresh rate set during
fir.init()call.
- fir.resolution() int¶
Returns the current resolution set during the
fir.init()call.
- fir.radiometric() bool¶
Returns if the thermal sensor reports accurate temperature readings (True or False). If False this means that the thermal sensor reports relative temperature readings based on its ambient temperature which may not be very accurate.
- fir.register_vsync_cb(cb) None¶
For the
fir.FIR_LEPTONmode only on the OpenMV Cam Pure Thermal.Registers callback
cbto be executed (in interrupt context) whenever the FLIR Lepton generates a new frame (but, before the frame is received).This nomially triggers at 9 Hz.
cbtakes no arguments.
- fir.register_frame_cb(cb) None¶
For the
fir.FIR_LEPTONmode only on the OpenMV Cam Pure Thermal.Registers callback
cbto be executed (in interrupt context) whenever the FLIR Lepton generates a new frame and the frame is ready to be read viafir.read_ir()orfir.snapshot().This nomially triggers at 9 Hz.
cbtakes no arguments.Use this to get an interrupt to schedule reading a frame later with
micropython.schedule().
- fir.get_frame_available() bool¶
Returns True if a frame is available to read by calling
fir.read_ir()orfir.snapshot().
- fir.trigger_ffc(timeout=-1) None¶
For the
fir.FIR_LEPTONmode only.Triggers the Flat-Field-Correction process on your FLIR Lepton which calibrates the thermal image. This process happens automatically with the sensor. However, you may call this function to force the process to happen.
timeoutif not -1 then how many milliseconds to wait for FFC to complete.
- fir.read_ta() float¶
Returns the ambient temperature (i.e. sensor temperature).
Example:
ta = fir.read_ta()
The value returned is a float that represents the temperature in celsius.
- fir.read_ir(hmirror=False, vflip=False, transpose=False, timeout=-1)¶
Returns a tuple containing the ambient temperature (i.e. sensor temperature), the temperature list (width * height), the minimum temperature seen, and the maximum temperature seen.
hmirrorif set to True horizontally mirrors theirarray.vflipif set to True vertically flips theirarray.transposeif set to True transposes theirarray.timeoutif not -1 then how many milliseconds to wait for the new frame.If you want to rotate an image by multiples of 90 degrees pass the following:
* vflip=False, hmirror=False, transpose=False -> 0 degree rotation * vflip=True, hmirror=False, transpose=True -> 90 degree rotation * vflip=True, hmirror=True, transpose=False -> 180 degree rotation * vflip=False, hmirror=True, transpose=True -> 270 degree rotation
Example:
ta, ir, to_min, to_max = fir.read_ir()
The values returned are floats that represent the temperature in celsius.
Note
iris a (width * height) list of floats (4-bytes each).
- fir.draw_ir(image: image.Image, ir, x: int | None = None, y: int | None = None, x_scale=1.0, y_scale=1.0, roi: Tuple[int, int, int, int] | None = None, rgb_channel=-1, alpha=128, color_palette=image.PALETTE_RAINBOW, alpha_palette=-1, hint=0, scale=Optional[Tuple[float, float]]) None¶
Draws an
irarray onimagewhose top-left corner starts at location x, y. This method automatically handles rendering the image passed into the correct pixel format for the destination image while also handling clipping seamlessly.x_scalecontrols how much the displayed image is scaled by in the x direction (float). If this value is negative the image will be flipped horizontally. Note that ify_scaleis not specified then it will matchx_scaleto maintain the aspect ratio.y_scalecontrols how much the displayed image is scaled by in the y direction (float). If this value is negative the image will be flipped vertically. Note that ifx_scaleis not specified then it will matchx_scaleto maintain the aspect ratio.roiis the region-of-interest rectangle tuple (x, y, w, h) of the source image to draw. This allows you to extract just the pixels in the ROI to scale and draw on the destination image.rgb_channelis the RGB channel (0=R, G=1, B=2) to extract from an RGB565 image (if passed) and to render onto the destination image. For example, if you passrgb_channel=1this will extract the green channel of the source RGB565 image and draw that in grayscale on the destination image.alphacontrols how much of the source image to blend into the destination image. A value of 255 draws an opaque source image while a value lower than 255 produces a blend between the source and destination image. 0 results in no modification to the destination image.color_paletteif not-1can be an a color palette enum or a 256 pixel in total RGB565 image to use as a color lookup table on the grayscale value of whatever the source image is. This is applied afterrgb_channelextraction if used.alpha_paletteif not-1can be a 256 pixel in total GRAYSCALE image to use as a alpha palette which modulates thealphavalue of the source image being drawn at a pixel pixel level allowing you to precisely control the alpha value of pixels based on their grayscale value. A pixel value of 255 in the alpha lookup table is opaque which anything less than 255 becomes more transparent until 0. This is applied afterrgb_channelextraction if used.hintcan be a logical OR of the flags:image.AREA: Use area scaling when downscaling versus the default of nearest neighbor.image.BILINEAR: Use bilinear scaling versus the default of nearest neighbor scaling.image.BICUBIC: Use bicubic scaling versus the default of nearest neighbor scaling.image.CENTER: Center the image being drawn on the display. This is applied after scaling.image.HMIRROR: Horizontally mirror the image.image.VFLIP: Vertically flip the image.image.TRANSPOSE: Transpose the image (swap x/y).image.EXTRACT_RGB_CHANNEL_FIRST: Do rgb_channel extraction before scaling.image.APPLY_COLOR_PALETTE_FIRST: Apply color palette before scaling.image.SCALE_ASPECT_KEEP: Scale the image being drawn to fit inside the display.image.SCALE_ASPECT_EXPAND: Scale the image being drawn to fill the display (results in cropping)image.SCALE_ASPECT_IGNORE: Scale the image being drawn to fill the display (results in stretching).image.ROTATE_90: Rotate the image by 90 degrees (this is just VFLIP | TRANSPOSE).image.ROTATE_180: Rotate the image by 180 degrees (this is just HMIRROR | VFLIP).image.ROTATE_270: Rotate the image by 270 degrees (this is just HMIRROR | TRANSPOSE).
scaleis a two value tuple which controls the min and max temperature (in celsius) to scale theirimage. By default it’s equal to the imageirmin andirmax.If x/y are not specified the image will be centered in the field of view. If x_scale/y_scale or x_size/y_size are not specified the
irarray will be scaled to fit on theimage.Note
To handle a transposed
irarrayread_irremembers if it was called withtransposedTrue. This is then passed todraw_irinternally.
- fir.snapshot(hmirror=False, vflip=False, transpose=False, x_scale=1.0, y_scale=1.0, roi: Tuple[int, int, int, int] | None = None, rgb_channel=-1, alpha=128, color_palette=image.PALETTE_RAINBOW, alpha_palette=None, hint=0, scale: Tuple[float, float] | None = None, pixformat=image.RGB565, copy_to_fb=False, timeout=-1) image.Image¶
Works like
sensor.snapshot()and returns animageobject that is eitherimage.GRAYSCALE(grayscale) orimage.RGB565(color). Ifcopy_to_fbis False then the new image is allocated on the MicroPython heap. However, the MicroPython heap is limited and may not have space to store the new image if exhausted. Instead, setcopy_to_fbto True to set the frame buffer to the new image making this function work just likesensor.snapshot().hmirrorif set to True horizontally mirrors the new image.vflipif set to True vertically flips the new image.transposeif set to True transposes the new image.If you want to rotate an image by multiples of 90 degrees pass the following:
* vflip=False, hmirror=False, transpose=False -> 0 degree rotation * vflip=True, hmirror=False, transpose=True -> 90 degree rotation * vflip=True, hmirror=True, transpose=False -> 180 degree rotation * vflip=False, hmirror=True, transpose=True -> 270 degree rotation
x_scalecontrols how much the displayed image is scaled by in the x direction (float). If this value is negative the image will be flipped horizontally. Note that ify_scaleis not specified then it will matchx_scaleto maintain the aspect ratio.y_scalecontrols how much the displayed image is scaled by in the y direction (float). If this value is negative the image will be flipped vertically. Note that ifx_scaleis not specified then it will matchx_scaleto maintain the aspect ratio.roiis the region-of-interest rectangle tuple (x, y, w, h) of the source image to draw. This allows you to extract just the pixels in the ROI to scale and draw on the destination image.rgb_channelis the RGB channel (0=R, G=1, B=2) to extract from an RGB565 image (if passed) and to render onto the destination image. For example, if you passrgb_channel=1this will extract the green channel of the source RGB565 image and draw that in grayscale on the destination image.alphacontrols how much of the source image to blend into the destination image. A value of 255 draws an opaque source image while a value lower than 255 produces a blend between the source and destination image. 0 results in no modification to the destination image.color_paletteif not-1can be an a color palette enum or a 256 pixel in total RGB565 image to use as a color lookup table on the grayscale value of whatever the source image is. This is applied afterrgb_channelextraction if used.alpha_paletteif not-1can be a 256 pixel in total GRAYSCALE image to use as a alpha palette which modulates thealphavalue of the source image being drawn at a pixel pixel level allowing you to precisely control the alpha value of pixels based on their grayscale value. A pixel value of 255 in the alpha lookup table is opaque which anything less than 255 becomes more transparent until 0. This is applied afterrgb_channelextraction if used.hintcan be a logical OR of the flags:image.AREA: Use area scaling when downscaling versus the default of nearest neighbor.image.BILINEAR: Use bilinear scaling versus the default of nearest neighbor scaling.image.BICUBIC: Use bicubic scaling versus the default of nearest neighbor scaling.image.CENTER: Center the image being drawn on the display. This is applied after scaling.image.HMIRROR: Horizontally mirror the image.image.VFLIP: Vertically flip the image.image.TRANSPOSE: Transpose the image (swap x/y).image.EXTRACT_RGB_CHANNEL_FIRST: Do rgb_channel extraction before scaling.image.APPLY_COLOR_PALETTE_FIRST: Apply color palette before scaling.image.SCALE_ASPECT_KEEP: Scale the image being drawn to fit inside the display.image.SCALE_ASPECT_EXPAND: Scale the image being drawn to fill the display (results in cropping)image.SCALE_ASPECT_IGNORE: Scale the image being drawn to fill the display (results in stretching).image.ROTATE_90: Rotate the image by 90 degrees (this is just VFLIP | TRANSPOSE).image.ROTATE_180: Rotate the image by 180 degrees (this is just HMIRROR | VFLIP).image.ROTATE_270: Rotate the image by 270 degrees (this is just HMIRROR | TRANSPOSE).
scaleis a two value tuple which controls the min and max temperature (in celsius) to scale theirimage. By default it’s equal to the imageirmin andirmax.pixformatif specified controls the final image pixel format.timeoutif not -1 then how many milliseconds to wait for the new frame.Returns an image object.