Spaces:
Runtime error
Runtime error
Commit
·
4d43790
1
Parent(s):
d057254
Upload 4 files
Browse files
b64.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
server.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import util
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@app.route('/classify_image', methods=['GET', 'POST'])
|
| 8 |
+
def classify_image():
|
| 9 |
+
image_data = request.form['image_data']
|
| 10 |
+
|
| 11 |
+
response = jsonify(util.classify_image(image_data))
|
| 12 |
+
|
| 13 |
+
response.headers.add('Access-Control-Allow-Origin', '*')
|
| 14 |
+
|
| 15 |
+
return response
|
| 16 |
+
|
| 17 |
+
if __name__ == "__main__":
|
| 18 |
+
print("Starting Python Flask Server For Sports Celebrity Image Classification")
|
| 19 |
+
util.load_saved_artifacts()
|
| 20 |
+
app.run(port=5000)
|
util.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import json
|
| 3 |
+
import numpy as np
|
| 4 |
+
import base64
|
| 5 |
+
import cv2
|
| 6 |
+
from wavelet import w2d
|
| 7 |
+
|
| 8 |
+
__class_name_to_number = {}
|
| 9 |
+
__class_number_to_name = {}
|
| 10 |
+
|
| 11 |
+
__model = None
|
| 12 |
+
|
| 13 |
+
def classify_image(image_base64_data, file_path=None):
|
| 14 |
+
|
| 15 |
+
imgs = get_cropped_image_if_2_eyes(file_path, image_base64_data)
|
| 16 |
+
|
| 17 |
+
result = []
|
| 18 |
+
for img in imgs:
|
| 19 |
+
scalled_raw_img = cv2.resize(img, (32, 32))
|
| 20 |
+
img_har = w2d(img, 'db1', 5)
|
| 21 |
+
scalled_img_har = cv2.resize(img_har, (32, 32))
|
| 22 |
+
combined_img = np.vstack((scalled_raw_img.reshape(32 * 32 * 3, 1), scalled_img_har.reshape(32 * 32, 1)))
|
| 23 |
+
|
| 24 |
+
len_image_array = 32*32*3 + 32*32
|
| 25 |
+
|
| 26 |
+
final = combined_img.reshape(1,len_image_array).astype(float)
|
| 27 |
+
result.append({
|
| 28 |
+
'class': class_number_to_name(__model.predict(final)[0]),
|
| 29 |
+
'class_probability': np.around(__model.predict_proba(final)*100,2).tolist()[0],
|
| 30 |
+
'class_dictionary': __class_name_to_number
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
return result
|
| 34 |
+
|
| 35 |
+
def class_number_to_name(class_num):
|
| 36 |
+
return __class_number_to_name[class_num]
|
| 37 |
+
|
| 38 |
+
def load_saved_artifacts():
|
| 39 |
+
print("loading saved artifacts...start")
|
| 40 |
+
global __class_name_to_number
|
| 41 |
+
global __class_number_to_name
|
| 42 |
+
|
| 43 |
+
with open("./artifacts/class_dictionary.json", "r") as f:
|
| 44 |
+
__class_name_to_number = json.load(f)
|
| 45 |
+
__class_number_to_name = {v:k for k,v in __class_name_to_number.items()}
|
| 46 |
+
|
| 47 |
+
global __model
|
| 48 |
+
if __model is None:
|
| 49 |
+
with open('./artifacts/saved_model.pkl', 'rb') as f:
|
| 50 |
+
__model = joblib.load(f)
|
| 51 |
+
print("loading saved artifacts...done")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def get_cv2_image_from_base64_string(b64str):
|
| 55 |
+
'''
|
| 56 |
+
credit: https://stackoverflow.com/questions/33754935/read-a-base-64-encoded-image-from-memory-using-opencv-python-library
|
| 57 |
+
:param uri:
|
| 58 |
+
:return:
|
| 59 |
+
'''
|
| 60 |
+
encoded_data = b64str.split(',')[1]
|
| 61 |
+
nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
|
| 62 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 63 |
+
return img
|
| 64 |
+
|
| 65 |
+
def get_cropped_image_if_2_eyes(image_path, image_base64_data):
|
| 66 |
+
face_cascade = cv2.CascadeClassifier('./opencv/haarcascades/haarcascade_frontalface_default.xml')
|
| 67 |
+
eye_cascade = cv2.CascadeClassifier('./opencv/haarcascades/haarcascade_eye.xml')
|
| 68 |
+
|
| 69 |
+
if image_path:
|
| 70 |
+
img = cv2.imread(image_path)
|
| 71 |
+
else:
|
| 72 |
+
img = get_cv2_image_from_base64_string(image_base64_data)
|
| 73 |
+
|
| 74 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 75 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 76 |
+
|
| 77 |
+
cropped_faces = []
|
| 78 |
+
for (x,y,w,h) in faces:
|
| 79 |
+
roi_gray = gray[y:y+h, x:x+w]
|
| 80 |
+
roi_color = img[y:y+h, x:x+w]
|
| 81 |
+
eyes = eye_cascade.detectMultiScale(roi_gray)
|
| 82 |
+
if len(eyes) >= 2:
|
| 83 |
+
cropped_faces.append(roi_color)
|
| 84 |
+
return cropped_faces
|
| 85 |
+
|
| 86 |
+
def get_b64_test_image_for_virat():
|
| 87 |
+
with open("b64.txt") as f:
|
| 88 |
+
return f.read()
|
| 89 |
+
|
| 90 |
+
if __name__ == '__main__':
|
| 91 |
+
load_saved_artifacts()
|
| 92 |
+
|
| 93 |
+
print(classify_image(get_b64_test_image_for_virat(), None))
|
| 94 |
+
|
| 95 |
+
# print(classify_image(None, "./test_images/federer1.jpg"))
|
| 96 |
+
# print(classify_image(None, "./test_images/federer2.jpg"))
|
| 97 |
+
# print(classify_image(None, "./test_images/virat1.jpg"))
|
| 98 |
+
# print(classify_image(None, "./test_images/virat2.jpg"))
|
| 99 |
+
# print(classify_image(None, "./test_images/virat3.jpg")) # Inconsistent result could be due to https://github.com/scikit-learn/scikit-learn/issues/13211
|
| 100 |
+
# print(classify_image(None, "./test_images/serena1.jpg"))
|
| 101 |
+
# print(classify_image(None, "./test_images/serena2.jpg"))
|
| 102 |
+
# print(classify_image(None, "./test_images/sharapova1.jpg"))
|
wavelet.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pywt
|
| 3 |
+
import cv2
|
| 4 |
+
|
| 5 |
+
def w2d(img, mode='haar', level=1):
|
| 6 |
+
imArray = img
|
| 7 |
+
#Datatype conversions
|
| 8 |
+
#convert to grayscale
|
| 9 |
+
imArray = cv2.cvtColor( imArray,cv2.COLOR_RGB2GRAY )
|
| 10 |
+
#convert to float
|
| 11 |
+
imArray = np.float32(imArray)
|
| 12 |
+
imArray /= 255;
|
| 13 |
+
# compute coefficients
|
| 14 |
+
coeffs=pywt.wavedec2(imArray, mode, level=level)
|
| 15 |
+
|
| 16 |
+
#Process Coefficients
|
| 17 |
+
coeffs_H=list(coeffs)
|
| 18 |
+
coeffs_H[0] *= 0;
|
| 19 |
+
|
| 20 |
+
# reconstruction
|
| 21 |
+
imArray_H=pywt.waverec2(coeffs_H, mode);
|
| 22 |
+
imArray_H *= 255;
|
| 23 |
+
imArray_H = np.uint8(imArray_H)
|
| 24 |
+
|
| 25 |
+
return imArray_H
|