Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,15 +18,10 @@ model, preprocess = clip.load("ViT-B/32")
|
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
model.to(device).eval()
|
20 |
|
21 |
-
def find_similarity(
|
22 |
try:
|
23 |
-
# Decode the base64 image to bytes
|
24 |
-
image_bytes = base64.b64decode(base64_image)
|
25 |
-
|
26 |
-
# Convert the bytes to a numpy array
|
27 |
-
image = np.array(Image.open(BytesIO(image_bytes)))
|
28 |
-
|
29 |
# Preprocess the image
|
|
|
30 |
image = preprocess(image).unsqueeze(0).to(device)
|
31 |
|
32 |
# Prepare input text
|
@@ -42,7 +37,8 @@ def find_similarity(base64_image, text_input):
|
|
42 |
text_features /= text_features.norm(dim=-1, keepdim=True)
|
43 |
similarity = (text_features @ image_features.T).squeeze(0).cpu().numpy()
|
44 |
|
45 |
-
return similarity
|
|
|
46 |
except Exception as e:
|
47 |
return json.dumps({"error": str(e)})
|
48 |
|
@@ -66,16 +62,11 @@ def segment_image(input_image, text_input):
|
|
66 |
x, y, w, h = map(int, mask_dict['bbox'])
|
67 |
cropped_region = image.crop((x, y, x + w, y + h))
|
68 |
|
69 |
-
# Convert to base64 image
|
70 |
-
buffered = BytesIO()
|
71 |
-
cropped_region.save(buffered, format="PNG")
|
72 |
-
segmented_image_base64 = base64.b64encode(buffered.getvalue()).decode()
|
73 |
-
|
74 |
# Calculate similarity for the segmented image
|
75 |
-
similarity = find_similarity(
|
76 |
|
77 |
# Append the segmented image and its similarity score
|
78 |
-
segmented_regions.append({"image":
|
79 |
|
80 |
# Sort the segmented images by similarity in descending order
|
81 |
segmented_regions.sort(key=lambda x: x["similarity"], reverse=True)
|
|
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
model.to(device).eval()
|
20 |
|
21 |
+
def find_similarity(image, text_input):
|
22 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Preprocess the image
|
24 |
+
image = Image.fromarray(image)
|
25 |
image = preprocess(image).unsqueeze(0).to(device)
|
26 |
|
27 |
# Prepare input text
|
|
|
37 |
text_features /= text_features.norm(dim=-1, keepdim=True)
|
38 |
similarity = (text_features @ image_features.T).squeeze(0).cpu().numpy()
|
39 |
|
40 |
+
return similarity.tolist() # Convert to a list
|
41 |
+
|
42 |
except Exception as e:
|
43 |
return json.dumps({"error": str(e)})
|
44 |
|
|
|
62 |
x, y, w, h = map(int, mask_dict['bbox'])
|
63 |
cropped_region = image.crop((x, y, x + w, y + h))
|
64 |
|
|
|
|
|
|
|
|
|
|
|
65 |
# Calculate similarity for the segmented image
|
66 |
+
similarity = find_similarity(np.array(cropped_region), text_input)
|
67 |
|
68 |
# Append the segmented image and its similarity score
|
69 |
+
segmented_regions.append({"image": input_image, "similarity": similarity})
|
70 |
|
71 |
# Sort the segmented images by similarity in descending order
|
72 |
segmented_regions.sort(key=lambda x: x["similarity"], reverse=True)
|