muhammadsalmanalfaridzi commited on
Commit
4ebddc7
·
verified ·
1 Parent(s): e5725c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -31
app.py CHANGED
@@ -37,39 +37,27 @@ def remove_background_bria(input_path):
37
 
38
  # Create a segmentation pipeline
39
  pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
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:
50
- # Adjust based on the actual output structure
51
- mask = segmentation_result[0].get("mask")
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):
 
37
 
38
  # Create a segmentation pipeline
39
  pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
40
+
41
+ # Load the image
42
+ input_image = Image.open(input_path).convert("RGBA")
 
43
 
44
  # Get the segmentation output
45
+ pillow_mask = pipe(input_image, return_mask=True) # Outputs a pillow mask
46
+ print("Mask obtained:", pillow_mask) # Debugging output
47
+
48
+ # Create an output image based on the mask
49
+ output_image = Image.new("RGBA", input_image.size)
50
+
51
+ # Use the mask to create the output image
52
+ for x in range(input_image.width):
53
+ for y in range(input_image.height):
54
+ # Assuming mask is in a binary format where foreground is True
55
+ if pillow_mask.getpixel((x, y)) > 0: # Adjust based on actual mask values
56
+ output_image.putpixel((x, y), input_image.getpixel((x, y)))
57
+ else:
58
+ output_image.putpixel((x, y), (0, 0, 0, 0)) # Set to transparent
59
+
60
+ return output_image
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  # Fungsi untuk memproses gambar menggunakan prompt
63
  def text_to_image(prompt):