add code
Browse files
app.py
CHANGED
|
@@ -1,61 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import plotly.graph_objs as go
|
| 4 |
-
|
| 5 |
|
| 6 |
def readRAW(path):
|
| 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 |
# 解析bin文件,数据shape是 (H, W, T) = (96, 240, 256)
|
| 34 |
def load_bin(file):
|
| 35 |
-
|
| 36 |
# 默认显示一张 sum 图像
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
norm_img = (img - img.min()) / (img.max() + 1e-8)
|
| 39 |
img_uint8 = (norm_img * 255).astype(np.uint8)
|
| 40 |
|
| 41 |
img_zoomed = np.repeat(np.repeat(img_uint8, 4, axis=0), 4, axis=1) # (96→192, 240→480)
|
| 42 |
|
| 43 |
-
return img_zoomed,
|
| 44 |
|
| 45 |
|
| 46 |
-
def plot_pixel_histogram(evt: gr.SelectData,
|
| 47 |
# print("evt:", evt)
|
| 48 |
x, y = evt.index # Gradio SelectData 对象
|
| 49 |
x = x // 4
|
| 50 |
y = y // 4
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
fig = go.Figure()
|
| 53 |
-
fig.add_trace(go.Scatter(y=
|
|
|
|
|
|
|
| 54 |
fig.update_layout(
|
| 55 |
-
title=f"Pixel ({x}, {y})
|
| 56 |
xaxis_title="帧索引 (T)",
|
| 57 |
-
|
|
|
|
| 58 |
)
|
|
|
|
|
|
|
| 59 |
return fig
|
| 60 |
|
| 61 |
|
|
@@ -65,9 +87,10 @@ with gr.Blocks() as demo:
|
|
| 65 |
file_input = gr.File(label="上传 .raw/.bin 文件", file_types=[".raw", ".bin"])
|
| 66 |
image_display = gr.Image(interactive=True, label="点击像素显示强度曲线")
|
| 67 |
histogram = gr.Plot(label="像素强度曲线")
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
file_input.change(load_bin, inputs=file_input, outputs=[image_display,
|
| 71 |
-
image_display.select(plot_pixel_histogram, inputs=
|
| 72 |
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import plotly.graph_objs as go
|
| 4 |
+
from scipy.ndimage import convolve
|
| 5 |
|
| 6 |
def readRAW(path):
|
| 7 |
|
| 8 |
+
arr = np.fromfile(path, dtype=np.int16).reshape(96,240,256)
|
| 9 |
+
# 将最后一维重塑为 (-1, 2),其中 -1 自动计算为 128
|
| 10 |
+
reshaped = arr.reshape(*arr.shape[:-1], -1, 2)
|
| 11 |
+
# 交换每一对中的两个元素
|
| 12 |
+
swapped = reshaped[..., :, ::-1]
|
| 13 |
+
# 恢复原始形状
|
| 14 |
+
histogram_data = swapped.reshape(arr.shape)
|
| 15 |
+
# 定义映射顺序:对每组8行进行调换
|
| 16 |
+
mapping = [0, 4, 1, 5, 2, 6, 3, 7]
|
| 17 |
+
# 每组包含的行数
|
| 18 |
+
group_size = 8
|
| 19 |
+
num_groups = 12 # 96/8
|
| 20 |
|
| 21 |
+
# 创建一个用于存储结果的数组(也可以原地修改)
|
| 22 |
+
output = np.empty_like(histogram_data)
|
| 23 |
|
| 24 |
+
# 对每个 group 分别进行行重排
|
| 25 |
+
for g in range(num_groups):
|
| 26 |
+
start = g * group_size
|
| 27 |
+
end = start + group_size
|
| 28 |
+
output[start:end,:,:] = histogram_data[start:end,:,:][mapping,:,:]
|
| 29 |
|
| 30 |
+
return output
|
| 31 |
|
| 32 |
|
| 33 |
# 解析bin文件,数据shape是 (H, W, T) = (96, 240, 256)
|
| 34 |
def load_bin(file):
|
| 35 |
+
raw_hist = readRAW(file.name)
|
| 36 |
# 默认显示一张 sum 图像
|
| 37 |
+
|
| 38 |
+
multishot = (raw_hist[...,254]*1024 + raw_hist[...,255])
|
| 39 |
+
normalize_data = 1 / multishot
|
| 40 |
+
nor_hist = (raw_hist) * normalize_data[...,np.newaxis]
|
| 41 |
+
|
| 42 |
+
img = np.sum(nor_hist[:, :, :-2], axis=2)
|
| 43 |
norm_img = (img - img.min()) / (img.max() + 1e-8)
|
| 44 |
img_uint8 = (norm_img * 255).astype(np.uint8)
|
| 45 |
|
| 46 |
img_zoomed = np.repeat(np.repeat(img_uint8, 4, axis=0), 4, axis=1) # (96→192, 240→480)
|
| 47 |
|
| 48 |
+
return img_zoomed, raw_hist, nor_hist
|
| 49 |
|
| 50 |
|
| 51 |
+
def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
|
| 52 |
# print("evt:", evt)
|
| 53 |
x, y = evt.index # Gradio SelectData 对象
|
| 54 |
x = x // 4
|
| 55 |
y = y // 4
|
| 56 |
+
raw_values = raw_hist[y, x, :]
|
| 57 |
+
nor_values = nor_hist[y, x, :]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# fig = go.Figure()
|
| 62 |
+
# fig.add_trace(go.Scatter(y=raw_values, mode="lines+markers"))
|
| 63 |
+
# fig.update_layout(
|
| 64 |
+
# title=f"Pixel ({x}, {y}) 在所有 {values.shape[0]} 帧的强度变化",
|
| 65 |
+
# xaxis_title="帧索引 (T)",
|
| 66 |
+
# yaxis_title="强度值",
|
| 67 |
+
# )
|
| 68 |
+
|
| 69 |
fig = go.Figure()
|
| 70 |
+
fig.add_trace(go.Scatter(y=raw_values, mode="lines", name="原始值", yaxis="y1"))
|
| 71 |
+
fig.add_trace(go.Scatter(y=nor_values, mode="lines", name="归一化", yaxis="y2"))
|
| 72 |
+
|
| 73 |
fig.update_layout(
|
| 74 |
+
title=f"Pixel ({x}, {y}) 双 Y 轴示意图",
|
| 75 |
xaxis_title="帧索引 (T)",
|
| 76 |
+
yaxis=dict(title="原始值", titlefont=dict(color="blue"), tickfont=dict(color="blue")),
|
| 77 |
+
yaxis2=dict(title="归一化", overlaying="y", side="right", titlefont=dict(color="red"), tickfont=dict(color="red")),
|
| 78 |
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
return fig
|
| 82 |
|
| 83 |
|
|
|
|
| 87 |
file_input = gr.File(label="上传 .raw/.bin 文件", file_types=[".raw", ".bin"])
|
| 88 |
image_display = gr.Image(interactive=True, label="点击像素显示强度曲线")
|
| 89 |
histogram = gr.Plot(label="像素强度曲线")
|
| 90 |
+
raw_hist = gr.State()
|
| 91 |
+
nor_hist = gr.State()
|
| 92 |
|
| 93 |
+
file_input.change(load_bin, inputs=file_input, outputs=[image_display, raw_hist, nor_hist])
|
| 94 |
+
image_display.select(plot_pixel_histogram, inputs=[ raw_hist, nor_hist], outputs=histogram)
|
| 95 |
|
| 96 |
demo.launch()
|