Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -52,8 +52,11 @@ def segment_image(input_image, text_input):
|
|
52 |
image_bytes = base64.b64decode(input_image)
|
53 |
image = Image.open(BytesIO(image_bytes))
|
54 |
|
55 |
-
# Convert the image to
|
56 |
-
image =
|
|
|
|
|
|
|
57 |
|
58 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
59 |
masks = mask_generator.generate(image)
|
@@ -62,13 +65,19 @@ def segment_image(input_image, text_input):
|
|
62 |
|
63 |
for i, mask_dict in enumerate(masks):
|
64 |
mask_data = (mask_dict['segmentation'] * 255).astype(np.uint8)
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
x, y, w, h = map(int, mask_dict['bbox'])
|
68 |
cropped_region = segmented_region[y:y+h, x:x+w]
|
69 |
|
70 |
# Convert to base64 image
|
71 |
-
_, buffer = cv2.imencode(".png",
|
72 |
segmented_image_base64 = base64.b64encode(buffer).decode()
|
73 |
|
74 |
# Calculate similarity for the segmented image
|
|
|
52 |
image_bytes = base64.b64decode(input_image)
|
53 |
image = Image.open(BytesIO(image_bytes))
|
54 |
|
55 |
+
# Convert the image to RGB color mode
|
56 |
+
image = image.convert("RGB")
|
57 |
+
|
58 |
+
# Convert the image to a numpy array
|
59 |
+
image = np.array(image)
|
60 |
|
61 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
62 |
masks = mask_generator.generate(image)
|
|
|
65 |
|
66 |
for i, mask_dict in enumerate(masks):
|
67 |
mask_data = (mask_dict['segmentation'] * 255).astype(np.uint8)
|
68 |
+
|
69 |
+
# Create a mask with the same shape as the original image
|
70 |
+
mask = np.zeros_like(image)
|
71 |
+
mask[:, :] = mask_data[:, :, np.newaxis]
|
72 |
+
|
73 |
+
# Apply the mask to the original image
|
74 |
+
segmented_region = cv2.bitwise_and(image, mask)
|
75 |
|
76 |
x, y, w, h = map(int, mask_dict['bbox'])
|
77 |
cropped_region = segmented_region[y:y+h, x:x+w]
|
78 |
|
79 |
# Convert to base64 image
|
80 |
+
_, buffer = cv2.imencode(".png", cropped_region)
|
81 |
segmented_image_base64 = base64.b64encode(buffer).decode()
|
82 |
|
83 |
# Calculate similarity for the segmented image
|