muhammadsalmanalfaridzi
commited on
Update app.py
Browse files
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 |
-
#
|
42 |
-
|
43 |
-
input_image = Image.open(input_path).convert("RGBA")
|
44 |
|
45 |
# Get the segmentation output
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
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):
|