prithivMLmods commited on
Commit
d04af5f
·
verified ·
1 Parent(s): 9d365dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -6,6 +6,7 @@ from PIL import Image
6
  import tempfile
7
  import uuid
8
  import math
 
9
  from typing import *
10
  from datetime import datetime
11
  from pathlib import Path
@@ -148,12 +149,33 @@ def end_session(req: gr.Request):
148
  shutil.rmtree(user_dir)
149
 
150
  def remove_background(input: Image.Image) -> Image.Image:
151
- with tempfile.NamedTemporaryFile(suffix='.png') as f:
152
- input = input.convert('RGB')
153
- input.save(f.name)
154
- output = rmbg_client.predict(handle_file(f.name), api_name="/image")[0][0]
155
- output = Image.open(output)
156
- return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  def preprocess_image(input: Image.Image) -> Image.Image:
159
  """Preprocess the input image: remove bg, crop, resize."""
@@ -397,46 +419,26 @@ def generate_3d(
397
 
398
  # 5. Auto-Rotation Logic (Animation)
399
  if auto_rotate:
400
- # Create an orbit animation
401
- # Radius approx 1.5 units (assuming model is unit scale centered)
402
  radius = 2.0
403
- frames = 120 # Number of frames for one rotation
404
 
405
  for i in range(frames):
406
  angle = 2 * math.pi * (i / frames)
407
  # Orbit in X-Z plane (Y is up)
408
  x = radius * math.sin(angle)
409
  z = radius * math.cos(angle)
410
- y = 0.5 # Slightly elevated
411
-
412
- # Simple look-at transform logic
413
- # Position: (x, y, z)
414
- # Target: (0, 0, 0)
415
- # Up: (0, 1, 0)
416
-
417
- # Rerun expects translation and rotation (quat or mat3)
418
- # For simplicity in this demo, we just log translation to visualize path
419
- # OR we log a camera object moving.
420
 
421
- # Logging a Pinhole camera that moves
422
- rr.set_time_sequence("frame", i)
423
 
424
- # Calculate Rotation Matrix to look at center
425
- # Forward vector (cam to target)
426
  fwd = np.array([-x, -y, -z])
427
  fwd = fwd / np.linalg.norm(fwd)
428
- # Right vector
429
  up = np.array([0, 1, 0])
430
  right = np.cross(fwd, up)
431
  right = right / np.linalg.norm(right)
432
- # Re-calculate Up
433
  new_up = np.cross(right, fwd)
434
-
435
- # Rerun Camera convention: -Z forward, +Y down, +X right?
436
- # OR standard computer vision: Z forward.
437
- # Since we set RIGHT_HAND_Y_UP world, we should align standard camera.
438
-
439
- # Construct rotation matrix [Right, Up, -Forward]
440
  rot_mat = np.column_stack((right, new_up, -fwd))
441
 
442
  rec.log(
 
6
  import tempfile
7
  import uuid
8
  import math
9
+ import time
10
  from typing import *
11
  from datetime import datetime
12
  from pathlib import Path
 
149
  shutil.rmtree(user_dir)
150
 
151
  def remove_background(input: Image.Image) -> Image.Image:
152
+ # Try multiple times if GPU is busy
153
+ max_retries = 3
154
+ for attempt in range(max_retries):
155
+ try:
156
+ with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f:
157
+ input = input.convert('RGB')
158
+ input.save(f.name)
159
+ f_path = f.name
160
+
161
+ # Call external API
162
+ output = rmbg_client.predict(handle_file(f_path), api_name="/image")[0][0]
163
+
164
+ # Cleanup temp input
165
+ try:
166
+ os.remove(f_path)
167
+ except:
168
+ pass
169
+
170
+ return Image.open(output)
171
+
172
+ except Exception as e:
173
+ print(f"RMBG attempt {attempt+1} failed: {e}")
174
+ if attempt < max_retries - 1:
175
+ time.sleep(2) # Wait a bit before retry
176
+ else:
177
+ print("Returning original image as fallback.")
178
+ return input # Fallback: return original if RMBG fails
179
 
180
  def preprocess_image(input: Image.Image) -> Image.Image:
181
  """Preprocess the input image: remove bg, crop, resize."""
 
419
 
420
  # 5. Auto-Rotation Logic (Animation)
421
  if auto_rotate:
 
 
422
  radius = 2.0
423
+ frames = 120
424
 
425
  for i in range(frames):
426
  angle = 2 * math.pi * (i / frames)
427
  # Orbit in X-Z plane (Y is up)
428
  x = radius * math.sin(angle)
429
  z = radius * math.cos(angle)
430
+ y = 0.5
 
 
 
 
 
 
 
 
 
431
 
432
+ # Use the correct recording object to set time
433
+ rec.set_time_sequence("frame", i)
434
 
435
+ # Rotation Matrix logic
 
436
  fwd = np.array([-x, -y, -z])
437
  fwd = fwd / np.linalg.norm(fwd)
 
438
  up = np.array([0, 1, 0])
439
  right = np.cross(fwd, up)
440
  right = right / np.linalg.norm(right)
 
441
  new_up = np.cross(right, fwd)
 
 
 
 
 
 
442
  rot_mat = np.column_stack((right, new_up, -fwd))
443
 
444
  rec.log(