Upload 2 files
Browse files- app.py +43 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.preprocessing import image
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
from keras import layers
|
7 |
+
|
8 |
+
# Load your trained Xception model
|
9 |
+
model = tf.keras.models.load_model("xception-head")
|
10 |
+
|
11 |
+
# Define the labels for your classification
|
12 |
+
class_labels = ['fresh', 'early decay', 'advanced decay','skeletonized'] # Replace with your actual class names
|
13 |
+
|
14 |
+
def classify_image(img):
|
15 |
+
# Preprocess the image to fit the model input shape
|
16 |
+
img = img.resize((299, 299)) # Xception takes 299x299 input size
|
17 |
+
img = np.array(img) / 255.0 # Normalize the image
|
18 |
+
img = np.expand_dims(img, axis=0)
|
19 |
+
|
20 |
+
# Make prediction
|
21 |
+
predictions = model.predict(img)
|
22 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
23 |
+
confidence = np.max(predictions)
|
24 |
+
return {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}, confidence
|
25 |
+
|
26 |
+
# Example images (local paths or URLs)
|
27 |
+
#example_images = [
|
28 |
+
#'examples/fresh.jpg', # Replace with actual local file paths or URLs
|
29 |
+
#]
|
30 |
+
|
31 |
+
# Gradio interface
|
32 |
+
demo = gr.Interface(
|
33 |
+
fn=classify_image,
|
34 |
+
title="Human Decomposition Image Classification",
|
35 |
+
description = "Predict the stage of decay (fresh, early decay, advanced decay, or skeletonized) of a head. This is a demo of one of our human decomposition image classification <a href=\"https://huggingface.co/icputrd/megyesi_decomposition_classification/blob/main/head/xception\">models</a>.",
|
36 |
+
inputs=gr.Image(type="pil"),
|
37 |
+
outputs=[gr.Label(num_top_classes=len(class_labels)), gr.Number()],
|
38 |
+
live=True,
|
39 |
+
article = "Author: <a href=\"https://www.linkedin.com/in/anna-maria-nau/\">Anna-Maria Nau</a>"
|
40 |
+
)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pillow
|
3 |
+
transformers
|
4 |
+
tensorflow==2.11.0
|
5 |
+
keras==2.11.0
|
6 |
+
numpy==1.24.4
|