How to Detect Humans Using Thermal Cameras

May 28, 2024 | Educational

Thermal imaging has become a vital component in numerous applications, especially in human detection scenarios. This blog post will guide you through the entire process of utilizing a model that detects humans in thermal images, leveraging the power of Python and the Ultralytics library. You will learn how to set up your environment, run inference, and even train your own model.

Use Case

This model has been specifically fine-tuned for detecting humans using thermal images, accommodating both pseudo-color and grayscale formats. Although initially optimized for human detection, it can also be adapted to recognize other objects with further fine-tuning.

Setting Up Your Environment

Before we can dive into the code, we need to install some essential dependencies. Ensure that you have Python installed on your machine, then run the following command in your terminal:

$ python -m pip install ultralytics supervision huggingface_hub

Running Inference

Below is a code snippet that allows you to run inference on thermal images for human detection. Think of this code as your detection assistant – it analyzes and identifies humans hidden within a sea of thermal heat.

# Import libraries
from huggingface_hub import hf_hub_download
from ultralytics import YOLO
from supervision import Detections
import cv2

# Download model
model_path = hf_hub_download(
    repo_id = "pitangent-ds/YOLOv8-human-detection-thermal",
    filename = "model.pt"
)

# Load model
model = YOLO(model_path)

# Method for inference
def inference(image_path):
    cv_image = cv2.imread(image_path, cv2.IMREAD_ANYCOLOR)
    model_output = model(cv_image, conf=0.6, verbose=False)
    detections = Detections.from_ultralytics(model_output[0])
    return detections

Understanding the Code

Imagine you have a friend who is an expert detective. You want to find out if there are any hidden figures (humans) among a lot of thermal heat (your thermal image). Here’s how the code plays out:

  • Import Libraries: Just like your detective needs tools to work effectively, this code imports libraries that provide functionalities for downloading models, performing detections, and processing images.
  • Download Model: Think of this as your detective studying a case file to become equipped with the necessary information about the suspects (the trained model in this case).
  • Load Model: After acquiring the case file (model), your friend is now ready to assess the situation (load the model).
  • Method for Inference: This is the detective’s process of analyzing a room. The method takes an image path, reads it, applies the model to detect humans, and relays the detection information (detections).

Training a Custom Model

If you wish to fine-tune the model on a custom dataset, here are the steps you’ll follow:

# Load model
from ultralytics import YOLO
import torch

model = YOLO('yolov8n.pt')

# Hyperparameters
hyperparams = {
    'batch': 32,
    'epochs': 30,
    'imgsz': [640, 480],
    'optimizer': 'AdamW',
    'cos_lr': True,
    'lr0': 3e-5,
    'warmup_epochs': 10
}

# Start training
model.train(
    device='cuda' if torch.cuda.is_available() else 'cpu',
    data='data.yaml',
    **hyperparams
)

Troubleshooting Ideas

If you encounter issues along the way, here are some troubleshooting steps:

  • Make sure all dependencies are correctly installed. Verify the installation of Python and utilize the command given above.
  • If the model fails to download, check your internet connection and ensure the repository ID is correct.
  • For errors related to the image path, confirm that the image exists at the specified location.
  • If you notice slow performance during training, consider reducing the batch size or image size.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With this guide at your fingertips, you can efficiently set up your environment and start detecting humans using thermal images. Remember that you can always modify and adapt the model for your specific needs, giving you the flexibility to venture into various object detection applications.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox