muhammadsalmanalfaridzi commited on
Commit
f9ea49f
β€’
1 Parent(s): 74c44d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -31,7 +31,7 @@ def remove_background_rembg(input_path):
31
  img = Image.open(io.BytesIO(output_image)).convert("RGBA")
32
  return img
33
 
34
- def remove_background_bria(input_image):
35
  """Remove background using the Bria model."""
36
  print("Removing background using Bria for the image.")
37
 
@@ -39,11 +39,11 @@ def remove_background_bria(input_image):
39
  pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True, device=0)
40
 
41
  # Ensure input_image is a PIL image
42
- if isinstance(input_image, str):
43
- input_image = Image.open(input_image).convert("RGBA")
44
 
45
  # Get the segmentation output
46
- segmentation_result = pipe(input_image)
47
 
48
  # Check if the output contains a mask
49
  if isinstance(segmentation_result, list) and len(segmentation_result) > 0:
@@ -52,24 +52,24 @@ def remove_background_bria(input_image):
52
 
53
  if mask is not None:
54
  # Create an output image based on the mask
55
- output_image = Image.new("RGBA", input_image.size)
56
- width, height = input_image.size
57
 
58
  for y in range(height):
59
  for x in range(width):
60
  # Check if the mask value is valid (assuming it's binary or a single channel)
61
  if mask[y][x] > 0: # Adjust based on actual mask values
62
- output_image.putpixel((x, y), input_image.getpixel((x, y)))
63
  else:
64
  output_image.putpixel((x, y), (0, 0, 0, 0)) # Set to transparent
65
 
66
  return output_image
67
  else:
68
  print("Mask is None in segmentation result.")
69
- return input_image # Return original if mask is None
70
  else:
71
  print("No valid segmentation result received.")
72
- return input_image # Return original if no valid result
73
 
74
  # Fungsi untuk memproses gambar menggunakan prompt
75
  def text_to_image(prompt):
 
31
  img = Image.open(io.BytesIO(output_image)).convert("RGBA")
32
  return img
33
 
34
+ def remove_background_bria(input_path):
35
  """Remove background using the Bria model."""
36
  print("Removing background using Bria for the image.")
37
 
 
39
  pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True, device=0)
40
 
41
  # Ensure input_image is a PIL image
42
+ if isinstance(input_path, str):
43
+ input_image = Image.open(input_path).convert("RGBA")
44
 
45
  # Get the segmentation output
46
+ segmentation_result = pipe(input_path)
47
 
48
  # Check if the output contains a mask
49
  if isinstance(segmentation_result, list) and len(segmentation_result) > 0:
 
52
 
53
  if mask is not None:
54
  # Create an output image based on the mask
55
+ output_image = Image.new("RGBA", input_path.size)
56
+ width, height = input_path.size
57
 
58
  for y in range(height):
59
  for x in range(width):
60
  # Check if the mask value is valid (assuming it's binary or a single channel)
61
  if mask[y][x] > 0: # Adjust based on actual mask values
62
+ output_image.putpixel((x, y), input_path.getpixel((x, y)))
63
  else:
64
  output_image.putpixel((x, y), (0, 0, 0, 0)) # Set to transparent
65
 
66
  return output_image
67
  else:
68
  print("Mask is None in segmentation result.")
69
+ return input_path # Return original if mask is None
70
  else:
71
  print("No valid segmentation result received.")
72
+ return input_path # Return original if no valid result
73
 
74
  # Fungsi untuk memproses gambar menggunakan prompt
75
  def text_to_image(prompt):