Ahmadkhan12 commited on
Commit
f7996c9
·
verified ·
1 Parent(s): 60f0d5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -32
app.py CHANGED
@@ -1,44 +1,40 @@
1
  import streamlit as st
2
- from PIL import Image, ImageOps
3
  import numpy as np
4
  import requests
 
 
 
5
 
6
- # Function for image preprocessing
7
- def preprocess_image(image):
8
- # Convert image to grayscale
9
- gray_image = ImageOps.grayscale(image)
10
- # Resize the image to 48x48 (common for emotion recognition models)
11
- resized_image = gray_image.resize((48, 48))
12
- # Convert the image to a numpy array
13
- image_array = np.array(resized_image)
14
- # Normalize the image array (values between 0 and 1)
15
- normalized_image = image_array / 255.0
16
- return normalized_image
17
 
18
- # Title and description
19
- st.title("Emotion Recognition for Autism Support")
20
- st.write("Upload an image, and the app will help identify emotions.")
21
 
22
- # Upload image section
23
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
24
-
25
- if uploaded_file:
26
- # Load the image using PIL
27
  image = Image.open(uploaded_file)
28
  st.image(image, caption="Uploaded Image", use_column_width=True)
29
- st.success("Image uploaded successfully!")
30
-
31
- # Preprocess the image
32
- st.write("Preprocessing the image for emotion recognition...")
33
- preprocessed_image = preprocess_image(image)
34
- st.write("Preprocessing complete. Ready for emotion analysis.")
35
 
36
- # Display preprocessed image
37
- st.image(Image.fromarray((preprocessed_image * 255).astype('uint8')), caption="Preprocessed Image")
 
 
 
 
38
 
39
- # Placeholder for emotion recognition (to be integrated with a model later)
40
- st.info("Emotion recognition will be added in the next step.")
 
 
 
 
 
 
 
 
41
 
42
  # Footer
43
- st.write("---")
44
- st.write("Developed to assist children with autism in recognizing emotions.")
 
1
  import streamlit as st
2
+ from PIL import Image
3
  import numpy as np
4
  import requests
5
+ from io import BytesIO
6
+ import cv2
7
+ from fer import FER # Ensure this package is in requirements.txt
8
 
9
+ # Title and Description
10
+ st.title("Emotion Recognition App")
11
+ st.write("Upload a face image to detect emotions using AI!")
 
 
 
 
 
 
 
 
12
 
13
+ # File upload section
14
+ uploaded_file = st.file_uploader("Upload a face image", type=["jpg", "jpeg", "png"])
 
15
 
16
+ if uploaded_file is not None:
17
+ # Display the uploaded image
 
 
 
18
  image = Image.open(uploaded_file)
19
  st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
 
 
 
 
20
 
21
+ # Convert image to a numpy array
22
+ img_array = np.array(image)
23
+
24
+ # Process the image for emotion detection
25
+ st.write("Detecting emotions...")
26
+ detector = FER(mtcnn=True) # Initialize the FER+ detector
27
 
28
+ try:
29
+ # Detect emotions
30
+ result = detector.top_emotion(img_array)
31
+ if result:
32
+ emotion, score = result
33
+ st.write(f"Detected Emotion: **{emotion.capitalize()}** with confidence {score * 100:.2f}%")
34
+ else:
35
+ st.write("No clear emotion detected. Please try another image.")
36
+ except Exception as e:
37
+ st.error(f"Error processing the image: {e}")
38
 
39
  # Footer
40
+ st.write("Emotion recognition powered by FER+")