Spaces:
Runtime error
Runtime error
rfmantoan
commited on
Commit
•
b6b8856
1
Parent(s):
881f993
Add fetching mechanism
Browse files- .gitattributes +1 -1
- requirements.txt +2 -1
- utils/fetch_image.py +8 -0
- utils/vector_database.py +5 -3
.gitattributes
CHANGED
@@ -33,4 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
-
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
CHANGED
@@ -7,4 +7,5 @@ open_clip_torch
|
|
7 |
numpy
|
8 |
pandas
|
9 |
Pillow
|
10 |
-
pymilvus
|
|
|
|
7 |
numpy
|
8 |
pandas
|
9 |
Pillow
|
10 |
+
pymilvus
|
11 |
+
requests
|
utils/fetch_image.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
|
5 |
+
def fetch_image(url):
|
6 |
+
response = requests.get(url)
|
7 |
+
img = Image.open(BytesIO(response.content))
|
8 |
+
return img
|
utils/vector_database.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from PIL import Image
|
2 |
from pymilvus import connections, Collection
|
3 |
from pymilvus import FieldSchema, CollectionSchema, DataType, Collection
|
|
|
4 |
|
5 |
def load_collection(name):
|
6 |
collection = Collection(name)
|
@@ -55,7 +56,7 @@ def search_in_milvus(collection, search_field, query_embedding, top_k=6):
|
|
55 |
search_field, # Field to search in
|
56 |
param=search_params,
|
57 |
limit=top_k, # Top k results
|
58 |
-
output_fields=["image_id", "metadata"]
|
59 |
)
|
60 |
|
61 |
# Step 2: Extract the relevant information from the search results
|
@@ -63,12 +64,13 @@ def search_in_milvus(collection, search_field, query_embedding, top_k=6):
|
|
63 |
for result in results[0]: # The first element of 'results' contains the search results
|
64 |
image_id = result.entity.get("image_id") # Retrieve the image ID
|
65 |
metadata = result.entity.get("metadata") # Retrieve metadata (such as description, brand, etc.)
|
|
|
66 |
similarity_score = result.distance # Retrieve similarity score (distance)
|
67 |
|
68 |
# Load the image (you can use PIL to load the image)
|
69 |
#image_path = "/content/drive/MyDrive/images/" + str(image_id) + ".jpg"
|
70 |
-
|
71 |
-
image =
|
72 |
|
73 |
# Append the image, metadata, and score to the search results
|
74 |
search_results.append({
|
|
|
1 |
from PIL import Image
|
2 |
from pymilvus import connections, Collection
|
3 |
from pymilvus import FieldSchema, CollectionSchema, DataType, Collection
|
4 |
+
from utils.fetch_image import fetch_image
|
5 |
|
6 |
def load_collection(name):
|
7 |
collection = Collection(name)
|
|
|
56 |
search_field, # Field to search in
|
57 |
param=search_params,
|
58 |
limit=top_k, # Top k results
|
59 |
+
output_fields=["image_id", "metadata", "url"]
|
60 |
)
|
61 |
|
62 |
# Step 2: Extract the relevant information from the search results
|
|
|
64 |
for result in results[0]: # The first element of 'results' contains the search results
|
65 |
image_id = result.entity.get("image_id") # Retrieve the image ID
|
66 |
metadata = result.entity.get("metadata") # Retrieve metadata (such as description, brand, etc.)
|
67 |
+
url = result.entity.get("url") # Retrieve url to fetch image
|
68 |
similarity_score = result.distance # Retrieve similarity score (distance)
|
69 |
|
70 |
# Load the image (you can use PIL to load the image)
|
71 |
#image_path = "/content/drive/MyDrive/images/" + str(image_id) + ".jpg"
|
72 |
+
#image = Image.open(image_path)
|
73 |
+
image = fetch_image(url)
|
74 |
|
75 |
# Append the image, metadata, and score to the search results
|
76 |
search_results.append({
|