Ahmadkhan12 commited on
Commit
af88cea
·
verified ·
1 Parent(s): 1016c45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
5
- from fer import FER
6
 
7
  # Set the page config
8
  st.set_page_config(page_title="Emotion Recognition App", layout="centered")
@@ -12,8 +11,15 @@ st.title("Emotion Recognition App")
12
  # Upload an image
13
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
14
 
15
- # Load FER emotion detection model
16
- emotion_detector = FER(mtcnn=True) # Use MTCNN for better face detection
 
 
 
 
 
 
 
17
 
18
  # Resize image to reduce memory usage
19
  def resize_image(image, max_size=(800, 800)):
@@ -36,24 +42,31 @@ if uploaded_file is not None:
36
  # Convert image to numpy array
37
  image_np = np.array(image)
38
 
39
- # Detect emotions
40
- results = emotion_detector.detect_emotions(image_np)
 
 
 
 
 
 
 
 
 
41
 
42
- if results:
43
- for face in results:
44
- # Get bounding box and detected emotion
45
- box = face["box"]
46
- emotions = face["emotions"]
47
- dominant_emotion = max(emotions, key=emotions.get)
48
 
49
- # Draw a rectangle around the face
50
- x, y, w, h = box
51
  cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
52
 
53
- # Display detected emotion
54
  cv2.putText(
55
  image_np,
56
- dominant_emotion,
57
  (x, y - 10),
58
  cv2.FONT_HERSHEY_SIMPLEX,
59
  0.9,
@@ -64,4 +77,4 @@ if uploaded_file is not None:
64
  # Display the processed image
65
  st.image(image_np, caption="Processed Image", use_column_width=True)
66
  else:
67
- st.warning("No faces detected or unable to determine emotions.")
 
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
  # Set the page config
7
  st.set_page_config(page_title="Emotion Recognition App", layout="centered")
 
11
  # Upload an image
12
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
13
 
14
+ # Load OpenCV's face detection model
15
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
16
+
17
+ # Load ONNX emotion detection model
18
+ emotion_model_path = "emotion_recognition.onnx" # Replace with your model path
19
+ emotion_net = cv2.dnn.readNetFromONNX(emotion_model_path)
20
+
21
+ # Emotion labels (based on model documentation)
22
+ emotion_labels = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
23
 
24
  # Resize image to reduce memory usage
25
  def resize_image(image, max_size=(800, 800)):
 
42
  # Convert image to numpy array
43
  image_np = np.array(image)
44
 
45
+ # Convert image to grayscale for face detection
46
+ gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
47
+
48
+ # Detect faces
49
+ faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
50
+
51
+ if len(faces) > 0:
52
+ for (x, y, w, h) in faces:
53
+ # Extract face ROI
54
+ face_roi = image_np[y:y+h, x:x+w]
55
+ face_blob = cv2.dnn.blobFromImage(face_roi, 1.0, (64, 64), (104, 117, 123), swapRB=True)
56
 
57
+ # Predict emotion
58
+ emotion_net.setInput(face_blob)
59
+ predictions = emotion_net.forward()
60
+ emotion_idx = np.argmax(predictions)
61
+ emotion = emotion_labels[emotion_idx]
 
62
 
63
+ # Draw rectangle around the face
 
64
  cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
65
 
66
+ # Display emotion
67
  cv2.putText(
68
  image_np,
69
+ emotion,
70
  (x, y - 10),
71
  cv2.FONT_HERSHEY_SIMPLEX,
72
  0.9,
 
77
  # Display the processed image
78
  st.image(image_np, caption="Processed Image", use_column_width=True)
79
  else:
80
+ st.warning("No faces detected in the image.")