Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,51 +3,34 @@ import tensorflow as tf
|
|
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 |
-
|
14 |
-
|
15 |
# Define prediction function
|
16 |
def predict_image(image):
|
17 |
-
#
|
18 |
-
|
19 |
-
image.save(image_bytes, format="JPEG")
|
20 |
-
|
21 |
-
# Load the image from the file-like object
|
22 |
-
image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
|
23 |
-
image = tf.keras.preprocessing.image.img_to_array(image)
|
24 |
image = np.expand_dims(image, axis=0)
|
25 |
|
26 |
-
# Make
|
27 |
-
|
28 |
|
29 |
-
# Get the
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
predicted_label = "Clean"
|
34 |
-
predicted_probability = probabilities[0][0] * 100 # Convert to percentage
|
35 |
-
else:
|
36 |
-
predicted_label = "Carries"
|
37 |
-
predicted_probability = probabilities[0][1] * 100 # Convert to percentage
|
38 |
|
39 |
# Return the prediction result as a dictionary
|
40 |
-
return {"Predicted Label": predicted_label, "
|
41 |
-
|
42 |
|
43 |
# Create the interface
|
44 |
input_interface = gr.Image(type="pil")
|
45 |
-
output_interface = "json"
|
46 |
-
|
47 |
iface = gr.Interface(
|
48 |
fn=predict_image,
|
49 |
inputs=input_interface,
|
50 |
-
outputs=
|
51 |
|
52 |
# Launch the interface
|
53 |
iface.launch(share=True)
|
|
|
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 |
|
|
|
|
|
|
|
11 |
# Define prediction function
|
12 |
def predict_image(image):
|
13 |
+
# Load and preprocess the image
|
14 |
+
image = np.array(image)/255
|
|
|
|
|
|
|
|
|
|
|
15 |
image = np.expand_dims(image, axis=0)
|
16 |
|
17 |
+
# Make predictions
|
18 |
+
predictions = model.predict(image)
|
19 |
|
20 |
+
# Get the predicted class and probability
|
21 |
+
predicted_class_index = np.argmax(predictions)
|
22 |
+
predicted_label = "Clean" if predicted_class_index == 0 else "Carries"
|
23 |
+
predicted_probability = np.max(tf.nn.softmax(predictions))
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Return the prediction result as a dictionary
|
26 |
+
return {"Predicted Label": predicted_label, "Probability": f"{predicted_probability * 100:.2f}%"}
|
|
|
27 |
|
28 |
# Create the interface
|
29 |
input_interface = gr.Image(type="pil")
|
|
|
|
|
30 |
iface = gr.Interface(
|
31 |
fn=predict_image,
|
32 |
inputs=input_interface,
|
33 |
+
outputs="json")
|
34 |
|
35 |
# Launch the interface
|
36 |
iface.launch(share=True)
|