| import gradio as gr |
| import tensorflow as tf |
| from tensorflow.keras.preprocessing import image |
| import numpy as np |
| from PIL import Image |
| from keras import layers |
|
|
| |
| model = tf.keras.models.load_model("xception-head") |
|
|
| |
| class_labels = ['fresh', 'early decay', 'advanced decay','skeletonized'] |
|
|
| def classify_image(img): |
| |
| img = img.resize((299, 299)) |
| img = np.array(img) / 255.0 |
| img = np.expand_dims(img, axis=0) |
|
|
| |
| predictions = model.predict(img) |
| predicted_class = np.argmax(predictions, axis=1)[0] |
| confidence = np.max(predictions) |
| return {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}, confidence |
|
|
| |
| example_images = [ |
| 'examples/fresh.jpg', |
| 'examples/early_decay.jpg', |
| 'examples/advanced_decay.jpg', |
| 'examples/skeletonized.jpg' |
| ] |
|
|
| |
| demo = gr.Interface( |
| fn=classify_image, |
| inputs=gr.Image(type="pil"), |
| outputs=[gr.Label(num_top_classes=len(class_labels)), gr.Number()], |
| live=True, |
| examples=example_images |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|