Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,106 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
|
|
|
| 3 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
-
# Initialize
|
| 9 |
mtcnn = MTCNN(image_size=160, margin=20, keep_all=False)
|
| 10 |
resnet = InceptionResnetV1(pretrained='vggface2').eval()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
else:
|
| 19 |
-
return
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
for url in urls:
|
| 33 |
-
path = download_image(url)
|
| 34 |
-
if path:
|
| 35 |
-
images.append(Image.open(path))
|
| 36 |
-
return images
|
| 37 |
-
|
| 38 |
-
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("# 🔍 Face Similarity Checker with Web Scraping")
|
| 40 |
-
|
| 41 |
-
with gr.Tab("Compare Uploaded Images"):
|
| 42 |
-
with gr.Row():
|
| 43 |
-
img1 = gr.Image(label="Image 1")
|
| 44 |
-
img2 = gr.Image(label="Image 2")
|
| 45 |
-
compare_button = gr.Button("Compare Faces")
|
| 46 |
-
result = gr.Textbox(label="Result")
|
| 47 |
-
compare_button.click(fn=compare_faces, inputs=[img1, img2], outputs=result)
|
| 48 |
-
|
| 49 |
-
with gr.Tab("Fetch Images from Web"):
|
| 50 |
-
query = gr.Textbox(label="Search Query")
|
| 51 |
-
fetch_button = gr.Button("Fetch Images")
|
| 52 |
-
gallery = gr.Gallery(label="Fetched Images")
|
| 53 |
-
fetch_button.click(fn=fetch_and_display_images, inputs=query, outputs=gallery)
|
| 54 |
-
|
| 55 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 7 |
+
from torch.nn.functional import cosine_similarity
|
| 8 |
+
import requests
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
+
from io import BytesIO
|
| 11 |
|
| 12 |
+
# Initialize models
|
| 13 |
mtcnn = MTCNN(image_size=160, margin=20, keep_all=False)
|
| 14 |
resnet = InceptionResnetV1(pretrained='vggface2').eval()
|
| 15 |
|
| 16 |
+
# Directory for temporary images
|
| 17 |
+
DB_DIR = "scraped_images"
|
| 18 |
+
os.makedirs(DB_DIR, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# Scrape image URLs from Bing
|
| 21 |
+
def fetch_image_urls(query, max_images=3):
|
| 22 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 23 |
+
search_url = f"https://www.bing.com/images/search?q={query.replace(' ', '+')}"
|
| 24 |
+
response = requests.get(search_url, headers=headers)
|
| 25 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 26 |
+
image_elements = soup.find_all('a', class_='iusc')
|
| 27 |
+
urls = []
|
| 28 |
+
for elem in image_elements[:max_images]:
|
| 29 |
+
m = elem.get('m')
|
| 30 |
+
if m:
|
| 31 |
+
try:
|
| 32 |
+
m_json = eval(m)
|
| 33 |
+
urls.append(m_json['murl'])
|
| 34 |
+
except:
|
| 35 |
+
continue
|
| 36 |
+
return urls
|
| 37 |
+
|
| 38 |
+
# Download and save images
|
| 39 |
+
def download_images(image_urls):
|
| 40 |
+
for i, url in enumerate(image_urls):
|
| 41 |
+
try:
|
| 42 |
+
response = requests.get(url)
|
| 43 |
+
img = Image.open(BytesIO(response.content)).convert('RGB')
|
| 44 |
+
img.save(os.path.join(DB_DIR, f"scraped_{i}.jpg"))
|
| 45 |
+
except:
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
# Load embeddings for downloaded images
|
| 49 |
+
def load_scraped_embeddings():
|
| 50 |
+
embeddings = {}
|
| 51 |
+
for file in os.listdir(DB_DIR):
|
| 52 |
+
if file.lower().endswith(('.jpg', '.png')):
|
| 53 |
+
img_path = os.path.join(DB_DIR, file)
|
| 54 |
+
img = Image.open(img_path).convert("RGB")
|
| 55 |
+
face = mtcnn(img)
|
| 56 |
+
if face is not None:
|
| 57 |
+
face = face.unsqueeze(0)
|
| 58 |
+
emb = resnet(face)
|
| 59 |
+
embeddings[file] = emb
|
| 60 |
+
return embeddings
|
| 61 |
+
|
| 62 |
+
# Compare uploaded face with scraped images
|
| 63 |
+
def identify_person(uploaded_img, search_query):
|
| 64 |
+
if uploaded_img is None:
|
| 65 |
+
return "Please upload an image."
|
| 66 |
+
|
| 67 |
+
# Step 1: Scrape and download images
|
| 68 |
+
for f in os.listdir(DB_DIR):
|
| 69 |
+
os.remove(os.path.join(DB_DIR, f))
|
| 70 |
+
image_urls = fetch_image_urls(search_query, max_images=5)
|
| 71 |
+
download_images(image_urls)
|
| 72 |
+
|
| 73 |
+
# Step 2: Load scraped embeddings
|
| 74 |
+
db_embeddings = load_scraped_embeddings()
|
| 75 |
+
|
| 76 |
+
# Step 3: Get uploaded face embedding
|
| 77 |
+
uploaded_face = mtcnn(uploaded_img.convert("RGB"))
|
| 78 |
+
if uploaded_face is None:
|
| 79 |
+
return "No face detected in the uploaded image."
|
| 80 |
+
uploaded_embedding = resnet(uploaded_face.unsqueeze(0))
|
| 81 |
+
|
| 82 |
+
# Step 4: Compare and find best match
|
| 83 |
+
best_match = None
|
| 84 |
+
best_score = -1
|
| 85 |
+
for name, emb in db_embeddings.items():
|
| 86 |
+
score = cosine_similarity(uploaded_embedding, emb).item()
|
| 87 |
+
if score > best_score:
|
| 88 |
+
best_score = score
|
| 89 |
+
best_match = name
|
| 90 |
+
|
| 91 |
+
if best_score > 0.7:
|
| 92 |
+
return f"Best Match: {best_match}\nSimilarity: {best_score:.2f}"
|
| 93 |
else:
|
| 94 |
+
return f"No confident match found.\nBest candidate: {best_match} (Score: {best_score:.2f})"
|
| 95 |
+
|
| 96 |
+
# Gradio Interface
|
| 97 |
+
iface = gr.Interface(
|
| 98 |
+
fn=identify_person,
|
| 99 |
+
inputs=[gr.Image(type="pil"), gr.Text(label="Search Query (e.g., Elon Musk)")],
|
| 100 |
+
outputs="text",
|
| 101 |
+
title="Reverse Face Search via Web Scraping",
|
| 102 |
+
description="Upload a face image and enter a name or query. The app scrapes the web for images and tries to identify the person."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|