-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermal_camera_init.py
107 lines (88 loc) · 3.65 KB
/
thermal_camera_init.py
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
from queue import Queue
from uvctypes import *
import numpy as np
import cv2
class ThermalCamera:
def __init__(self):
self.BUF_SIZE = 2
self.q = Queue(self.BUF_SIZE)
self.PTR_PY_FRAME_CALLBACK = CFUNCTYPE(None, POINTER(uvc_frame), c_void_p)(self.py_frame_callback)
self.ctx = POINTER(uvc_context)()
self.dev = POINTER(uvc_device)()
self.devh = POINTER(uvc_device_handle)()
self.ctrl = uvc_stream_ctrl()
self.init_thermal_data_frames()
def py_frame_callback(self, frame, userptr):
array_pointer = cast(frame.contents.data, POINTER(c_uint16 * (frame.contents.width * frame.contents.height)))
data = np.frombuffer(
array_pointer.contents, dtype=np.dtype(np.uint16)
).reshape(
frame.contents.height, frame.contents.width
)
if frame.contents.data_bytes != (2 * frame.contents.width * frame.contents.height):
return
if not self.q.full():
self.q.put(data)
def init_thermal_data_frames(self):
res = libuvc.uvc_init(byref(self.ctx), 0)
if res < 0:
print("uvc_init error")
exit(1)
try:
res = libuvc.uvc_find_device(self.ctx, byref(self.dev), PT_USB_VID, PT_USB_PID, 0)
if res < 0:
print("uvc_find_device error")
exit(1)
try:
res = libuvc.uvc_open(self.dev, byref(self.devh))
if res < 0:
print("uvc_open error")
exit(1)
print("device opened!")
print_device_info(self.devh)
print_device_formats(self.devh)
set_manual_ffc(self.devh)
frame_formats = uvc_get_frame_formats_by_guid(self.devh, VS_FMT_GUID_Y16)
if len(frame_formats) == 0:
print("device does not support Y16")
exit(1)
libuvc.uvc_get_stream_ctrl_format_size(self.devh, byref(self.ctrl), UVC_FRAME_FORMAT_Y16,
frame_formats[0].wWidth, frame_formats[0].wHeight,
int(1e7 / frame_formats[0].dwDefaultFrameInterval)
)
except Exception as e:
print("Error while opening camera")
print(e)
libuvc.uvc_unref_device(self.dev)
except Exception as e:
print("Error while opening camera")
print(e)
libuvc.uvc_exit(self.ctx)
def read_thermal_data(self):
res = libuvc.uvc_start_streaming(self.devh, byref(self.ctrl), self.PTR_PY_FRAME_CALLBACK, None, 0)
if res < 0:
print("uvc_start_streaming failed: {0}".format(res))
exit(1)
try:
while True:
data = self.q.get(True, 500)
if data is not None:
yield data
except Exception as e:
print("Error while reading from camera")
print(e)
finally:
print("______________STOPPED_____________________")
libuvc.uvc_stop_streaming(self.devh)
def raw_to_8bit(self, data):
cv2.normalize(data, data, 0, 65535, cv2.NORM_MINMAX)
np.right_shift(data, 8, data)
return cv2.cvtColor(np.uint8(data), cv2.COLOR_GRAY2RGB)
def performffc(self):
perform_manual_ffc(self.devh)
def print_shutter_info(self):
print_shutter_info(self.devh)
def setmanualffc(self):
set_manual_ffc(self.devh)
def setautoffc(self):
set_auto_ffc(self.devh)