-
Notifications
You must be signed in to change notification settings - Fork 77
/
faiss_clip_search_client_demo.py
49 lines (39 loc) · 1.45 KB
/
faiss_clip_search_client_demo.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description: Use Faiss for image similarity search demo
"""
import sys
import numpy as np
sys.path.append('..')
from similarities import ClipClient, ClipItem
def main():
# Client
client = ClipClient('http://0.0.0.0:8002')
# 获取嵌入,支持获取文本嵌入、图片嵌入
text_input = "This is a sample text."
emb = client.get_emb(text=text_input)
print(f"Embedding for '{text_input}': {emb}")
# input image
image_input = "data/image1.png"
emb = client.get_emb(image=image_input)
print(f"Embedding for '{image_input}': {emb}")
# 获取相似度,支持计算图文相似度、图片相似度
item1 = ClipItem(image="data/image1.png")
item2 = ClipItem(text="老虎")
similarity = client.get_similarity(item1, item2)
print(f"Similarity between item1 and item2: {similarity}")
# 搜索
# 1. 文搜图
search_input = "This is a sample text."
search_results = client.search(text=search_input)
print(f"Search results for '{search_input}': {search_results}")
# 2. 图搜图
search_input = "data/image1.png"
search_results = client.search(image=search_input)
print(f"Search results for '{search_input}': {search_results}")
# 3. 向量搜图
search_results = client.search(emb=np.random.randn(512).tolist())
print(f"Search results for emb search: {search_results}")
if __name__ == '__main__':
main()