nithinm19 commited on
Commit
4aab464
1 Parent(s): 9fb1349

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -4,6 +4,8 @@ from PIL import Image
4
  import torch
5
  import requests
6
  from io import BytesIO
 
 
7
 
8
  # Load model and processor from Hugging Face
9
  def load_model_and_processor():
@@ -36,38 +38,42 @@ def image_to_3d(image):
36
  return "Model or processor not loaded."
37
 
38
  try:
39
- # Preprocess the input image
40
  inputs = processor(images=image, return_tensors="pt")
41
 
42
  # Run inference
43
  with torch.no_grad():
44
  outputs = model(**inputs)
45
 
46
- # Placeholder return, replace this with actual 3D visualization logic
47
- return "3D model generated from input image!"
 
 
 
 
 
 
 
 
 
 
 
48
  except Exception as e:
49
  return f"Error during inference: {str(e)}"
50
 
51
  # Load the example image for the Gradio interface
52
  example_image = load_example_image()
53
 
54
- # Description of image types to upload
55
- image_type_description = """
56
- Upload a clear image of a single object with minimal background distractions for best results. Example image types:
57
- - Objects such as cars, furniture, geometric shapes, or architectural structures.
58
- """
59
-
60
  # Gradio interface setup
61
  interface = gr.Interface(
62
  fn=image_to_3d,
63
  inputs=gr.Image(type="pil", label="Upload an Image"),
64
- outputs="text", # Placeholder output (replace with 3D rendering if needed)
65
  title="OpenLRM Mix-Large 1.1 - Image to 3D",
66
  description="Upload an image to generate a 3D model using OpenLRM Mix-Large 1.1.",
67
- examples=[[example_image]] if example_image else None, # Include the example image if loaded
68
- theme="compact", # Compact layout for better UI experience
69
- allow_flagging="never"
70
  )
71
 
72
  # Display a suggestion below the upload widget
73
- interface.launch(description=image_type_description)
 
4
  import torch
5
  import requests
6
  from io import BytesIO
7
+ import trimesh
8
+ import plotly.graph_objects as go
9
 
10
  # Load model and processor from Hugging Face
11
  def load_model_and_processor():
 
38
  return "Model or processor not loaded."
39
 
40
  try:
41
+ # Preprocess input image
42
  inputs = processor(images=image, return_tensors="pt")
43
 
44
  # Run inference
45
  with torch.no_grad():
46
  outputs = model(**inputs)
47
 
48
+ # Convert outputs to a 3D mesh (replace with actual logic based on model output)
49
+ # Assuming 'vertices' and 'faces' are returned by the model (adjust as needed)
50
+ vertices = outputs['vertices'].numpy() # Placeholder for vertex output
51
+ faces = outputs['faces'].numpy() # Placeholder for face output
52
+
53
+ # Create a mesh using trimesh
54
+ mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
55
+
56
+ # Visualize the mesh using Plotly
57
+ fig = go.Figure(data=[go.Mesh3d(x=vertices[:,0], y=vertices[:,1], z=vertices[:,2],
58
+ i=faces[:,0], j=faces[:,1], k=faces[:,2])])
59
+
60
+ return fig # return the figure for display
61
  except Exception as e:
62
  return f"Error during inference: {str(e)}"
63
 
64
  # Load the example image for the Gradio interface
65
  example_image = load_example_image()
66
 
 
 
 
 
 
 
67
  # Gradio interface setup
68
  interface = gr.Interface(
69
  fn=image_to_3d,
70
  inputs=gr.Image(type="pil", label="Upload an Image"),
71
+ outputs=gr.Plot(label="3D Model"),
72
  title="OpenLRM Mix-Large 1.1 - Image to 3D",
73
  description="Upload an image to generate a 3D model using OpenLRM Mix-Large 1.1.",
74
+ examples=[[example_image]] if example_image else None,
75
+ theme="compact"
 
76
  )
77
 
78
  # Display a suggestion below the upload widget
79
+ interface.launch()