abdurrahman22224 commited on
Commit
3762c5d
1 Parent(s): 12a2172
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from tensorflow.keras.preprocessing.image import img_to_array, load_img
3
+ import gradio as gr
4
+ import tensorflow as tf
5
+ import numpy as np
6
+ from PIL import Image
7
+ import cv2
8
+ from tensorflow.keras.preprocessing import image
9
+
10
+
11
+ emotion_labels = {'angry': 0, 'disgust': 1, 'fear': 2, 'happy': 3, 'neutral': 4, 'sad': 5, 'surprise': 6}
12
+ index_to_emotion = {v: k for k, v in emotion_labels.items()}
13
+
14
+
15
+ def prepare_image(img_pil):
16
+ """Preprocess the PIL image to fit your model's input requirements."""
17
+ # Convert the PIL image to a numpy array with the target size
18
+ img = img_pil.resize((224, 224))
19
+ img_array = img_to_array(img)
20
+ img_array = np.expand_dims(img_array, axis=0) # Convert single image to a batch.
21
+ img_array /= 255.0 # Rescale pixel values to [0,1], as done during training
22
+ return img_array
23
+
24
+
25
+
26
+ # Define the Gradio interface
27
+ def predict_emotion(image):
28
+ # Preprocess the image
29
+ processed_image = prepare_image(image)
30
+ # Make prediction using the model
31
+ prediction = model.predict(processed_image)
32
+ # Get the emotion label with the highest probability
33
+ predicted_class = np.argmax(prediction, axis=1)
34
+ predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion")
35
+ return predicted_emotion
36
+
37
+ interface = gr.Interface(
38
+ fn=predict_emotion, # Your prediction function
39
+ inputs=gr.Image(type="pil"), # Input for uploading an image, directly compatible with PIL images
40
+ outputs="text", # Output as text displaying the predicted emotion
41
+ title="Emotion Detection",
42
+ description="Upload an image and see the predicted emotion."
43
+ )
44
+
45
+ # Launch the Gradio interface
46
+ interface.launch()