JohnChiu commited on
Commit
2d07b10
·
1 Parent(s): 2972fa3

Add application file

Browse files
Files changed (1) hide show
  1. app.py +48 -67
app.py CHANGED
@@ -4,49 +4,22 @@ import plotly.graph_objs as go
4
  from scipy.ndimage import convolve
5
  from gradio_imageslider import ImageSlider
6
 
7
- def readRAW(path):
8
-
9
- arr = np.fromfile(path, dtype=np.int16).reshape(96,240,256)
10
- # 将最后一维重塑为 (-1, 2),其中 -1 自动计算为 128
11
- reshaped = arr.reshape(*arr.shape[:-1], -1, 2)
12
- # 交换每一对中的两个元素
13
- swapped = reshaped[..., :, ::-1]
14
- # 恢复原始形状
15
- histogram_data = swapped.reshape(arr.shape)
16
- # 定义映射顺序:对每组8行进行调换
17
- mapping = [0, 4, 1, 5, 2, 6, 3, 7]
18
- # 每组包含的行数
19
- group_size = 8
20
- num_groups = 12 # 96/8
21
-
22
- # 创建一个用于存储结果的数组(也可以原地修改)
23
- output = np.empty_like(histogram_data)
24
-
25
- # 对每个 group 分别进行行重排
26
- for g in range(num_groups):
27
- start = g * group_size
28
- end = start + group_size
29
- output[start:end,:,:] = histogram_data[start:end,:,:][mapping,:,:]
30
-
31
- return output
32
-
33
 
34
- # 解析bin文件,数据shape是 (H, W, T) = (96, 240, 256)
35
  def load_bin(file):
36
  raw_hist = readRAW(file.name)
37
- # 默认显示一张 sum 图像
38
 
39
- multishot = (raw_hist[...,254]*1024 + raw_hist[...,255])
40
- normalize_data = 1 / multishot * 12000
41
- nor_hist = (raw_hist) * normalize_data[...,np.newaxis]
42
 
43
  img = np.sum(nor_hist[:, :, :-2], axis=2)
44
  norm_img = (img - img.min()) / (img.max() + 1e-8)
45
  img_uint8 = (norm_img * 255).astype(np.uint8)
 
46
 
47
- img_zoomed = np.repeat(np.repeat(img_uint8, 4, axis=0), 4, axis=1) # (96→192, 240→480)
48
-
49
- return img_zoomed, raw_hist, nor_hist
50
 
51
 
52
  def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
@@ -66,32 +39,8 @@ def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
66
  xaxis_title="帧索引 (T)",
67
  yaxis_title="强度值",
68
  )
69
-
70
- # fig = go.Figure()
71
- # fig.add_trace(go.Scatter(y=raw_values, mode="lines", name="原始值", yaxis="y1"))
72
- # fig.add_trace(go.Scatter(y=nor_values, mode="lines", name="归一化", yaxis="y2"))
73
-
74
- # fig.update_layout(
75
- # title=f"Pixel ({x}, {y}) 双 Y 轴示意图",
76
- # xaxis_title="帧索引 (T)",
77
-
78
- # yaxis=dict(
79
- # title=dict(text="原始值", font=dict(color="blue")),
80
- # tickfont=dict(color="blue")
81
- # ),
82
-
83
- # yaxis2=dict(
84
- # title=dict(text="归一化", font=dict(color="red")),
85
- # tickfont=dict(color="red"),
86
- # overlaying="y",
87
- # side="right"
88
- # )
89
- # )
90
-
91
-
92
  return fig
93
 
94
-
95
  def to_uint8_image(arr):
96
  norm = (arr - arr.min()) / (arr.ptp() + 1e-8)
97
  return (norm * 255).astype(np.uint8)
@@ -129,24 +78,56 @@ def plot_depth(nor_hist):
129
  return [(img_tof, img_filter)]
130
 
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  with gr.Blocks() as demo:
133
  gr.Markdown("## 上传 96×240×256 int16 `.bin/.raw` 文件,点击图像像素查看该像素的 256 帧直方图")
134
-
135
  file_input = gr.File(label="上传 .raw/.bin 文件", file_types=[".raw", ".bin"])
136
  image_display = gr.Image(interactive=True, label="点击像素显示强度曲线")
137
 
138
- depth_image_slider = ImageSlider(label="Filter Depth Map with Slider View", elem_id='img-display-output', position=0.5,)
139
-
140
-
141
 
142
  histogram = gr.Plot(label="像素强度曲线")
143
  raw_hist = gr.State()
144
  nor_hist = gr.State()
145
 
 
 
 
 
 
 
146
 
147
- file_input.change(load_bin, inputs=file_input, outputs=[image_display, raw_hist, nor_hist])
148
- file_input.change(plot_depth, inputs=nor_hist, outputs=[depth_image_slider])
149
-
150
- image_display.select(plot_pixel_histogram, inputs=[ raw_hist, nor_hist], outputs=histogram)
 
151
 
152
- demo.launch()
 
4
  from scipy.ndimage import convolve
5
  from gradio_imageslider import ImageSlider
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
8
  def load_bin(file):
9
  raw_hist = readRAW(file.name)
 
10
 
11
+ multishot = (raw_hist[..., 254] * 1024 + raw_hist[..., 255]).astype(np.float32)
12
+ normalize_data = np.where(multishot != 0, 12000 / multishot, 0)
13
+ nor_hist = raw_hist * normalize_data[..., np.newaxis]
14
 
15
  img = np.sum(nor_hist[:, :, :-2], axis=2)
16
  norm_img = (img - img.min()) / (img.max() + 1e-8)
17
  img_uint8 = (norm_img * 255).astype(np.uint8)
18
+ img_zoomed = np.repeat(np.repeat(img_uint8, 4, axis=0), 4, axis=1)
19
 
20
+ depth_slider_imgs = plot_depth(nor_hist) # 👈 直接在这里计算
21
+
22
+ return img_zoomed, raw_hist, nor_hist, depth_slider_imgs
23
 
24
 
25
  def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
 
39
  xaxis_title="帧索引 (T)",
40
  yaxis_title="强度值",
41
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  return fig
43
 
 
44
  def to_uint8_image(arr):
45
  norm = (arr - arr.min()) / (arr.ptp() + 1e-8)
46
  return (norm * 255).astype(np.uint8)
 
78
  return [(img_tof, img_filter)]
79
 
80
 
81
+ def readRAW(path):
82
+
83
+ arr = np.fromfile(path, dtype=np.int16).reshape(96,240,256)
84
+ # 将最后一维重塑为 (-1, 2),其中 -1 自动计算为 128
85
+ reshaped = arr.reshape(*arr.shape[:-1], -1, 2)
86
+ # 交换每一对中的两个元素
87
+ swapped = reshaped[..., :, ::-1]
88
+ # 恢复原始形状
89
+ histogram_data = swapped.reshape(arr.shape)
90
+ # 定义映射顺序:对每组8行进行调换
91
+ mapping = [0, 4, 1, 5, 2, 6, 3, 7]
92
+ # 每组包含的行数
93
+ group_size = 8
94
+ num_groups = 12 # 96/8
95
+
96
+ # 创建一个用于存储结果的数组(也可以原地修改)
97
+ output = np.empty_like(histogram_data)
98
+
99
+ # 对每个 group 分别进行行重排
100
+ for g in range(num_groups):
101
+ start = g * group_size
102
+ end = start + group_size
103
+ output[start:end,:,:] = histogram_data[start:end,:,:][mapping,:,:]
104
+
105
+ return output
106
+
107
+
108
  with gr.Blocks() as demo:
109
  gr.Markdown("## 上传 96×240×256 int16 `.bin/.raw` 文件,点击图像像素查看该像素的 256 帧直方图")
110
+
111
  file_input = gr.File(label="上传 .raw/.bin 文件", file_types=[".raw", ".bin"])
112
  image_display = gr.Image(interactive=True, label="点击像素显示强度曲线")
113
 
114
+ depth_image_slider = ImageSlider(label="Filter Depth Map with Slider View", elem_id='img-display-output', position=0.5)
 
 
115
 
116
  histogram = gr.Plot(label="像素强度曲线")
117
  raw_hist = gr.State()
118
  nor_hist = gr.State()
119
 
120
+ # 单一入口统一触发
121
+ file_input.change(
122
+ load_bin,
123
+ inputs=file_input,
124
+ outputs=[image_display, raw_hist, nor_hist, depth_image_slider]
125
+ )
126
 
127
+ image_display.select(
128
+ plot_pixel_histogram,
129
+ inputs=[raw_hist, nor_hist],
130
+ outputs=histogram
131
+ )
132
 
133
+ demo.launch()