wushuang98 commited on
Commit
20abc40
·
verified ·
1 Parent(s): 84bd0e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -59,11 +59,26 @@ pipe.to("cuda")
59
 
60
  rmbg = BiRefNet(device="cuda")
61
 
62
- @spaces.GPU(duration=600)
63
  def run_rembg(image: Image.Image):
64
  return rmbg.run(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- @spaces.GPU(duration=600)
67
  def image2mesh(
68
  image: Any,
69
  resolution: str = '1024',
@@ -71,29 +86,24 @@ def image2mesh(
71
  simplify_ratio: float = 0.95,
72
  output_path: str = 'outputs/web'
73
  ):
74
-
75
- torch.cuda.empty_cache()
76
-
77
  if not os.path.exists(output_path):
78
  os.makedirs(output_path)
79
 
80
  uid = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
81
  image.save(os.path.join(output_path, uid + '.png'))
82
 
83
- mesh = pipe(
84
- image,
85
- sdf_resolution=int(resolution),
86
- mc_threshold=0.2,
87
- remesh=simplify,
88
- simplify_ratio=simplify_ratio,
89
- )["mesh"]
90
 
91
  mesh_path = os.path.join(output_path, f'{uid}.obj')
92
  mesh.export(
93
  mesh_path,
94
  include_normals=True,
95
  )
96
- torch.cuda.empty_cache()
97
 
98
  return mesh_path
99
 
 
59
 
60
  rmbg = BiRefNet(device="cuda")
61
 
62
+ @spaces.GPU(duration=120)
63
  def run_rembg(image: Image.Image):
64
  return rmbg.run(image)
65
+
66
+ @spaces.GPU(duration=120)
67
+ def generate_mesh(
68
+ image: Any,
69
+ resolution: str = '1024',
70
+ ):
71
+ torch.cuda.empty_cache()
72
+ mesh = pipe(
73
+ image,
74
+ sdf_resolution=int(resolution),
75
+ mc_threshold=0.2,
76
+ remesh=False,
77
+ )["mesh"]
78
+ torch.cuda.empty_cache()
79
+ return mesh
80
+
81
 
 
82
  def image2mesh(
83
  image: Any,
84
  resolution: str = '1024',
 
86
  simplify_ratio: float = 0.95,
87
  output_path: str = 'outputs/web'
88
  ):
89
+
 
 
90
  if not os.path.exists(output_path):
91
  os.makedirs(output_path)
92
 
93
  uid = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
94
  image.save(os.path.join(output_path, uid + '.png'))
95
 
96
+ mesh = generate_mesh(image, resolution)
97
+
98
+ if simplify:
99
+ mesh = postprocess_mesh(mesh.vertices, mesh.faces, simplify=simplify,
100
+ simplify_ratio=simplify_ratio, fill_holes=False, verbose=True)
 
 
101
 
102
  mesh_path = os.path.join(output_path, f'{uid}.obj')
103
  mesh.export(
104
  mesh_path,
105
  include_normals=True,
106
  )
 
107
 
108
  return mesh_path
109