szili2011 commited on
Commit
dd48924
1 Parent(s): 613f10b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
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 here
6
- model = tf.keras.models.load_model("pain_analysis.h5") # Ensure you replace this with your actual model path
7
 
8
  def predict(image):
9
- image = np.array(image) / 255.0 # Normalize the image
10
- # Ensure the image is in the shape your model expects
 
 
 
 
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
- # If your model outputs a probability distribution, you might want to take the argmax
16
- predicted_class = np.argmax(result, axis=1)
 
17
 
18
- return predicted_class # Return the predicted class or whatever output format you need
 
 
 
 
 
 
 
 
19
 
20
- # Use gr.Image directly for inputs
21
  iface = gr.Interface(
22
  fn=predict,
23
- inputs=gr.Image(type="numpy"), # Use gr.Image directly
24
- outputs="label" # Specify the output type as needed
 
 
25
  )
26
 
27
- iface.launch(share=True) # Set share=True to create a public link
 
 
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()