Spaces:
Runtime error
Runtime error
Shrikrishna
commited on
Commit
•
02c83d1
1
Parent(s):
3d37a7c
Upload 4 files
Browse files- app.py +192 -0
- haarcascade_eye.xml +0 -0
- haarcascade_frontalface_default.xml +0 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
import base64
|
4 |
+
import json
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
+
import pywt
|
8 |
+
import joblib
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
__class_name_to_number = {}
|
14 |
+
__class_number_to_name = {}
|
15 |
+
__model = None
|
16 |
+
|
17 |
+
st.header("Welcome to Sports Person Classifier!")
|
18 |
+
col1,col2,col3,col4,col5 = st.columns(5)
|
19 |
+
with col1:
|
20 |
+
messi = cv2.imread("messi.jpeg")
|
21 |
+
#st.header("Lionel Messi")
|
22 |
+
st.image(messi,width=150, caption='Lionel Messi')
|
23 |
+
with col2:
|
24 |
+
maria = cv2.imread("sharapova.jpeg")
|
25 |
+
#st.header("Maria Sharapova")
|
26 |
+
st.image(maria,width=150, caption='Maria Sharapova')
|
27 |
+
with col3:
|
28 |
+
roger = cv2.imread("federer.jpeg")
|
29 |
+
#st.header("Roger Federer")
|
30 |
+
st.image(roger,width=150, caption='Roger Federer')
|
31 |
+
with col4:
|
32 |
+
serena = cv2.imread("serena.jpeg")
|
33 |
+
#st.header("Serena Williams")
|
34 |
+
st.image(serena,width=150, caption='Serena Williams')
|
35 |
+
with col5:
|
36 |
+
virat = cv2.imread("virat.jpeg")
|
37 |
+
#st.header("Virat Kohli")
|
38 |
+
st.image(virat,width=150, caption='Virat Kohli')
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
def classify_image(image_base64_data, file_path=None):
|
43 |
+
|
44 |
+
imgs = get_cropped_image_if_2_eyes_new(file_path, image_base64_data)
|
45 |
+
|
46 |
+
result = []
|
47 |
+
for img in imgs:
|
48 |
+
scalled_raw_img = cv2.resize(img, (32, 32))
|
49 |
+
img_har = w2d(img, 'db1', 5)
|
50 |
+
scalled_img_har = cv2.resize(img_har, (32, 32))
|
51 |
+
combined_img = np.vstack((scalled_raw_img.reshape(32 * 32 * 3, 1), scalled_img_har.reshape(32 * 32, 1)))
|
52 |
+
|
53 |
+
len_image_array = 32*32*3 + 32*32
|
54 |
+
|
55 |
+
final = combined_img.reshape(1,len_image_array).astype(float)
|
56 |
+
result.append({
|
57 |
+
'class': class_number_to_name(__model.predict(final)[0]),
|
58 |
+
'class_probability': np.around(__model.predict_proba(final)*100,2).tolist()[0],
|
59 |
+
'class_dictionary': __class_name_to_number
|
60 |
+
})
|
61 |
+
|
62 |
+
return result
|
63 |
+
|
64 |
+
|
65 |
+
def get_cropped_image_if_2_eyes_new(file_path, image_base64_data):
|
66 |
+
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
67 |
+
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
|
68 |
+
|
69 |
+
if file_path:
|
70 |
+
img = cv2.imread(file_path)
|
71 |
+
#st.image(img,width=150, caption='Uploaded Image')
|
72 |
+
else:
|
73 |
+
img = get_cv2_image_from_base64_string(image_base64_data)
|
74 |
+
|
75 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
76 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
77 |
+
|
78 |
+
cropped_faces = []
|
79 |
+
for (x,y,w,h) in faces:
|
80 |
+
roi_gray = gray[y:y+h, x:x+w]
|
81 |
+
roi_color = img[y:y+h, x:x+w]
|
82 |
+
eyes = eye_cascade.detectMultiScale(roi_gray)
|
83 |
+
if len(eyes) >= 2:
|
84 |
+
cropped_faces.append(roi_color)
|
85 |
+
return cropped_faces
|
86 |
+
|
87 |
+
|
88 |
+
def w2d(img, mode='haar', level=1):
|
89 |
+
imArray = img
|
90 |
+
#Datatype conversions
|
91 |
+
#convert to grayscale
|
92 |
+
imArray = cv2.cvtColor( imArray,cv2.COLOR_RGB2GRAY )
|
93 |
+
#convert to float
|
94 |
+
imArray = np.float32(imArray)
|
95 |
+
imArray /= 255;
|
96 |
+
# compute coefficients
|
97 |
+
coeffs=pywt.wavedec2(imArray, mode, level=level)
|
98 |
+
|
99 |
+
#Process Coefficients
|
100 |
+
coeffs_H=list(coeffs)
|
101 |
+
coeffs_H[0] *= 0;
|
102 |
+
|
103 |
+
# reconstruction
|
104 |
+
imArray_H=pywt.waverec2(coeffs_H, mode);
|
105 |
+
imArray_H *= 255;
|
106 |
+
imArray_H = np.uint8(imArray_H)
|
107 |
+
|
108 |
+
return imArray_H
|
109 |
+
|
110 |
+
def get_cv2_image_from_base64_string(b64str):
|
111 |
+
'''
|
112 |
+
credit: https://stackoverflow.com/questions/33754935/read-a-base-64-encoded-image-from-memory-using-opencv-python-library
|
113 |
+
:param uri:
|
114 |
+
:return:
|
115 |
+
'''
|
116 |
+
encoded_data = b64str.split(',')[1]
|
117 |
+
nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
|
118 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
119 |
+
return img
|
120 |
+
|
121 |
+
def load_saved_artifacts():
|
122 |
+
print("loading saved artifacts...start")
|
123 |
+
global __class_name_to_number
|
124 |
+
global __class_number_to_name
|
125 |
+
|
126 |
+
with open("class_dictionary.json", "r") as f:
|
127 |
+
__class_name_to_number = json.load(f)
|
128 |
+
__class_number_to_name = {v:k for k,v in __class_name_to_number.items()}
|
129 |
+
|
130 |
+
global __model
|
131 |
+
if __model is None:
|
132 |
+
__model = joblib.load('saved_model.pkl')
|
133 |
+
#st.text("loading saved artifacts...done")
|
134 |
+
return True
|
135 |
+
|
136 |
+
def class_number_to_name(class_num):
|
137 |
+
return __class_number_to_name[class_num]
|
138 |
+
|
139 |
+
def get_b64_test_image_for_virat():
|
140 |
+
with open("b64.txt") as f:
|
141 |
+
return f.read()
|
142 |
+
|
143 |
+
def save_uploaded_image(uploaded_image):
|
144 |
+
try:
|
145 |
+
with open(uploaded_image.name, 'wb') as f:
|
146 |
+
f.write(uploaded_image.getbuffer())
|
147 |
+
return {"complete":True, "filename":uploaded_image.name}
|
148 |
+
except:
|
149 |
+
return {"complete":False, "filename":""}
|
150 |
+
|
151 |
+
|
152 |
+
uploaded_image = st.file_uploader('Choose an image')
|
153 |
+
|
154 |
+
if uploaded_image is not None:
|
155 |
+
# save the image in a directory
|
156 |
+
image_dict = save_uploaded_image(uploaded_image)
|
157 |
+
|
158 |
+
if image_dict["complete"]:
|
159 |
+
display_image = image_dict["filename"]
|
160 |
+
st.header("Image Uploded!, Processing...")
|
161 |
+
if load_saved_artifacts():
|
162 |
+
img = cv2.imread(display_image)
|
163 |
+
img = cv2.resize(img, (130, 130))
|
164 |
+
|
165 |
+
result = classify_image(get_b64_test_image_for_virat(), display_image)
|
166 |
+
#st.text(result[0])
|
167 |
+
|
168 |
+
col6,col7 = st.columns(2)
|
169 |
+
with col6:
|
170 |
+
st.header("Uploded Image: ")
|
171 |
+
st.image(img,width=130, caption='Uploaded Image')
|
172 |
+
with col7:
|
173 |
+
celeb = result[0]['class']
|
174 |
+
st.header("Predicted Image: ")
|
175 |
+
if celeb == "lionel_messi":
|
176 |
+
messi = cv2.imread("messi.jpeg")
|
177 |
+
st.image(messi,width=150, caption='Lionel Messi')
|
178 |
+
elif celeb == "maria_sharapova":
|
179 |
+
maria = cv2.imread("sharapova.jpeg")
|
180 |
+
st.image(maria,width=150, caption='Maria Sharapova')
|
181 |
+
elif celeb == "roger_federer":
|
182 |
+
roger = cv2.imread("federer.jpeg")
|
183 |
+
st.image(roger,width=150, caption='Roger Federer')
|
184 |
+
elif celeb == "serena_williams":
|
185 |
+
serena = cv2.imread("serena.jpeg")
|
186 |
+
st.image(serena,width=150, caption='Serena Williams')
|
187 |
+
elif celeb == "virat_kohli":
|
188 |
+
virat = cv2.imread("virat.jpeg")
|
189 |
+
st.image(virat,width=150, caption='Virat Kohli')
|
190 |
+
|
191 |
+
|
192 |
+
|
haarcascade_eye.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
haarcascade_frontalface_default.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PyWavelets
|
2 |
+
opencv-python
|
3 |
+
matplotlib
|
4 |
+
pybase64
|
5 |
+
scikit-learn
|
6 |
+
joblib
|
7 |
+
PyWavelets
|