ginipick commited on
Commit
10c000d
·
verified ·
1 Parent(s): 852ded9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -171,34 +171,35 @@ def _gpu_process(img: Image.Image, prompt: str | BoundingBox | None) -> tuple[Im
171
  return mask, bbox, time_log
172
 
173
  def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str | None = None) -> tuple[tuple[Image.Image, Image.Image, Image.Image], gr.DownloadButton]:
174
- if img.width > 2048 or img.height > 2048:
175
- orig_res = max(img.width, img.height)
176
- img.thumbnail((2048, 2048))
177
- if isinstance(prompt, tuple):
178
- x0, y0, x1, y1 = (int(x * 2048 / orig_res) for x in prompt)
179
- prompt = (x0, y0, x1, y1)
 
180
 
181
- mask, bbox, time_log = _gpu_process(img, prompt)
182
- masked_alpha = apply_mask(img, mask, defringe=True)
183
 
184
- if bg_prompt:
185
- try:
186
  background = generate_background(bg_prompt, img.width, img.height)
187
  combined = combine_with_background(masked_alpha, background)
188
- except Exception as e:
189
- raise gr.Error(f"Background processing failed: {str(e)}")
190
- else:
191
- combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)
192
 
193
- thresholded = mask.point(lambda p: 255 if p > 10 else 0)
194
- bbox = thresholded.getbbox()
195
- to_dl = masked_alpha.crop(bbox)
196
 
197
- temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
198
- to_dl.save(temp, format="PNG")
199
- temp.close()
200
 
201
- return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
 
 
 
202
 
203
  def process_bbox(img: Image.Image, box_input: str) -> tuple[list[Image.Image], str]:
204
  try:
 
171
  return mask, bbox, time_log
172
 
173
  def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str | None = None) -> tuple[tuple[Image.Image, Image.Image, Image.Image], gr.DownloadButton]:
174
+ try:
175
+ if img.width > 2048 or img.height > 2048:
176
+ orig_res = max(img.width, img.height)
177
+ img.thumbnail((2048, 2048))
178
+ if isinstance(prompt, tuple):
179
+ x0, y0, x1, y1 = (int(x * 2048 / orig_res) for x in prompt)
180
+ prompt = (x0, y0, x1, y1)
181
 
182
+ mask, bbox, time_log = _gpu_process(img, prompt)
183
+ masked_alpha = apply_mask(img, mask, defringe=True)
184
 
185
+ if bg_prompt:
 
186
  background = generate_background(bg_prompt, img.width, img.height)
187
  combined = combine_with_background(masked_alpha, background)
188
+ else:
189
+ combined = Image.alpha_composite(Image.new("RGBA", masked_alpha.size, "white"), masked_alpha)
 
 
190
 
191
+ thresholded = mask.point(lambda p: 255 if p > 10 else 0)
192
+ bbox = thresholded.getbbox()
193
+ to_dl = masked_alpha.crop(bbox)
194
 
195
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
196
+ to_dl.save(temp, format="PNG")
197
+ temp.close()
198
 
199
+ return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
200
+
201
+ except Exception as e:
202
+ raise gr.Error(f"Processing failed: {str(e)}")
203
 
204
  def process_bbox(img: Image.Image, box_input: str) -> tuple[list[Image.Image], str]:
205
  try: