Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 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 |
-
#
|
| 18 |
base_name = os.path.splitext(img_file)[0]
|
| 19 |
|
| 20 |
-
#
|
| 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 |
-
#
|
| 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"⚠️
|
| 37 |
|
| 38 |
-
print(f"✅
|
| 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"
|
| 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, #
|
| 58 |
-
height=800, #
|
| 59 |
object_fit="contain",
|
| 60 |
-
allow_preview=False #
|
| 61 |
)
|
| 62 |
|
| 63 |
-
#
|
| 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()
|