-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage_temp.py
More file actions
118 lines (95 loc) · 3.39 KB
/
average_temp.py
File metadata and controls
118 lines (95 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import clr
import sys
import os
import time
import platform
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from collections import deque
import pythoncom
pythoncom.CoInitialize()
from import_clr import *
clr.AddReference("ManagedIR16Filters")
clr.AddReference("LeptonUVC")
from Lepton import CCI
from IR16Filters import IR16Capture, NewIR16FrameEvent, NewBytesFrameEvent
found_device = None
for dev in CCI.GetDevices():
if dev.Name.startswith("PureThermal"):
found_device = dev
break
if not found_device:
print("Couldn't find Lepton device")
sys.exit(1)
lep = found_device.Open()
# Perform a one-time flat-field correction (FFC)
lep.sys.RunFFCNormalization()
# Set gain mode to LOW
lep.sys.SetGainMode(CCI.Sys.GainMode.LOW)
# Prepare a small buffer for incoming raw frames (only keep the latest)
incoming_frames = deque(maxlen=1)
def got_a_frame(short_array, width, height):
incoming_frames.append((height, width, short_array))
# Set up the capture pipeline
capture = IR16Capture()
capture.SetupGraphWithBytesCallback(NewBytesFrameEvent(got_a_frame))
capture.RunGraph()
# Attempt to enable T-Linear (linear temperature output)
tlinear = False
try:
lep.rad.SetTLinearEnableStateChecked(True)
tlinear = True
print("T-Linear mode enabled: pixel values are mapped linearly to temperature.")
except:
print("T-Linear not supported on this device; using raw centi-kelvin values.")
# Conversion helpers
def centikelvin_to_celsius(t_ck):
# Convert from centi-kelvin to degrees Celsius
return (t_ck - 27315) / 100.0
def to_fahrenheit(t_ck):
# Convert from centi-kelvin to degrees Fahrenheit
c = centikelvin_to_celsius(t_ck)
return c * 9.0 / 5.0 + 32.0
plt.ion()
fig, ax = plt.subplots()
img = None
print("Starting frame capture. Press Ctrl-C to stop.")
try:
while True:
# Wait until we have at least one frame
if not incoming_frames:
time.sleep(0.1)
continue
# Grab the most recent raw frame (height, width, rawBuffer)
height, width, raw_buffer = incoming_frames[-1]
# Convert rawBuffer (flat sequence of uint16) to a 2D NumPy array
arr = np.fromiter(raw_buffer, dtype=np.uint16).reshape((height, width))
# Compute max and average in centi-kelvin
max_ck = arr.max()
mean_ck = arr.mean()
# Convert to Celsius and Fahrenheit
max_c = centikelvin_to_celsius(max_ck)
mean_c = centikelvin_to_celsius(mean_ck)
max_f = to_fahrenheit(max_ck)
mean_f = to_fahrenheit(mean_ck)
# Print temperatures
print(f"Max: {max_f:.2f} °F / {max_c:.2f} °C "
f"Mean: {mean_f:.2f} °F / {mean_c:.2f} °C")
# Display/update the thermal image
if img is None:
img = ax.imshow(arr, cmap=cm.plasma, vmin=arr.min(), vmax=arr.max())
plt.colorbar(img, ax=ax)
ax.set_title("Live Thermal")
plt.show()
else:
img.set_data(arr)
img.set_clim(vmin=arr.min(), vmax=arr.max())
fig.canvas.draw()
# Small pause to allow the plot to update
plt.pause(0.1)
except KeyboardInterrupt:
print("\nCapture stopped by user.")
finally:
capture.StopGraph()
print("Graph stopped. Exiting.")