Upload 11 files
Browse files- .gitattributes +2 -0
- app.py +66 -0
- features_matrix.pkl +3 -0
- heels.jpg +3 -0
- image_names.pkl +3 -0
- images.csv +0 -0
- kurti.jpg +0 -0
- necklace.jpg +0 -0
- perfume.jpg +0 -0
- shirt.jpg +3 -0
- shoes.jpg +0 -0
- watches.jpg +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
heels.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
shirt.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import io
|
6 |
+
import pickle
|
7 |
+
from tensorflow.keras.applications import VGG16
|
8 |
+
from tensorflow.keras.preprocessing import image
|
9 |
+
from tensorflow.keras.applications.vgg16 import preprocess_input
|
10 |
+
from tensorflow.keras.models import Model
|
11 |
+
import gradio as gr
|
12 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
13 |
+
|
14 |
+
# Load precomputed data
|
15 |
+
with open('image_names.pkl', 'rb') as file:
|
16 |
+
image_names = pickle.load(file)
|
17 |
+
|
18 |
+
with open('features_matrix.pkl', 'rb') as file:
|
19 |
+
features_matrix = pickle.load(file)
|
20 |
+
|
21 |
+
image_df = pd.read_csv('images.csv')
|
22 |
+
|
23 |
+
# VGG16
|
24 |
+
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
25 |
+
model = Model(inputs=base_model.input, outputs=base_model.output)
|
26 |
+
|
27 |
+
def extract_feature_from_image(img, model):
|
28 |
+
"""Extract features from a single image using the model."""
|
29 |
+
img = preprocess_image(img)
|
30 |
+
features = model.predict(img)
|
31 |
+
return features.reshape(-1) # Flatten the feature vector
|
32 |
+
|
33 |
+
def preprocess_image(img, target_size=(224, 224)):
|
34 |
+
"""Preprocess a single image for feature extraction."""
|
35 |
+
img = img.resize(target_size)
|
36 |
+
img_array = image.img_to_array(img)
|
37 |
+
img_array = np.expand_dims(img_array, axis=0)
|
38 |
+
img_array = preprocess_input(img_array)
|
39 |
+
return img_array
|
40 |
+
|
41 |
+
def find_similar_images(custom_image_features, features_matrix, top_n=5):
|
42 |
+
"""Find the most similar images to the custom image."""
|
43 |
+
similarities = cosine_similarity([custom_image_features], features_matrix)
|
44 |
+
most_similar_indices = np.argsort(similarities[0])[::-1][:top_n]
|
45 |
+
return most_similar_indices
|
46 |
+
|
47 |
+
def recommend_similar_images(img):
|
48 |
+
"""Recommend similar images based on the uploaded image."""
|
49 |
+
query_feature = extract_feature_from_image(img, model)
|
50 |
+
similar_indices = find_similar_images(query_feature, features_matrix, top_n=5)
|
51 |
+
similar_images = [image_df['link'][image_df['filename'] == image_names[idx]].values[0] for idx in similar_indices]
|
52 |
+
return similar_images
|
53 |
+
|
54 |
+
def get_recommendations_from_image(uploaded_image):
|
55 |
+
"""Recommend similar images based on the uploaded image."""
|
56 |
+
img = uploaded_image
|
57 |
+
similar_image_urls = recommend_similar_images(img)
|
58 |
+
similar_images = [Image.open(requests.get(img_url, stream=True).raw) for img_url in similar_image_urls]
|
59 |
+
return similar_images
|
60 |
+
|
61 |
+
# Gradio interface
|
62 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
63 |
+
outputs = gr.Gallery(label="Recommended Images", columns=3) # Set the number of columns to fit images better
|
64 |
+
|
65 |
+
gr.Interface(fn=get_recommendations_from_image, inputs=image_input, outputs=outputs, examples=['shirt.jpg','perfume.jpg','shoes.jpg','kurti.jpg','watches.jpg','heels.jpg','necklace.jpg'],
|
66 |
+
title="Fashion Image Recommender").launch()
|
features_matrix.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:655b407275b04f831035488161a9ad0b648626ea68933bd0d7063bbbca42708a
|
3 |
+
size 3449800867
|
heels.jpg
ADDED
Git LFS Details
|
image_names.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:42eb7d6e242d7573d59510126c809b8747d25695c442dd8f9e2b9383c4d81be6
|
3 |
+
size 407150
|
images.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
kurti.jpg
ADDED
necklace.jpg
ADDED
perfume.jpg
ADDED
shirt.jpg
ADDED
Git LFS Details
|
shoes.jpg
ADDED
watches.jpg
ADDED