add code
Browse files
app.py
CHANGED
|
@@ -1,7 +1,30 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
|
| 4 |
+
def parse_bin_to_image(file, width, height, channels):
|
| 5 |
+
# 读取二进制数据
|
| 6 |
+
with open(file.name, 'rb') as f:
|
| 7 |
+
data = np.frombuffer(f.read(), dtype=np.float32)
|
| 8 |
+
|
| 9 |
+
expected_size = width * height * channels
|
| 10 |
+
if data.size != expected_size:
|
| 11 |
+
return f"❌ 文件大小不匹配:预期 {expected_size} float32,实际为 {data.size}"
|
| 12 |
+
|
| 13 |
+
# 重塑形状
|
| 14 |
+
|
| 15 |
+
img = data.reshape((height, width, channels)) # (H, W, C)
|
| 16 |
+
img = np.sum(img[...,:-2],axis=2)
|
| 17 |
+
return img
|
| 18 |
+
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=parse_bin_to_image,
|
| 21 |
+
inputs=[
|
| 22 |
+
gr.File(label="上传 .bin 文件", file_types=[".bin"]),
|
| 23 |
+
gr.Number(value=240, label="图像宽度 (W)"),
|
| 24 |
+
gr.Number(value=96, label="图像高度 (H)"),
|
| 25 |
+
gr.Number(value=256, label="通道数 (C, 1=灰度, 3=RGB)")
|
| 26 |
+
],
|
| 27 |
+
outputs=gr.Image(type="numpy", label="可视化图像")
|
| 28 |
+
)
|
| 29 |
|
|
|
|
| 30 |
demo.launch()
|