DonPab1o commited on
Commit
600b7aa
Β·
verified Β·
1 Parent(s): 6c12262

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -115
app.py CHANGED
@@ -5,7 +5,7 @@ import imageio
5
  import numpy as np
6
  import torch
7
  import rembg
8
- from PIL import Image
9
  from torchvision.transforms import v2
10
  from pytorch_lightning import seed_everything
11
  from omegaconf import OmegaConf
@@ -192,35 +192,6 @@ def make3d(images):
192
  # get triplane
193
  planes = model.forward_planes(images, input_cameras)
194
 
195
- # # get video
196
- # chunk_size = 20 if IS_FLEXICUBES else 1
197
- # render_size = 384
198
-
199
- # frames = []
200
- # for i in tqdm(range(0, render_cameras.shape[1], chunk_size)):
201
- # if IS_FLEXICUBES:
202
- # frame = model.forward_geometry(
203
- # planes,
204
- # render_cameras[:, i:i+chunk_size],
205
- # render_size=render_size,
206
- # )['img']
207
- # else:
208
- # frame = model.synthesizer(
209
- # planes,
210
- # cameras=render_cameras[:, i:i+chunk_size],
211
- # render_size=render_size,
212
- # )['images_rgb']
213
- # frames.append(frame)
214
- # frames = torch.cat(frames, dim=1)
215
-
216
- # images_to_video(
217
- # frames[0],
218
- # video_fpath,
219
- # fps=30,
220
- # )
221
-
222
- # print(f"Video saved to {video_fpath}")
223
-
224
  # get mesh
225
  mesh_out = model.extract_mesh(
226
  planes,
@@ -239,6 +210,37 @@ def make3d(images):
239
  return mesh_fpath, mesh_glb_fpath
240
 
241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  _HEADER_ = '''
243
  <h2><b>Official πŸ€— Gradio Demo</b></h2><h2><a href='https://github.com/TencentARC/InstantMesh' target='_blank'><b>InstantMesh: Efficient 3D Mesh Generation from a Single Image with Sparse-view Large Reconstruction Models</b></a></h2>
244
 
@@ -277,98 +279,96 @@ If you have any questions, feel free to open a discussion or contact us at <b>bl
277
 
278
 
279
  with gr.Blocks() as demo:
280
- gr.Markdown(_HEADER_)
281
- with gr.Row(variant="panel"):
282
- with gr.Column():
283
- with gr.Row():
284
- input_image = gr.Image(
285
- label="Input Image",
286
- image_mode="RGBA",
287
- sources="upload",
288
- #width=256,
289
- #height=256,
290
- type="pil",
291
- elem_id="content_image",
292
- )
293
- processed_image = gr.Image(
294
- label="Processed Image",
295
- image_mode="RGBA",
296
- #width=256,
297
- #height=256,
298
- type="pil",
299
- interactive=False
300
- )
301
- with gr.Row():
302
- with gr.Group():
303
- do_remove_background = gr.Checkbox(
304
- label="Remove Background", value=True
305
  )
306
- sample_seed = gr.Number(value=42, label="Seed Value", precision=0)
307
-
308
- sample_steps = gr.Slider(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  label="Sample Steps",
310
  minimum=30,
311
  maximum=75,
312
  value=75,
313
  step=5
314
  )
315
-
316
- with gr.Row():
317
- submit = gr.Button("Generate", elem_id="generate", variant="primary")
318
-
319
- with gr.Row(variant="panel"):
320
- gr.Examples(
321
- examples=[
322
- os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
323
- ],
324
- inputs=[input_image],
325
- label="Examples",
326
- cache_examples=False,
327
- examples_per_page=16
328
  )
329
-
330
- with gr.Column():
331
-
332
- with gr.Row():
333
-
334
- with gr.Column():
335
- mv_show_images = gr.Image(
336
- label="Generated Multi-views",
337
- type="pil",
338
- width=379,
339
- interactive=False
340
- )
341
-
342
- # with gr.Column():
343
- # output_video = gr.Video(
344
- # label="video", format="mp4",
345
- # width=379,
346
- # autoplay=True,
347
- # interactive=False
348
- # )
349
-
350
- with gr.Row():
351
- with gr.Tab("OBJ"):
352
- output_model_obj = gr.Model3D(
353
- label="Output Model (OBJ Format)",
354
- interactive=False,
355
- )
356
- gr.Markdown("Note: Downloaded .obj model will be flipped. Export .glb instead or manually flip it before usage.")
357
- with gr.Tab("GLB"):
358
- output_model_glb = gr.Model3D(
359
- label="Output Model (GLB Format)",
360
- interactive=False,
361
- )
362
- gr.Markdown("Note: The model shown here has a darker appearance. Download to get correct results.")
363
-
364
- with gr.Row():
365
- gr.Markdown('''Try a different <b>seed value</b> if the result is unsatisfying (Default: 42).''')
366
-
367
- gr.Markdown(_CITE_)
368
-
369
  mv_images = gr.State()
370
-
371
- submit.click(fn=check_input_image, inputs=[input_image]).success(
 
372
  fn=preprocess,
373
  inputs=[input_image, do_remove_background],
374
  outputs=[processed_image],
@@ -376,11 +376,21 @@ with gr.Blocks() as demo:
376
  fn=generate_mvs,
377
  inputs=[processed_image, sample_steps, sample_seed],
378
  outputs=[mv_images, mv_show_images]
379
-
380
  ).success(
381
  fn=make3d,
382
  inputs=[mv_images],
383
  outputs=[output_model_obj, output_model_glb]
384
  )
 
 
 
 
 
 
 
 
 
 
385
 
386
- demo.launch()
 
 
5
  import numpy as np
6
  import torch
7
  import rembg
8
+ from PIL import Image, ImageDraw, ImageFont
9
  from torchvision.transforms import v2
10
  from pytorch_lightning import seed_everything
11
  from omegaconf import OmegaConf
 
192
  # get triplane
193
  planes = model.forward_planes(images, input_cameras)
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  # get mesh
196
  mesh_out = model.extract_mesh(
197
  planes,
 
210
  return mesh_fpath, mesh_glb_fpath
211
 
212
 
213
+ # New function to generate 2D pixel art sprites
214
+ def generate_pixel_art(prompt, remove_background=True, sample_steps=75, seed=42):
215
+ """Generate a pixel art sprite based on the prompt"""
216
+ seed_everything(seed)
217
+
218
+ # Create a simple image with text as starting point
219
+ text_img = Image.new('RGB', (512, 512), color=(255, 255, 255))
220
+ draw = ImageDraw.Draw(text_img)
221
+ # Try to load a font, use default if not available
222
+ try:
223
+ font = ImageFont.truetype("Arial", 20)
224
+ except:
225
+ font = ImageFont.load_default()
226
+
227
+ # Add prompt as text
228
+ pixel_prompt = f"Pixel art: {prompt}"
229
+ draw.text((10, 10), pixel_prompt, fill=(0, 0, 0), font=font)
230
+
231
+ # Process through the pipeline
232
+ processed_img = preprocess(text_img, remove_background)
233
+
234
+ # Generate the pixel art
235
+ result_img, _ = generate_mvs(processed_img, sample_steps, seed)
236
+
237
+ # Save to a temporary file
238
+ sprite_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
239
+ result_img.save(sprite_path)
240
+
241
+ return sprite_path
242
+
243
+
244
  _HEADER_ = '''
245
  <h2><b>Official πŸ€— Gradio Demo</b></h2><h2><a href='https://github.com/TencentARC/InstantMesh' target='_blank'><b>InstantMesh: Efficient 3D Mesh Generation from a Single Image with Sparse-view Large Reconstruction Models</b></a></h2>
246
 
 
279
 
280
 
281
  with gr.Blocks() as demo:
282
+ gr.Markdown("# InstantMesh and Pixel Art Generator")
283
+
284
+ with gr.Tab("3D Model Generation"):
285
+ with gr.Row(variant="panel"):
286
+ with gr.Column():
287
+ with gr.Row():
288
+ input_image = gr.Image(
289
+ label="Input Image",
290
+ image_mode="RGBA",
291
+ sources="upload",
292
+ type="pil",
293
+ elem_id="content_image",
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  )
295
+ processed_image = gr.Image(
296
+ label="Processed Image",
297
+ image_mode="RGBA",
298
+ type="pil",
299
+ interactive=False
300
+ )
301
+ with gr.Row():
302
+ with gr.Group():
303
+ do_remove_background = gr.Checkbox(
304
+ label="Remove Background", value=True
305
+ )
306
+ sample_seed = gr.Number(value=42, label="Seed Value", precision=0)
307
+ sample_steps = gr.Slider(
308
+ label="Sample Steps",
309
+ minimum=30,
310
+ maximum=75,
311
+ value=75,
312
+ step=5
313
+ )
314
+
315
+ with gr.Row():
316
+ submit_3d = gr.Button("Generate 3D Model", elem_id="generate", variant="primary")
317
+
318
+ with gr.Column():
319
+ with gr.Row():
320
+ with gr.Column():
321
+ mv_show_images = gr.Image(
322
+ label="Generated Multi-views",
323
+ type="pil",
324
+ width=379,
325
+ interactive=False
326
+ )
327
+
328
+ with gr.Row():
329
+ with gr.Tab("OBJ"):
330
+ output_model_obj = gr.Model3D(
331
+ label="Output Model (OBJ Format)",
332
+ interactive=False,
333
+ )
334
+ with gr.Tab("GLB"):
335
+ output_model_glb = gr.Model3D(
336
+ label="Output Model (GLB Format)",
337
+ interactive=False,
338
+ )
339
+
340
+ with gr.Tab("Pixel Art Generation"):
341
+ with gr.Row(variant="panel"):
342
+ with gr.Column():
343
+ pixel_prompt = gr.Textbox(
344
+ label="Describe your pixel art sprite",
345
+ placeholder="green cactus snake character, side view, game sprite",
346
+ lines=3
347
+ )
348
+ with gr.Row():
349
+ pixel_bg_remove = gr.Checkbox(label="Remove Background", value=True)
350
+ pixel_seed = gr.Number(value=42, label="Seed Value", precision=0)
351
+ pixel_steps = gr.Slider(
352
  label="Sample Steps",
353
  minimum=30,
354
  maximum=75,
355
  value=75,
356
  step=5
357
  )
358
+ submit_pixel = gr.Button("Generate Pixel Art", variant="primary")
359
+
360
+ with gr.Column():
361
+ pixel_output = gr.Image(
362
+ label="Generated Pixel Art Sprite",
363
+ type="pil",
364
+ interactive=False
 
 
 
 
 
 
365
  )
366
+
367
+ # Set up event handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
  mv_images = gr.State()
369
+
370
+ # 3D Model generation flow
371
+ submit_3d.click(fn=check_input_image, inputs=[input_image]).success(
372
  fn=preprocess,
373
  inputs=[input_image, do_remove_background],
374
  outputs=[processed_image],
 
376
  fn=generate_mvs,
377
  inputs=[processed_image, sample_steps, sample_seed],
378
  outputs=[mv_images, mv_show_images]
 
379
  ).success(
380
  fn=make3d,
381
  inputs=[mv_images],
382
  outputs=[output_model_obj, output_model_glb]
383
  )
384
+
385
+ # Pixel Art generation flow
386
+ submit_pixel.click(
387
+ fn=generate_pixel_art,
388
+ inputs=[pixel_prompt, pixel_bg_remove, pixel_steps, pixel_seed],
389
+ outputs=[pixel_output]
390
+ )
391
+
392
+ # Enable API access
393
+ demo.queue(concurrency_count=1)
394
 
395
+ # Launch with API access enabled
396
+ demo.launch(enable_api=True, share=False)