Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,8 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def find_nearest_neighbors(encoded_images, input_image, top_n=5):
|
| 4 |
"""
|
|
@@ -26,6 +30,45 @@ def get_image(index):
|
|
| 26 |
else:
|
| 27 |
return dataset["test"][index-split]
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
demo = gr.Interface(fn=greet,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
encoded_images = np.load("X_encoded_compressed.npy")
|
| 6 |
|
| 7 |
def find_nearest_neighbors(encoded_images, input_image, top_n=5):
|
| 8 |
"""
|
|
|
|
| 30 |
else:
|
| 31 |
return dataset["test"][index-split]
|
| 32 |
|
| 33 |
+
def process_image(image):
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
def inference(image):
|
| 37 |
+
|
| 38 |
+
input_image = process_image(image)
|
| 39 |
+
|
| 40 |
+
nearest_neighbors = find_nearest_neighbors(encoded_images, input_image, top_n=5)
|
| 41 |
+
|
| 42 |
+
# Print the results
|
| 43 |
+
print("Nearest neighbors (index, distance):")
|
| 44 |
+
for neighbor in nearest_neighbors:
|
| 45 |
+
print(neighbor)
|
| 46 |
+
|
| 47 |
+
top4 = [int(i[0]) for i in nearest_neighbors[:4]]
|
| 48 |
+
print(f"top 4: {top4}")
|
| 49 |
+
|
| 50 |
+
for i in top4:
|
| 51 |
+
im = get_image(i)
|
| 52 |
+
print(im["label"], im["timestamp"])
|
| 53 |
+
|
| 54 |
+
n=2
|
| 55 |
+
plt.figure(figsize=(8, 8))
|
| 56 |
+
for i, (image1, image2) in enumerate(zip(top4[:2], top4[2:])):
|
| 57 |
+
ax = plt.subplot(2, n, i + 1)
|
| 58 |
+
image1 = get_image(image1)["image"]
|
| 59 |
+
image2 = get_image(image2)["image"]
|
| 60 |
+
|
| 61 |
+
plt.imshow(image1)
|
| 62 |
+
plt.gray()
|
| 63 |
+
ax.get_xaxis().set_visible(False)
|
| 64 |
+
ax.get_yaxis().set_visible(False)
|
| 65 |
+
|
| 66 |
+
ax = plt.subplot(2, n, i + 1 + n)
|
| 67 |
+
plt.imshow(image2)
|
| 68 |
+
plt.gray()
|
| 69 |
+
ax.get_xaxis().set_visible(False)
|
| 70 |
+
ax.get_yaxis().set_visible(False)
|
| 71 |
+
|
| 72 |
|
| 73 |
|
| 74 |
demo = gr.Interface(fn=greet,
|