Ahmadkhan12 commited on
Commit
bb9fde7
·
verified ·
1 Parent(s): e36cb90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import streamlit as st
2
- import numpy as np
3
  import cv2
 
4
  from PIL import Image
 
5
 
6
  # Set the page config
7
  st.set_page_config(page_title="Emotion Recognition App", layout="centered")
@@ -11,16 +12,8 @@ st.title("Emotion Recognition App")
11
  # Upload an image
12
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
13
 
14
- # Load OpenCV's pre-trained face detection model
15
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
16
-
17
- # Define a simple emotion detection function
18
- def detect_emotion(face):
19
- """
20
- Mock function to assign a random emotion.
21
- Replace with an actual emotion detection model.
22
- """
23
- return "Happy" # Replace with your logic
24
 
25
  # Resize image to reduce memory usage
26
  def resize_image(image, max_size=(800, 800)):
@@ -43,24 +36,24 @@ if uploaded_file is not None:
43
  # Convert image to numpy array
44
  image_np = np.array(image)
45
 
46
- # Convert image to grayscale for face detection
47
- gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
48
 
49
- # Detect faces
50
- faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
 
 
 
 
51
 
52
- if len(faces) > 0:
53
- for (x, y, w, h) in faces:
54
- # Draw rectangle around the face
55
  cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
56
 
57
- # Assign a dummy emotion
58
- emotion = detect_emotion(None)
59
-
60
- # Display emotion above the face
61
  cv2.putText(
62
  image_np,
63
- emotion,
64
  (x, y - 10),
65
  cv2.FONT_HERSHEY_SIMPLEX,
66
  0.9,
@@ -71,4 +64,4 @@ if uploaded_file is not None:
71
  # Display the processed image
72
  st.image(image_np, caption="Processed Image", use_column_width=True)
73
  else:
74
- st.warning("No faces detected in the image.")
 
1
  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
  # 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
  # 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
  # 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.")