Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- app.py +108 -0
- face_feature_extractor.ipynb +0 -0
- features.npy +3 -0
- image_paths.pkl +3 -0
- label_dict.json +1 -0
- labels.npy +3 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from facenet_pytorch import InceptionResnetV1
|
| 6 |
+
import numpy as np
|
| 7 |
+
import json
|
| 8 |
+
import pickle
|
| 9 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 10 |
+
from mtcnn import MTCNN
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="FaceTwin AI", page_icon="🤖", layout="centered")
|
| 13 |
+
|
| 14 |
+
st.title("FaceTwin AI 🤖")
|
| 15 |
+
st.subheader("Find your closest celebrity match through AI-powered facial similarity 📸 ⭐")
|
| 16 |
+
st.write("---")
|
| 17 |
+
|
| 18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@st.cache_resource(show_spinner="Loading VGGFace2 model...")
|
| 22 |
+
def load_vgg_model():
|
| 23 |
+
model = InceptionResnetV1(pretrained='vggface2').eval().to(device)
|
| 24 |
+
return model
|
| 25 |
+
|
| 26 |
+
@st.cache_resource(show_spinner="Initializing face detector...")
|
| 27 |
+
def load_face_detector():
|
| 28 |
+
return MTCNN()
|
| 29 |
+
|
| 30 |
+
@st.cache_data(show_spinner="Loading embeddings and metadata...")
|
| 31 |
+
def load_feature_data():
|
| 32 |
+
features_list = np.load("features.npy")
|
| 33 |
+
labels_list = np.load("labels.npy")
|
| 34 |
+
with open("image_paths.pkl", "rb") as f:
|
| 35 |
+
imgs_path = pickle.load(f)
|
| 36 |
+
with open("label_dict.json", "r") as f:
|
| 37 |
+
label_dict = json.load(f)
|
| 38 |
+
return features_list, labels_list, imgs_path, label_dict
|
| 39 |
+
|
| 40 |
+
vgg_feature_extractor = load_vgg_model()
|
| 41 |
+
face_detector = load_face_detector()
|
| 42 |
+
features_list, labels_list, imgs_path, label_dict = load_feature_data()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
transformer = transforms.Compose([
|
| 47 |
+
transforms.Resize((160, 160)),
|
| 48 |
+
transforms.ToTensor(),
|
| 49 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5],
|
| 50 |
+
std=[0.5, 0.5, 0.5])
|
| 51 |
+
])
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def predict_actor(img):
|
| 57 |
+
img = np.array(img)
|
| 58 |
+
results = face_detector.detect_faces(img)
|
| 59 |
+
|
| 60 |
+
if len(results) == 0:
|
| 61 |
+
return None, None, "No face detected. Please upload a clearer image."
|
| 62 |
+
|
| 63 |
+
x, y, width, height = results[0]['box']
|
| 64 |
+
face = img[y:y + height, x:x + width]
|
| 65 |
+
face = Image.fromarray(face).convert("RGB")
|
| 66 |
+
|
| 67 |
+
img_tensor = transformer(face).unsqueeze(0).to(device)
|
| 68 |
+
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
features = vgg_feature_extractor(img_tensor)
|
| 71 |
+
|
| 72 |
+
flattened_features = features.flatten().cpu().numpy().astype('float16')
|
| 73 |
+
|
| 74 |
+
similarities = cosine_similarity([flattened_features], features_list)
|
| 75 |
+
best_index = np.argmax(similarities)
|
| 76 |
+
|
| 77 |
+
label = str(labels_list[best_index])
|
| 78 |
+
name = label_dict[label]
|
| 79 |
+
img_path = imgs_path[best_index]
|
| 80 |
+
|
| 81 |
+
return name, img_path, None
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# ===============================
|
| 85 |
+
# 🎛️ STREAMLIT UI
|
| 86 |
+
# ===============================
|
| 87 |
+
upload_img = st.file_uploader("Upload your image", type=["jpg", "jpeg", "png"])
|
| 88 |
+
|
| 89 |
+
if st.button("Find my Celebrity Match ⭐"):
|
| 90 |
+
if upload_img is not None:
|
| 91 |
+
img = Image.open(upload_img)
|
| 92 |
+
|
| 93 |
+
with st.spinner("Analyzing your face..."):
|
| 94 |
+
name, img2_path, error_msg = predict_actor(img)
|
| 95 |
+
|
| 96 |
+
if error_msg:
|
| 97 |
+
st.error(error_msg)
|
| 98 |
+
else:
|
| 99 |
+
img2 = Image.open(img2_path)
|
| 100 |
+
col1, col2 = st.columns(2)
|
| 101 |
+
with col1:
|
| 102 |
+
st.image(img, caption="You", use_container_width=True)
|
| 103 |
+
with col2:
|
| 104 |
+
st.image(img2, caption=f"Your Celebrity Twin: {name}", use_container_width=True)
|
| 105 |
+
|
| 106 |
+
st.success(f"🎉 You look like **{name}**!")
|
| 107 |
+
else:
|
| 108 |
+
st.warning("⚠️ Please upload an image first.")
|
face_feature_extractor.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
features.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3ae95ff240a59ce4eacf783a2c2efaa20047ac3b69905e1f42ed0d32efa29855
|
| 3 |
+
size 8872064
|
image_paths.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:820ce83d5a4bad7ebe7f3611f3a23701380e73f5866913c0e0fe39bce0d562cf
|
| 3 |
+
size 790969
|
label_dict.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"0": "Arjun Rampal", "1": "Amitabh Bachchan", "2": "Farhan Akhtar", "3": "Amy Jackson", "4": "Akshay Kumar", "5": "Anil Kapoor", "6": "Abhay Deol", "7": "Abhishek Bachchan", "8": "Emraan Hashmi", "9": "Bipasha Basu", "10": "Deepika Padukone", "11": "Aishwarya Rai", "12": "Arjun Kapoor", "13": "Ileana DCruz", "14": "Huma Qureshi", "15": "Bobby Deol", "16": "Aftab Shivdasani", "17": "Bhumi Pednekar", "18": "Arshad Warsi", "19": "Govinda", "20": "Disha Patani", "21": "Anushka Sharma", "22": "Ajay Devgn", "23": "Akshaye Khanna", "24": "Asin", "25": "Alia Bhatt", "26": "Esha Gupta", "27": "Anushka Shetty", "28": "Amrita Rao", "29": "Hrithik Roshan", "30": "Ameesha Patel", "31": "Ayushmann Khurrana", "32": "Aamir Khan", "33": "Zareen Khan", "34": "Sidharth Malhotra", "35": "Sunny Deol", "36": "Tamannaah Bhatia", "37": "Saif Ali Khan", "38": "Vivek Oberoi", "39": "Varun Dhawan", "40": "Randeep Hooda", "41": "Uday Chopra", "42": "Shraddha Kapoor", "43": "Sonakshi Sinha", "44": "Rani Mukerji", "45": "Richa Chadda", "46": "Yami Gautam", "47": "Vidya Balan", "48": "Shruti Haasan", "49": "Sonam Kapoor", "50": "Suniel Shetty", "51": "Vaani Kapoor", "52": "Salman Khan", "53": "Shahid Kapoor", "54": "Sara Ali Khan", "55": "Riteish Deshmukh", "56": "Taapsee Pannu", "57": "Ranveer Singh", "58": "Shah Rukh Khan", "59": "Tusshar Kapoor", "60": "Shreyas Talpade", "61": "Tiger Shroff", "62": "Sushant Singh Rajput", "63": "Tabu", "64": "Vicky Kaushal", "65": "Shilpa Shetty", "66": "Sanjay Dutt", "67": "Jacqueline Fernandez", "68": "Madhuri Dixit", "69": "Manoj Bajpayee", "70": "Katrina Kaif", "71": "Kajal Aggarwal", "72": "John Abraham", "73": "Lara Dutta", "74": "Priyanka Chopra", "75": "Kareena Kapoor", "76": "Irrfan Khan", "77": "Kartik Aaryan", "78": "Nargis Fakhri", "79": "Kunal Khemu", "80": "Rajkummar Rao", "81": "Prabhas", "82": "Kangana Ranaut", "83": "Naseeruddin Shah", "84": "Nushrat Bharucha", "85": "Kriti Sanon", "86": "Ranbir Kapoor", "87": "Pooja Hegde", "88": "Nana Patekar", "89": "Mrunal Thakur", "90": "Juhi Chawla", "91": "Paresh Rawal", "92": "Kiara Advani", "93": "Kriti Kharbanda", "94": "Preity Zinta", "95": "Prachi Desai", "96": "Parineeti Chopra", "97": "Kajol", "98": "Karisma Kapoor", "99": "R Madhavan"}
|
labels.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4a056e0d1f5c6cd9fa6c32cd2218fe84c5c3116b81d0e9261fac03778dab8207
|
| 3 |
+
size 17456
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.51.0
|
| 2 |
+
facenet-pytorch==2.6.0
|
| 3 |
+
torch==2.2.2
|
| 4 |
+
torchvision==0.17.2
|
| 5 |
+
numpy==1.26.4
|
| 6 |
+
pillow==10.2.0
|
| 7 |
+
mtcnn==1.0.0
|
| 8 |
+
pillow==10.2.0
|
| 9 |
+
scikit-learn==1.7.2
|
| 10 |
+
tensorflow==2.19.0
|