asimfayaz commited on
Commit
5323302
·
1 Parent(s): 986c990

Fixed image upload errors

Browse files
Files changed (1) hide show
  1. gradio_app.py +55 -21
gradio_app.py CHANGED
@@ -491,21 +491,38 @@ def _gen_shape(
491
  num_chunks=200000,
492
  randomize_seed: bool = False,
493
  ):
494
- if not MV_MODE and image is None and caption is None:
 
 
 
 
 
495
  raise gr.Error("Please provide either a caption or an image.")
496
- if MV_MODE:
497
- if mv_image_front is None and mv_image_back is None \
498
- and mv_image_left is None and mv_image_right is None:
499
- raise gr.Error("Please provide at least one view image.")
 
500
  image = {}
501
- if mv_image_front:
 
502
  image['front'] = mv_image_front
503
- if mv_image_back:
 
 
504
  image['back'] = mv_image_back
505
- if mv_image_left:
 
 
506
  image['left'] = mv_image_left
507
- if mv_image_right:
 
 
508
  image['right'] = mv_image_right
 
 
 
 
509
 
510
  seed = int(randomize_seed_fn(seed, randomize_seed))
511
 
@@ -540,18 +557,29 @@ def _gen_shape(
540
 
541
  # remove disk io to make responding faster, uncomment at your will.
542
  # image.save(os.path.join(save_folder, 'input.png'))
543
- if MV_MODE:
544
- start_time = time.time()
 
 
545
  for k, v in image.items():
546
- if check_box_rembg or v.mode == "RGB":
547
- img = rmbg_worker(v.convert('RGB'))
548
- image[k] = img
549
- time_meta['remove background'] = time.time() - start_time
550
- else:
551
- if check_box_rembg or image.mode == "RGB":
552
- start_time = time.time()
553
- image = rmbg_worker(image.convert('RGB'))
554
- time_meta['remove background'] = time.time() - start_time
 
 
 
 
 
 
 
 
 
555
 
556
  # remove disk io to make responding faster, uncomment at your will.
557
  # image.save(os.path.join(save_folder, 'rembg.png'))
@@ -581,7 +609,13 @@ def _gen_shape(
581
  stats['number_of_vertices'] = mesh.vertices.shape[0]
582
 
583
  stats['time'] = time_meta
584
- main_image = image if not MV_MODE else image['front']
 
 
 
 
 
 
585
  return mesh, main_image, save_folder, stats, seed
586
 
587
  @spaces.GPU(duration=180)
 
491
  num_chunks=200000,
492
  randomize_seed: bool = False,
493
  ):
494
+ # Check if we're using multi-view mode based on inputs
495
+ using_multiview = (mv_image_front is not None or mv_image_back is not None or
496
+ mv_image_left is not None or mv_image_right is not None)
497
+
498
+ # Single image mode validation
499
+ if not using_multiview and image is None and caption is None:
500
  raise gr.Error("Please provide either a caption or an image.")
501
+
502
+ # Multi-view mode validation and processing
503
+ if using_multiview:
504
+ # Check if any valid images were provided
505
+ has_valid_image = False
506
  image = {}
507
+
508
+ if mv_image_front is not None:
509
  image['front'] = mv_image_front
510
+ has_valid_image = True
511
+
512
+ if mv_image_back is not None:
513
  image['back'] = mv_image_back
514
+ has_valid_image = True
515
+
516
+ if mv_image_left is not None:
517
  image['left'] = mv_image_left
518
+ has_valid_image = True
519
+
520
+ if mv_image_right is not None:
521
  image['right'] = mv_image_right
522
+ has_valid_image = True
523
+
524
+ if not has_valid_image:
525
+ raise gr.Error("Please provide at least one view image.")
526
 
527
  seed = int(randomize_seed_fn(seed, randomize_seed))
528
 
 
557
 
558
  # remove disk io to make responding faster, uncomment at your will.
559
  # image.save(os.path.join(save_folder, 'input.png'))
560
+ # Process images based on whether we're using multi-view mode
561
+ start_time = time.time()
562
+
563
+ if isinstance(image, dict): # Multi-view mode
564
  for k, v in image.items():
565
+ if v is not None and (check_box_rembg or v.mode == "RGB"):
566
+ try:
567
+ img = rmbg_worker(v.convert('RGB'))
568
+ image[k] = img
569
+ except Exception as e:
570
+ print(f"Error processing {k} view: {e}")
571
+ # Keep the original image if background removal fails
572
+ pass
573
+ else: # Single image mode
574
+ if image is not None and (check_box_rembg or image.mode == "RGB"):
575
+ try:
576
+ image = rmbg_worker(image.convert('RGB'))
577
+ except Exception as e:
578
+ print(f"Error removing background: {e}")
579
+ # Keep the original image if background removal fails
580
+ pass
581
+
582
+ time_meta['remove background'] = time.time() - start_time
583
 
584
  # remove disk io to make responding faster, uncomment at your will.
585
  # image.save(os.path.join(save_folder, 'rembg.png'))
 
609
  stats['number_of_vertices'] = mesh.vertices.shape[0]
610
 
611
  stats['time'] = time_meta
612
+
613
+ # Select the main image for display based on what's available
614
+ if isinstance(image, dict) and 'front' in image:
615
+ main_image = image['front'] # Use front view as main image in multi-view mode
616
+ else:
617
+ main_image = image # Use the single image in single-image mode
618
+
619
  return mesh, main_image, save_folder, stats, seed
620
 
621
  @spaces.GPU(duration=180)