Compare commits
64 Commits
57a2c3197e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de88f4a856 | ||
|
|
9c9eb81437 | ||
|
|
9d6254b478 | ||
|
|
9e8cc5d575 | ||
|
|
ea5b3c6860 | ||
|
|
36efc0da0a | ||
|
|
cddd5317c3 | ||
|
|
b1a7baae19 | ||
|
|
c993f74511 | ||
|
|
39551f3794 | ||
|
|
bfaa0eca2f | ||
|
|
373b17d8a5 | ||
|
|
ca742aa204 | ||
|
|
831322e44a | ||
|
|
24b65a8ee5 | ||
|
|
005292adfc | ||
|
|
a512e710fa | ||
|
|
e43f8b0efb | ||
|
|
2ba4528c1d | ||
|
|
0af76ed532 | ||
|
|
2347158093 | ||
|
|
faccc3d20b | ||
|
|
4bdd3a8411 | ||
|
|
34931f9362 | ||
|
|
6bcc541c86 | ||
|
|
e74fa87103 | ||
|
|
0ff8d2b1fb | ||
|
|
a917f75ce0 | ||
|
|
49f9aa98ed | ||
|
|
5acd03828d | ||
|
|
bba0e3a093 | ||
|
|
fe440960f4 | ||
|
|
4f638cdd64 | ||
|
|
c29499d9b0 | ||
|
|
acf9b2c4c4 | ||
|
|
d661bf27ae | ||
|
|
679a87bf45 | ||
|
|
d9cb5986ee | ||
|
|
fccc2ba2e5 | ||
|
|
17c7d0e555 | ||
|
|
7d75ad7596 | ||
|
|
45ec502eca | ||
|
|
fa5a16a8ea | ||
|
|
82d11e868f | ||
|
|
85bda0fa27 | ||
|
|
0ec9d70cd6 | ||
|
|
1e59c59aca | ||
|
|
787bdfe5f7 | ||
|
|
d6c5058f2e | ||
|
|
3ea0c74e7f | ||
|
|
862a48131e | ||
|
|
6725a9af63 | ||
|
|
312637fa72 | ||
|
|
6af9b21be1 | ||
|
|
5f5834835c | ||
|
|
ade93550ad | ||
|
|
9b2f8d9377 | ||
|
|
44849f5b66 | ||
|
|
0554eaaafd | ||
|
|
056bb2f96c | ||
|
|
a612d4f7d7 | ||
|
|
ce6fa022f9 | ||
|
|
18c952f94d | ||
|
|
b7714ca957 |
1
.env.example
Normal file
1
.env.example
Normal file
@@ -0,0 +1 @@
|
||||
THERMALCAM_IFACE=enp1s0f0
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,6 @@
|
||||
/venv
|
||||
/frames
|
||||
**/*.png
|
||||
target
|
||||
.env
|
||||
MTAM1.3.0-20200525WAI-H*
|
||||
|
||||
1
.python-version
Normal file
1
.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.13
|
||||
5153
Cargo.lock
generated
Normal file
5153
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
31
Cargo.toml
Normal file
31
Cargo.toml
Normal file
@@ -0,0 +1,31 @@
|
||||
[package]
|
||||
name = "thermaldecoder"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.77"
|
||||
axum = "0.7.4"
|
||||
bracket-color = "0.8.7"
|
||||
clap = { version = "4.5.1", features = ["derive"] }
|
||||
crossbeam = "0.8.4"
|
||||
crossbeam-channel = "0.5.11"
|
||||
crossterm = { version = "0.27.0", features = ["event-stream"] }
|
||||
dotenv = "0.15.0"
|
||||
eframe = "0.26.2"
|
||||
egui = "0.26.2"
|
||||
futures = "0.3.30"
|
||||
futures-timer = "3.0.3"
|
||||
indicatif = "0.17.7"
|
||||
pcap = { version = "1.2.0", features = ["capture-stream"] }
|
||||
pcap-parser = { version = "0.14.1", features = ["data"] }
|
||||
png = "0.17.10"
|
||||
pyo3 = { version = "0.20.0", "features" = ["extension-module"] }
|
||||
reqwest = { version = "0.11.24", features = ["json"] }
|
||||
serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] }
|
||||
tokio = { version = "1.36.0", features = ["full"] }
|
||||
tracing-subscriber = "0.3.18"
|
||||
tui-textarea = "0.4.0"
|
||||
v4l = { version = "0.14.0", features = ["v4l2"], default-features = false }
|
||||
38
README.md
Normal file
38
README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
### Thermal decoder
|
||||
|
||||
https://telavivmakers.org/tamiwiki/projects/thermalcam
|
||||
|
||||
|
||||
### Starting the stream
|
||||
|
||||
#### Enable jumbo frames
|
||||
|
||||
```
|
||||
sudo ip link set eth0 mtu 9000
|
||||
```
|
||||
|
||||
#### Send start packet
|
||||
You need to send a special packet.
|
||||
|
||||
Sending it via sudo because of raw sockets:
|
||||
```bash
|
||||
sudo ./venv/bin/python ./replay.py
|
||||
```
|
||||
|
||||
To send it you need the capability to open sockets in raw mode, but that does not work well with scripts (see [1]
|
||||
|
||||
[1] setcap for executables, not helpful for python scripts:
|
||||
```
|
||||
setcap cap_net_raw,cap_net_admin=eip ./replay.py
|
||||
```
|
||||
|
||||
### Rust lib usage
|
||||
|
||||
# if you don't already have a virtualenv. Linux specific, adjust to your OS.
|
||||
```bash
|
||||
virtualenv venv
|
||||
. venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
(cd thermaldecoder; maturin develop -r)
|
||||
python test_rust.py
|
||||
```
|
||||
BIN
bootLoginPlay.pcapng
Normal file
BIN
bootLoginPlay.pcapng
Normal file
Binary file not shown.
128
cvview.py
128
cvview.py
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
import cv2
|
||||
import argparse
|
||||
import numpy as np
|
||||
from datetime import datetime
|
||||
|
||||
# Set up the argument parser
|
||||
parser = argparse.ArgumentParser(description="Visualize image files and display pixel values on hover.")
|
||||
@@ -11,44 +13,47 @@ args = parser.parse_args()
|
||||
img_path = args.path
|
||||
|
||||
|
||||
# Function to display the image and pixel values along with the frame index
|
||||
def show_pixel_values(image_path):
|
||||
def mouse_event(event, x, y, flags, param):
|
||||
if event == cv2.EVENT_MOUSEMOVE:
|
||||
pixel_value = img[y, x]
|
||||
text = f'Value: {pixel_value}, Location: ({x},{y})'
|
||||
img_text = img.copy()
|
||||
# Overlay the frame index
|
||||
frame_index = get_frame_index(image_path)
|
||||
cv2.putText(img_text, f'Frame: {frame_index}', (10, img_text.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
|
||||
cv2.putText(img_text, text, (50, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
|
||||
cv2.imshow('Image', img_text)
|
||||
def calibrate(x):
|
||||
x = x.astype(float)
|
||||
x /= 256.0
|
||||
#print('{}..{}'.format(x.max(), x.min()))
|
||||
ret = ((-1.665884e-08) * x**4.
|
||||
+ (1.347094e-05) * x**3.
|
||||
+ (-4.396264e-03) * x**2.
|
||||
+ (9.506939e-01) * x
|
||||
+ (-6.353247e+01))
|
||||
#print('{}..{}'.format(ret.max(), ret.min()))
|
||||
ret = np.where(ret > 0, ret, 0)
|
||||
#print('{}..{}'.format(ret.max(), ret.min()))
|
||||
ret = ret.astype('b')
|
||||
#print('{}..{}'.format(ret.max(), ret.min()))
|
||||
return ret
|
||||
|
||||
img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
|
||||
if img is None:
|
||||
print(f"Failed to load image at {image_path}. Check the file path and integrity.")
|
||||
return False
|
||||
cv2.namedWindow('Image')
|
||||
cv2.setMouseCallback('Image', mouse_event)
|
||||
cv2.imshow('Image', img)
|
||||
return True
|
||||
# Global variables for the last mouse position
|
||||
last_x, last_y = 0, 0
|
||||
img, calibrated_img = None, None
|
||||
|
||||
|
||||
# Function to get the frame index from the filename
|
||||
def get_frame_index(filename):
|
||||
return os.path.splitext(os.path.basename(filename))[0][-4:]
|
||||
return os.path.splitext(os.path.basename(filename))[0][-5:]
|
||||
|
||||
|
||||
# Function to modify the numeric part of the filename
|
||||
def modify_filename(filename, increment=True):
|
||||
def modify_filename(filename, frame_increment=1):
|
||||
directory, basename = os.path.split(filename)
|
||||
basename_no_ext, ext = os.path.splitext(basename)
|
||||
print(f"Modifying filename {basename_no_ext} in directory {directory}.")
|
||||
if len(basename_no_ext) < 4 or not basename_no_ext[-4:].isdigit():
|
||||
if len(basename_no_ext) < 5 or not basename_no_ext[-5:].isdigit():
|
||||
raise ValueError("Filename does not end with five digits.")
|
||||
num_part = basename_no_ext[-4:]
|
||||
num = int(num_part) + (1 if increment else -1)
|
||||
new_name = f"{basename_no_ext[:-4]}{num:04d}{ext}"
|
||||
|
||||
num_part = basename_no_ext[-5:]
|
||||
num = int(num_part) + frame_increment
|
||||
|
||||
# Handle rollover
|
||||
num = num % 100000 # Modulo 100000 for 5 digits
|
||||
|
||||
new_name = f"{basename_no_ext[:-5]}{num:05d}{ext}"
|
||||
new_path = os.path.join(directory, new_name)
|
||||
if not os.path.exists(new_path):
|
||||
print(f"No file found at {new_path}.")
|
||||
@@ -56,6 +61,49 @@ def modify_filename(filename, increment=True):
|
||||
return new_path
|
||||
|
||||
|
||||
# Function to display the image and pixel values along with the frame index
|
||||
def show_pixel_values(image_path):
|
||||
global img, calibrated_img, last_x, last_y
|
||||
|
||||
def mouse_event(event, x, y, flags, param):
|
||||
global last_x, last_y
|
||||
if event == cv2.EVENT_MOUSEMOVE:
|
||||
last_x, last_y = x, y
|
||||
update_display(x, y)
|
||||
|
||||
img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
|
||||
if img is None:
|
||||
print(f"Failed to load image at {image_path}. Check the file path and integrity.")
|
||||
return False
|
||||
|
||||
calibrated_img = calibrate(img) # Calibrate the image for display
|
||||
|
||||
cv2.namedWindow('Image')
|
||||
cv2.setMouseCallback('Image', mouse_event)
|
||||
update_display(last_x, last_y) # Initial display update
|
||||
return True
|
||||
|
||||
# Function to update the display with pixel values
|
||||
def update_display(x, y):
|
||||
global img, calibrated_img
|
||||
original_pixel_value = img[y, x]
|
||||
calibrated_pixel_value = calibrated_img[y, x]
|
||||
text_original = f'Original: {original_pixel_value}, Loc: ({x},{y})'
|
||||
text_calibrated = f'Calibrated: {calibrated_pixel_value}'
|
||||
img_text = img.copy()
|
||||
frame_index = get_frame_index(img_path)
|
||||
cv2.putText(img_text, f'Frame: {frame_index}', (10, img_text.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
|
||||
cv2.putText(img_text, text_original, (5, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
|
||||
cv2.putText(img_text, text_calibrated+"c", (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
|
||||
cv2.imshow('Image', img_text)
|
||||
return img_text # Return the image with text for saving
|
||||
|
||||
def save_frame(img_text):
|
||||
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
save_path = f"frame_{current_time}.png"
|
||||
cv2.imwrite(save_path, img_text)
|
||||
print(f"Frame saved as {save_path}")
|
||||
|
||||
# Ensure the provided path is a valid file
|
||||
if not os.path.isfile(img_path):
|
||||
print("The provided path is not a valid file.")
|
||||
@@ -70,13 +118,25 @@ while True:
|
||||
key = cv2.waitKey(0)
|
||||
if key == 27: # ESC key to exit
|
||||
break
|
||||
elif key == 91: # '[' key
|
||||
img_path = modify_filename(img_path, increment=False)
|
||||
elif key == 93: # ']' key
|
||||
img_path = modify_filename(img_path, increment=True)
|
||||
elif key in [91, 93, ord('{'), ord('}')]: # Keys for frame navigation
|
||||
if key == 91: # '[' key
|
||||
img_path = modify_filename(img_path, frame_increment=-1)
|
||||
elif key == 93: # ']' key
|
||||
img_path = modify_filename(img_path, frame_increment=1)
|
||||
elif key == ord('{'): # Shift + '['
|
||||
img_path = modify_filename(img_path, frame_increment=-50)
|
||||
elif key == ord('}'): # Shift + ']'
|
||||
img_path = modify_filename(img_path, frame_increment=50)
|
||||
|
||||
if not show_pixel_values(img_path):
|
||||
break # Exit if the new image cannot be loaded
|
||||
else:
|
||||
update_display(last_x, last_y) # Update display with last known mouse position
|
||||
|
||||
# Show the new image
|
||||
if not show_pixel_values(img_path):
|
||||
break # Exit the loop if the new image cannot be loaded
|
||||
elif key == ord('s'): # 's' key for saving
|
||||
# Update the display to get the latest overlay and save it
|
||||
img_text_with_overlays = update_display(last_x, last_y)
|
||||
save_frame(img_text_with_overlays)
|
||||
continue # Skip the frame reload if saving
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
cv2.destroyAllWindows()
|
||||
235
decode.py
235
decode.py
@@ -1,8 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import os
|
||||
import subprocess
|
||||
from io import BytesIO
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
from datetime import datetime
|
||||
import pandas as pd
|
||||
import pcapng
|
||||
from struct import unpack
|
||||
@@ -11,6 +15,7 @@ from PIL import Image
|
||||
|
||||
# Create the parser
|
||||
parser = argparse.ArgumentParser(description="Process a pcap file.")
|
||||
parser.add_argument("--live", action="store_true", help="Process images live")
|
||||
|
||||
# Add an argument for the pcap file, with a default value
|
||||
parser.add_argument('input_file', nargs='?', default='in.pcap', help='The pcap file to process')
|
||||
@@ -18,33 +23,40 @@ parser.add_argument('input_file', nargs='?', default='in.pcap', help='The pcap f
|
||||
# Parse the arguments
|
||||
args = parser.parse_args()
|
||||
|
||||
# Now use args.input_file as the file to process
|
||||
input_file = args.input_file
|
||||
basename = os.path.splitext(os.path.basename(input_file))[0]
|
||||
|
||||
# TODO - probably a better way to do this
|
||||
def live_capture_cb(cb):
|
||||
def outer(pkt):
|
||||
data = bytes(pkt)
|
||||
l = len(data)
|
||||
if l == 6972:
|
||||
cb(data)
|
||||
scapy.all.sniff(iface="enp1s0f0", filter='udp', prn=outer)
|
||||
|
||||
|
||||
# Read packets from a pcap file
|
||||
scanner = pcapng.scanner.FileScanner(open(input_file, "rb"))
|
||||
blocks = list(tqdm(scanner))
|
||||
|
||||
# Helper function to safely get an attribute from an object
|
||||
def tryget(obj, att):
|
||||
if hasattr(obj, att):
|
||||
return getattr(obj, att)
|
||||
return None
|
||||
def rightsize(it):
|
||||
for i, obj in enumerate(it):
|
||||
if isinstance(obj, bytes):
|
||||
l = len(obj)
|
||||
data = obj
|
||||
else:
|
||||
if not hasattr(obj, 'packet_len'):
|
||||
continue
|
||||
l = obj.packet_len
|
||||
data = obj.packet_data
|
||||
if l != 6972:
|
||||
continue
|
||||
yield data
|
||||
|
||||
|
||||
# Create a DataFrame with packet lengths
|
||||
df = pd.DataFrame(
|
||||
[{"index": i, "length": tryget(obj, "packet_len")} for i, obj in enumerate(blocks)]
|
||||
)
|
||||
def removestart(it):
|
||||
"Remove the UDP header from the packets"
|
||||
for x in it:
|
||||
yield removestart_inner(x)
|
||||
|
||||
|
||||
# Filter and extract data packets of a specific length
|
||||
data = [blocks[i] for i in df[df.length == 6972.0].index]
|
||||
|
||||
# Remove the UDP header from the packets
|
||||
raw = [d.packet_data[0x2A:] for d in data]
|
||||
def removestart_inner(x):
|
||||
return x[0x2A:]
|
||||
|
||||
|
||||
# Function to parse packet data
|
||||
@@ -59,65 +71,148 @@ def parse(data):
|
||||
return ret
|
||||
|
||||
|
||||
# Parse each packet and create a DataFrame
|
||||
df = pd.DataFrame([parse(d) for d in raw])
|
||||
df2 = df[[c for c in df.columns if c != "data"]]
|
||||
def parsed(it):
|
||||
for x in it:
|
||||
yield parse(x)
|
||||
|
||||
|
||||
class FrameCollector:
|
||||
def __init__(self):
|
||||
self.current = []
|
||||
|
||||
def handle(self, obj):
|
||||
ret = None
|
||||
if obj['part'] == 0:
|
||||
if len(self.current) > 0:
|
||||
ret = b"".join(self.current)
|
||||
self.current = []
|
||||
#otherdata = []
|
||||
self.current.append(obj["data"])
|
||||
return ret
|
||||
#otherdata.append(obj)
|
||||
|
||||
def last(self):
|
||||
if len(self.current) > 0:
|
||||
return b"".join(current)
|
||||
return None
|
||||
|
||||
|
||||
# Function to group data into frames
|
||||
def getframes(df):
|
||||
frames = []
|
||||
current = []
|
||||
for i, row in df.iterrows():
|
||||
if row.part == 0:
|
||||
if len(current) > 0:
|
||||
frames.append(current)
|
||||
current = []
|
||||
current.append(row["data"])
|
||||
if len(current) > 0:
|
||||
frames.append(current)
|
||||
return [b"".join(parts) for parts in frames]
|
||||
def frames(it):
|
||||
handler = FrameCollector()
|
||||
#otherdata = []
|
||||
for obj in it:
|
||||
ret = handler.handle(obj)
|
||||
if ret:
|
||||
yield ret
|
||||
last = handler.last()
|
||||
if last:
|
||||
yield last
|
||||
|
||||
|
||||
# Function to convert binary frame data into images
|
||||
def image16(frame, width, height, pixelformat=">H"):
|
||||
return [
|
||||
Image.fromarray(np.frombuffer(frame, dtype=pixelformat).reshape(width, height))
|
||||
for frame in frames
|
||||
if len(frame) == 2 * width * height
|
||||
WIDTH = 384
|
||||
HEIGHT = 288
|
||||
|
||||
|
||||
def bad_frame(frame, width=WIDTH, height=HEIGHT):
|
||||
return len(frame) != width * height * 2 # 16 bpp
|
||||
|
||||
|
||||
def skip_bad_frames(it, width=WIDTH, height=HEIGHT):
|
||||
for frame in it:
|
||||
if bad_frame(frame): # 16 bpp
|
||||
# Will be fixed when we stopped doing restarts
|
||||
#print(f'{len(frame)} != {width} * {height} * 2')
|
||||
continue
|
||||
yield frame
|
||||
|
||||
|
||||
def iterimages(it, width=WIDTH, height=HEIGHT, pixelformat=">H"):
|
||||
for frame in it:
|
||||
yield Image.fromarray(np.frombuffer(frame, dtype=pixelformat).reshape(width, height))
|
||||
|
||||
|
||||
def process_video():
|
||||
# Now use args.input_file as the file to process
|
||||
input_file = args.input_file
|
||||
basename = os.path.splitext(os.path.basename(input_file))[0]
|
||||
stream = open(input_file, 'rb')
|
||||
# Read packets from a pcap file
|
||||
scanner = pcapng.scanner.FileScanner(stream)
|
||||
blocks = tqdm(scanner)
|
||||
|
||||
# Get frames and convert them to images
|
||||
frames = skip_bad_frames(frames(parsed(removestart(rightsize(blocks)))))
|
||||
|
||||
# Create the directory for frames if not exists
|
||||
frame_dir = f"frames/{basename}"
|
||||
if not os.path.exists(frame_dir):
|
||||
os.makedirs(frame_dir)
|
||||
|
||||
# Save each image as a PNG file
|
||||
images = iterimages(it=frames)
|
||||
for i, img in enumerate(images):
|
||||
img.save(f'frames/{basename}/{basename}_{i:04}.png')
|
||||
ffmpeg_input = f"frames/{basename}/{basename}_%04d.png"
|
||||
command = [
|
||||
"ffmpeg",
|
||||
"-y", # Overwrite output file without asking
|
||||
"-hide_banner", # Hide banner
|
||||
"-loglevel", "info", # Log level
|
||||
"-f", "image2", # Input format
|
||||
"-framerate", "25", # Framerate
|
||||
"-i", ffmpeg_input, # Input file pattern
|
||||
"-vf", "transpose=1", # Video filter for transposing
|
||||
"-s", "384x288", # Size of one frame
|
||||
"-vcodec", "libopenh264", # Video codec
|
||||
"-pix_fmt", "yuv420p", # Pixel format: YUV 4:2:0
|
||||
"thermal.mp4", # Output file in MP4 container
|
||||
]
|
||||
|
||||
subprocess.run(command)
|
||||
print("to play: ffplay thermal.mp4")
|
||||
|
||||
# Get frames and convert them to images
|
||||
frames = getframes(df)
|
||||
images = image16(frames, 384, 288)
|
||||
|
||||
# Create the directory for frames if not exists
|
||||
frame_dir = f"frames/{basename}"
|
||||
if not os.path.exists(frame_dir):
|
||||
os.makedirs(frame_dir)
|
||||
if args.live:
|
||||
# TODO: to video via ffmpeg; right now just a single png
|
||||
# of the last frame
|
||||
def todo_live_ffmpeg():
|
||||
output = 'to_ffmpeg'
|
||||
# live: write to named pipe
|
||||
if not Path(output).exists():
|
||||
print(f'making fifo at {output}')
|
||||
os.mkfifo(output)
|
||||
fd = open(output, 'wb')
|
||||
for frame in frames:
|
||||
fd.write(frame)
|
||||
|
||||
# Save each image as a PNG file
|
||||
for i, img in enumerate(tqdm(images)):
|
||||
img.save(f'frames/{basename}/{basename}_{i:04}.png')
|
||||
print('live stream, import scapy')
|
||||
import scapy.all
|
||||
print('open stream')
|
||||
|
||||
# Produce a video from the saved images
|
||||
ffmpeg_input = f"frames/{basename}/{basename}_%04d.png"
|
||||
command = [
|
||||
"ffmpeg",
|
||||
"-y", # Overwrite output file without asking
|
||||
"-hide_banner", # Hide banner
|
||||
"-loglevel", "info", # Log level
|
||||
"-f", "image2", # Input format
|
||||
"-framerate", "25", # Framerate
|
||||
"-i", ffmpeg_input, # Input file pattern
|
||||
"-vf", "transpose=1", # Video filter for transposing
|
||||
"-s", "384x288", # Size of one frame
|
||||
"-vcodec", "libx264", # Video codec
|
||||
"-pix_fmt", "yuv420p", # Pixel format: YUV 4:2:0
|
||||
"thermal.mp4", # Output file in MP4 container
|
||||
]
|
||||
class PacketHandler:
|
||||
def __init__(self, cb):
|
||||
self.frame_collector = FrameCollector()
|
||||
self.cb = cb
|
||||
|
||||
subprocess.run(command)
|
||||
def handle(self, pkt):
|
||||
pkt = removestart_inner(pkt)
|
||||
parsed = parse(pkt)
|
||||
frame_maybe = self.frame_collector.handle(parsed)
|
||||
if not frame_maybe or bad_frame(frame_maybe):
|
||||
return
|
||||
self.cb(frame_maybe)
|
||||
|
||||
progress = tqdm()
|
||||
|
||||
def on_frame(frame):
|
||||
progress.update(1)
|
||||
Image.fromarray(np.frombuffer(frame, dtype='>H').reshape(WIDTH, HEIGHT)).save(f'live.new.png')
|
||||
os.rename('live.new.png', 'live.png')
|
||||
|
||||
handler = PacketHandler(on_frame)
|
||||
live_capture_cb(handler.handle)
|
||||
|
||||
else:
|
||||
process_video()
|
||||
|
||||
print("to play: ffplay thermal.mp4")
|
||||
|
||||
111
examples/cutoff.rs
Normal file
111
examples/cutoff.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use std::{collections::HashMap, io::stdout, time::Duration};
|
||||
|
||||
use futures::{future::FutureExt, select, StreamExt};
|
||||
use futures_timer::Delay;
|
||||
|
||||
use crossterm::{
|
||||
cursor::position,
|
||||
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode, KeyEvent},
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode},
|
||||
};
|
||||
|
||||
async fn set_cutoff(cutoff: f64, freq: f64) -> Result<(), reqwest::Error> {
|
||||
// Some simple CLI args requirements...
|
||||
let url = format!("http://localhost:3000/cutoff");
|
||||
let mut map = HashMap::new();
|
||||
map.insert("min_cutoff", cutoff);
|
||||
map.insert("max_cutoff", cutoff + 10.0);
|
||||
map.insert("freq_hz", freq);
|
||||
let client = reqwest::Client::new();
|
||||
let res = client.post(url).json(&map).send().await?;
|
||||
|
||||
// eprintln!("Response: {:?} {}", res.version(), res.status());
|
||||
// eprintln!("Headers: {:#?}\n", res.headers());
|
||||
|
||||
// let body = res.text().await?;
|
||||
// println!("{body}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const HELP: &str = r#"EventStream based on futures_util::Stream with tokio
|
||||
- Keyboard, mouse and terminal resize events enabled
|
||||
- Prints "." every second if there's no event
|
||||
- Hit "c" to print current cursor position
|
||||
- Use Esc to quit
|
||||
"#;
|
||||
|
||||
async fn print_events() {
|
||||
let mut reader = EventStream::new();
|
||||
let mut cutoff = 30.0;
|
||||
let mut last_cutoff = cutoff;
|
||||
let mut freq = 1.0;
|
||||
|
||||
loop {
|
||||
let mut delay = Delay::new(Duration::from_millis(1_000)).fuse();
|
||||
let mut event = reader.next().fuse();
|
||||
let mut change = false;
|
||||
select! {
|
||||
_ = delay => {
|
||||
},
|
||||
maybe_event = event => {
|
||||
match maybe_event {
|
||||
Some(Ok(event)) => {
|
||||
if event == Event::Key(KeyCode::Char('c').into()) {
|
||||
println!("Cursor position: {:?}\r", position());
|
||||
}
|
||||
|
||||
if event == Event::Key(KeyCode::Esc.into()) {
|
||||
break;
|
||||
}
|
||||
if let Event::Key(k) = event {
|
||||
if let KeyCode::Char(c) = k.code {
|
||||
change = true;
|
||||
match c {
|
||||
'[' => {
|
||||
cutoff -= 1.0;
|
||||
}
|
||||
']' => {
|
||||
cutoff += 1.0;
|
||||
}
|
||||
'1' => {
|
||||
freq *= 0.9;
|
||||
}
|
||||
'2' => {
|
||||
freq *= 1.1;
|
||||
}
|
||||
_ => {
|
||||
change = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if change {
|
||||
set_cutoff(cutoff, freq).await.unwrap();
|
||||
println!("cutoff = {}\r", cutoff);
|
||||
}
|
||||
}
|
||||
Some(Err(e)) => println!("Error: {:?}\r", e),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
println!("{}", HELP);
|
||||
|
||||
enable_raw_mode()?;
|
||||
|
||||
let mut stdout = stdout();
|
||||
execute!(stdout, EnableMouseCapture)?;
|
||||
|
||||
print_events().await;
|
||||
|
||||
execute!(stdout, DisableMouseCapture)?;
|
||||
|
||||
disable_raw_mode()
|
||||
}
|
||||
11
examples/main.rs
Normal file
11
examples/main.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use thermaldecoder::decode_to_files;
|
||||
|
||||
use std::env;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let mut arg = env::args();
|
||||
arg.next(); // skip executable
|
||||
let filename = arg.next().ok_or(anyhow::anyhow!("unexpected"))?;
|
||||
decode_to_files(&filename)?;
|
||||
Ok(())
|
||||
}
|
||||
17
examples/replay.rs
Normal file
17
examples/replay.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use std::net::UdpSocket;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
{
|
||||
let socket = UdpSocket::bind("192.168.0.1:8091")?;
|
||||
|
||||
// Receives a single datagram message on the socket. If `buf` is too small to hold
|
||||
// the message, it will be cut off.
|
||||
let buf = [
|
||||
1, 0x20, 1, 0x80, 0x1b, 0x40, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0x0f, 0, 0, 0, 1, 0, 0, 1,
|
||||
0, 0, 0x20, 0x2b, 0,
|
||||
];
|
||||
socket.set_broadcast(true)?;
|
||||
socket.send_to(&buf, "192.168.0.255:8092")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
49
gst.py
Normal file
49
gst.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import gi
|
||||
gi.require_version('Gst', '1.0')
|
||||
from gi.repository import Gst, GLib
|
||||
|
||||
# Initialize GStreamer
|
||||
Gst.init(None)
|
||||
|
||||
# Define the pipeline
|
||||
pipeline = Gst.parse_launch(
|
||||
"v4l2src device=/dev/video0 ! "
|
||||
"video/x-raw, format=GRAY16_LE, width=358, height=288, framerate=30/1 ! "
|
||||
"videoconvert ! "
|
||||
"x264enc bitrate=500 speed-preset=ultrafast tune=zerolatency ! "
|
||||
"tee name=t "
|
||||
"t. ! queue ! "
|
||||
"hlsdemux ! "
|
||||
"hlssink playlist-root=http://example.com/live location=/var/www/html/live/segment%05d.ts playlist-location=/var/www/html/live/playlist.m3u8 "
|
||||
"t. ! queue ! "
|
||||
"filesink location=/path/to/save/output.h264"
|
||||
)
|
||||
|
||||
# Start playing
|
||||
pipeline.set_state(Gst.State.PLAYING)
|
||||
|
||||
# Wait until error or EOS
|
||||
bus = pipeline.get_bus()
|
||||
|
||||
# Error handling
|
||||
def on_message(bus, message, loop):
|
||||
mtype = message.type
|
||||
if mtype == Gst.MessageType.ERROR:
|
||||
err, debug = message.parse_error()
|
||||
print("Error: %s" % err, debug)
|
||||
loop.quit()
|
||||
elif mtype == Gst.MessageType.EOS:
|
||||
print("End of stream")
|
||||
loop.quit()
|
||||
|
||||
loop = GLib.MainLoop()
|
||||
bus.add_signal_watch()
|
||||
bus.connect("message", on_message, loop)
|
||||
|
||||
try:
|
||||
loop.run()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
# Free resources
|
||||
pipeline.set_state(Gst.State.NULL)
|
||||
BIN
indesk.pcapng
Normal file
BIN
indesk.pcapng
Normal file
Binary file not shown.
9
listen.py
Normal file
9
listen.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from socket import socket, AF_INET, SOCK_DGRAM
|
||||
|
||||
s = socket(AF_INET, SOCK_DGRAM)
|
||||
s.bind(('', 8090))
|
||||
|
||||
while True:
|
||||
d = s.recvfrom(1024)
|
||||
print(d)
|
||||
|
||||
7
live_vid.sh
Executable file
7
live_vid.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
# Enable to get a dot file, to turn to png: dot -Tpng -osomething.png something.dot
|
||||
#export GST_DEBUG_DUMP_DOT_DIR=$(pwd)
|
||||
gst-launch-1.0 filesrc location=output.raw \
|
||||
! rawvideoparse use_sink_caps=false height=384 width=288 format=gray16-be \
|
||||
! videoconvertscale \
|
||||
! autovideosink
|
||||
10
pyproject.toml
Normal file
10
pyproject.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[project]
|
||||
name = "thermalcam"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"python-dotenv>=1.2.1",
|
||||
"scapy>=2.6.1",
|
||||
]
|
||||
@@ -1,3 +0,0 @@
|
||||
### Thermal decoder
|
||||
|
||||
https://wiki.telavivmakers.org/tamiwiki/projects/thermalcam
|
||||
27
replay.py
Executable file
27
replay.py
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
#replay the "trigger" packet.
|
||||
#this packets will start the source broadcasting its packets.
|
||||
|
||||
import base64
|
||||
from scapy.all import *
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Base64 encoded packet data
|
||||
encoded_packet = "////////AAFsWfAKCABFAAA4KB0AAIARkEfAqAABwKgA/x+bH5wA2QAAASABgBtAACAAAAAAAAAADwAAAAEAAAEAACArAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//////////////////////////////////////////AAAAAAAAAAIBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
|
||||
|
||||
# Decode the Base64 encoded packet
|
||||
decoded_packet = base64.b64decode(encoded_packet)
|
||||
|
||||
# Load packet with Scapy
|
||||
packet = Ether(decoded_packet)
|
||||
#print(packet.show(dump=True))
|
||||
|
||||
iface = os.environ.get('THERMALCAM_IFACE', 'enp1s0f0')
|
||||
print(f'using interface {iface}')
|
||||
|
||||
# (packet)
|
||||
sendp(packet, iface=iface)
|
||||
@@ -12,6 +12,7 @@ jedi==0.19.1
|
||||
kiwisolver==1.4.5
|
||||
matplotlib==3.8.2
|
||||
matplotlib-inline==0.1.6
|
||||
maturin==1.4.0
|
||||
numpy==1.26.2
|
||||
opencv-python==4.8.1.78
|
||||
packaging==23.2
|
||||
|
||||
2
run_dhcp_server.sh
Executable file
2
run_dhcp_server.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
sudo dnsmasq -i enp1s0f0 --local-service --dhcp-range=192.168.0.10,192.168.0.100 --dhcp-leasefile=dhcp.lease -d
|
||||
13
run_live.sh
Executable file
13
run_live.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
cd $(dirname $0)
|
||||
|
||||
set -e
|
||||
|
||||
# Python works but stutters
|
||||
#sudo ./venv/bin/python ./decode.py --live
|
||||
cargo build --release
|
||||
TARGET=./target/release/thermaldecoder
|
||||
# setcap does not work yet (EPERM on socket AF_PACKET)
|
||||
# sudo setcap cap_net_raw,cap_net_admin=eip $TARGET
|
||||
#sudo strace -f -o live.strace $TARGET /dev/video0
|
||||
sudo RUST_BACKTRACE=full $TARGET "$@"
|
||||
13
run_live_debug.sh
Executable file
13
run_live_debug.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
cd $(dirname $0)
|
||||
|
||||
set -e
|
||||
|
||||
# Python works but stutters
|
||||
#sudo ./venv/bin/python ./decode.py --live
|
||||
cargo build --example live
|
||||
TARGET=./target/debug/examples/live
|
||||
# setcap does not work yet (EPERM on socket AF_PACKET)
|
||||
# sudo setcap cap_net_raw,cap_net_admin=eip $TARGET
|
||||
#sudo strace -f -o live.strace $TARGET /dev/video0
|
||||
sudo RUST_BACKTRACE=full $TARGET "$@"
|
||||
2
rustdecode.sh
Executable file
2
rustdecode.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
cargo run --release --example main -- "$@"
|
||||
71
src/main.rs
Normal file
71
src/main.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
pub(crate) mod offline;
|
||||
pub(crate) mod stream;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use stream::{initialize, start_stream_thread, Streamer};
|
||||
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
streamer: Arc<Mutex<Streamer>>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
println!("creating streamer");
|
||||
let streamer = initialize();
|
||||
println!("creating streamer thread");
|
||||
start_stream_thread(streamer.clone());
|
||||
let state = AppState { streamer };
|
||||
|
||||
println!("starting web interface");
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
// `GET /` goes to `root`
|
||||
.route("/", get(root))
|
||||
// `POST /users` goes to `set_cutoff`
|
||||
.route("/cutoff", post(set_cutoff))
|
||||
.with_state(state);
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
// basic handler that responds with a static string
|
||||
async fn root() -> &'static str {
|
||||
"Hello, TAMI!"
|
||||
}
|
||||
|
||||
async fn set_cutoff(
|
||||
State(state): State<AppState>,
|
||||
// this argument tells axum to parse the request body
|
||||
// as JSON into a `CreateUser` type
|
||||
Json(payload): Json<SetCutoff>,
|
||||
) -> (StatusCode, Json<bool>) {
|
||||
let mut streamer = state.streamer.lock().unwrap();
|
||||
streamer.min_cutoff = payload.min;
|
||||
streamer.max_cutoff = payload.max;
|
||||
streamer.freq_hz = payload.hz;
|
||||
println!("updated to {:?}", payload);
|
||||
(StatusCode::OK, true.into())
|
||||
}
|
||||
|
||||
// the input to our `create_user` handler
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct SetCutoff {
|
||||
min: f64,
|
||||
max: f64,
|
||||
hz: f64,
|
||||
}
|
||||
287
src/offline.rs
Normal file
287
src/offline.rs
Normal file
@@ -0,0 +1,287 @@
|
||||
use indicatif::ProgressBar;
|
||||
use indicatif::ProgressBarIter;
|
||||
use png;
|
||||
use pyo3::exceptions::PyValueError;
|
||||
use pyo3::prelude::*;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::BufWriter;
|
||||
use std::path::Path;
|
||||
|
||||
use pcap_parser::traits::PcapReaderIterator;
|
||||
use pcap_parser::*;
|
||||
|
||||
struct PacketsIterator {
|
||||
cap: PcapNGReader<ProgressBarIter<File>>,
|
||||
i: usize,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl Iterator for PacketsIterator {
|
||||
type Item = Vec<u8>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let mut ret = None;
|
||||
while ret.is_none() {
|
||||
match self.cap.next() {
|
||||
Ok((offset, packet)) => {
|
||||
self.i += 1;
|
||||
self.size += offset;
|
||||
match packet {
|
||||
PcapBlockOwned::Legacy(_block) => {
|
||||
println!("dunno");
|
||||
}
|
||||
PcapBlockOwned::LegacyHeader(_block) => {
|
||||
println!("dunnoheader");
|
||||
}
|
||||
PcapBlockOwned::NG(block) => {
|
||||
if block.is_data_block() {
|
||||
match block {
|
||||
Block::EnhancedPacket(ref epb) => {
|
||||
if epb.origlen == 6972 {
|
||||
// remove udp header
|
||||
ret = Some(epb.data[0x2a..].to_vec());
|
||||
}
|
||||
}
|
||||
Block::SimplePacket(ref ep) => {
|
||||
if ep.origlen == 6972 {
|
||||
println!("found one regular");
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("unsupported packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.cap.consume(offset)
|
||||
}
|
||||
Err(PcapError::Eof) => break,
|
||||
Err(PcapError::Incomplete) => {
|
||||
self.cap.refill().unwrap();
|
||||
}
|
||||
Err(_) => {
|
||||
println!("unexpected error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl PacketsIterator {
|
||||
fn new(filename: &str) -> anyhow::Result<Self> {
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(false)
|
||||
.create(false)
|
||||
.open(filename)?;
|
||||
let pb = ProgressBar::new(file.metadata()?.len());
|
||||
let wrap = pb.wrap_read(file);
|
||||
let cap = PcapNGReader::new(65535, wrap)?;
|
||||
Ok(PacketsIterator { cap, i: 0, size: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C, packed)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Header {
|
||||
c1: u32,
|
||||
c2: u16,
|
||||
pub part: u16,
|
||||
a: u16,
|
||||
ffaa: u16,
|
||||
b: u16,
|
||||
c: u16,
|
||||
d: u16,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
pub fn read(data: &[u8]) -> anyhow::Result<Self> {
|
||||
Ok(Header {
|
||||
c1: u32::from_be_bytes([data[0], data[1], data[2], data[3]]),
|
||||
c2: u16::from_be_bytes([data[4], data[5]]),
|
||||
part: u16::from_be_bytes([data[6], data[7]]),
|
||||
a: u16::from_be_bytes([data[8], data[9]]),
|
||||
ffaa: u16::from_be_bytes([data[10], data[11]]),
|
||||
b: u16::from_be_bytes([data[12], data[13]]),
|
||||
c: u16::from_be_bytes([data[14], data[15]]),
|
||||
d: u16::from_be_bytes([data[16], data[17]]),
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn read_via_cast(data: &[u8]) -> anyhow::Result<&Self> {
|
||||
let size = std::mem::size_of::<Self>();
|
||||
if data.len() < size {
|
||||
return Err(anyhow::anyhow!("not large enough"));
|
||||
}
|
||||
let mem = &data[..size].as_ptr();
|
||||
Ok(unsafe { &*mem.cast() })
|
||||
}
|
||||
}
|
||||
|
||||
pub const HDR_SIZE: usize = std::mem::size_of::<Header>();
|
||||
|
||||
pub struct Frame {
|
||||
#[allow(dead_code)]
|
||||
pub header: Header,
|
||||
pub raw: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
fn pixels(&self) -> Vec<u16> {
|
||||
(0..self.raw.len())
|
||||
.step_by(2)
|
||||
.map(|i| u16::from_be_bytes([self.raw[i], self.raw[i + 1]]))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Decoder {
|
||||
packiter: PacketsIterator,
|
||||
parts: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
pub fn new(filename: &str) -> anyhow::Result<Self> {
|
||||
let packiter = PacketsIterator::new(filename)?;
|
||||
Ok(Decoder {
|
||||
packiter,
|
||||
parts: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Decoder {
|
||||
type Item = Frame;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let mut ret = None;
|
||||
while let Some(packet) = self.packiter.next() {
|
||||
let hdr = match Header::read(&packet) {
|
||||
Ok(header) => header,
|
||||
Err(e) => {
|
||||
println!("error reading header: {:?}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let data = packet[HDR_SIZE..].to_vec();
|
||||
if hdr.part == 0 && self.parts.len() > 0 {
|
||||
ret = Some(Frame {
|
||||
header: hdr,
|
||||
raw: self.parts.concat(),
|
||||
});
|
||||
self.parts.clear();
|
||||
}
|
||||
self.parts.push(data);
|
||||
if ret.is_some() {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn write_raw_frame(name: &str, data: &[u8]) -> anyhow::Result<()> {
|
||||
let path = Path::new(&name);
|
||||
let file = File::create(path)?;
|
||||
let ref mut w = BufWriter::new(file);
|
||||
let mut encoder = png::Encoder::new(w, 288, 384);
|
||||
encoder.set_color(png::ColorType::Grayscale);
|
||||
encoder.set_depth(png::BitDepth::Sixteen);
|
||||
let mut writer = encoder.write_header()?;
|
||||
writer.write_image_data(data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn write_calibrated_frame(name: &str, data: &[u16]) -> anyhow::Result<()> {
|
||||
let path = Path::new(&name);
|
||||
let file = File::create(path).unwrap();
|
||||
let ref mut w = BufWriter::new(file);
|
||||
let mut encoder = png::Encoder::new(w, 288, 384);
|
||||
encoder.set_color(png::ColorType::Grayscale);
|
||||
encoder.set_depth(png::BitDepth::Eight);
|
||||
let mut writer = encoder.write_header()?;
|
||||
//let samples: &[u16] = unsafe { std::slice::from_raw_parts(p.cast(), frame.len() / 2) };
|
||||
let frame = data
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|x| {
|
||||
let x: f64 = x.into();
|
||||
let x = x / 256.0;
|
||||
((-1.665884e-08) * x.powf(4.)
|
||||
+ (1.347094e-05) * x.powf(3.)
|
||||
+ (-4.396264e-03) * x.powf(2.)
|
||||
+ (9.506939e-01) * x
|
||||
+ (-6.353247e+01)) as u8
|
||||
})
|
||||
.collect::<Vec<u8>>();
|
||||
writer.write_image_data(&frame)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
struct PyFrameIterator {
|
||||
iter: Decoder,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl PyFrameIterator {
|
||||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<PyObject> {
|
||||
match slf.iter.next() {
|
||||
Some(f) => {
|
||||
let pixels = f.pixels();
|
||||
let p = Python::with_gil(|py| pixels.into_py(py));
|
||||
Some(p)
|
||||
}
|
||||
None => None.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn decode(filename: &str) -> PyResult<PyFrameIterator> {
|
||||
let decoder = Decoder::new(filename).map_err(|_| PyValueError::new_err("failed to read"))?;
|
||||
let iter = PyFrameIterator { iter: decoder };
|
||||
Ok(iter.into())
|
||||
}
|
||||
|
||||
/// writes to frames/<basename of filename>
|
||||
#[allow(dead_code)]
|
||||
pub fn decode_to_files(filename: &str) -> anyhow::Result<()> {
|
||||
let frameiter = Decoder::new(filename)?;
|
||||
let basename = std::path::Path::new(filename)
|
||||
.file_stem()
|
||||
.ok_or(anyhow::anyhow!("cannot get basename"))?
|
||||
.to_str()
|
||||
.ok_or(anyhow::anyhow!("cannot convert to utf-8 from os name"))?;
|
||||
let target_dir = format!("frames/{}", basename);
|
||||
let target_dir = std::path::Path::new(&target_dir);
|
||||
if !target_dir.exists() {
|
||||
std::fs::create_dir(target_dir)?;
|
||||
}
|
||||
for (i, frame) in frameiter.enumerate() {
|
||||
let name = format!("frames/{}/{:05}.png", basename, i);
|
||||
if let Err(_e) = write_raw_frame(&name, &frame.raw) {
|
||||
println!("skipping bad frame {}", i);
|
||||
continue;
|
||||
}
|
||||
let name = format!("{}/temp_{:05}.png", target_dir.display(), i);
|
||||
let pixels = frame.pixels();
|
||||
write_calibrated_frame(&name, &pixels)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn thermaldecoder(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(decode, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
209
src/stream.rs
Normal file
209
src/stream.rs
Normal file
@@ -0,0 +1,209 @@
|
||||
use crate::offline::{Header, HDR_SIZE};
|
||||
use bracket_color::prelude::*;
|
||||
use clap::Parser;
|
||||
use dotenv::dotenv;
|
||||
use std::time::SystemTime;
|
||||
use std::{
|
||||
io::Write,
|
||||
sync::{Arc, Mutex},
|
||||
thread::spawn,
|
||||
};
|
||||
use v4l::video::Output;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
temperature: bool,
|
||||
#[arg(short, long, default_value = "/dev/video0")]
|
||||
device: String,
|
||||
#[arg(short, long)]
|
||||
red_cutoff: Option<f64>,
|
||||
}
|
||||
|
||||
fn pixel_to_celcius(x: u16) -> u16 {
|
||||
let x: f64 = x.into();
|
||||
let x = x / 256.0;
|
||||
let t = (-1.665884e-08) * x.powf(4.)
|
||||
+ (1.347094e-05) * x.powf(3.)
|
||||
+ (-4.396264e-03) * x.powf(2.)
|
||||
+ (9.506939e-01) * x
|
||||
+ (-6.353247e+01);
|
||||
(t * 256.0) as u16
|
||||
}
|
||||
|
||||
/// https://en.wikipedia.org/wiki/HSL_and_HSV
|
||||
/// convert to the expected dynamic range first. We insert values in [0..256)
|
||||
/// h in [0, 360] degrees
|
||||
/// s in [0, 1]
|
||||
/// v in [0, 1]
|
||||
fn once_upon_a_time_hsv2rgb(h: u8, s: u8, v: u8) -> (u8, u8, u8) {
|
||||
let h = (h as f64) / 256.0 * 360.0;
|
||||
let s = (s as f64) / 256.0;
|
||||
let v = (v as f64) / 256.0;
|
||||
(0, 0, 0)
|
||||
}
|
||||
|
||||
fn rgb_to_u8s(rgb: &RGB) -> (u8, u8, u8) {
|
||||
(
|
||||
(rgb.r * 256.) as u8,
|
||||
(rgb.g * 256.) as u8,
|
||||
(rgb.b * 256.) as u8,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) struct Streamer {
|
||||
pub(crate) min_cutoff: f64,
|
||||
pub(crate) max_cutoff: f64,
|
||||
pub(crate) freq_hz: f64,
|
||||
}
|
||||
|
||||
pub(crate) fn initialize() -> Arc<Mutex<Streamer>> {
|
||||
let args = Args::parse();
|
||||
Arc::new(Mutex::new(Streamer {
|
||||
min_cutoff: args.red_cutoff.unwrap_or(26.),
|
||||
max_cutoff: args.red_cutoff.unwrap_or(26.) + 10.0,
|
||||
freq_hz: 1.0,
|
||||
}))
|
||||
}
|
||||
|
||||
fn main(streamer: Arc<Mutex<Streamer>>) -> anyhow::Result<()> {
|
||||
dotenv().ok();
|
||||
let args = Args::parse();
|
||||
let device = match std::env::var("THERMALCAM_IFACE=enp1s0f0") {
|
||||
Ok(d) => {
|
||||
let device = pcap::Device::list()
|
||||
.expect("device list failed")
|
||||
.into_iter()
|
||||
.find(|x| x.name == d)
|
||||
.expect(&format!("could not find device {}", d));
|
||||
device
|
||||
}
|
||||
Err(_) => pcap::Device::lookup()
|
||||
.expect("device lookup failed")
|
||||
.expect("no device available"),
|
||||
};
|
||||
// get the default Device
|
||||
|
||||
println!("Using device {}", device.name);
|
||||
let output = args.device;
|
||||
println!("Using output v4l2loopback device {}", output);
|
||||
|
||||
const WIDTH: usize = 288;
|
||||
const HEIGHT: usize = 384;
|
||||
println!("reading cutoff");
|
||||
let start = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs_f64();
|
||||
let greyscale = !args.temperature;
|
||||
let fourcc_repr = if greyscale {
|
||||
[
|
||||
b'Y', // | 0b10000000
|
||||
b'1', b'6',
|
||||
b' ', // Note: not using b' ' | 0x80, (V4L2_PIX_FMT_Y16_BE)
|
||||
// because VID_S_FMT ioctl returns EINVAL, so just swap the bytes here
|
||||
]
|
||||
} else {
|
||||
// RGB32 is 4 bytes R, G, B, A
|
||||
[b'R', b'G', b'B', b'4']
|
||||
};
|
||||
println!("using four cc {:?}", fourcc_repr);
|
||||
let bytes_per_pixel = if greyscale { 2 } else { 4 };
|
||||
let fourcc = v4l::format::FourCC { repr: fourcc_repr };
|
||||
let mut out = v4l::Device::with_path(output)?;
|
||||
// To find the fourcc code, use v4l2-ctl --list-formats-out /dev/video0
|
||||
// (or read the source :)
|
||||
// flip axes
|
||||
let format = v4l::Format::new(HEIGHT as u32, WIDTH as u32, fourcc);
|
||||
Output::set_format(&out, &format)?;
|
||||
|
||||
// Setup Capture
|
||||
let mut cap = pcap::Capture::from_device(device)
|
||||
.unwrap()
|
||||
.immediate_mode(true)
|
||||
.open()
|
||||
.unwrap();
|
||||
|
||||
// get a packet and print its bytes
|
||||
const PACKET_LEN: usize = 6972;
|
||||
// input is grayscale 16 bits per pixel
|
||||
const FRAME_LEN: usize = WIDTH * HEIGHT * 2;
|
||||
let mut frame = [0u8; FRAME_LEN];
|
||||
let mut len = 0;
|
||||
let output_frame_len = WIDTH * HEIGHT * bytes_per_pixel;
|
||||
let mut swapped_vec = vec![0u8; output_frame_len];
|
||||
let swapped = &mut swapped_vec;
|
||||
while let Ok(p) = cap.next_packet() {
|
||||
let data = p.data;
|
||||
if data.len() != PACKET_LEN {
|
||||
continue;
|
||||
}
|
||||
let data = &data[0x2a..];
|
||||
let header = match Header::read(data) {
|
||||
Ok(header) => header,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let data = &data[HDR_SIZE..];
|
||||
if (header.part == 0 && len > 0)
|
||||
// do not write out of bounds - would panic, instead just skip
|
||||
|| (data.len() + len > FRAME_LEN)
|
||||
{
|
||||
if len == FRAME_LEN {
|
||||
// read once per frame, can make it lower if need be
|
||||
let state = streamer.lock().unwrap();
|
||||
let mid = (state.min_cutoff + state.max_cutoff) / 2.0;
|
||||
let range = state.max_cutoff - state.min_cutoff;
|
||||
let hz = state.freq_hz;
|
||||
let now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs_f64();
|
||||
let dt = now - start;
|
||||
let cutoff = mid + f64::sin(dt * hz) * 0.5 * range;
|
||||
// swap the bytes, we are using LE, not BE, 16 bit grayscale
|
||||
// possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2
|
||||
for i in 0..FRAME_LEN / 2 {
|
||||
let x = i % WIDTH;
|
||||
let y = (i / WIDTH) % HEIGHT;
|
||||
let mut pixel = u16::from_be_bytes([frame[i * 2], frame[i * 2 + 1]]);
|
||||
if greyscale {
|
||||
if args.temperature {
|
||||
pixel = pixel_to_celcius(pixel);
|
||||
}
|
||||
let pixel_swapped = pixel.to_le_bytes();
|
||||
let out_i = ((HEIGHT - 1 - y) + (WIDTH - 1 - x) * HEIGHT) * 2;
|
||||
swapped[out_i..out_i + 2].copy_from_slice(&pixel_swapped);
|
||||
} else {
|
||||
pixel = pixel_to_celcius(pixel);
|
||||
let (r, g, b) = if pixel > (256.0 * cutoff) as u16 {
|
||||
let p = pixel - (256.0 * cutoff) as u16;
|
||||
let rgb = HSV::from_f32(0.0, (p as f32) / 256.0, 0.0).to_rgb();
|
||||
rgb_to_u8s(&rgb)
|
||||
} else {
|
||||
let rgb =
|
||||
HSV::from_f32(pixel as f32 / 65536.0, 0.0, pixel as f32 / 65536.0)
|
||||
.to_rgb();
|
||||
rgb_to_u8s(&rgb)
|
||||
};
|
||||
let out_i = ((HEIGHT - 1 - y) + (WIDTH - 1 - x) * HEIGHT) * 4;
|
||||
swapped[out_i..out_i + 4].copy_from_slice(&[0, r, g, b]);
|
||||
}
|
||||
}
|
||||
out.write_all(&swapped[..])?;
|
||||
}
|
||||
len = 0;
|
||||
}
|
||||
frame[len..len + data.len()].copy_from_slice(data);
|
||||
len += data.len();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn start_stream_thread(streamer: Arc<Mutex<Streamer>>) {
|
||||
spawn(move || {
|
||||
if let Err(e) = main(streamer) {
|
||||
println!("oops: {:?}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
12
start.sh
Executable file
12
start.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
. .env
|
||||
IFACE=$THERMALCAM_IFACE
|
||||
echo "using iface $IFACE"
|
||||
echo "checking mtu"
|
||||
mtu=$(ip link show ${IFACE} | grep -o 'mtu [0-9]*' | gawk '{print $2}')
|
||||
echo "mtu = $mtu"
|
||||
if [ $mtu -lt 9000 ]; then
|
||||
echo "setting mtu to 9000"
|
||||
sudo ip link set $IFACE mtu 9000
|
||||
fi
|
||||
sudo uv run ./replay.py
|
||||
14
test2.py
Executable file
14
test2.py
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from thermaldecoder import decode
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
root = Path('f2')
|
||||
root.mkdir(exist_ok=True)
|
||||
frames = decode('heatbed90_hotend_200_ice0.pcapng')
|
||||
f = next(frames)
|
||||
f = next(frames)
|
||||
f = np.array(f)
|
||||
f.shape = (384, 288)
|
||||
plt.imshow(f)
|
||||
plt.show()
|
||||
32
test_rust.py
Executable file
32
test_rust.py
Executable file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from thermaldecoder import decode
|
||||
import numpy as np
|
||||
import subprocess
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
# Create a directory to store the frames if it doesn't exist
|
||||
root = Path('frames')
|
||||
root.mkdir(exist_ok=True)
|
||||
|
||||
# Decode the frames from the pcap file
|
||||
frames = list(decode('indesk.pcapng'))
|
||||
|
||||
# Iterate over the frames
|
||||
for i, frame in enumerate(frames):
|
||||
try:
|
||||
# Convert the frame to an image file
|
||||
img_path = root / f"frame_{i}.png"
|
||||
f = np.array(frame)
|
||||
f.shape = (384, 288)
|
||||
plt.imshow(f)
|
||||
plt.axis('off')
|
||||
plt.savefig(img_path, bbox_inches='tight', pad_inches=0)
|
||||
plt.close()
|
||||
|
||||
# Use ffmpeg to display the image
|
||||
subprocess.run(['ffmpeg', '-i', str(img_path), '-vf', 'scale=800:600', '-framerate', '25', '-f', 'image2pipe', '-'], check=True)
|
||||
|
||||
except ValueError as e:
|
||||
print(f"Error processing frame {i}: {e}")
|
||||
BIN
thermal.mp4
BIN
thermal.mp4
Binary file not shown.
553
thermalcamdecoder/Cargo.lock
generated
553
thermalcamdecoder/Cargo.lock
generated
@@ -1,553 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9d19de80eff169429ac1e9f48fffb163916b448a44e8e046186232046d9e1f9"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "circular"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fc239e0f6cb375d2402d48afb92f76f5404fd1df208a41930ec81eda078bea"
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.15.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"unicode-width",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||
|
||||
[[package]]
|
||||
name = "fdeflate"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "209098dd6dfc4445aa6111f0e98653ac323eaa4dfd212c9ca3931bf9955c31bd"
|
||||
dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.17.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fb28741c9db9a713d93deb3bb9515c20788cef5815265bee4980e87bde7e0f25"
|
||||
dependencies = [
|
||||
"console",
|
||||
"instant",
|
||||
"number_prefix",
|
||||
"portable-atomic",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "2.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8"
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.151"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
|
||||
dependencies = [
|
||||
"adler",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "number_prefix"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pcap-parser"
|
||||
version = "0.14.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b79dfb40aef938ed2082c9ae9443f4eba21b79c1a9d6cfa071f5c2bd8d829491"
|
||||
dependencies = [
|
||||
"circular",
|
||||
"nom",
|
||||
"rusticata-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.17.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd75bf2d8dd3702b9707cdbc56a5b9ef42cec752eb8b3bafc01234558442aa64"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"crc32fast",
|
||||
"fdeflate",
|
||||
"flate2",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.71"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04e8453b658fe480c3e70c8ed4e3d3ec33eb74988bd186561b0cc66b85c3bc4b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"indoc",
|
||||
"libc",
|
||||
"memoffset",
|
||||
"parking_lot",
|
||||
"pyo3-build-config",
|
||||
"pyo3-ffi",
|
||||
"pyo3-macros",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-build-config"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a96fe70b176a89cff78f2fa7b3c930081e163d5379b4dcdf993e3ae29ca662e5"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"target-lexicon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-ffi"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "214929900fd25e6604661ed9cf349727c8920d47deff196c4e28165a6ef2a96b"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"pyo3-build-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-macros"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dac53072f717aa1bfa4db832b39de8c875b7c7af4f4a6fe93cdbf9264cf8383b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"pyo3-macros-backend",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-macros-backend"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7774b5a8282bd4f25f803b1f0d945120be959a36c72e08e7cd031c792fdfd424"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rusticata-macros"
|
||||
version = "4.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
|
||||
dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.193"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.193"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "target-lexicon"
|
||||
version = "0.12.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a"
|
||||
|
||||
[[package]]
|
||||
name = "thermalcamdecoder"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"indicatif",
|
||||
"pcap-parser",
|
||||
"png",
|
||||
"pyo3",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.45.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
|
||||
dependencies = [
|
||||
"windows-targets 0.42.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.42.2",
|
||||
"windows_aarch64_msvc 0.42.2",
|
||||
"windows_i686_gnu 0.42.2",
|
||||
"windows_i686_msvc 0.42.2",
|
||||
"windows_x86_64_gnu 0.42.2",
|
||||
"windows_x86_64_gnullvm 0.42.2",
|
||||
"windows_x86_64_msvc 0.42.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.48.5",
|
||||
"windows_aarch64_msvc 0.48.5",
|
||||
"windows_i686_gnu 0.48.5",
|
||||
"windows_i686_msvc 0.48.5",
|
||||
"windows_x86_64_gnu 0.48.5",
|
||||
"windows_x86_64_gnullvm 0.48.5",
|
||||
"windows_x86_64_msvc 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
@@ -1,14 +0,0 @@
|
||||
[package]
|
||||
name = "thermalcamdecoder"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.77"
|
||||
indicatif = "0.17.7"
|
||||
pcap-parser = { version = "0.14.1", features = ["data"] }
|
||||
png = "0.17.10"
|
||||
pyo3 = "0.20.0"
|
||||
serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] }
|
||||
@@ -1,168 +0,0 @@
|
||||
use indicatif::{ProgressBar, ProgressIterator};
|
||||
use png;
|
||||
use std::fs::File;
|
||||
use std::io::BufWriter;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
use pcap_parser::traits::PcapReaderIterator;
|
||||
use pcap_parser::*;
|
||||
|
||||
fn get_packets_without_udp_header() -> anyhow::Result<(Vec<Vec<u8>>, usize, usize)> {
|
||||
let file = File::open("in.pcap")?;
|
||||
let mut cap = PcapNGReader::new(65535, file)?;
|
||||
let mut i = 0;
|
||||
let mut size = 0;
|
||||
let mut data = vec![];
|
||||
loop {
|
||||
match cap.next() {
|
||||
Ok((offset, packet)) => {
|
||||
i += 1;
|
||||
size += offset;
|
||||
match packet {
|
||||
PcapBlockOwned::Legacy(_block) => {
|
||||
println!("dunno");
|
||||
}
|
||||
PcapBlockOwned::LegacyHeader(_block) => {
|
||||
println!("dunnoheader");
|
||||
}
|
||||
PcapBlockOwned::NG(block) => {
|
||||
if block.is_data_block() {
|
||||
match block {
|
||||
Block::EnhancedPacket(ref epb) => {
|
||||
if epb.origlen == 6972 {
|
||||
// remove udp header
|
||||
data.push(epb.data[0x2a..].to_vec());
|
||||
}
|
||||
}
|
||||
Block::SimplePacket(ref ep) => {
|
||||
if ep.origlen == 6972 {
|
||||
println!("found one regular");
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("unsupported packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cap.consume(offset)
|
||||
}
|
||||
Err(PcapError::Eof) => break,
|
||||
Err(PcapError::Incomplete) => {
|
||||
cap.refill().unwrap();
|
||||
}
|
||||
Err(_) => {
|
||||
println!("unexpected error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((data, i, size))
|
||||
}
|
||||
|
||||
#[repr(C, packed)]
|
||||
#[derive(Clone, Debug)]
|
||||
struct Header {
|
||||
c1: u32,
|
||||
c2: u16,
|
||||
part: u16,
|
||||
a: u16,
|
||||
ffaa: u16,
|
||||
b: u16,
|
||||
c: u16,
|
||||
d: u16,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
fn read(data: &[u8]) -> anyhow::Result<Self> {
|
||||
Ok(Header {
|
||||
c1: u32::from_be_bytes([data[0], data[1], data[2], data[3]]),
|
||||
c2: u16::from_be_bytes([data[4], data[5]]),
|
||||
part: u16::from_be_bytes([data[6], data[7]]),
|
||||
a: u16::from_be_bytes([data[8], data[9]]),
|
||||
ffaa: u16::from_be_bytes([data[10], data[11]]),
|
||||
b: u16::from_be_bytes([data[12], data[13]]),
|
||||
c: u16::from_be_bytes([data[14], data[15]]),
|
||||
d: u16::from_be_bytes([data[16], data[17]]),
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn read_via_cast(data: &[u8]) -> anyhow::Result<&Self> {
|
||||
let size = std::mem::size_of::<Self>();
|
||||
if data.len() < size {
|
||||
return Err(anyhow::anyhow!("not large enough"));
|
||||
}
|
||||
let mem = &data[..size].as_ptr();
|
||||
Ok(unsafe { &*mem.cast() })
|
||||
}
|
||||
}
|
||||
|
||||
const HDR_SIZE: usize = std::mem::size_of::<Header>();
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let (data, i, size) = get_packets_without_udp_header()?;
|
||||
|
||||
println!("found {} packets, saved {}, {} size", i, data.len(), size);
|
||||
let mut dump = File::create_new("dump.bin");
|
||||
let mut frames = vec![];
|
||||
let mut parts = vec![];
|
||||
for packet in data.iter() {
|
||||
if let Ok(ref mut dump) = dump {
|
||||
dump.write_all(&packet)?;
|
||||
}
|
||||
let hdr = Header::read(packet)?;
|
||||
let data = packet[HDR_SIZE..].to_vec();
|
||||
if hdr.part == 0 && parts.len() > 0 {
|
||||
frames.push(parts.concat());
|
||||
parts.clear();
|
||||
}
|
||||
parts.push(data);
|
||||
}
|
||||
println!("found {} frames", frames.len());
|
||||
println!("writing raw pngs");
|
||||
let pb = ProgressBar::new(frames.len() as u64);
|
||||
for (i, frame) in frames.iter().enumerate().progress_with(pb) {
|
||||
let name = format!("{:03}.png", i);
|
||||
let path = Path::new(&name);
|
||||
let file = File::create(path).unwrap();
|
||||
let ref mut w = BufWriter::new(file);
|
||||
let mut encoder = png::Encoder::new(w, 288, 384);
|
||||
encoder.set_color(png::ColorType::Grayscale);
|
||||
encoder.set_depth(png::BitDepth::Sixteen);
|
||||
let mut writer = encoder.write_header()?;
|
||||
writer.write_image_data(&frame)?;
|
||||
}
|
||||
|
||||
println!("writing calibrated (value is temperature)");
|
||||
let pb = ProgressBar::new(frames.len() as u64);
|
||||
for (i, frame) in frames.iter().enumerate().progress_with(pb) {
|
||||
let name = format!("temp_{:03}.png", i);
|
||||
let path = Path::new(&name);
|
||||
let file = File::create(path).unwrap();
|
||||
let ref mut w = BufWriter::new(file);
|
||||
let mut encoder = png::Encoder::new(w, 288, 384);
|
||||
encoder.set_color(png::ColorType::Grayscale);
|
||||
encoder.set_depth(png::BitDepth::Eight);
|
||||
let mut writer = encoder.write_header()?;
|
||||
let p = frame.as_ptr();
|
||||
let samples: &[u16] = unsafe { std::slice::from_raw_parts(p.cast(), frame.len() / 2) };
|
||||
let frame = samples
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|x| {
|
||||
let x: f64 = x.into();
|
||||
((-1.665884e-08) * x.powf(4.)
|
||||
+ (1.347094e-05) * x.powf(3.)
|
||||
+ (-4.396264e-03) * x.powf(2.)
|
||||
+ (9.506939e-01) * x
|
||||
+ (-6.353247e+01)) as u8
|
||||
})
|
||||
.collect::<Vec<u8>>();
|
||||
writer.write_image_data(&frame)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
36
uv.lock
generated
Normal file
36
uv.lock
generated
Normal file
@@ -0,0 +1,36 @@
|
||||
version = 1
|
||||
revision = 3
|
||||
requires-python = ">=3.13"
|
||||
|
||||
[[package]]
|
||||
name = "python-dotenv"
|
||||
version = "1.2.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/f0/26/19cadc79a718c5edbec86fd4919a6b6d3f681039a2f6d66d14be94e75fb9/python_dotenv-1.2.1.tar.gz", hash = "sha256:42667e897e16ab0d66954af0e60a9caa94f0fd4ecf3aaf6d2d260eec1aa36ad6", size = 44221, upload-time = "2025-10-26T15:12:10.434Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/14/1b/a298b06749107c305e1fe0f814c6c74aea7b2f1e10989cb30f544a1b3253/python_dotenv-1.2.1-py3-none-any.whl", hash = "sha256:b81ee9561e9ca4004139c6cbba3a238c32b03e4894671e181b671e8cb8425d61", size = 21230, upload-time = "2025-10-26T15:12:09.109Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scapy"
|
||||
version = "2.6.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/df/2f/035d3888f26d999e9680af8c7ddb7ce4ea0fd8d0e01c000de634c22dcf13/scapy-2.6.1.tar.gz", hash = "sha256:7600d7e2383c853e5c3a6e05d37e17643beebf2b3e10d7914dffcc3bc3c6e6c5", size = 2247754, upload-time = "2024-11-05T08:43:23.488Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/4b/34/8695b43af99d0c796e4b7933a0d7df8925f43a8abdd0ff0f6297beb4de3a/scapy-2.6.1-py3-none-any.whl", hash = "sha256:88a998572049b511a1f3e44f4aa7c62dd39c6ea2aa1bb58434f503956641789d", size = 2420670, upload-time = "2024-11-05T08:43:21.285Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thermalcam"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "python-dotenv" },
|
||||
{ name = "scapy" },
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "python-dotenv", specifier = ">=1.2.1" },
|
||||
{ name = "scapy", specifier = ">=2.6.1" },
|
||||
]
|
||||
Reference in New Issue
Block a user