eskayML commited on
Commit
7467d36
·
1 Parent(s): 1c1371e

Create new file

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow import keras
2
+ from keras.models import load_model
3
+
4
+ # importing the preprocessing steps for the model architecture which i used for transfer learning
5
+ from keras.applications.xception import preprocess_input
6
+
7
+ import numpy as np
8
+ from keras.preprocessing.image import load_img, img_to_array
9
+ import streamlit as st
10
+ import cv2
11
+
12
+
13
+ # import tensorflow as tf
14
+ # print(tf.__version__)
15
+ # print(np.__version__)
16
+ # print(st.__version__)
17
+ # print(cv2.__version__)
18
+
19
+
20
+ st.write('# Cat and Dog Classifier')
21
+ st.markdown(
22
+ '''
23
+ This app uses transfer learning on the Xception model to predict images of cats and dogs.
24
+ It achieved an accuracy of approx. 99 percent on the validation set.
25
+
26
+ > ### Enter an image of either a cat or a dog for the model to predict.
27
+ '''
28
+ )
29
+
30
+ # image_path = 'sample_images/hang-niu-Tn8DLxwuDMA-unsplash.jpg'
31
+
32
+ model = load_model('cat_and_dog_classifier.h5')
33
+
34
+
35
+ def test_image(object_image):
36
+ # Convert the file to an opencv image.
37
+ file_bytes = np.asarray(bytearray(object_image.read()), dtype=np.uint8)
38
+ opencv_image = cv2.imdecode(file_bytes, 1)
39
+ opencv_image = cv2.resize(opencv_image, (200, 200))
40
+ opencv_image.shape = (1, 200, 200, 3)
41
+ opencv_image = preprocess_input(opencv_image)
42
+ predictions = model.predict(opencv_image)
43
+
44
+ if predictions[0, 0] >= 0.5:
45
+ result = 'DOG'
46
+ confidence = predictions[0, 0] * 100
47
+ else:
48
+ result = 'CAT'
49
+ confidence = 100 - (predictions[0, 0] * 100)
50
+
51
+ return result, round(confidence, 2)
52
+ # it returns the predicted label and the precision i.e the confidence score
53
+
54
+
55
+ object_image = st.file_uploader("Upload an image...", type=[
56
+ 'png', 'jpg', 'webp', 'jpeg'])
57
+ submit = st.button('Predict')
58
+
59
+ if submit:
60
+ if object_image is not None:
61
+ output = test_image(object_image)
62
+
63
+ # Displaying the image
64
+ st.image(object_image, channels="BGR")
65
+ st.markdown(f"""## This is an image of a: {output[0]} """)
66
+ st.write(f'# Confidence: ${ output[1]}$ %')
67
+
68
+
69
+ # print(f'The image was predicted as a {test_image(image_path)}')
70
+