add code
Browse files
app.py
CHANGED
|
@@ -4,30 +4,84 @@ import plotly.graph_objs as go
|
|
| 4 |
from scipy.ndimage import convolve
|
| 5 |
from gradio_imageslider import ImageSlider
|
| 6 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def readRAW(path):
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
reshaped = arr.reshape(*arr.shape[:-1], -1, 2)
|
| 13 |
-
|
| 14 |
-
swapped = reshaped[..., :, ::-1]
|
| 15 |
-
# 恢复原始形状
|
| 16 |
histogram_data = swapped.reshape(arr.shape)
|
| 17 |
-
|
|
|
|
| 18 |
mapping = [0, 4, 1, 5, 2, 6, 3, 7]
|
| 19 |
-
# 每组包含的行数
|
| 20 |
group_size = 8
|
| 21 |
-
num_groups = 12 # 96/8
|
| 22 |
-
|
| 23 |
-
# 创建一个用于存储结果的数组(也可以原地修改)
|
| 24 |
output = np.empty_like(histogram_data)
|
| 25 |
|
| 26 |
-
# 对每个 group 分别进行行重排
|
| 27 |
for g in range(num_groups):
|
| 28 |
start = g * group_size
|
| 29 |
end = start + group_size
|
| 30 |
-
output[start:end,:,:] = histogram_data[start:end,:,:][mapping,:,:]
|
| 31 |
|
| 32 |
return output
|
| 33 |
|
|
|
|
| 4 |
from scipy.ndimage import convolve
|
| 5 |
from gradio_imageslider import ImageSlider
|
| 6 |
import cv2
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# def readRAW(path):
|
| 10 |
+
|
| 11 |
+
# arr = np.fromfile(path, dtype=np.int16).reshape(96,240,256)
|
| 12 |
+
# # 将最后一维重塑为 (-1, 2),其中 -1 自动计算为 128
|
| 13 |
+
# reshaped = arr.reshape(*arr.shape[:-1], -1, 2)
|
| 14 |
+
# # 交换每一对中的两个元素
|
| 15 |
+
# swapped = reshaped[..., :, ::-1]
|
| 16 |
+
# # 恢复原始形状
|
| 17 |
+
# histogram_data = swapped.reshape(arr.shape)
|
| 18 |
+
# # 定义映射顺序:对每组8行进行调换
|
| 19 |
+
# mapping = [0, 4, 1, 5, 2, 6, 3, 7]
|
| 20 |
+
# # 每组包含的行数
|
| 21 |
+
# group_size = 8
|
| 22 |
+
# num_groups = 12 # 96/8
|
| 23 |
+
|
| 24 |
+
# # 创建一个用于存储结果的数组(也可以原地修改)
|
| 25 |
+
# output = np.empty_like(histogram_data)
|
| 26 |
+
|
| 27 |
+
# # 对每个 group 分别进行行重排
|
| 28 |
+
# for g in range(num_groups):
|
| 29 |
+
# start = g * group_size
|
| 30 |
+
# end = start + group_size
|
| 31 |
+
# output[start:end,:,:] = histogram_data[start:end,:,:][mapping,:,:]
|
| 32 |
+
|
| 33 |
+
# return output
|
| 34 |
+
|
| 35 |
+
def mipi_raw10_to_raw8_scaled(raw10_data):
|
| 36 |
+
raw10_data = np.frombuffer(raw10_data, dtype=np.uint8)
|
| 37 |
+
n_blocks = len(raw10_data) // 5
|
| 38 |
+
raw10_data = raw10_data[:n_blocks * 5].reshape(-1, 5)
|
| 39 |
+
|
| 40 |
+
p0 = (raw10_data[:, 0].astype(np.uint16) << 2) | ((raw10_data[:, 4] >> 0) & 0x03)
|
| 41 |
+
p1 = (raw10_data[:, 1].astype(np.uint16) << 2) | ((raw10_data[:, 4] >> 2) & 0x03)
|
| 42 |
+
p2 = (raw10_data[:, 2].astype(np.uint16) << 2) | ((raw10_data[:, 4] >> 4) & 0x03)
|
| 43 |
+
p3 = (raw10_data[:, 3].astype(np.uint16) << 2) | ((raw10_data[:, 4] >> 6) & 0x03)
|
| 44 |
+
|
| 45 |
+
raw8_data = np.empty((n_blocks * 4 * 2,), dtype=np.uint8)
|
| 46 |
+
raw8_data[0::8] = p0 & 0xFF
|
| 47 |
+
raw8_data[1::8] = p0 >> 8
|
| 48 |
+
raw8_data[2::8] = p1 & 0xFF
|
| 49 |
+
raw8_data[3::8] = p1 >> 8
|
| 50 |
+
raw8_data[4::8] = p2 & 0xFF
|
| 51 |
+
raw8_data[5::8] = p2 >> 8
|
| 52 |
+
raw8_data[6::8] = p3 & 0xFF
|
| 53 |
+
raw8_data[7::8] = p3 >> 8
|
| 54 |
+
|
| 55 |
+
return raw8_data.tobytes()
|
| 56 |
|
| 57 |
def readRAW(path):
|
| 58 |
+
filesize = os.path.getsize(path)
|
| 59 |
|
| 60 |
+
with open(path, "rb") as f:
|
| 61 |
+
raw_data = f.read()
|
| 62 |
+
|
| 63 |
+
# Case 1: 如果是 MIPI RAW10 格式,大小为 7,372,800 字节
|
| 64 |
+
if filesize == 7372800:
|
| 65 |
+
raw_data = mipi_raw10_to_raw8_scaled(raw_data)
|
| 66 |
+
|
| 67 |
+
# 转换为 int16 并 reshape
|
| 68 |
+
arr = np.frombuffer(raw_data, dtype=np.int16).reshape(96, 240, 256)
|
| 69 |
+
|
| 70 |
+
# Byte Swap: [x,y,256] → [x,y,128,2] → swap last dim → [x,y,256]
|
| 71 |
reshaped = arr.reshape(*arr.shape[:-1], -1, 2)
|
| 72 |
+
swapped = reshaped[..., ::-1]
|
|
|
|
|
|
|
| 73 |
histogram_data = swapped.reshape(arr.shape)
|
| 74 |
+
|
| 75 |
+
# Line remapping (每组8行:0,4,1,5,...)
|
| 76 |
mapping = [0, 4, 1, 5, 2, 6, 3, 7]
|
|
|
|
| 77 |
group_size = 8
|
| 78 |
+
num_groups = 12 # 96 / 8
|
|
|
|
|
|
|
| 79 |
output = np.empty_like(histogram_data)
|
| 80 |
|
|
|
|
| 81 |
for g in range(num_groups):
|
| 82 |
start = g * group_size
|
| 83 |
end = start + group_size
|
| 84 |
+
output[start:end, :, :] = histogram_data[start:end, :, :][mapping, :, :]
|
| 85 |
|
| 86 |
return output
|
| 87 |
|