Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow.keras.models import load_model
|
2 |
+
|
3 |
+
# importing the preprocessing steps for the model architecture which i used for transfer learning
|
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 test_image(object_image):
|
37 |
+
# Convert the file to an opencv image.
|
38 |
+
file_bytes = np.asarray(bytearray(object_image.read()), dtype=np.uint8)
|
39 |
+
opencv_image = cv2.imdecode(file_bytes, 1)
|
40 |
+
opencv_image = cv2.resize(opencv_image, (200, 200))
|
41 |
+
opencv_image.shape = (1, 200, 200, 3)
|
42 |
+
opencv_image = preprocess_input(opencv_image)
|
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 |
+
# print(f'The image was predicted as a {test_image(image_path)}')
|