# Thermal Camera

This example demonstrates using a thermal camera to display raw temperature data with a color map and real-time RGB thermal video.
It includes mouse interaction to show the temperature at the cursor, combining data processing, visualization, and user
interaction for thermal imaging applications.

## Demo

## Setup

Please run the [install script](https://github.com/luxonis/depthai-python/blob/main/examples/install_requirements.py) to download
all required dependencies. Please note that this script must be ran from git context, so you have to download the
[depthai-python](https://github.com/luxonis/depthai-python) repository first and then run the script

```bash
git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py
```

For additional information, please follow the [installation guide](https://docs.luxonis.com/software/depthai/manual-install.md).

## Source code

#### Python

```python
#!/usr/bin/env python3

import depthai as dai
import cv2
import numpy as np

mouseX, mouseY = 0, 0

def onMouse(event, x, y, *args):
    global mouseX, mouseY
    mouseX = x
    mouseY = y

device = dai.Device()
pipeline = dai.Pipeline()

# Thermal camera
thermalCam = pipeline.create(dai.node.Camera)
thermalCam.setFps(25) # Limit to 25 to match what the sensor can do, capped even if left at default, but warns.
width, height = -1, -1
thermalFound = False
for features in device.getConnectedCameraFeatures():
    if dai.CameraSensorType.THERMAL in features.supportedTypes:
        thermalFound = True
        thermalCam.setBoardSocket(features.socket)
        width, height = features.width, features.height
        break
if not thermalFound:
    raise RuntimeError("No thermal camera found!")
thermalCam.setPreviewSize(width, height)

# Output raw: FP16 temperature data (degrees Celsius)
xoutRaw = pipeline.create(dai.node.XLinkOut)
xoutRaw.setStreamName("thermal_raw")
thermalCam.raw.link(xoutRaw.input)

# Output preview,video, isp: RGB or NV12 or YUV420 thermal image.
xoutImage = pipeline.create(dai.node.XLinkOut)
xoutImage.setStreamName("image")
thermalCam.preview.link(xoutImage.input)
device.startPipeline(pipeline)

qRaw = device.getOutputQueue("thermal_raw", 2, False)
qImage = device.getOutputQueue("image", 2, False)

RAW_WINDOW_NAME = "temperature"
IMAGE_WINDOW_NAME = "image"
# Scale 4x and position one next to another
cv2.namedWindow(RAW_WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.namedWindow(IMAGE_WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.moveWindow(RAW_WINDOW_NAME, 0, 0)
cv2.resizeWindow(RAW_WINDOW_NAME, width * 4, height * 4)
cv2.moveWindow(IMAGE_WINDOW_NAME, width * 4, 0)
cv2.resizeWindow(IMAGE_WINDOW_NAME, width * 4, height * 4)
cv2.setMouseCallback(RAW_WINDOW_NAME, onMouse)
cv2.setMouseCallback(IMAGE_WINDOW_NAME, onMouse)

while True:
    inRaw = qRaw.get()
    inImg = qImage.get()

    # Retrieve one point of fp16 data
    frame = inRaw.getCvFrame().astype(np.float32)
    colormappedFrame = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    colormappedFrame = cv2.applyColorMap(colormappedFrame, cv2.COLORMAP_MAGMA)
    if (
        mouseX < 0
        or mouseY < 0
        or mouseX >= frame.shape[1]
        or mouseY >= frame.shape[0]
    ):
        mouseX = max(0, min(mouseX, frame.shape[1] - 1))
        mouseY = max(0, min(mouseY, frame.shape[0] - 1))
    textColor = (255, 255, 255)
    # Draw crosshair
    cv2.line(
        colormappedFrame,
        (mouseX - 10, mouseY),
        (mouseX + 10, mouseY),
        textColor,
        1,
    )
    cv2.line(
        colormappedFrame,
        (mouseX, mouseY - 10),
        (mouseX, mouseY + 10),
        textColor,
        1,
    )
    # Draw deg C
    text = f"{frame[mouseY, mouseX]:.2f} deg C"
    putTextLeft = mouseX > colormappedFrame.shape[1] / 2
    cv2.putText(
        colormappedFrame,
        text,
        (mouseX - 100 if putTextLeft else mouseX + 10, mouseY - 10),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.5,
        textColor,
        1,
    )
    cv2.imshow(RAW_WINDOW_NAME, colormappedFrame)

    cv2.imshow(IMAGE_WINDOW_NAME, inImg.getCvFrame())

    if cv2.waitKey(1) == ord("q"):
        break
```

#### C++

```cpp
/*
    Thermal camera example.
    Streams temperature and thermal image from thermal sensor and displays them.
*/
#include <algorithm>
#include <cstdio>

#include "depthai/depthai.hpp"

volatile int mouseX, mouseY = 0;
void mouseCallback(int event, int x, int y, int flags, void* userdata) {
    mouseX = x;
    mouseY = y;
}

const cv::Scalar WHITE(255, 255, 255);

int main() {
    dai::Device d;
    dai::Pipeline pipeline;
    auto thermal = pipeline.create<dai::node::Camera>();
    // Find the sensor width, height.
    int width, height;
    bool thermal_found = false;
    for(auto& features : d.getConnectedCameraFeatures()) {
        if(std::find_if(features.supportedTypes.begin(),
                        features.supportedTypes.end(),
                        [](const dai::CameraSensorType& type) { return type == dai::CameraSensorType::THERMAL; })
           != features.supportedTypes.end()) {
            thermal->setBoardSocket(features.socket);  // Thermal will always be on CAM_E
            width = features.width;
            height = features.height;
            thermal_found = true;
        }
    }
    if(!thermal_found) {
        throw std::runtime_error("Thermal camera not found!");
    }
    thermal->setPreviewSize(width, height);
    auto xlink = pipeline.create<dai::node::XLinkOut>();
    auto xlinkRaw = pipeline.create<dai::node::XLinkOut>();
    // Output preview,video, isp: RGB or NV12 or YUV420 thermal image.
    thermal->preview.link(xlink->input);
    // Output raw: FP16 temperature data (degrees Celsius)
    thermal->raw.link(xlinkRaw->input);

    xlinkRaw->setStreamName("thermal_raw");
    xlink->setStreamName("thermal");
    d.startPipeline(pipeline);
    auto q = d.getOutputQueue("thermal", 2, false);
    auto qRaw = d.getOutputQueue("thermal_raw", 2, false);

    const char* tempWindow = "temperature";
    const char* imageWindow = "image";
    cv::namedWindow(tempWindow, cv::WINDOW_NORMAL);
    cv::setMouseCallback(tempWindow, mouseCallback);
    cv::namedWindow(imageWindow, cv::WINDOW_NORMAL);
    cv::setMouseCallback(imageWindow, mouseCallback);
    // Scale 4x and position one next to another
    cv::moveWindow(tempWindow, 0, 0);
    cv::resizeWindow(tempWindow, width * 4, height * 4);
    cv::moveWindow(imageWindow, width * 4, 0);
    cv::resizeWindow(imageWindow, width * 4, height * 4);
    while(true) {
        auto temp = qRaw->tryGet<dai::ImgFrame>();
        if(temp) {
            auto frame = temp->getCvFrame();
            // Retrieve one point of fp16 data
            cv::Mat frameFp32(temp->getHeight(), temp->getWidth(), CV_32F);
            frame.convertTo(frameFp32, CV_32F);
            cv::Mat normalized;
            cv::normalize(frameFp32, normalized, 0, 255, cv::NORM_MINMAX, CV_8UC1);
            cv::Mat colormapped(temp->getHeight(), temp->getWidth(), CV_8UC3);
            cv::applyColorMap(normalized, colormapped, cv::COLORMAP_MAGMA);
            if(mouseX < 0 || mouseY < 0 || mouseX >= colormapped.cols || mouseY >= colormapped.rows) {
                mouseX = std::max(0, std::min(static_cast<int>(mouseX), colormapped.cols - 1));
                mouseY = std::max(0, std::min(static_cast<int>(mouseY), colormapped.rows - 1));
            }
            double min, max;
            cv::minMaxLoc(frameFp32, &min, &max);
            auto textColor = WHITE;
            // Draw crosshair
            cv::line(colormapped, cv::Point(mouseX - 10, mouseY), cv::Point(mouseX + 10, mouseY), textColor, 1);
            cv::line(colormapped, cv::Point(mouseX, mouseY - 10), cv::Point(mouseX, mouseY + 10), textColor, 1);
            // Draw deg C
            char text[32];
            snprintf(text, sizeof(text), "%.1f deg C", frameFp32.at<float>(mouseY, mouseX));
            bool putTextLeft = mouseX > colormapped.cols / 2;
            cv::putText(colormapped, text, cv::Point(putTextLeft ? mouseX - 100 : mouseX + 10, mouseY - 10), cv::FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1);
            cv::imshow(tempWindow, colormapped);
        }
        auto image = q->tryGet<dai::ImgFrame>();
        if(image) {
            cv::imshow(imageWindow, image->getCvFrame());
        }
        int key = cv::waitKey(1);
        if(key == 'q') {
            break;
        }
    }

    return 0;
}
```

## Pipeline

### examples/thermal_camera.pipeline.json

```json
{
  "pipeline": {
    "connections": [
      {
        "node1Id": 0,
        "node1Output": "raw",
        "node1OutputGroup": "",
        "node2Id": 1,
        "node2Input": "in",
        "node2InputGroup": ""
      },
      {
        "node1Id": 0,
        "node1Output": "preview",
        "node1OutputGroup": "",
        "node2Id": 2,
        "node2Input": "in",
        "node2InputGroup": ""
      }
    ],
    "globalProperties": {
      "calibData": null,
      "cameraTuningBlobSize": null,
      "cameraTuningBlobUri": "",
      "leonCssFrequencyHz": 700000000.0,
      "leonMssFrequencyHz": 700000000.0,
      "pipelineName": null,
      "pipelineVersion": null,
      "sippBufferSize": 18432,
      "sippDmaBufferSize": 16384,
      "xlinkChunkSize": -1
    },
    "nodes": [
      [
        0,
        {
          "id": 0,
          "ioInfo": [
            [
              [
                "",
                "inputConfig"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 1,
                "name": "inputConfig",
                "queueSize": 8,
                "type": 3,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "raw"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 6,
                "name": "raw",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "still"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 7,
                "name": "still",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "inputControl"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 2,
                "name": "inputControl",
                "queueSize": 8,
                "type": 3,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "video"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 3,
                "name": "video",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "isp"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 4,
                "name": "isp",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "preview"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 5,
                "name": "preview",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "frameEvent"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 8,
                "name": "frameEvent",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ]
          ],
          "name": "Camera",
          "properties": {
            "boardSocket": 4,
            "calibAlpha": null,
            "cameraName": "",
            "colorOrder": 0,
            "fp16": false,
            "fps": 25.0,
            "imageOrientation": -1,
            "initialControl": {
              "aeLockMode": false,
              "aeMaxExposureTimeUs": 0,
              "aeRegion": {
                "height": 0,
                "priority": 0,
                "width": 0,
                "x": 0,
                "y": 0
              },
              "afRegion": {
                "height": 0,
                "priority": 0,
                "width": 0,
                "x": 0,
                "y": 0
              },
              "antiBandingMode": 0,
              "autoFocusMode": 3,
              "awbLockMode": false,
              "awbMode": 0,
              "brightness": 0,
              "captureIntent": 0,
              "chromaDenoise": 0,
              "cmdMask": 0,
              "contrast": 0,
              "controlMode": 0,
              "effectMode": 0,
              "expCompensation": 0,
              "expManual": {
                "exposureTimeUs": 0,
                "frameDurationUs": 0,
                "sensitivityIso": 0
              },
              "frameSyncMode": 0,
              "lensPosAutoInfinity": 0,
              "lensPosAutoMacro": 0,
              "lensPosition": 0,
              "lensPositionRaw": 0.0,
              "lowPowerNumFramesBurst": 0,
              "lowPowerNumFramesDiscard": 0,
              "lumaDenoise": 0,
              "miscControls": [],
              "saturation": 0,
              "sceneMode": 0,
              "sharpness": 0,
              "strobeConfig": {
                "activeLevel": 0,
                "enable": 0,
                "gpioNumber": 0
              },
              "strobeTimings": {
                "durationUs": 0,
                "exposureBeginOffsetUs": 0,
                "exposureEndOffsetUs": 0
              },
              "wbColorTemp": 0
            },
            "interleaved": true,
            "isp3aFps": 0,
            "ispScale": {
              "horizDenominator": 0,
              "horizNumerator": 0,
              "vertDenominator": 0,
              "vertNumerator": 0
            },
            "numFramesPoolIsp": 3,
            "numFramesPoolPreview": 4,
            "numFramesPoolRaw": 3,
            "numFramesPoolStill": 4,
            "numFramesPoolVideo": 4,
            "previewHeight": 192,
            "previewKeepAspectRatio": true,
            "previewWidth": 256,
            "rawPacked": null,
            "resolutionHeight": -1,
            "resolutionWidth": -1,
            "sensorCropX": -1.0,
            "sensorCropY": -1.0,
            "sensorType": -1,
            "stillHeight": -1,
            "stillWidth": -1,
            "videoHeight": -1,
            "videoWidth": -1,
            "warpMeshHeight": 0,
            "warpMeshSource": -1,
            "warpMeshStepHeight": 32,
            "warpMeshStepWidth": 32,
            "warpMeshUri": "",
            "warpMeshWidth": 0
          }
        }
      ],
      [
        1,
        {
          "id": 1,
          "ioInfo": [
            [
              [
                "",
                "in"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 9,
                "name": "in",
                "queueSize": 8,
                "type": 3,
                "waitForMessage": true
              }
            ]
          ],
          "name": "XLinkOut",
          "properties": {
            "maxFpsLimit": -1.0,
            "metadataOnly": false,
            "streamName": "thermal_raw"
          }
        }
      ],
      [
        2,
        {
          "id": 2,
          "ioInfo": [
            [
              [
                "",
                "in"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 10,
                "name": "in",
                "queueSize": 8,
                "type": 3,
                "waitForMessage": true
              }
            ]
          ],
          "name": "XLinkOut",
          "properties": {
            "maxFpsLimit": -1.0,
            "metadataOnly": false,
            "streamName": "image"
          }
        }
      ]
    ]
  }
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
