-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_encoder.py
34 lines (26 loc) · 1.08 KB
/
test_encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from clip_encoder import ClipEncoder
import time
def main():
# Initialize the encoder
encoder = ClipEncoder()
# Encode the images in the static/images directory
encoder.encode_images('static/images')
# Save the index
encoder.save_index()
# Test a text search
print("\nTesting text search...")
query = "a beautiful landscape"
results = encoder.search(query, k=3)
print(f"Top 3 results for '{query}':")
for i, result in enumerate(results):
print(f"{i+1}. {result['filename']} (score: {result['score']:.3f})")
# If there are results, test image search with the first result
if results:
print("\nTesting image search...")
image_path = results[0]['image_path']
image_results = encoder.search_by_image(image_path, k=3)
print(f"Top 3 similar images to {results[0]['filename']}:")
for i, result in enumerate(image_results):
print(f"{i+1}. {result['filename']} (score: {result['score']:.3f})")
if __name__ == "__main__":
main()