Mulinjushi commited on
Commit
b1a0748
·
verified ·
1 Parent(s): 38289f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
app.py CHANGED
@@ -1,76 +1,77 @@
1
  import gradio as gr
2
  import os
3
 
4
- # 1. 定义文件夹路径
5
- IMG_DIR = "example_image" # 你的图片文件夹名
6
- MODEL_DIR = "f3c" # 你的模型文件夹名
7
 
8
- # 2. 自动扫描并匹配文件
9
  examples = []
10
 
11
- # 检查文件夹是否存在,避免报错
12
  if os.path.exists(IMG_DIR) and os.path.exists(MODEL_DIR):
13
- # 获取所有 png 图片
14
  image_files = sorted([f for f in os.listdir(IMG_DIR) if f.endswith('.png')])
15
 
16
  for img_file in image_files:
17
- # 获取不带后缀的文件名 (例如 "6" "typical_creature_dragon")
18
  base_name = os.path.splitext(img_file)[0]
19
 
20
- # 构造对应的 GLB 文件名:规则是 "文件名_demo_trellis.glb"
21
  expected_glb_name = f"{base_name}_demo_trellis.glb"
22
 
23
- # 拼接完整路径
24
  img_path = os.path.join(IMG_DIR, img_file)
25
  glb_path = os.path.join(MODEL_DIR, expected_glb_name)
26
 
27
- # 关键步骤:只有当对应的 3D 文件确实存在时,才添加到展示列表
28
  if os.path.exists(glb_path):
29
  examples.append({
30
  "image": img_path,
31
  "model": glb_path,
32
- "label": base_name # 图片下方的文字标签
33
  })
34
  else:
35
- # 如果没找到对应的模型,可以在后台打印一下,方便排查
36
- print(f"⚠️ 未找到匹配模型: 图片 {img_file} -> 缺少 {expected_glb_name}")
37
 
38
- print(f"✅ 成功加载了 {len(examples)} 组数据")
39
 
40
- # 3. 定义点击事件:返回对应的模型路径
41
  def display_model(evt: gr.SelectData):
42
  if evt.index < len(examples):
43
  return examples[evt.index]["model"]
44
  return None
45
 
46
- # 4. 构建界面
47
  with gr.Blocks(title="3D Model Viewer") as demo:
48
  gr.Markdown("## 3D Accelerated Instances Showcase")
49
- gr.Markdown(f"点击左侧图片,右侧查看 3D 效果。共加载 {len(examples)} 个实例。")
50
 
51
  with gr.Row():
52
- # 左侧:图片画廊
53
  with gr.Column(scale=1):
54
  gallery = gr.Gallery(
55
  value=[item["image"] for item in examples],
56
  label="Instance Gallery",
57
- columns=3, # 一行显示3张图
58
- height=800, # 设置高度,图片多时会出现滚动条
59
  object_fit="contain",
60
- allow_preview=False # 禁止点击放大图片
61
  )
62
 
63
- # 右侧:3D 展示区
64
  with gr.Column(scale=2):
65
  model_3d = gr.Model3D(
66
- clear_color=[0.2, 0.2, 0.2, 1.0], # 深灰色背景,看模型更清晰
67
  label="3D Interactive View",
68
- camera_position=(0, 0, 2.5) # 调整相机距离
69
  )
70
 
71
- # 绑定交互
72
  gallery.select(fn=display_model, outputs=model_3d)
73
 
74
- # 启动
75
  if __name__ == "__main__":
 
76
  demo.launch()
 
1
  import gradio as gr
2
  import os
3
 
4
+ # 1. Define directory paths
5
+ IMG_DIR = "example_image" # Directory for images
6
+ MODEL_DIR = "f3c" # Directory for 3D models
7
 
8
+ # 2. Automatically scan and match files
9
  examples = []
10
 
11
+ # Check if directories exist to avoid errors
12
  if os.path.exists(IMG_DIR) and os.path.exists(MODEL_DIR):
13
+ # Get all png images, sorted
14
  image_files = sorted([f for f in os.listdir(IMG_DIR) if f.endswith('.png')])
15
 
16
  for img_file in image_files:
17
+ # Get filename without extension (e.g., "6" or "typical_creature_dragon")
18
  base_name = os.path.splitext(img_file)[0]
19
 
20
+ # Construct the expected GLB filename: rule is "filename_demo_trellis.glb"
21
  expected_glb_name = f"{base_name}_demo_trellis.glb"
22
 
23
+ # Construct full paths
24
  img_path = os.path.join(IMG_DIR, img_file)
25
  glb_path = os.path.join(MODEL_DIR, expected_glb_name)
26
 
27
+ # Critical step: Only add to the list if the corresponding 3D file actually exists
28
  if os.path.exists(glb_path):
29
  examples.append({
30
  "image": img_path,
31
  "model": glb_path,
32
+ "label": base_name # Label displayed below the image
33
  })
34
  else:
35
+ # Print to logs if a matching model is missing (for debugging)
36
+ print(f"⚠️ Matching model not found: Image {img_file} -> Missing {expected_glb_name}")
37
 
38
+ print(f"✅ Successfully loaded {len(examples)} pairs of data")
39
 
40
+ # 3. Define click event: Return the corresponding model path
41
  def display_model(evt: gr.SelectData):
42
  if evt.index < len(examples):
43
  return examples[evt.index]["model"]
44
  return None
45
 
46
+ # 4. Build the interface
47
  with gr.Blocks(title="3D Model Viewer") as demo:
48
  gr.Markdown("## 3D Accelerated Instances Showcase")
49
+ gr.Markdown(f"Click on an image on the left to view the 3D effect on the right. Loaded {len(examples)} instances in total.")
50
 
51
  with gr.Row():
52
+ # Left column: Image Gallery
53
  with gr.Column(scale=1):
54
  gallery = gr.Gallery(
55
  value=[item["image"] for item in examples],
56
  label="Instance Gallery",
57
+ columns=3, # Number of images per row
58
+ height=800, # Set height (scrollable if many images)
59
  object_fit="contain",
60
+ allow_preview=False # Disable click-to-zoom
61
  )
62
 
63
+ # Right column: 3D Viewer
64
  with gr.Column(scale=2):
65
  model_3d = gr.Model3D(
66
+ clear_color=[0.2, 0.2, 0.2, 1.0], # Dark gray background for better contrast
67
  label="3D Interactive View",
68
+ camera_position=(0, 0, 2.5) # Adjust initial camera distance
69
  )
70
 
71
+ # Bind interaction: Gallery click -> Update Model3D
72
  gallery.select(fn=display_model, outputs=model_3d)
73
 
74
+ # Launch the app
75
  if __name__ == "__main__":
76
+ # ssr_mode=False is crucial to prevent "bool is not iterable" errors in some environments
77
  demo.launch()