Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import tensorflow as tf
|
|
|
4 |
|
5 |
-
# Load your model
|
6 |
-
model =
|
7 |
|
8 |
def predict(image):
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
12 |
-
# Make predictions using your model
|
13 |
-
result = model.predict(image) # Perform prediction
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
iface = gr.Interface(
|
22 |
fn=predict,
|
23 |
-
inputs=gr.Image(type="numpy"), #
|
24 |
-
outputs="
|
|
|
|
|
25 |
)
|
26 |
|
27 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
|
6 |
+
# Load your trained pain classification model
|
7 |
+
model = load_model("path/to/your/pain_analysis.h5") # Adjust the path as necessary
|
8 |
|
9 |
def predict(image):
|
10 |
+
# Resize the image to the expected input size of (148, 148)
|
11 |
+
target_size = (148, 148)
|
12 |
+
image = tf.image.resize(image, target_size)
|
13 |
+
|
14 |
+
# Normalize the image to [0, 1]
|
15 |
+
image = np.array(image) / 255.0
|
16 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
|
|
|
|
17 |
|
18 |
+
# Perform prediction
|
19 |
+
result = model.predict(image)
|
20 |
+
predicted_class = np.argmax(result, axis=1) # Get the predicted class
|
21 |
|
22 |
+
# Map the predicted class index to pain levels
|
23 |
+
pain_levels = {
|
24 |
+
0: "No Pain",
|
25 |
+
1: "Mild Pain",
|
26 |
+
2: "Moderate Pain",
|
27 |
+
3: "Severe Pain",
|
28 |
+
}
|
29 |
+
|
30 |
+
return pain_levels[predicted_class[0]] # Return the corresponding pain level
|
31 |
|
32 |
+
# Define the Gradio interface
|
33 |
iface = gr.Interface(
|
34 |
fn=predict,
|
35 |
+
inputs=gr.Image(type="numpy"), # Expecting image input as numpy array
|
36 |
+
outputs="text", # Return the predicted pain level as text
|
37 |
+
title="Pain Level Classification Model",
|
38 |
+
description="Upload an image to classify the pain level using the trained model."
|
39 |
)
|
40 |
|
41 |
+
# Launch the app
|
42 |
+
iface.launch()
|