치춘짱베리굿나이스

200720 bin -> RGB json / csv 변환기 본문

Python/기타

200720 bin -> RGB json / csv 변환기

치춘 2021. 2. 8. 20:14

# -*- coding:cp949 -*-
import csv
import os
import json
import numpy as np
import struct
def getrgb888(r, g, b):
i = 1
if r >= g and r >= b:
i = r / 255 + 1
elif g >= r and g >= b:
i = g / 255 + 1
elif b >= g and b >= r:
i = b / 255 + 1
if i != 0:
r_ = r / i
g_ = g / i
b_ = b / i
else:
r_ = r
g_ = g
b_ = b
if r_ > 30:
r_ = r_ - 30
if g_ > 30:
g_ = g_ - 30
if b > 30:
b_ = b_ - 30
r_ = r_ * 255 / 225
g_ = g_ * 255 / 225
b_ = b_ * 255 / 225
if r_ > 255:
r_ = 255
if g_ > 255:
g_ = 255
if b_ > 255:
b_ = 255
rgblist = [r_, g_, b_]
return rgblist
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
direction = input("input the foldername : ")
curr_path = os.path.join(os.path.abspath(os.getcwd()), direction)
txt_path = os.path.join(os.path.abspath(os.getcwd()), 'tmp_filelist.txt')
with open(txt_path, 'wt') as f:
for root, dirs, files in os.walk(curr_path):
if files:
for file in files:
if file.find('.bin') > 0:
filename = os.path.join(root, file)
f.write(filename + '\n')
with open(txt_path, 'r') as file:
lines = file.readlines()
lines.sort()
for line in lines:
bfname = ''.join([os.path.splitext(line)[0], '.bin'])
tfname = ''.join([os.path.splitext(line)[0], '.json'])
csvname = ''.join([os.path.splitext(line)[0], '.csv'])
with open(bfname, 'rb') as BF:
with open(tfname, 'w') as TF:
CF = open(csvname, 'w', newline='')
wr = csv.writer(CF)
wr.writerow(['index', 'AcX', 'AcY', 'AcZ', 'tmp', 'GyX', 'GyY', 'GyZ', 'C', 'R', 'G', 'B', 'PPM'])
bin_data = {}
j = 0
while True:
temp = BF.read(1) # 0x02 (Start of File)
if not temp: break
m_AcX = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
m_AcY = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
m_AcZ = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
tmp = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
m_GyX = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
m_GyY = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
m_GyZ = np.int16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
c_C = np.uint16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
c_R = np.uint16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
c_G = np.uint16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
c_B = np.uint16(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8))
PPM = np.int(0x0000 | int(struct.unpack('B', BF.read(1))[0]) |
(int(struct.unpack('B', BF.read(1))[0]) << 8) |
(int(struct.unpack('B', BF.read(1))[0]) << 16) |
(int(struct.unpack('B', BF.read(1))[0]) << 24))
rgb = getrgb888(c_R, c_G, c_B)
bin_data[j] = {
"index": j,
"m_AcX": m_AcX,
"m_AcY": m_AcY,
"m_AcZ": m_AcZ,
"tmp": tmp,
"m_GyX": m_GyX,
"m_GyY": m_GyY,
"m_GyZ": m_GyZ,
"C": np.uint16(c_C),
"R": np.uint16(rgb[0]),
"G": np.uint16(rgb[1]),
"B": np.uint16(rgb[2]),
"PPM": PPM
}
temp = BF.read(1) # 0x03 (End of File)
wr.writerow([j, m_AcX, m_AcY, m_AcZ, tmp, m_GyX, m_GyY, m_GyZ, np.uint16(c_C), np.uint16(rgb[0]), np.uint16(rgb[1]), np.uint16(rgb[2]), PPM])
j += 1
TF_dump = json.dumps(bin_data, cls=NumpyEncoder)
json.dump(TF_dump, TF)
CF.close()
view raw binread.py hosted with ❤ by GitHub

폴더 내에 존재하는 모든 bin 파일을 찾아서 RGB값 / 기울기값 파싱하여 보기좋게 json csv로 저장하는 코드

졸업작품의 일환

하나하나 16진수 변환해서 보기 불편했기 때문

'Python > 기타' 카테고리의 다른 글

201125 따봉박는 치춘 (Discord bot)  (0) 2021.02.08
200720 음성 파일 Text 변환 (feat Google STT)  (0) 2021.02.08
191123 OpenCV 갖고놀기  (0) 2021.02.07
Comments