Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,13 @@ from transformers import pipeline
|
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
|
7 |
-
# Initialize session state for results if not already present
|
8 |
if 'results' not in st.session_state:
|
9 |
st.session_state['results'] = []
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Disable PyplotGlobalUseWarning
|
12 |
st.set_option('deprecation.showPyplotGlobalUse', False)
|
@@ -20,12 +24,13 @@ st.title("Emotion Recognition with vit-face-expression")
|
|
20 |
# Upload images
|
21 |
uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
|
22 |
|
23 |
-
# Store selected file names
|
24 |
-
selected_file_names = []
|
25 |
-
|
26 |
# Display thumbnail images alongside file names and sizes in the sidebar
|
27 |
selected_images = []
|
28 |
if uploaded_images:
|
|
|
|
|
|
|
|
|
29 |
# Add a "Select All" checkbox in the sidebar
|
30 |
select_all = st.sidebar.checkbox("Select All", False)
|
31 |
|
@@ -38,57 +43,46 @@ if uploaded_images:
|
|
38 |
|
39 |
if selected:
|
40 |
selected_images.append(image)
|
41 |
-
selected_file_names.append(img.name)
|
42 |
|
43 |
if st.button("Predict Emotions") and selected_images:
|
44 |
# Predict emotion for each selected image using the pipeline
|
45 |
st.session_state['results'] = [pipe(image) for image in selected_images]
|
46 |
-
# Display images and predicted emotions
|
47 |
-
for i, (image, result) in enumerate(zip(selected_images, st.session_state['results'])):
|
48 |
-
predicted_class = result[0]["label"]
|
49 |
-
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
50 |
-
st.image(image, caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
51 |
-
st.write(f"Emotion Scores for #{i+1} Image")
|
52 |
-
st.write(f"{predicted_emotion}: {result[0]['score']:.4f}")
|
53 |
-
# Use the index to get the corresponding filename
|
54 |
-
st.write(f"Original File Name: {selected_file_names[i] if i < len(selected_file_names) else 'Unknown'}")
|
55 |
|
56 |
# Generate DataFrame from results
|
57 |
if st.button("Generate DataFrame"):
|
58 |
-
# Access the results from the session state
|
59 |
results = st.session_state['results']
|
|
|
|
|
60 |
if results:
|
61 |
-
|
62 |
-
|
63 |
-
emotion_scores = {
|
64 |
-
'Neutral': [],
|
65 |
-
'Happy': [],
|
66 |
-
'Surprise': [],
|
67 |
-
'Disgust': [],
|
68 |
-
'Angry': [],
|
69 |
-
# Add other emotions if necessary
|
70 |
-
'Sad': [], # Add this if you have 'sad' scores in your results
|
71 |
-
'Fear': [] # Add this if you have 'fear' scores in your results
|
72 |
-
|
73 |
-
}
|
74 |
|
75 |
-
# Iterate over the results and populate the
|
76 |
-
for result_set in results:
|
77 |
# Initialize a dictionary for the current set with zeros
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
for result in result_set:
|
81 |
# Capitalize the label and update the score in the current set
|
82 |
emotion = result['label'].capitalize()
|
83 |
score = round(result['score'], 4) # Round the score to 4 decimal places
|
84 |
-
|
85 |
|
86 |
-
#
|
87 |
-
|
88 |
-
emotion_scores[emotion].append(score)
|
89 |
|
90 |
-
# Convert the
|
91 |
-
df_emotions = pd.DataFrame(
|
92 |
|
93 |
# Display the DataFrame
|
94 |
st.write(df_emotions)
|
|
|
4 |
import pandas as pd
|
5 |
import matplotlib.pyplot as plt
|
6 |
|
7 |
+
# Initialize session state for results, image names, and image sizes if not already present
|
8 |
if 'results' not in st.session_state:
|
9 |
st.session_state['results'] = []
|
10 |
+
if 'image_names' not in st.session_state:
|
11 |
+
st.session_state['image_names'] = []
|
12 |
+
if 'image_sizes' not in st.session_state:
|
13 |
+
st.session_state['image_sizes'] = []
|
14 |
|
15 |
# Disable PyplotGlobalUseWarning
|
16 |
st.set_option('deprecation.showPyplotGlobalUse', False)
|
|
|
24 |
# Upload images
|
25 |
uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
|
26 |
|
|
|
|
|
|
|
27 |
# Display thumbnail images alongside file names and sizes in the sidebar
|
28 |
selected_images = []
|
29 |
if uploaded_images:
|
30 |
+
# Reset the image names and sizes lists each time new images are uploaded
|
31 |
+
st.session_state['image_names'] = [img.name for img in uploaded_images]
|
32 |
+
st.session_state['image_sizes'] = [round(img.size / 1024.0, 1) for img in uploaded_images]
|
33 |
+
|
34 |
# Add a "Select All" checkbox in the sidebar
|
35 |
select_all = st.sidebar.checkbox("Select All", False)
|
36 |
|
|
|
43 |
|
44 |
if selected:
|
45 |
selected_images.append(image)
|
|
|
46 |
|
47 |
if st.button("Predict Emotions") and selected_images:
|
48 |
# Predict emotion for each selected image using the pipeline
|
49 |
st.session_state['results'] = [pipe(image) for image in selected_images]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Generate DataFrame from results
|
52 |
if st.button("Generate DataFrame"):
|
53 |
+
# Access the results, image names, and sizes from the session state
|
54 |
results = st.session_state['results']
|
55 |
+
image_names = st.session_state['image_names']
|
56 |
+
image_sizes = st.session_state['image_sizes']
|
57 |
if results:
|
58 |
+
# Initialize an empty list to store all the data
|
59 |
+
data = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# Iterate over the results and populate the list with dictionaries
|
62 |
+
for i, result_set in enumerate(results):
|
63 |
# Initialize a dictionary for the current set with zeros
|
64 |
+
current_data = {
|
65 |
+
'Neutral': 0,
|
66 |
+
'Happy': 0,
|
67 |
+
'Surprise': 0,
|
68 |
+
'Disgust': 0,
|
69 |
+
'Angry': 0,
|
70 |
+
# Add other emotions if necessary
|
71 |
+
'Image Name': image_names[i],
|
72 |
+
'Image Size (KB)': image_sizes[i]
|
73 |
+
}
|
74 |
|
75 |
for result in result_set:
|
76 |
# Capitalize the label and update the score in the current set
|
77 |
emotion = result['label'].capitalize()
|
78 |
score = round(result['score'], 4) # Round the score to 4 decimal places
|
79 |
+
current_data[emotion] = score
|
80 |
|
81 |
+
# Append the current data to the data list
|
82 |
+
data.append(current_data)
|
|
|
83 |
|
84 |
+
# Convert the list of dictionaries into a pandas DataFrame
|
85 |
+
df_emotions = pd.DataFrame(data)
|
86 |
|
87 |
# Display the DataFrame
|
88 |
st.write(df_emotions)
|