A lightweight Python library for thermal sensing and analytics on ARM Linux platforms
Project description
๐ฅ PyThermal
A lightweight Python library for thermal sensing and analytics on ARM Linux platforms. It provides unified APIs for recording, visualization, and intelligent analysis of thermal data from Hikvision or compatible infrared sensors.
๐ก๏ธ Features
-
Raw Frame Recording Capture and store radiometric thermal frames (e.g., 96ร96, 16-bit raw) with timestamps.
-
Colored Visualization Generate pseudo-color thermal images (e.g., 240ร240 RGB) with adjustable color maps.
-
Live Stream Interface Stream frames in real time, perform temperature conversion and display dynamically.
-
Shared Memory Architecture Efficient zero-copy access to thermal data via shared memory interface.
-
Thermal Analytics (Future Development) Built-in lightweight models for:
- Human body detection
- Skeleton (pose) detection
- ROI temperature statistics (max / min / avg)
-
Offline Replay and Analysis (Future Development) Replay recorded sessions for algorithm benchmarking or dataset generation.
๐ Installation
Prerequisites
Before installing the Python package, you need to set up the thermal camera permissions and native runtime:
cd pythermal-devkit
./setup.sh
This script will:
- Install required system dependencies (cross-compiler, FFmpeg libraries)
- Set up USB device permissions for the thermal camera
- Compile the native thermal recorder (
pythermal-recorder)
After running setup.sh, you may need to:
- Disconnect and reconnect your thermal camera
- Log out and log back in (or restart) for permissions to take effect
Install Python Package
Install directly on an ARM Linux device (e.g., Jetson, OrangePi, Raspberry Pi):
uv pip install pythermal
Or from source:
git clone https://github.com/<your-org>/pythermal.git
cd pythermal-devkit
uv pip install .
โ Bundled Native Runtime The package ships with the native thermal recorder (
pythermal-recorder) and required shared libraries (.sofiles) underpythermal/_native/armLinux/.
๐ง Quick Start
1. Initialize Thermal Device
The ThermalDevice class manages the thermal camera initialization by starting pythermal-recorder in a separate process and providing access to thermal data via shared memory.
from pythermal import ThermalDevice
# Create and start thermal device
device = ThermalDevice()
device.start() # Starts pythermal-recorder subprocess and initializes shared memory
# Access shared memory for reading thermal data
shm = device.get_shared_memory()
# When done, stop the device
device.stop()
Or use as a context manager:
with ThermalDevice() as device:
shm = device.get_shared_memory()
# Use shared memory...
# Device automatically stops on exit
2. Live View
Display real-time thermal imaging feed:
from pythermal import ThermalLiveView
viewer = ThermalLiveView()
viewer.run() # Opens OpenCV window with live thermal feed
Or with a shared device:
from pythermal import ThermalDevice, ThermalLiveView
device = ThermalDevice()
device.start()
viewer = ThermalLiveView(device=device)
viewer.run() # Uses the shared device
device.stop()
Controls:
- Press
qto quit - Press
tto toggle between YUYV view and temperature view - Move mouse over image to see temperature at cursor position
3. Record Thermal Frames
from pythermal import ThermalRecorder
import time
rec = ThermalRecorder(output_dir="recordings", color=True)
rec.start() # Starts device and begins recording
rec.record_loop(duration=10) # Record for 10 seconds
rec.stop() # Stop recording
This records both:
- Raw temperature frames (
96ร96, uint16) - YUYV visual frames (
240ร240) - Colored RGB frames (
240ร240, uint8 RGB) ifcolor=True
4. Access Thermal Data Directly
from pythermal import ThermalDevice, ThermalSharedMemory
device = ThermalDevice()
device.start()
shm = device.get_shared_memory()
# Check for new frame
if shm.has_new_frame():
# Get metadata
metadata = shm.get_metadata()
print(f"Frame {metadata.seq}: {metadata.min_temp:.1f}ยฐC - {metadata.max_temp:.1f}ยฐC")
# Get YUYV frame
yuyv_frame = shm.get_yuyv_frame()
# Get temperature array (96x96, uint16)
temp_array = shm.get_temperature_array()
# Get temperature map in Celsius (96x96, float32)
temp_celsius = shm.get_temperature_map_celsius()
# Mark frame as read
shm.mark_frame_read()
device.stop()
๐งฉ Command Line Interface
| Command | Description |
|---|---|
pythermal-preview |
Live preview with temperature overlay |
Example:
pythermal-preview
This will start the thermal device and display a live view window.
๐งฐ API Overview
| Class | Purpose |
|---|---|
ThermalDevice |
Manages thermal camera initialization via subprocess and shared memory access |
ThermalSharedMemory |
Reads thermal data from shared memory (YUYV frames, temperature arrays, metadata) |
ThermalRecorder |
Records raw and colored frames to files |
ThermalLiveView |
Displays live thermal imaging feed with OpenCV |
FrameMetadata |
Named tuple containing frame metadata (seq, flag, dimensions, temperatures) |
๐งช Requirements
- Python โฅ 3.9
- ARM Linux environment (Jetson / OrangePi / Raspberry Pi)
- NumPy, OpenCV (auto-installed via pip)
- Thermal camera connected via USB
- Proper USB permissions (set up via
setup.sh)
โ๏ธ Architecture
Native Runtime
The library uses a native binary (pythermal-recorder) that runs as a separate process and writes thermal data to shared memory (/dev/shm/yuyv240_shm). The Python library communicates with this process via shared memory for efficient zero-copy data access.
Bundled Files
The package includes the following native files under pythermal/_native/armLinux/:
pythermal/_native/armLinux/
โโโ pythermal-recorder # Main thermal recorder executable
โโโ libHCUSBSDK.so # Hikvision USB SDK library
โโโ libhpr.so # Hikvision processing library
โโโ libusb-1.0.so* # USB library dependencies
โโโ libuvc.so # UVC library
Shared Memory Layout
The shared memory (/dev/shm/yuyv240_shm) contains:
Offset Size Content
0 FRAME_SZ YUYV frame data (240ร240ร2 bytes)
FRAME_SZ TEMP_DATA_SIZE Temperature array (96ร96ร2 bytes, uint16)
FRAME_SZ+TEMP ... Metadata:
- seq (4 bytes, uint32)
- flag (4 bytes, uint32, 1=new, 0=consumed)
- width (4 bytes, uint32)
- height (4 bytes, uint32)
- min_temp (4 bytes, float)
- max_temp (4 bytes, float)
- avg_temp (4 bytes, float)
- reserved (4 bytes)
Process Management
The ThermalDevice class:
- Starts
pythermal-recorderas a subprocess - Waits for shared memory to become available
- Provides access to thermal data via
ThermalSharedMemory - Automatically cleans up the process on exit
Troubleshooting
-
FileNotFoundError: pythermal-recorder not foundMake sure you've runsetup.shto compile the native binaries, and that the package was installed correctly. -
PermissionError: pythermal-recorder is not executableRunchmod +xon the executable, or reinstall the package. -
TimeoutError: Shared memory did not become available- Check that the thermal camera is connected via USB
- Verify USB permissions are set up correctly (run
setup.sh) - Try disconnecting and reconnecting the camera
- Check that no other process is using the thermal camera
-
RuntimeError: Thermal recorder process exited unexpectedlyCheck the process output for error messages. Common issues:- Camera not detected
- Missing USB permissions
- Missing shared libraries (check
LD_LIBRARY_PATH)
๐ฆ Directory Structure
pythermal-devkit/
โโโ pythermal/
โ โโโ __init__.py
โ โโโ device.py # ThermalDevice class (manages subprocess)
โ โโโ thermal_shared_memory.py # Shared memory reader
โ โโโ record.py # ThermalRecorder class
โ โโโ live_view.py # ThermalLiveView class
โ โโโ _native/
โ โโโ armLinux/
โ โโโ pythermal-recorder
โ โโโ *.so # Native libraries
โโโ setup.sh # Setup script for permissions and compilation
โโโ setup-thermal-permissions.sh
โโโ setup.py # Python package setup
โโโ README.md
๐ License
This library is released under the Apache 2.0 License for research and non-commercial use.
Only the compiled native library (.so) is shipped; no vendor source or headers are distributed.
๐ก Acknowledgements
Developed by AIoT Lab, CUHK
๐ง Contact
To access the thermal devices, please contact thermal@thingx-tech.com.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pythermal-0.1.1.tar.gz.
File metadata
- Download URL: pythermal-0.1.1.tar.gz
- Upload date:
- Size: 1.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f253d0173077ce22d5d5886e77bf1d071215da3351f17ccebfe4dcfe5b2554cb
|
|
| MD5 |
4f5617ed7a70f830792a791299859b4e
|
|
| BLAKE2b-256 |
e2722f3ff561efcc7ce94e44e10a0fbb8897f152c95efb9a8e96d48b27baa695
|
File details
Details for the file pythermal-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pythermal-0.1.1-py3-none-any.whl
- Upload date:
- Size: 1.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b50fb446b156d4ebed2fd8e480be6a8bc01ecaedddfcc8dfcb73e4ce118b4ca8
|
|
| MD5 |
1c2c3c805d0b12b8ff5c32ae73c75a3e
|
|
| BLAKE2b-256 |
c750de409337f3b89bb9183bc4f39d6d408b3faa2b9c639f3e45418535ce83fe
|