-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from luxonis/feature/thermal_image_parsing
Feature: Add Thermal Image Parser
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import depthai as dai | ||
import numpy as np | ||
|
||
|
||
def create_thermal_message(thermal_image: np.array) -> dai.ImgFrame: | ||
"""Creates a thermal image message in the form of an ImgFrame using the provided | ||
thermal image array. | ||
Args: | ||
thermal_image (np.array): A NumPy array representing the thermal image with shape (CHW or HWC). | ||
Returns: | ||
dai.ImgFrame: An ImgFrame object containing the thermal information. | ||
""" | ||
|
||
if not isinstance(thermal_image, np.ndarray): | ||
raise ValueError(f"Expected numpy array, got {type(thermal_image)}.") | ||
if len(thermal_image.shape) != 3: | ||
raise ValueError(f"Expected 3D input, got {len(thermal_image.shape)}D input.") | ||
|
||
if thermal_image.shape[0] == 1: | ||
thermal_image = thermal_image[0, :, :] # CHW to HW | ||
elif thermal_image.shape[2] == 1: | ||
thermal_image = thermal_image[:, :, 0] # HWC to HW | ||
else: | ||
raise ValueError( | ||
"Unexpected image shape. Expected CHW or HWC, got", thermal_image.shape | ||
) | ||
|
||
thermal_image = thermal_image.astype(np.uint16) | ||
|
||
imgFrame = dai.ImgFrame() | ||
imgFrame.setFrame(thermal_image) | ||
imgFrame.setWidth(thermal_image.shape[1]) | ||
imgFrame.setHeight(thermal_image.shape[0]) | ||
imgFrame.setType(dai.ImgFrame.Type.RAW16) | ||
|
||
return imgFrame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import depthai as dai | ||
|
||
from ..messages.creators import create_thermal_message | ||
|
||
|
||
class ThermalImageParser(dai.node.ThreadedHostNode): | ||
def __init__(self): | ||
dai.node.ThreadedHostNode.__init__(self) | ||
self.input = dai.Node.Input(self) | ||
self.out = dai.Node.Output(self) | ||
|
||
def run(self): | ||
"""Postprocessing logic for a model with thermal image output (e.g. UGSR-FA). | ||
Returns: | ||
dai.ImgFrame: uint16, HW thermal image. | ||
""" | ||
|
||
while self.isRunning(): | ||
try: | ||
output: dai.NNData = self.input.get() | ||
except dai.MessageQueue.QueueException: | ||
break # Pipeline was stopped | ||
|
||
output_layer_names = output.getAllLayerNames() | ||
if len(output_layer_names) != 1: | ||
raise ValueError( | ||
f"Expected 1 output layer, got {len(output_layer_names)}." | ||
) | ||
output = output.getTensor(output_layer_names[0]) | ||
|
||
thermal_map = output[0] | ||
|
||
thermal_message = create_thermal_message(thermal_map=thermal_map) | ||
self.out.send(thermal_message) |