Kevin King commited on
Commit
439423b
·
1 Parent(s): 91c9eb4

FEAT: Update image upload functionality and integrate emotion analysis with DeepFace

Browse files
Files changed (3) hide show
  1. .gitignore +8 -2
  2. requirements.txt +8 -2
  3. src/streamlit_app.py +55 -15
.gitignore CHANGED
@@ -1,8 +1,14 @@
1
  # Personal files
 
 
 
2
  .venv/*
3
 
 
4
  *.ipynb_checkpoints/*
5
  *.ipynb_checkpoints
6
-
7
  *__pycache__/*
8
- *__pycache__
 
 
 
 
1
  # Personal files
2
+ PLAN.MD
3
+
4
+ # Environment files
5
  .venv/*
6
 
7
+ # Python cache files and directories
8
  *.ipynb_checkpoints/*
9
  *.ipynb_checkpoints
 
10
  *__pycache__/*
11
+ *__pycache__
12
+
13
+ # DeepFace model cache
14
+ .deepface_cache/
requirements.txt CHANGED
@@ -1,2 +1,8 @@
1
- streamlit
2
- moviepy==1.0.3
 
 
 
 
 
 
 
1
+ streamlit==1.35.0
2
+ Pillow==10.3.0
3
+ numpy==1.26.4
4
+ # Core AI library for this test
5
+ deepface==0.0.94
6
+ # Backend for DeepFace
7
+ tensorflow-cpu==2.16.1
8
+ tf-keras==2.16.0
src/streamlit_app.py CHANGED
@@ -1,22 +1,62 @@
1
- import streamlit as st
2
- import tempfile
3
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- st.set_page_config(page_title="Video Upload Test")
6
- st.title("Video Upload and Playback Test")
 
7
 
8
- # Create a file uploader widget
9
- uploaded_file = st.file_uploader("Choose a video file...", type=["mp4", "mov", "avi", "mkv"])
 
10
 
11
  if uploaded_file is not None:
12
- # The uploader gives us an in-memory file buffer.
13
- # We need to save it to a temporary file on the server's disk for st.video to use it.
14
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tfile:
15
- tfile.write(uploaded_file.read())
16
- temp_video_path = tfile.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Display the video player
19
- st.video(temp_video_path)
 
 
 
 
20
 
21
- # Clean up the temporary file after use
22
- os.unlink(temp_video_path)
 
 
 
1
  import os
2
+ import streamlit as st
3
+ from PIL import Image
4
+ import numpy as np
5
+ from deepface import DeepFace
6
+ import logging
7
+ import cv2 # <-- THIS LINE WAS ADDED
8
+
9
+ # Set home directories for model caching. This is good practice even for local testing.
10
+ # On Windows, you might need to adjust this path if you encounter issues.
11
+ os.environ['DEEPFACE_HOME'] = os.path.join(os.getcwd(), '.deepface_cache')
12
+
13
+ # --- Page Configuration ---
14
+ st.set_page_config(
15
+ page_title="FER Test",
16
+ page_icon="😀",
17
+ layout="centered"
18
+ )
19
+
20
+ st.title("Step 1: Facial Emotion Recognition (FER) Test")
21
+ st.write("Upload an image with a face to test the DeepFace library.")
22
 
23
+ # --- Logger Configuration ---
24
+ logging.basicConfig(level=logging.INFO)
25
+ logging.getLogger('deepface').setLevel(logging.ERROR)
26
 
27
+
28
+ # --- UI and Processing Logic ---
29
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
30
 
31
  if uploaded_file is not None:
32
+ # Read the uploaded image file
33
+ pil_image = Image.open(uploaded_file)
34
+
35
+ # Convert the PIL image to a NumPy array
36
+ numpy_image = np.array(pil_image)
37
+
38
+ # DeepFace expects the image in BGR format, so we convert from RGB
39
+ image_bgr = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
40
+
41
+
42
+ st.image(pil_image, caption="Image Uploaded", use_column_width=True)
43
+
44
+ with st.spinner("Analyzing image for emotion..."):
45
+ try:
46
+ # Analyze the image using DeepFace
47
+ analysis = DeepFace.analyze(
48
+ img_path=image_bgr,
49
+ actions=['emotion'],
50
+ enforce_detection=False, # Don't crash if no face is found
51
+ silent=True
52
+ )
53
 
54
+ if isinstance(analysis, list) and len(analysis) > 0:
55
+ dominant_emotion = analysis[0]['dominant_emotion']
56
+ st.success(f"Dominant Emotion Detected: **{dominant_emotion.capitalize()}**")
57
+ st.write(analysis[0]['emotion']) # Display all emotion scores
58
+ else:
59
+ st.warning("No face detected in the image.")
60
 
61
+ except Exception as e:
62
+ st.error(f"An error occurred during analysis: {e}")