-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglimpse.py
291 lines (255 loc) · 8.24 KB
/
glimpse.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import usb1
from usbmsgs import *
USB_VID = 0x04b4
USB_PID = 0x8051
context = usb1.USBContext()
handle = context.openByVendorIDAndProductID( USB_VID, USB_PID, skip_on_error = True)
if handle is None:
print("ERROR")
exit(0)
print(handle)
IN = 0x80
OUT = 0x00
m = MSG_SET_PARAMETERS_S()
m.MessageNumber = 0
m.MessageID = 8
m.MessageSize = 20
m.doSendIMUData = 1
m.doSendProcessedThermalData = 0
m.Sync_DecimationRate = 1
m.Sync_TimeOut = 300
m.Thermal_Refresh_Rate = 32
m.Thermal_Resolution = 18
m.IMU_Command_Char = 0
class MsgFactory:
msgdict = {6: MSG_SENSOR_EEPROM_T, 7: MSG_NEW_RAWDATA_T}
@staticmethod
def create(msg_id, message):
if not MsgFactory.msgdict.has_key(msg_id):
return None
cls = MsgFactory.msgdict[msg_id]
msg = cls(message)
return msg
RowsPerSensor = 4
ColumnsPerSensor = 16
NumRowsOfSensor = 6
NumColumnsOfSensor = 2
TotalColumns = NumColumnsOfSensor * ColumnsPerSensor
TotalRows = NumRowsOfSensor * RowsPerSensor
import cv, cv2
import numpy as np
def create_blank(width, height, rgb_color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((height, width, 3), np.uint8)
# Since OpenCV uses BGR, convert the color first
color = tuple(reversed(rgb_color))
# Fill image with color
image[:] = color
return image
def load_colormap():
cmap = {}
lst = []
with open("CoolWarmUChar257.csv") as f:
for line in f:
line = line.strip()
sp = line.split(",")
sp = map(float, sp)
lst.append((sp[3], sp[2], sp[1]))
return lst
from threading import Thread
class Thermal(Thread):
def __init__(self, handle, ctrl):
Thread.__init__(self)
self.handle = handle
self.sensors = {}
self.Image = np.zeros((TotalColumns, TotalRows))#[[0]*TotalRows] * TotalColumns
self.width = 640
self.height = 480
self.Im = create_blank(self.width, self.height)
self.ctrl = ctrl
def _read_message(self):
message = ''
while True:
packet = self.handle.bulkRead(0x82, 64)
message += packet
if len(packet) % 64 == 0 and len(packet) != 0:
continue
else:
break
return message
def new_raw_data(self, msg):
for sensor_id in range(SENSORS_TOTAL):
self.sensors[sensor_id].LastResults = [0] * FETCH_ALL_LENGTH_WORDS
for i in range(FETCH_ALL_LENGTH_WORDS):
self.sensors[sensor_id].LastResults[i] = msg.RawData[sensor_id * FETCH_ALL_LENGTH_WORDS + i]
self.sensors[sensor_id].calc_temperature()
scale = 1.0
offset = 0.0
self.Image = np.zeros((TotalColumns, TotalRows))#[[0]*TotalRows] * TotalColumns
for sensor_id in range(SENSORS_TOTAL):
i = 0
first_col = sensor_id % NumColumnsOfSensor * ColumnsPerSensor;
first_row = sensor_id / NumColumnsOfSensor * RowsPerSensor
c = first_col
while c < first_col + ColumnsPerSensor:
r = first_row
while r < first_row + RowsPerSensor:
self.Image[c][r] = self.sensors[sensor_id].Temperature[i] * scale + offset
r += 1
i += 1
c += 1
self.ctrl.lock.acquire()
self.ctrl.tempQueue.append(self.Image)
self.ctrl.haveTemp = True
self.ctrl.lock.release()
self.ctrl.event()
return True
#return self.show_image()
def show_image(self):
for r in range(TotalRows):
for c in range(TotalColumns):
temp = self.Image[c][r]
if temp < 20: temp = 20
if temp > 40: temp = 39.9
color_i = int((temp - 20) / 20.0 * self.len_cm)
color = self.color_map[color_i]
#print(self.pixel_width)
x = c * self.pixel_width
y = r * self.pixel_height
cv2.rectangle(self.Im, (x, y), (x+self.pixel_width, y+self.pixel_height), color, -1)
cv2.imshow('frame', self.Im)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
return False
return True
def run(self):
while True:
try:
if not self._run_():
break
except Exception as e:
print("error", e)
break
def _run_(self):
message = self._read_message()
if len(message) == 0: return True
header = MSG_HEADER(message[:4])
msg = MsgFactory.create(header.MessageID, message)
if msg != None:
return msg.handle(self)
else:
#print(header.MessageID, header.MessageSize)
return True
import Queue
from threading import Lock
from util import sendFrame
import request_pb2
HOST = "archon.cs.washington.edu"
PORT = 9999
label = ''
class SendThread(Thread):
def __init__(self, frame):
Thread.__init__(self)
self.frame = frame
def run(self):
global label
try:
label_, latency = sendFrame(self.frame, HOST, PORT, request_pb2.OBJECT)
except:
return
label = label_
print(label)
import time
last = time.time()
class ShowThread(Thread):
def __init__(self, im, temp):
Thread.__init__(self)
self.Image = im
self.temp = temp
self.width = 640
self.height = 480
self.temp_im = create_blank(self.width, self.height)
self.pixel_width = self.width / TotalColumns
self.pixel_height = self.height / TotalRows
self.color_map = load_colormap()
self.len_cm = len(self.color_map)
def show_image(self):
for r in range(TotalRows):
for c in range(TotalColumns):
temp = self.temp[c][r]
if temp < 20: temp = 20
if temp > 40: temp = 39.9
color_i = int((temp - 20) / 20.0 * self.len_cm)
color = self.color_map[color_i]
#print(self.pixel_width)
x = c * self.pixel_width
y = r * self.pixel_height
cv2.rectangle(self.temp_im, (x, y), (x+self.pixel_width, y+self.pixel_height), color, -1)
cv2.imshow('frame', self.temp_im)
return True
def run(self):
#self.show_image()
temp = self.temp > 32
shape = temp.shape
rightmost = -1
topmost = 100
found = False
for i in range(shape[0]):
for j in range(shape[1]):
if temp[i][j]:
if i > rightmost: rightmost = i
if j < topmost: topmost = j
found = True
if found:
global last
h = 300
x = max(0, min(int(rightmost/32.0*640 - h/2), 640-h))
y = max(0, min(int(topmost/24.0*480)-30, 480-h))
cv2.rectangle(self.Image, (x, y), (x+h, y+h), (255,0,0), 3)
now = time.time()
if now-last > 1:
SendThread(self.Image[y:y+h, x:x+h]).start()
last = now
cv2.imshow('rgb', self.Image)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
return False
class GlimpseController:
def __init__(self):
self._reset()
self.lock = Lock()
def _reset(self):
self.imageQueue = []
self.tempQueue = []
self.haveImage = False
self.haveTemp = False
def event(self):
self.lock.acquire()
if self.haveImage and self.haveTemp:
im = self.imageQueue[-1]
temp = self.tempQueue[-1]
self._reset()
self.lock.release()
st = ShowThread(im, temp)
st.start()
else:
self.lock.release()
ctrl = GlimpseController()
data = 0x00
handle.claimInterface(0)
try:
res = handle.bulkWrite(0x01, m._pack(), 50)
except:
pass
thermal = Thermal(handle, ctrl)
thermal.start()
from pg_rgb import ImageThread
im = ImageThread(ctrl)
im.start()
thermal.join()
im.join()
handle.releaseInterface(0)
handle.close()
#cv2.waitKey()
cv2.destroyAllWindows()