SakibRumu commited on
Commit
06c9264
·
verified ·
1 Parent(s): 6b140a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -30
app.py CHANGED
@@ -1,35 +1,55 @@
1
  import gradio as gr
2
- from fastai.vision.all import *
 
 
3
 
4
- # Load your trained model (replace with your model path)
5
- model_path = "efficientnet_emotion_model.pkl"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  model = load_learner(model_path)
7
 
8
- # Emotion prediction function with percentage
9
- def predict_emotion(image):
10
- # Perform prediction using your trained model
11
- pred_class, pred_idx, outputs = model.predict(image)
12
-
13
- # Get the probability (confidence) of the prediction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  confidence = outputs[pred_idx] * 100 # Convert to percentage
15
-
16
- return f"Predicted Emotion: {pred_class}", f"Confidence: {confidence:.2f}%"
17
-
18
- # Create the Gradio interface
19
- with gr.Blocks(theme='gstaff/xkcd') as demo:
20
- gr.Markdown("# Emotion Recognition Classifier")
21
-
22
- with gr.Row():
23
- with gr.Column():
24
- # Image input widget
25
- img_input = gr.Image(type="pil", label="Upload an image")
26
-
27
- # Text output widgets for emotion prediction and confidence percentage
28
- label_output = gr.Textbox(label="Predicted Emotion")
29
- confidence_output = gr.Textbox(label="Confidence Percentage")
30
-
31
- # Button to trigger emotion classification
32
- img_input.upload(predict_emotion, img_input, [label_output, confidence_output])
33
-
34
- # Run the app
35
- demo.launch(share=True)
 
1
  import gradio as gr
2
+ from fastai.vision.all import load_learner, PILImage
3
+ from PIL import Image
4
+ import torch
5
 
6
+ # Define the class mapping for the RAF-DB dataset
7
+ class_mapping = {
8
+ "1": "Surprise",
9
+ "2": "Fear",
10
+ "3": "Disgust",
11
+ "4": "Happiness",
12
+ "5": "Sadness",
13
+ "6": "Anger",
14
+ "7": "Contempt"
15
+ }
16
+
17
+ # Define the function to map folder names to labels
18
+ def get_raf_label(file_path):
19
+ # Use the class mapping to get the label
20
+ return class_mapping[str(file_path.parent.name)]
21
+
22
+ # Load the model
23
+ model_path = "Emotion_model_vggface2.pkl" # Replace with your actual model path
24
  model = load_learner(model_path)
25
 
26
+ # Define the emotion classes
27
+ emotion_classes = list(class_mapping.values()) # Get emotion classes from the class mapping
28
+
29
+ # Gradio interface
30
+ st.title("Emotion Recognition Classifier")
31
+
32
+ # Upload an image
33
+ file = st.file_uploader("Upload an image of a face", type=["jpeg", "jpg", "png"])
34
+
35
+ if file is None:
36
+ st.write("Please upload an image to detect the emotion.")
37
+ else:
38
+ # Display the uploaded image
39
+ image = Image.open(file)
40
+ st.image(image, caption="Uploaded Image", use_column_width=True)
41
+
42
+ # Convert the image to a format that the model can accept
43
+ img = PILImage.create(file)
44
+
45
+ # Predict the emotion
46
+ st.write("Classifying the emotion...")
47
+ pred_class, pred_idx, outputs = model.predict(img)
48
+
49
+ # Get the predicted label and confidence
50
+ predicted_emotion = emotion_classes[pred_idx]
51
  confidence = outputs[pred_idx] * 100 # Convert to percentage
52
+
53
+ # Show results
54
+ st.write(f"**Predicted Emotion:** {predicted_emotion}")
55
+ st.write(f"**Confidence:** {confidence:.2f}%")