Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,8 @@
|
|
1 |
# All imports
|
2 |
import streamlit as st
|
3 |
import tensorflow as tf
|
4 |
-
from tensorflow import keras
|
5 |
from PIL import Image
|
6 |
-
from tensorflow.keras.preprocessing import image
|
7 |
import io
|
8 |
-
from collections import Counter
|
9 |
import numpy as np
|
10 |
|
11 |
def load_image():
|
@@ -13,8 +10,13 @@ def load_image():
|
|
13 |
if uploaded_file is not None:
|
14 |
image_data = uploaded_file.getvalue()
|
15 |
st.image(image_data)
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
def
|
18 |
model_name = 'Model/model.h5'
|
19 |
model = tf.keras.models.load_model(model_name)
|
20 |
return model
|
@@ -22,54 +24,31 @@ def load_models():
|
|
22 |
def load_labels():
|
23 |
with open('Oxford-102_Flower_dataset_labels.txt', 'r') as file:
|
24 |
data = file.read().splitlines()
|
25 |
-
|
26 |
-
return flower_dict
|
27 |
-
|
28 |
-
def load_image():
|
29 |
-
uploaded_file = st.file_uploader(label='Pick an image to test')
|
30 |
-
if uploaded_file is not None:
|
31 |
-
image_data = uploaded_file.getvalue()
|
32 |
-
st.image(image_data)
|
33 |
-
img = Image.open(io.BytesIO(image_data))
|
34 |
-
img = img.resize((224,224))
|
35 |
-
return img
|
36 |
-
else:
|
37 |
-
return None
|
38 |
-
|
39 |
-
def predict(model, categories, img):
|
40 |
-
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
41 |
-
prediction = [img_array]
|
42 |
-
prediction_test = [1]
|
43 |
-
test_ds = tf.data.Dataset.from_tensor_slices((prediction, prediction_test))
|
44 |
-
test_ds = test_ds.cache().batch(32).prefetch(buffer_size = tf.data.experimental.AUTOTUNE)
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# Finding 3 highest values
|
51 |
-
high = k.most_common(3)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return
|
60 |
|
61 |
def main():
|
62 |
-
st.title('Oxford 102 Flower
|
63 |
-
model =
|
64 |
-
|
65 |
image = load_image()
|
66 |
result = st.button('Run on image')
|
67 |
-
if result:
|
68 |
st.write('Calculating results...')
|
69 |
-
|
70 |
-
st.
|
71 |
-
st.
|
72 |
|
73 |
if __name__ == '__main__':
|
74 |
main()
|
75 |
-
|
|
|
1 |
# All imports
|
2 |
import streamlit as st
|
3 |
import tensorflow as tf
|
|
|
4 |
from PIL import Image
|
|
|
5 |
import io
|
|
|
6 |
import numpy as np
|
7 |
|
8 |
def load_image():
|
|
|
10 |
if uploaded_file is not None:
|
11 |
image_data = uploaded_file.getvalue()
|
12 |
st.image(image_data)
|
13 |
+
img = Image.open(io.BytesIO(image_data))
|
14 |
+
img = img.resize((224,224))
|
15 |
+
return img
|
16 |
+
else:
|
17 |
+
return None
|
18 |
|
19 |
+
def load_model():
|
20 |
model_name = 'Model/model.h5'
|
21 |
model = tf.keras.models.load_model(model_name)
|
22 |
return model
|
|
|
24 |
def load_labels():
|
25 |
with open('Oxford-102_Flower_dataset_labels.txt', 'r') as file:
|
26 |
data = file.read().splitlines()
|
27 |
+
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
def predict(model, labels, img):
|
30 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
31 |
+
img_array = tf.expand_dims(img_array, 0) # Create a batch
|
|
|
|
|
|
|
32 |
|
33 |
+
prediction = model.predict(img_array)
|
34 |
+
predicted_class = np.argmax(prediction[0], axis=-1)
|
35 |
+
|
36 |
+
flower = labels[predicted_class]
|
37 |
+
closeness = np.round(prediction[0][predicted_class] * 100, 2)
|
38 |
+
|
39 |
+
return flower, closeness
|
40 |
|
41 |
def main():
|
42 |
+
st.title('Oxford 102 Flower Classification Demo')
|
43 |
+
model = load_model()
|
44 |
+
labels = load_labels()
|
45 |
image = load_image()
|
46 |
result = st.button('Run on image')
|
47 |
+
if result and image is not None:
|
48 |
st.write('Calculating results...')
|
49 |
+
flower, closeness = predict(model, labels, image)
|
50 |
+
st.write(f'Flower Type: {flower}')
|
51 |
+
st.write(f'Closeness: {closeness}%')
|
52 |
|
53 |
if __name__ == '__main__':
|
54 |
main()
|
|