Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,8 +51,10 @@ def find_similarity(base64_image, text_input):
|
|
| 51 |
def segment_image(input_image, text_input):
|
| 52 |
image_bytes = base64.b64decode(input_image)
|
| 53 |
image = Image.open(BytesIO(image_bytes))
|
| 54 |
-
|
| 55 |
-
image
|
|
|
|
|
|
|
| 56 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 57 |
masks = mask_generator.generate(image)
|
| 58 |
|
|
@@ -66,7 +68,7 @@ def segment_image(input_image, text_input):
|
|
| 66 |
cropped_region = segmented_region[y:y+h, x:x+w]
|
| 67 |
|
| 68 |
# Convert to base64 image
|
| 69 |
-
_, buffer = cv2.imencode(".png",
|
| 70 |
segmented_image_base64 = base64.b64encode(buffer).decode()
|
| 71 |
|
| 72 |
# Calculate similarity for the segmented image
|
|
@@ -81,6 +83,42 @@ def segment_image(input_image, text_input):
|
|
| 81 |
# Return the segmented images in descending order of similarity
|
| 82 |
return segmented_regions
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# Create Gradio components
|
| 85 |
input_image = gr.Textbox(label="Base64 Image", lines=8)
|
| 86 |
text_input = gr.Textbox(label="Text Input") # Use Textbox with a label
|
|
|
|
| 51 |
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 a numpy array
|
| 56 |
+
image = np.array(image)
|
| 57 |
+
|
| 58 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 59 |
masks = mask_generator.generate(image)
|
| 60 |
|
|
|
|
| 68 |
cropped_region = segmented_region[y:y+h, x:x+w]
|
| 69 |
|
| 70 |
# Convert to base64 image
|
| 71 |
+
_, buffer = cv2.imencode(".png", cropped_region)
|
| 72 |
segmented_image_base64 = base64.b64encode(buffer).decode()
|
| 73 |
|
| 74 |
# Calculate similarity for the segmented image
|
|
|
|
| 83 |
# Return the segmented images in descending order of similarity
|
| 84 |
return segmented_regions
|
| 85 |
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# def segment_image(input_image, text_input):
|
| 90 |
+
# image_bytes = base64.b64decode(input_image)
|
| 91 |
+
# image = Image.open(BytesIO(image_bytes))
|
| 92 |
+
|
| 93 |
+
# image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
|
| 94 |
+
# mask_generator = SamAutomaticMaskGenerator(sam)
|
| 95 |
+
# masks = mask_generator.generate(image)
|
| 96 |
+
|
| 97 |
+
# segmented_regions = [] # List to store segmented regions with similarity scores
|
| 98 |
+
|
| 99 |
+
# for i, mask_dict in enumerate(masks):
|
| 100 |
+
# mask_data = (mask_dict['segmentation'] * 255).astype(np.uint8)
|
| 101 |
+
# segmented_region = cv2.bitwise_and(image, image, mask=mask_data)
|
| 102 |
+
|
| 103 |
+
# x, y, w, h = map(int, mask_dict['bbox'])
|
| 104 |
+
# cropped_region = segmented_region[y:y+h, x:x+w]
|
| 105 |
+
|
| 106 |
+
# # Convert to base64 image
|
| 107 |
+
# _, buffer = cv2.imencode(".png", cv2.cvtColor(cropped_region, cv2.COLOR_BGR2RGB))
|
| 108 |
+
# segmented_image_base64 = base64.b64encode(buffer).decode()
|
| 109 |
+
|
| 110 |
+
# # Calculate similarity for the segmented image
|
| 111 |
+
# similarity = find_similarity(segmented_image_base64, text_input)
|
| 112 |
+
|
| 113 |
+
# # Append the segmented image and its similarity score
|
| 114 |
+
# segmented_regions.append({"image": segmented_image_base64, "similarity": similarity})
|
| 115 |
+
|
| 116 |
+
# # Sort the segmented images by similarity in descending order
|
| 117 |
+
# segmented_regions.sort(key=lambda x: x["similarity"], reverse=True)
|
| 118 |
+
|
| 119 |
+
# # Return the segmented images in descending order of similarity
|
| 120 |
+
# return segmented_regions
|
| 121 |
+
|
| 122 |
# Create Gradio components
|
| 123 |
input_image = gr.Textbox(label="Base64 Image", lines=8)
|
| 124 |
text_input = gr.Textbox(label="Text Input") # Use Textbox with a label
|