JohnChiu commited on
Commit
ec63f19
·
1 Parent(s): 94f4bac

support packed

Browse files
Files changed (1) hide show
  1. app.py +74 -14
app.py CHANGED
@@ -2,20 +2,55 @@ import gradio as gr
2
  import numpy as np
3
  import plotly.graph_objs as go
4
  from scipy.ndimage import convolve
5
-
6
 
7
  def readRAW(path):
8
 
9
- output = np.fromfile(path, dtype=np.int16).reshape(31,40,64)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # output = np.fromfile(path, dtype=np.int16).reshape(31,40,64*2)
11
  # output = np.fromfile(path, dtype=np.int16).reshape(30,40,64)
12
 
13
- return output
14
 
15
 
16
  def load_bin(file):
 
 
 
 
17
  # raw_hist = readRAW(file.name)[1:,...].astype(np.float32)
18
  raw_hist = readRAW(file.name).astype(np.float32)
 
 
 
 
 
 
19
  # raw_hist = readRAW(file.name)
20
  # 默认显示一张 sum 图像
21
 
@@ -28,12 +63,19 @@ def load_bin(file):
28
  # nor_hist = (raw_hist)
29
 
30
  img = np.sum(nor_hist[1:, :, :-2], axis=2)
31
- norm_img = (img - img.min()) / (img.max() + 1e-8)
 
32
  img_uint8 = (norm_img * 255).astype(np.uint8)
33
 
34
- img_zoomed = np.repeat(np.repeat(img_uint8, 16, axis=0), 16, axis=1)
 
 
 
 
 
 
35
 
36
- return img_zoomed, raw_hist, nor_hist
37
 
38
 
39
  def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
@@ -41,8 +83,20 @@ def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
41
  x, y = evt.index # Gradio SelectData 对象
42
  x = x // 16
43
  y = y // 16
44
- raw_values = raw_hist[y, x, :]
45
- nor_values = nor_hist[y, x, :]
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  vctEmbd = raw_hist[:1,:,:].flatten().astype(np.int32) >> 2
48
  fRX_Temp = (vctEmbd[15] << 3) + vctEmbd[14]
@@ -53,17 +107,20 @@ def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
53
 
54
  # fTx_Temp = float(vctEmbd[61]+((vctEmbd[63] & 0xc0) << 2)) * 0.178 - 38.18
55
  # LDVCC = ((((vctEmbd[63]&0x30)<<4) + vctEmbd[60] - 110) * 13.7 + 5000) / 1000
56
-
 
57
  fig = go.Figure()
58
  fig.add_trace(go.Scatter(y=raw_values, mode="lines+markers"))
59
  fig.update_layout(
60
  title=f"Pixel ({x}, {y}) 在所有 {raw_values.shape[0]} 帧的强度变化 {f'RX: {fRX_Temp} °C'} {f'TX: {fTx_Temp:.2f} °C'} {f'LDVCC: {LDVCC:.2f} V'} {f'BVD: {BVD} V'}",
61
  xaxis_title="帧索引 (T)",
62
  yaxis_title="强度值",
 
 
63
  )
64
 
65
 
66
- return fig
67
 
68
  # def plot_depth(nor_hist):
69
 
@@ -100,14 +157,17 @@ with gr.Blocks() as demo:
100
  gr.Markdown("## 上传 31,40,64 int16 `.bin/.raw` 文件,点击图像像素查看该像素的 64 帧直方图")
101
 
102
  file_input = gr.File(label="上传 .raw/.bin 文件", file_types=[".raw", ".bin"])
103
- image_display = gr.Image(interactive=True, label="点击像素显示强度曲线")
 
104
 
105
  histogram = gr.Plot(label="像素强度曲线")
106
  raw_hist = gr.State()
107
  nor_hist = gr.State()
 
108
 
109
- file_input.change(load_bin, inputs=file_input, outputs=[image_display, raw_hist, nor_hist])
110
 
111
- image_display.select(plot_pixel_histogram, inputs=[ raw_hist, nor_hist], outputs=histogram)
112
 
113
- demo.launch(share=True)
 
 
2
  import numpy as np
3
  import plotly.graph_objs as go
4
  from scipy.ndimage import convolve
5
+ import os
6
 
7
  def readRAW(path):
8
 
9
+ filesize = os.path.getsize(path)
10
+ print(filesize)
11
+ if filesize == 31*40*64*2:
12
+ output = np.fromfile(path, dtype=np.int16)
13
+ else:
14
+ with open(path, "rb") as f:
15
+ raw_data = f.read()
16
+
17
+
18
+ raw10 = np.frombuffer(raw_data, dtype=np.uint8)
19
+ n_blocks = raw10.shape[0] // 5
20
+
21
+ raw10 = raw10[:n_blocks * 5].reshape(-1, 5)
22
+
23
+ B0 = raw10[:, 0].astype(np.uint16)
24
+ B1 = raw10[:, 1].astype(np.uint16)
25
+ B2 = raw10[:, 2].astype(np.uint16)
26
+ B3 = raw10[:, 3].astype(np.uint16)
27
+ B4 = raw10[:, 4]
28
+
29
+ p0 = (B0 << 2) | ((B4 >> 0) & 0x03)
30
+ p1 = (B1 << 2) | ((B4 >> 2) & 0x03)
31
+ p2 = (B2 << 2) | ((B4 >> 4) & 0x03)
32
+ p3 = (B3 << 2) | ((B4 >> 6) & 0x03)
33
+
34
+ output = np.stack([p0, p1, p2, p3], axis=1).flatten()
35
  # output = np.fromfile(path, dtype=np.int16).reshape(31,40,64*2)
36
  # output = np.fromfile(path, dtype=np.int16).reshape(30,40,64)
37
 
38
+ return output.reshape(31,40,64)
39
 
40
 
41
  def load_bin(file):
42
+
43
+
44
+
45
+
46
  # raw_hist = readRAW(file.name)[1:,...].astype(np.float32)
47
  raw_hist = readRAW(file.name).astype(np.float32)
48
+
49
+ print("raw_hist shape:", raw_hist[0,0,:])
50
+ # raw_hist = raw_hist[::-1, ::-1, :]
51
+
52
+ print("raw_hist shape:", raw_hist[0,0,:])
53
+
54
  # raw_hist = readRAW(file.name)
55
  # 默认显示一张 sum 图像
56
 
 
63
  # nor_hist = (raw_hist)
64
 
65
  img = np.sum(nor_hist[1:, :, :-2], axis=2)
66
+ img = np.log(img +1)
67
+ norm_img = (img - img.min()) / (img.max())
68
  img_uint8 = (norm_img * 255).astype(np.uint8)
69
 
70
+ img_tc_zoomed = np.repeat(np.repeat(img_uint8, 16, axis=0), 16, axis=1)
71
+
72
+
73
+ img = np.argmax(nor_hist[1:, :, 5:-2], axis=2)
74
+ norm_img = (img - img.min()) / (img.max() + 1e-8)
75
+ img_uint8 = (norm_img * 255).astype(np.uint8)
76
+ img_tof_zoomed = np.repeat(np.repeat(img_uint8, 16, axis=0), 16, axis=1)
77
 
78
+ return img_tc_zoomed,img_tof_zoomed, raw_hist, nor_hist
79
 
80
 
81
  def plot_pixel_histogram(evt: gr.SelectData, raw_hist, nor_hist):
 
83
  x, y = evt.index # Gradio SelectData 对象
84
  x = x // 16
85
  y = y // 16
86
+ raw_values = raw_hist[y+1, x, :]
87
+
88
+ tof = np.argmax(nor_hist[y+1, x, :-2])
89
+ range = 5
90
+ sim_values = nor_hist[y+1, x, tof-range:tof+range+1]
91
+ histogram_sim = raw_hist[1:, :, tof-range:tof+range+1]
92
+ print(sim_values.shape, histogram_sim.shape)
93
+
94
+ img = np.tensordot(sim_values,histogram_sim, axes=(0, 2))
95
+
96
+ # img = np.log(img +1)
97
+ norm_img = (img - img.min()) / (img.max() + 1e-8)
98
+ img_uint8 = (norm_img * 255).astype(np.uint8)
99
+ img_tof_zoomed = np.repeat(np.repeat(img_uint8, 16, axis=0), 16, axis=1)
100
 
101
  vctEmbd = raw_hist[:1,:,:].flatten().astype(np.int32) >> 2
102
  fRX_Temp = (vctEmbd[15] << 3) + vctEmbd[14]
 
107
 
108
  # fTx_Temp = float(vctEmbd[61]+((vctEmbd[63] & 0xc0) << 2)) * 0.178 - 38.18
109
  # LDVCC = ((((vctEmbd[63]&0x30)<<4) + vctEmbd[60] - 110) * 13.7 + 5000) / 1000
110
+ y_min = np.min(raw_values[:-2]) - 10
111
+ y_max = np.max(raw_values[:-2]) + 10
112
  fig = go.Figure()
113
  fig.add_trace(go.Scatter(y=raw_values, mode="lines+markers"))
114
  fig.update_layout(
115
  title=f"Pixel ({x}, {y}) 在所有 {raw_values.shape[0]} 帧的强度变化 {f'RX: {fRX_Temp} °C'} {f'TX: {fTx_Temp:.2f} °C'} {f'LDVCC: {LDVCC:.2f} V'} {f'BVD: {BVD} V'}",
116
  xaxis_title="帧索引 (T)",
117
  yaxis_title="强度值",
118
+ yaxis=dict(
119
+ range=[y_min, y_max]) # Set the min and max for y-axis
120
  )
121
 
122
 
123
+ return fig, img_tof_zoomed
124
 
125
  # def plot_depth(nor_hist):
126
 
 
157
  gr.Markdown("## 上传 31,40,64 int16 `.bin/.raw` 文件,点击图像像素查看该像素的 64 帧直方图")
158
 
159
  file_input = gr.File(label="上传 .raw/.bin 文件", file_types=[".raw", ".bin"])
160
+ image_tc_display = gr.Image(interactive=True, label="tc")
161
+ image_tof_display = gr.Image(interactive=True, label="tof")
162
 
163
  histogram = gr.Plot(label="像素强度曲线")
164
  raw_hist = gr.State()
165
  nor_hist = gr.State()
166
+ image_sim_display = gr.Image(interactive=True, label="sim")
167
 
168
+ file_input.change(load_bin, inputs=file_input, outputs=[image_tc_display, image_tof_display, raw_hist, nor_hist])
169
 
170
+ image_tof_display.select(plot_pixel_histogram, inputs=[ raw_hist, nor_hist], outputs=[histogram,image_sim_display])
171
 
172
+ # demo.launch(share=True)
173
+ demo.launch(share=False)