Spaces:
Runtime error
Runtime error
Samuel Schmidt
commited on
Commit
·
f365545
1
Parent(s):
53f6094
Update src/app.py
Browse files- src/app.py +17 -13
src/app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from colordescriptor import ColorDescriptor
|
2 |
from CLIP import CLIPImageEncoder
|
3 |
from LBP import LBPImageEncoder
|
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
import cv2
|
@@ -50,6 +51,12 @@ def check_index(ds):
|
|
50 |
return index_dataset(ds)
|
51 |
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
dataset_with_embeddings = check_index(candidate_subset)
|
54 |
|
55 |
# Main function, to find similar images
|
@@ -69,8 +76,16 @@ def get_neighbors(query_image, selected_descriptor, top_k=5):
|
|
69 |
cd = ColorDescriptor((8, 12, 3))
|
70 |
qi_embedding = cd.describe(query_image)
|
71 |
qi_np = np.array(qi_embedding)
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
images = retrieved_examples['image'] #retrieved images is a dict, with images and embeddings
|
75 |
return images
|
76 |
if "CLIP" == selected_descriptor:
|
@@ -126,17 +141,6 @@ with gr.Blocks() as demo:
|
|
126 |
)
|
127 |
btn.click(get_neighbors, inputs=[image_input, descr_dropdown], outputs=[gallery_output])
|
128 |
|
129 |
-
# gr.Markdown(
|
130 |
-
# """
|
131 |
-
# ## Upload your own data to the CBIR
|
132 |
-
# WARNING! Please be aware, that what you are uploading here, will be public to anyone.
|
133 |
-
# Don't upload images for which you don't have the rights to do so.
|
134 |
-
# """)
|
135 |
-
|
136 |
-
# file_output = gr.File()
|
137 |
-
# upload_button = gr.UploadButton("Click to upload a file", file_types=["image"], file_count="multiple")
|
138 |
-
# upload_button.upload(upload_file, upload_button, file_output)
|
139 |
-
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
demo.launch()
|
|
|
1 |
from colordescriptor import ColorDescriptor
|
2 |
from CLIP import CLIPImageEncoder
|
3 |
from LBP import LBPImageEncoder
|
4 |
+
from helper import chi2_distance, euclidian_distance
|
5 |
import gradio as gr
|
6 |
import os
|
7 |
import cv2
|
|
|
51 |
return index_dataset(ds)
|
52 |
|
53 |
|
54 |
+
def find_similar_images(method):
|
55 |
+
if method == "FAISS":
|
56 |
+
|
57 |
+
return retrieved examples
|
58 |
+
|
59 |
+
|
60 |
dataset_with_embeddings = check_index(candidate_subset)
|
61 |
|
62 |
# Main function, to find similar images
|
|
|
76 |
cd = ColorDescriptor((8, 12, 3))
|
77 |
qi_embedding = cd.describe(query_image)
|
78 |
qi_np = np.array(qi_embedding)
|
79 |
+
if distance_measure == "FAISS":
|
80 |
+
scores, retrieved_examples = dataset_with_embeddings.get_nearest_examples(
|
81 |
+
'color_embeddings', qi_np, k=top_k)
|
82 |
+
elif distance_measure == "Chi-squared":
|
83 |
+
tmp_dataset = dataset_with_embeddings.map(lambda row: {'distance': chi2_distance(histA=query_vector, histB=row['color_embeddings'])})
|
84 |
+
retrieved_examples = tmp_dataset.sort("distance")
|
85 |
+
else:
|
86 |
+
tmp_dataset = dataset_with_embeddings.map(lambda row: {'distance': euclidian_distance(histA=query_vector, histB=row['color_embeddings'])})
|
87 |
+
retrieved_examples = tmp_dataset.sort("distance")
|
88 |
+
|
89 |
images = retrieved_examples['image'] #retrieved images is a dict, with images and embeddings
|
90 |
return images
|
91 |
if "CLIP" == selected_descriptor:
|
|
|
141 |
)
|
142 |
btn.click(get_neighbors, inputs=[image_input, descr_dropdown], outputs=[gallery_output])
|
143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
demo.launch()
|