Skip to content

Commit d4f5cb6

Browse files
authored
Merge pull request #10 from deckbsd/add-painani-image-decoder-module
add painani image decoder for both network and db
2 parents ddb7339 + 4069224 commit d4f5cb6

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from glouton.modules.observationModuleBase import ObservationModuleBase
2+
from glouton.modules.painaniDecoder import PainaniDecoder
3+
4+
5+
class NtPainaniImageDecoder(ObservationModuleBase):
6+
def __init__(self, wdir):
7+
ObservationModuleBase.__init__(self, wdir)
8+
self.decoder = PainaniDecoder()
9+
10+
def runAfterDownloadCompleted(self, full_path):
11+
self.decoder.DecodeImages(full_path, "data_*")

glouton/modules/painaniDecoder.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import glob
3+
import binascii
4+
5+
6+
class PainaniDecoder:
7+
def __init__(self):
8+
pass
9+
10+
def DecodeImages(self, full_path, file_pattern):
11+
files = glob.glob(os.path.join(full_path, file_pattern))
12+
mode = 'rb'
13+
images = {}
14+
chunks = []
15+
for file in files:
16+
with open(file, mode) as f:
17+
payload = bytearray(f.read())
18+
magic = bytearray([0x03, 0xf0, 0xcc, 0xcc])
19+
magic_id = payload[14:18]
20+
offset = 37
21+
if magic_id == magic:
22+
image_id = int.from_bytes(payload[25:28], 'big')
23+
if image_id not in images.keys():
24+
images[image_id] = {}
25+
chunk_id = int.from_bytes(payload[28:30], 'big')
26+
chunk_size = payload[36]
27+
chunk = payload[offset:offset + chunk_size]
28+
if len(chunk) == chunk_size:
29+
images[image_id][chunk_id] = chunk
30+
31+
for image_key, chunk in sorted(images.items()):
32+
image_name = os.path.join(
33+
full_path, "image_" + str(image_key) + ".j2c")
34+
print("writing image:", image_name)
35+
img_sz = 0
36+
chunks = 0
37+
f = open(image_name, 'w+b')
38+
for ch_key, ch_value in sorted(chunk.items()):
39+
chunks += 1
40+
binary_data = []
41+
binary_data = bytearray(ch_value)
42+
f.write(binary_data)
43+
img_sz += len(binary_data)
44+
print("Image", image_key, "size:",
45+
chunks, "chunks,", img_sz, "bytes")
46+
f.close()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from glouton.modules.telemetryModuleBase import TelemetryModuleBase
2+
from glouton.modules.painaniDecoder import PainaniDecoder
3+
import binascii
4+
import os
5+
6+
7+
class PainaniImageDecoder(TelemetryModuleBase):
8+
def __init__(self, wdir):
9+
TelemetryModuleBase.__init__(self, wdir)
10+
self.__decoder = PainaniDecoder()
11+
self.__frame_counter = 0
12+
13+
def runAfterDownload(self, frame, full_path, telemetry):
14+
timestamp = telemetry['timestamp']
15+
self.__frame_counter += 1
16+
file = os.path.join(full_path, timestamp.replace(
17+
':', '-') + "_" + str(self.__frame_counter) + ".frame")
18+
with open(file, 'wb') as f:
19+
f.write(binascii.unhexlify(frame))
20+
21+
def runAfterDownloadCompleted(self, full_path):
22+
self.__decoder.DecodeImages(full_path, "*.frame")

glouton/workers/endModuleWorker.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ def __init__(self, queue, download_end_status):
66
def execute(self):
77
self.__download_end_status.wait()
88
while self.__commands.empty() == False:
9-
#try:
10-
command = self.__commands.get()
11-
command.process()
12-
self.__commands.task_done()
13-
#except:
14-
# pass
9+
try:
10+
command = self.__commands.get()
11+
command.process()
12+
self.__commands.task_done()
13+
except:
14+
pass
1515

1616
self.__download_end_status.clear()

0 commit comments

Comments
 (0)