Astridkraft commited on
Commit
8061fe8
·
verified ·
1 Parent(s): 789efa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -248,7 +248,7 @@ def scale_image_and_mask_together(image, mask, target_size=512):
248
  def enhanced_composite_with_sam(original_image, inpaint_result, original_mask,
249
  padding_info, bbox_coords, mode):
250
  """
251
- VERBESSERTES COMPOSITING MIT SAM-MASKEN
252
  Berücksichtigt die präzisen Kanten der SAM-Maske
253
  """
254
  print(f"🎨 Verbessertes Compositing für Modus: {mode}")
@@ -322,36 +322,42 @@ def enhanced_composite_with_sam(original_image, inpaint_result, original_mask,
322
  Image.Resampling.LANCZOS
323
  )
324
  return final_image.convert("RGB")
325
-
326
- # Bearbeiteten Bereich ausschneiden und hochskalieren
327
- x1, y1, x2, y2 = bbox_coords
328
- bbox_scaled = (
329
- int(x1 * scale_factor),
330
- int(y1 * scale_factor),
331
- int(x2 * scale_factor),
332
- int(y2 * scale_factor)
333
- )
334
-
335
- # Sicherstellen, dass BBox gültig ist
336
  bbox_scaled = (
337
- max(0, bbox_scaled[0]),
338
- max(0, bbox_scaled[1]),
339
- min(scaled_width, bbox_scaled[2]),
340
- min(scaled_height, bbox_scaled[3])
 
 
 
 
 
 
 
 
 
 
341
  )
342
-
343
- if bbox_scaled[2] > bbox_scaled[0] and bbox_scaled[3] > bbox_scaled[1]:
344
- edited_region = downscaled_result.crop(bbox_scaled)
 
 
345
 
346
  # Auf ORIGINAL-BBox-Größe skalieren
347
- original_bbox_size = (x2 - x1, y2 - y1)
 
348
  edited_region_fullsize = edited_region.resize(
349
  original_bbox_size,
350
  Image.Resampling.LANCZOS
351
  )
352
 
353
  # SAM-Maske für den Bereich zuschneiden und weichzeichnen
354
- mask_cropped = original_mask.crop((x1, y1, x2, y2))
355
  soft_mask = mask_cropped.filter(ImageFilter.GaussianBlur(3))
356
 
357
  # Alpha-Compositing mit präziser SAM-Maske
@@ -361,14 +367,13 @@ def enhanced_composite_with_sam(original_image, inpaint_result, original_mask,
361
  temp_image = Image.new("RGBA", original_bbox_size, (0, 0, 0, 0))
362
  temp_image.paste(edited_rgba, (0, 0), mask_rgba)
363
 
364
- final_image.paste(temp_image, (x1, y1), temp_image)
365
- #final_image.paste(temp_image, (x1, y1), mask_rgba)
 
366
 
367
- print(f"✅ Verbessertes Compositing abgeschlossen. Finale Größe: {final_image.size}")
368
  return final_image.convert("RGB")
369
 
370
 
371
-
372
  def auto_detect_face_area(image):
373
  """Optimierten Vorschlag für Gesichtsbereich ohne externe Bibliotheken"""
374
  width, height = image.size
 
248
  def enhanced_composite_with_sam(original_image, inpaint_result, original_mask,
249
  padding_info, bbox_coords, mode):
250
  """
251
+ COMPOSITING MIT SAM-MASKEN UND BBox-KOORDINATEN
252
  Berücksichtigt die präzisen Kanten der SAM-Maske
253
  """
254
  print(f"🎨 Verbessertes Compositing für Modus: {mode}")
 
322
  Image.Resampling.LANCZOS
323
  )
324
  return final_image.convert("RGB")
325
+
326
+
327
+ #BBox-Koordinaten korrekt transformieren
328
+ #Die BBox-Koordinaten müssen vom Originalbild nach 512x512 transformiert werden
 
 
 
 
 
 
 
329
  bbox_scaled = (
330
+ int(bbox_coords[0] * scale_factor),
331
+ int(bbox_coords[1] * scale_factor),
332
+ int(bbox_coords[2] * scale_factor),
333
+ int(bbox_coords[3] * scale_factor)
334
+ )
335
+
336
+
337
+ #Mit den Padding-Offsets wird bei nicht quadratischen 512x512 Bildern
338
+ #das Padding hinzugefügt
339
+ bbox_in_512 = (
340
+ bbox_scaled[0] + x_offset,
341
+ bbox_scaled[1] + y_offset,
342
+ bbox_scaled[2] + x_offset,
343
+ bbox_scaled[3] + y_offset
344
  )
345
+
346
+ if bbox_in_512[2] > bbox_in_512[0] and bbox_in_512[3] > bbox_in_512[1]:
347
+ # Bearbeiteten Bereich aus dem 512×512-Ergebnis ausschneiden
348
+ edited_region = inpaint_result.crop(bbox_in_512)
349
+
350
 
351
  # Auf ORIGINAL-BBox-Größe skalieren
352
+ original_bbox_size = (bbox_coords[2] - bbox_coords[0],
353
+ bbox_coords[3] - bbox_coords[1])
354
  edited_region_fullsize = edited_region.resize(
355
  original_bbox_size,
356
  Image.Resampling.LANCZOS
357
  )
358
 
359
  # SAM-Maske für den Bereich zuschneiden und weichzeichnen
360
+ mask_cropped = original_mask.crop(bbox_coords)
361
  soft_mask = mask_cropped.filter(ImageFilter.GaussianBlur(3))
362
 
363
  # Alpha-Compositing mit präziser SAM-Maske
 
367
  temp_image = Image.new("RGBA", original_bbox_size, (0, 0, 0, 0))
368
  temp_image.paste(edited_rgba, (0, 0), mask_rgba)
369
 
370
+ final_image.paste(temp_image, (bbox_coords[0], bbox_coords[1]), temp_image)
371
+
372
+ print(f"✅ Korrektes Compositing abgeschlossen. Finale Größe: {final_image.size}")
373
 
 
374
  return final_image.convert("RGB")
375
 
376
 
 
377
  def auto_detect_face_area(image):
378
  """Optimierten Vorschlag für Gesichtsbereich ohne externe Bibliotheken"""
379
  width, height = image.size