Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,22 @@
|
|
1 |
from tensorflow.keras.models import load_model
|
2 |
-
|
3 |
-
|
4 |
-
from tensorflow.keras.applications.xception import preprocess_input
|
5 |
-
|
6 |
import numpy as np
|
7 |
-
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
8 |
-
import streamlit as st
|
9 |
-
import cv2
|
10 |
-
|
11 |
-
|
12 |
-
# import tensorflow as tf
|
13 |
-
# print(tf.__version__)
|
14 |
-
# print(np.__version__)
|
15 |
-
# print(st.__version__)
|
16 |
-
# print(cv2.__version__)
|
17 |
-
|
18 |
-
|
19 |
-
st.write('# Cat and Dog Classifier')
|
20 |
-
st.markdown(
|
21 |
-
'''
|
22 |
-
This app uses transfer learning on the Xception model to predict images of cats and dogs.
|
23 |
-
It achieved an accuracy of approx. 99 percent on the validation set.
|
24 |
-
|
25 |
-
*View on [Github](https://github.com/eskayML/cat-and-dogs-classification)*
|
26 |
-
|
27 |
-
> ### Enter an image of either a cat or a dog for the model to predict.
|
28 |
-
'''
|
29 |
-
)
|
30 |
-
|
31 |
-
# image_path = 'sample_images/hang-niu-Tn8DLxwuDMA-unsplash.jpg'
|
32 |
|
33 |
model = load_model('Pikachu_and_Raichu.h5')
|
34 |
|
|
|
35 |
|
36 |
-
def
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
predictions = model.predict(opencv_image)
|
44 |
-
|
45 |
-
if predictions[0, 0] >= 0.5:
|
46 |
-
result = 'DOG'
|
47 |
-
confidence = predictions[0, 0] * 100
|
48 |
-
else:
|
49 |
-
result = 'CAT'
|
50 |
-
confidence = 100 - (predictions[0, 0] * 100)
|
51 |
-
|
52 |
-
return result, round(confidence, 2)
|
53 |
-
# it returns the predicted label and the precision i.e the confidence score
|
54 |
-
|
55 |
-
|
56 |
-
object_image = st.file_uploader("Upload an image...", type=[
|
57 |
-
'png', 'jpg', 'webp', 'jpeg'])
|
58 |
-
submit = st.button('Predict')
|
59 |
-
|
60 |
-
if submit:
|
61 |
-
if object_image is not None:
|
62 |
-
output = test_image(object_image)
|
63 |
-
|
64 |
-
# Displaying the image
|
65 |
-
st.image(object_image, channels="BGR")
|
66 |
-
st.markdown(f"""## This is an image of a: {output[0]} """)
|
67 |
-
st.write(f'# Confidence: ${ output[1]}$ %')
|
68 |
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
1 |
from tensorflow.keras.models import load_model
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
|
|
|
|
4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
model = load_model('Pikachu_and_Raichu.h5')
|
7 |
|
8 |
+
classnames = ['Pikachu', 'Raichu']
|
9 |
|
10 |
+
def predict(img):
|
11 |
+
img=img.reshape(-1,224,224,3)
|
12 |
+
"""images_list = []
|
13 |
+
images_list.append(np.array(img))
|
14 |
+
x = np.asarray(images_list)"""
|
15 |
+
prediction = model.predict(img)[0]
|
16 |
+
return {classnames[i]: float(prediction[i]) for i in range(len(classnames))}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
image = gr.inputs.Image(shape=(298, 384))
|
19 |
+
label = gr.outputs.Label(num_top_classes=2)
|
20 |
|
21 |
+
gr.Interface(fn=predict, inputs=image, title="Garbage Classifier",
|
22 |
+
description="This is a Garbage Classification Model Trained using Dataset 11 by Sud.Deployed to Hugging Faces using Gradio.",outputs=label,interpretation='default').launch(debug=True,enable_queue=True)
|