Recreate app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,150 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
-
iface.launch()
|
|
|
1 |
+
import tensorflow.keras as keras
|
2 |
+
import extract_bottleneck_features
|
3 |
+
import cv2
|
4 |
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
from glob import glob
|
7 |
+
from keras.preprocessing import image
|
8 |
+
InceptionV3_model = keras.models.load_model("saved_models/weights.best.InceptionV3.hdf5",)
|
9 |
+
|
10 |
+
dog_names = [item[20:-1] for item in sorted(glob("dogImages/train/*/"))]
|
11 |
+
labels = dog_names
|
12 |
+
|
13 |
+
def extract_InceptionV3(tensor):
|
14 |
+
from keras.applications.inception_v3 import InceptionV3, preprocess_input
|
15 |
+
return InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
|
16 |
+
|
17 |
+
|
18 |
+
def extract_Resnet50(tensor):
|
19 |
+
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
|
20 |
+
return ResNet50(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
|
21 |
+
|
22 |
+
|
23 |
+
###########################################
|
24 |
+
|
25 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
26 |
+
|
27 |
+
######################################
|
28 |
+
|
29 |
+
import tensorflow as tf
|
30 |
+
from keras.preprocessing import image
|
31 |
+
from tqdm import tqdm
|
32 |
+
|
33 |
+
######################################
|
34 |
+
|
35 |
+
from tensorflow.keras.applications.resnet50 import ResNet50
|
36 |
+
# define ResNet50 model
|
37 |
+
ResNet50_model = ResNet50(weights='imagenet')
|
38 |
+
|
39 |
+
from keras.preprocessing import image
|
40 |
+
from tqdm import tqdm
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
45 |
+
|
46 |
+
def ResNet50_predict_labels(img):
|
47 |
+
# returns prediction vector for image located at img_path
|
48 |
+
img = np.expand_dims(img, axis=0)
|
49 |
+
img = preprocess_input((img))
|
50 |
+
return np.argmax(ResNet50_model.predict(img))
|
51 |
+
|
52 |
+
|
53 |
+
def path_to_tensor(img_path):
|
54 |
+
# loads RGB image as PIL.Image.Image type
|
55 |
+
#img = image.load_img(img_path, target_size=(224, 224))
|
56 |
+
# convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
|
57 |
+
#x = image.img_to_array(img)
|
58 |
+
# convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
|
59 |
+
return np.expand_dims(img_path, axis=0)
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
# extract pre-trained face detector
|
64 |
+
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml')
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
def face_detector(image):
|
69 |
+
"""
|
70 |
+
returns "True" if face is detected in image stored at image
|
71 |
+
|
72 |
+
"""
|
73 |
+
|
74 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
75 |
+
faces = face_cascade.detectMultiScale(gray)
|
76 |
+
if len(faces) > 0:
|
77 |
+
return "Number of human faces found in this image: {}". format(len(faces))
|
78 |
+
else:
|
79 |
+
return "There are no human faces in this image"
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
def InceptionV3_prediction_breed(img_path):
|
85 |
+
"""
|
86 |
+
Return: dog breed that is predicted by the model
|
87 |
+
input: image
|
88 |
+
"""
|
89 |
+
|
90 |
+
# extract bottleneck features
|
91 |
+
bottleneck_feature = extract_InceptionV3(path_to_tensor(img_path))
|
92 |
+
# obtain predicted vector
|
93 |
+
predicted_vector = InceptionV3_model.predict(bottleneck_feature)
|
94 |
+
# return dog breed that is predicted by the model
|
95 |
+
return dog_names[np.argmax(predicted_vector)].split('.')[-1]
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
def dog_detector(img):
|
100 |
+
"""
|
101 |
+
input: uploaded image by user
|
102 |
+
return: "True" if a dog is detected in the image stored at img
|
103 |
+
"""
|
104 |
+
|
105 |
+
prediction = ResNet50_predict_labels(img)
|
106 |
+
return ((prediction <= 268) & (prediction >= 151))
|
107 |
+
|
108 |
+
def identify_dog_app(img):
|
109 |
+
"""This function predicts the breed of the human or dog"
|
110 |
+
|
111 |
+
input: uploaded image by user
|
112 |
+
Return: dog or human, and breed of the uploaded image
|
113 |
+
"""
|
114 |
+
|
115 |
+
breed = InceptionV3_prediction_breed(img)
|
116 |
+
if dog_detector(img):
|
117 |
+
return("This looks like a dog and its breed is:"),"{}".format(breed)
|
118 |
+
elif face_detector(img):
|
119 |
+
return("This looks like a human but might be classified as a dog of the following breed:"),"{}".format(breed)
|
120 |
+
else:
|
121 |
+
return("I have no idea what this might be. Please upload another image!"), ("Not applicable")
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
image = gr.inputs.Image(shape=(224, 224), label="Image")
|
127 |
+
label = gr.outputs.Label(num_top_classes=1)
|
128 |
+
|
129 |
+
iface = gr.Interface(
|
130 |
+
fn=identify_dog_app,
|
131 |
+
inputs=image,
|
132 |
+
outputs=[gr.outputs.Label(label="Human or Dog?"), gr.outputs.Label(label="Breed:")],
|
133 |
+
title="Human or dog Identification - Breed Classification",
|
134 |
+
#description ="Please find the jypyter notebook on ___",
|
135 |
+
article =
|
136 |
+
'<b><span style="color: #ff9900;">Acknowledgement:</span></b><br/>'
|
137 |
+
+'<p><span style="color: #ff9900;">I would like to express my special thanks of gratitude'
|
138 |
+
+'to Misk & Sdaia for giving me the opportunity to enrol in "Data Scientist" Udacity nanodegree,'
|
139 |
+
+' as well as to my mentor Mr. Haroon who was of great help during my learning journey.</span></p>'
|
140 |
+
+'<p><span style="color: #ff9900;">This is my capstone project and herewith I finish this ND.</span></p>',
|
141 |
+
|
142 |
+
theme="dark-huggingface"
|
143 |
+
|
144 |
+
)
|
145 |
+
|
146 |
+
iface.launch(share=True)
|
147 |
+
|
148 |
+
|
149 |
|
|
|
|
|
150 |
|
|
|
|