Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,49 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
model_path = 'final_teath_classifier.h5'
|
| 9 |
model = tf.keras.models.load_model(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Define prediction function
|
| 11 |
def predict_image(image):
|
|
|
|
| 12 |
image_bytes = io.BytesIO()
|
| 13 |
image.save(image_bytes, format="JPEG")
|
| 14 |
-
|
| 15 |
# Load the image from the file-like object
|
| 16 |
image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
|
| 17 |
-
img_array =
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"prediction": probability_good
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
return result
|
| 32 |
|
| 33 |
# Create the interface
|
| 34 |
-
input_interface = gr.Image(type="pil")
|
|
|
|
|
|
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=predict_image,
|
| 37 |
inputs=input_interface,
|
| 38 |
-
outputs="
|
| 39 |
|
| 40 |
# Launch the interface
|
| 41 |
iface.launch(share=True)
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import io
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
# Load the model
|
| 9 |
model_path = 'final_teath_classifier.h5'
|
| 10 |
model = tf.keras.models.load_model(model_path)
|
| 11 |
+
|
| 12 |
+
# Define preprocessing function
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
# Resize the image to match input size
|
| 15 |
+
#image = image.resize((256, 256))
|
| 16 |
+
# Convert image to array and preprocess input
|
| 17 |
+
image = tf.keras.preprocessing.image.img_to_array(image)
|
| 18 |
+
# Add batch dimension
|
| 19 |
+
img_array = np.expand_dims(image, axis=0)
|
| 20 |
+
return img_array
|
| 21 |
+
|
| 22 |
# Define prediction function
|
| 23 |
def predict_image(image):
|
| 24 |
+
# Save the image to a file-like object
|
| 25 |
image_bytes = io.BytesIO()
|
| 26 |
image.save(image_bytes, format="JPEG")
|
|
|
|
| 27 |
# Load the image from the file-like object
|
| 28 |
image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
|
| 29 |
+
img_array = preprocess_image(image)
|
| 30 |
+
outputs = model.predict(img_array)
|
| 31 |
+
predictions = tf.nn.softmax(outputs.logits, axis=-1)
|
| 32 |
+
predicted_class = np.argmax(predictions)
|
| 33 |
+
if predicted_class == 0:
|
| 34 |
+
predict_label = "Clean"
|
| 35 |
+
else:
|
| 36 |
+
predict_label = "Carries"
|
| 37 |
+
confidence = float(np.max(predictions))
|
| 38 |
+
prediction_dict = {"prediction": predict_label, "confidence": confidence}
|
| 39 |
+
return prediction_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Create the interface
|
| 42 |
+
input_interface = gr.Image(type = "pil")
|
| 43 |
+
output_interface = "json"
|
| 44 |
+
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=predict_image,
|
| 47 |
inputs=input_interface,
|
| 48 |
+
outputs=gr.Textbox("output"))
|
| 49 |
|
| 50 |
# Launch the interface
|
| 51 |
iface.launch(share=True)
|