Upload 2 files
Browse files- app.py +2 -2
- image_feature.py +4 -4
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import image_feature as func
|
3 |
|
4 |
|
5 |
-
def
|
6 |
return func.infer1(image1, image2)
|
7 |
|
8 |
|
@@ -15,7 +15,7 @@ def work(image1, image2):
|
|
15 |
# demo.launch()
|
16 |
|
17 |
# 定义你的界面
|
18 |
-
with gr.Interface(fn=
|
19 |
inputs=[gr.Textbox(label='图片1', lines=1), gr.Textbox(label='图片2', lines=1)], # 两个文本输入框
|
20 |
outputs=[gr.Textbox(lines=3, label="推理结果")], # 输出为文本
|
21 |
title="图片相似度推理", # 界面标题
|
|
|
2 |
import image_feature as func
|
3 |
|
4 |
|
5 |
+
def work11(image1, image2):
|
6 |
return func.infer1(image1, image2)
|
7 |
|
8 |
|
|
|
15 |
# demo.launch()
|
16 |
|
17 |
# 定义你的界面
|
18 |
+
with gr.Interface(fn=work11,
|
19 |
inputs=[gr.Textbox(label='图片1', lines=1), gr.Textbox(label='图片2', lines=1)], # 两个文本输入框
|
20 |
outputs=[gr.Textbox(lines=3, label="推理结果")], # 输出为文本
|
21 |
title="图片相似度推理", # 界面标题
|
image_feature.py
CHANGED
@@ -54,9 +54,9 @@ model = AutoModel.from_pretrained("google/vit-base-patch16-224").to(DEVICE)
|
|
54 |
|
55 |
|
56 |
# 推理
|
57 |
-
def
|
58 |
# image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
|
59 |
-
image = Image.open(requests.get(
|
60 |
inputs = processor(image, return_tensors="pt").to(DEVICE)
|
61 |
outputs = model(**inputs)
|
62 |
return outputs.pooler_output
|
@@ -64,8 +64,8 @@ def infer(img):
|
|
64 |
|
65 |
# 推理
|
66 |
def infer1(image1, image2):
|
67 |
-
embed_real =
|
68 |
-
embed_gen =
|
69 |
similarity_score = cosine_similarity(embed_real, embed_gen, dim=1)
|
70 |
print(similarity_score)
|
71 |
# 如果你想在CPU上操作这个值,你需要先将tensor移动到CPU
|
|
|
54 |
|
55 |
|
56 |
# 推理
|
57 |
+
def infer2(url):
|
58 |
# image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
|
59 |
+
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
60 |
inputs = processor(image, return_tensors="pt").to(DEVICE)
|
61 |
outputs = model(**inputs)
|
62 |
return outputs.pooler_output
|
|
|
64 |
|
65 |
# 推理
|
66 |
def infer1(image1, image2):
|
67 |
+
embed_real = infer2(image1)
|
68 |
+
embed_gen = infer2(image2)
|
69 |
similarity_score = cosine_similarity(embed_real, embed_gen, dim=1)
|
70 |
print(similarity_score)
|
71 |
# 如果你想在CPU上操作这个值,你需要先将tensor移动到CPU
|