|
|
|
|
|
import requests |
|
import numpy as np |
|
import pandas as pd |
|
from io import BytesIO |
|
import pickle |
|
|
|
import gradio as gr |
|
from PIL import Image as PILIMAGE |
|
from IPython.display import Image |
|
from IPython.core.display import HTML |
|
|
|
from embedding_model import EmbeddingModel |
|
|
|
|
|
|
|
|
|
embed_model = EmbeddingModel() |
|
|
|
|
|
imgs_info_df = pd.read_csv('15K Nocturna Valencia Banco Mediolanum_info.csv') |
|
|
|
|
|
with open('15K_Nocturna_Valencia_Banco_Mediolanum_embeddings.pkl', 'rb') as handle: |
|
embed_dict = pickle.load(handle) |
|
|
|
img_names_list = list(embed_dict.keys()) |
|
img_embeddings_np = np.array(list(embed_dict.values())) |
|
|
|
|
|
|
|
def find_best_matches(text_search, num_imgs=9): |
|
|
|
indexes = embed_model.get_similar_images_indexes(img_embeddings_np, text_search, n=num_imgs) |
|
target_img_filenames = [img_names_list[index] for index in indexes] |
|
target_urls = [] |
|
for img_name in target_img_filenames: |
|
item = imgs_info_df[imgs_info_df['filename'] == img_name.rsplit('.', 1)[0]] |
|
target_urls.append(item['url'].values[0]) |
|
|
|
matched_images = [] |
|
for i, url in enumerate(target_urls): |
|
|
|
response = requests.get(url + "?w=640") |
|
img = PILIMAGE.open(BytesIO(response.content)) |
|
matched_images.append(img) |
|
|
|
return matched_images |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
title = "15K Nocturna valencia image search engine 📸", |
|
description = "Image search engine from a text input for the photo gallery of 15k Nocturna Valencia running race based on their semantic content.", |
|
article = "You find more information about this demo on my ✨ github repository [davertor](https://github.com/davertor/image-search-engine-for-flickr-photos-gallery)", |
|
fn=find_best_matches, |
|
inputs=[ |
|
gr.Textbox( |
|
lines=1, |
|
label="Write what you are looking for in an image...", |
|
placeholder="Text Here..."), |
|
gr.Slider(1, 9, step=1, value=3) |
|
], |
|
outputs=gr.Gallery().style(grid=[1], height="auto"), |
|
examples=[ |
|
[("persons in the podium"), 3], |
|
[("a man with orange t-shirt"), 3], |
|
] |
|
).launch() |