Shrikrishna commited on
Commit
ec77159
1 Parent(s): 0b6cffd

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +99 -0
  2. embedding.pkl +3 -0
  3. filenames.pkl +3 -0
  4. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow import keras
2
+ from keras.preprocessing import image
3
+ import pickle
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ import streamlit as st
6
+ from PIL import Image
7
+ import os
8
+ import cv2
9
+ from mtcnn import MTCNN
10
+ import numpy as np
11
+
12
+
13
+ #keras.applications.resnet50.ResNet50
14
+
15
+ #VGGFace(model='resnet50',include_top=False,input_shape=(224,224,3),pooling='avg')
16
+
17
+ #st.text("Hello Welcome")
18
+ detector = MTCNN()
19
+ model = keras.applications.resnet50.ResNet50(
20
+ include_top=False,
21
+ input_shape=(224,224,3),
22
+ pooling='avg',
23
+ weights='imagenet'
24
+ )
25
+ feature_list = pickle.load(open('embedding.pkl', 'rb'))
26
+ filenames = pickle.load(open('filenames.pkl', 'rb'))
27
+
28
+ filenames = [sub.replace('/kaggle/input/bollywood-celeb-localized-face-dataset/', 'https://technirmitisoftwares.com/img_data/data/') for sub in filenames]
29
+
30
+ def save_uploaded_image(uploaded_image):
31
+ try:
32
+ with open(uploaded_image.name, 'wb') as f:
33
+ f.write(uploaded_image.getbuffer())
34
+ return True
35
+ except:
36
+ return False
37
+
38
+
39
+ def extract_features(img_path, model, detector):
40
+ img = cv2.imread(img_path)
41
+ results = detector.detect_faces(img)
42
+
43
+ x, y, width, height = results[0]['box']
44
+
45
+ face = img[y:y + height, x:x + width]
46
+
47
+ # extract its features
48
+ image = Image.fromarray(face)
49
+ image = image.resize((224, 224))
50
+
51
+ face_array = np.asarray(image)
52
+
53
+ face_array = face_array.astype('float32')
54
+
55
+ expanded_img = np.expand_dims(face_array, axis=0)
56
+ preprocessed_img = keras.applications.resnet50.preprocess_input(expanded_img)
57
+ result = model.predict(preprocessed_img).flatten()
58
+ return result
59
+
60
+
61
+ def recommend(feature_list,features):
62
+ similarity = []
63
+ for i in range(len(feature_list)):
64
+ similarity.append(cosine_similarity(features.reshape(1, -1), feature_list[i].reshape(1, -1))[0][0])
65
+
66
+ index_pos = sorted(list(enumerate(similarity)), reverse=True, key=lambda x: x[1])[0][0]
67
+ return index_pos
68
+
69
+
70
+
71
+ st.title('Which bollywood celebrity are you?')
72
+
73
+ uploaded_image = st.file_uploader('Choose an image')
74
+
75
+ if uploaded_image is not None:
76
+ # save the image in a directory
77
+ if save_uploaded_image(uploaded_image):
78
+ display_image = Image.open(uploaded_image)
79
+ st.header("Image Uploded!, Processing...")
80
+ #st.image(display_image)
81
+
82
+ # extract the features
83
+ features = extract_features(uploaded_image.name, model, detector)
84
+ #st.text(features)
85
+ #st.text(features.shape)
86
+
87
+ # recommend
88
+ index_pos = recommend(feature_list,features)
89
+ predicted_actor = filenames[index_pos]
90
+ #st.header(predicted_actor)
91
+ # display
92
+ col1,col2 = st.columns(2)
93
+
94
+ with col1:
95
+ st.header('Your uploaded image')
96
+ st.image(display_image,width=150)
97
+ with col2:
98
+ st.header("Look Like: " + predicted_actor.split("/")[7])
99
+ st.image(filenames[index_pos],width=150)
embedding.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c7f487b833163517d66a9a234f933104d4e4e10a5e9db757aa3098fc81b8407a
3
+ size 71477400
filenames.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:96eb16a7402a2c5dc1c7bd55fef34d672cb25c0cd9f0e8538bca606a50453d32
3
+ size 1309928
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ mtcnn
2
+ tensorflow
3
+ keras
4
+ Keras
5
+ keras-vggface
6
+ Keras_Vggface
7
+ keras_applications
8
+ Keras_Applications
9
+ numpy
10
+ scikit-learn