Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
def search_image(image):
|
| 8 |
+
# Функция для поиска изображения в TinEye
|
| 9 |
+
def tineye_search(image):
|
| 10 |
+
url = "https://tineye.com/search"
|
| 11 |
+
files = {"image": ("image.jpg", image)}
|
| 12 |
+
response = requests.post(url, files=files)
|
| 13 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 14 |
+
results = soup.find_all("div", class_="result-image")
|
| 15 |
+
return [result.find("img")["src"] for result in results[:5]]
|
| 16 |
+
|
| 17 |
+
# Функция для поиска изображения в Google Images
|
| 18 |
+
def google_search(image):
|
| 19 |
+
url = "https://images.google.com/searchbyimage/upload"
|
| 20 |
+
files = {"encoded_image": ("image.jpg", image)}
|
| 21 |
+
response = requests.post(url, files=files, allow_redirects=False)
|
| 22 |
+
search_url = response.headers["Location"]
|
| 23 |
+
response = requests.get(search_url)
|
| 24 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 25 |
+
results = soup.find_all("img", class_="rg_i")
|
| 26 |
+
return [result["src"] for result in results[:5] if "src" in result.attrs]
|
| 27 |
+
|
| 28 |
+
# Преобразование изображения в байты
|
| 29 |
+
img_byte_arr = io.BytesIO()
|
| 30 |
+
image.save(img_byte_arr, format='JPEG')
|
| 31 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 32 |
+
|
| 33 |
+
# Поиск изображения
|
| 34 |
+
tineye_results = tineye_search(img_byte_arr)
|
| 35 |
+
google_results = google_search(img_byte_arr)
|
| 36 |
+
|
| 37 |
+
return tineye_results, google_results
|
| 38 |
+
|
| 39 |
+
# Создание интерфейса Gradio
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=search_image,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs=[
|
| 44 |
+
gr.Gallery(label="TinEye Results"),
|
| 45 |
+
gr.Gallery(label="Google Images Results")
|
| 46 |
+
],
|
| 47 |
+
title="Image Search Engine",
|
| 48 |
+
description="Upload an image to search for similar images using TinEye and Google Images.",
|
| 49 |
+
theme="huggingface",
|
| 50 |
+
layout="vertical"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Запуск интерфейса
|
| 54 |
+
iface.launch()
|