aus10powell commited on
Commit
93e43ad
·
1 Parent(s): 41176f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -35
app.py CHANGED
@@ -1,39 +1,63 @@
1
- import streamlit as st
2
  from PIL import Image, ImageOps
3
- import io
4
- from img_classification import teachable_machine_classification, load_model
5
-
 
6
  from tensorflow import keras
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
 
 
 
8
 
9
- st.set_option("deprecation.showfileUploaderEncoding", False)
10
-
11
- st.title("Detecting presence of Poison Oak")
12
- st.header("Poison Oak Classification Example")
13
- st.text("Upload an image for classification as poison oak or no poison oak")
14
-
15
-
16
- # Load trained model
17
- model = load_model("./best_model.h5")
18
-
19
- print("Starting Streamlit app")
20
- uploaded_file = st.file_uploader("Select an image ...", type=["jpg", "png", "jpeg"])
21
-
22
- if uploaded_file is not None:
23
- image = Image.open(uploaded_file)
24
- st.image(image, caption="Uploaded image", use_column_width=True)
25
- st.write("")
26
- st.write("Classifying...")
27
- label = teachable_machine_classification(img=image, model=model)
28
- if label <= 0.2:
29
- st.write("Very unlikely that this is poison oak.")
30
- elif (label > 0.2) & (label <= 0.6):
31
- st.write(
32
- "Unsure from this picture. You may need to retake a closer/clearer picture."
33
- )
34
- elif (label > 0.6) & (label <= 0.7):
35
- st.write("Decent chance that this is poison oak.")
36
- else:
37
- st.write("{:.1f}% chance that this might be poison oak".format(label * 100))
38
- else:
39
- st.write("No file uploaded")
 
1
+ from tensorflow import keras
2
  from PIL import Image, ImageOps
3
+ import numpy as np
4
+ import io, os
5
+ import logging
6
+ import keras_metrics
7
  from tensorflow import keras
8
+ import utils
9
+ ## Configs
10
+ keras.utils.get_custom_objects()['recall'] = utils.recall
11
+ keras.utils.get_custom_objects()['precision'] = utils.precision
12
+ keras.utils.get_custom_objects()['f1'] = utils.f1
13
+
14
+
15
+ def teachable_machine_classification(img=None, model=None):
16
+ """Performs inference on image uploaded"""
17
+
18
+ # Create the array of the right shape to feed into the keras model
19
+ data = np.ndarray(shape=(1, 299, 299, 3), dtype=np.float32)
20
+ image = img
21
+ # image sizing
22
+ size = (299, 299)
23
+ image = ImageOps.fit(image, size, Image.ANTIALIAS)
24
+
25
+ # turn the image into a numpy array
26
+ image_array = np.asarray(image)
27
+
28
+ # Normalize the image
29
+ normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
30
+
31
+ # Load the image into the array
32
+ data[0] = normalized_image_array
33
+
34
+ # run the inference
35
+ prediction = model.predict(data)
36
+
37
+ print("Prediction", prediction)
38
+ return prediction[0][
39
+ 1
40
+ ] # np.argmax(prediction) # return position of the highest probability
41
+
42
+
43
+ def load_model(weights_file=None):
44
+ """Loads trained keras model"""
45
+ dependencies = {
46
+ "binary_f1_score": keras_metrics.binary_f1_score,
47
+ "binary_precision": keras_metrics.binary_precision,
48
+ "binary_recall": keras_metrics.binary_recall,
49
+ }
50
+
51
+ try:
52
+ assert os.path.exists(weights_file), f"File '{weights_file}' does not exist"
53
+ # Load the model
54
+ model = keras.models.load_model(
55
+ weights_file, custom_objects=dependencies, compile=False
56
+ )
57
 
58
+ return model
59
+ except Exception as e:
60
+ logging.error("ERROR: ", e)
61
+ print("ERROR: ", e, " Failed to load ML model")
62
 
63
+ return None