Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as s
|
| 2 |
+
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification, pipeline
|
| 3 |
+
|
| 4 |
+
!wget http://vis-www.cs.umass.edu/lfw/lfw.tgz
|
| 5 |
+
!tar -xvf /content/lfw.tgz
|
| 6 |
+
|
| 7 |
+
!pip install tensorflow
|
| 8 |
+
!pip install tqdm
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import numpy as np
|
| 12 |
+
import tensorflow as tf
|
| 13 |
+
from tensorflow.keras.applications import ResNet50
|
| 14 |
+
from tensorflow.keras.applications.resnet import preprocess_input
|
| 15 |
+
from tensorflow.keras.preprocessing import image
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from tqdm import tqdm
|
| 18 |
+
|
| 19 |
+
resnet50_model = ResNet50(weights='imagenet', include_top=False, pooling='avg')
|
| 20 |
+
|
| 21 |
+
def get_image_features(img_path):
|
| 22 |
+
loaded_img = image.load_img(img_path, target_size=(224, 224))
|
| 23 |
+
img_array = image.img_to_array(loaded_img)
|
| 24 |
+
expanded_array = np.expand_dims(img_array, axis=0)
|
| 25 |
+
preprocessed_img = preprocess_input(expanded_array)
|
| 26 |
+
features = resnet50_model.predict(preprocessed_img)
|
| 27 |
+
return features.flatten()
|
| 28 |
+
|
| 29 |
+
extracted_features = {}
|
| 30 |
+
|
| 31 |
+
lfw_dir = '/content/lfw'
|
| 32 |
+
all_files = []
|
| 33 |
+
|
| 34 |
+
for root_dir, subdir_list, file_list in os.walk(lfw_dir):
|
| 35 |
+
for file_name in file_list:
|
| 36 |
+
if file_name.endswith('.jpg'):
|
| 37 |
+
img_path = os.path.join(root_dir, file_name)
|
| 38 |
+
all_files.append(img_path)
|
| 39 |
+
|
| 40 |
+
for file_path in tqdm(all_files, desc="Processing images"):
|
| 41 |
+
img_features = get_image_features(file_path)
|
| 42 |
+
file_name = os.path.basename(file_path)
|
| 43 |
+
extracted_features[file_name] = img_features
|
| 44 |
+
|
| 45 |
+
from sklearn.neighbors import NearestNeighbors
|
| 46 |
+
|
| 47 |
+
def find_similar_images(target_image, feature_dictionary, num_neighbors=10):
|
| 48 |
+
img_names = list(feature_dictionary.keys())
|
| 49 |
+
features_list = np.array([feature_dictionary[name] for name in img_names])
|
| 50 |
+
neighbors_model = NearestNeighbors(n_neighbors=num_neighbors, algorithm='auto', metric='euclidean')
|
| 51 |
+
neighbors_model.fit(features_list)
|
| 52 |
+
target_image_feature = feature_dictionary[target_image].reshape(1, -1)
|
| 53 |
+
_, img_indices = neighbors_model.kneighbors(target_image_feature)
|
| 54 |
+
retrieved_images = [img_names[index] for index in img_indices.flatten()]
|
| 55 |
+
return retrieved_images
|
| 56 |
+
|
| 57 |
+
query_img = 'Francis_Ricciardone_0001.jpg'
|
| 58 |
+
similar_imgs = find_similar_images(query_img, extracted_features, num_neighbors=11)
|
| 59 |
+
|
| 60 |
+
print(f"Images similar to {query_img}:")
|
| 61 |
+
for count, img_name in enumerate(similar_imgs[1:], start=1):
|
| 62 |
+
print(f"{count}: {img_name}")
|
| 63 |
+
|
| 64 |
+
|