Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,38 +19,57 @@ uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_
|
|
19 |
# Display thumbnail images alongside file names and sizes in the sidebar
|
20 |
selected_images = []
|
21 |
if uploaded_images:
|
22 |
-
# Add 'Select All' and 'Unselect All' checkboxes
|
23 |
-
select_all = st.sidebar.checkbox('Select All', key='select_all')
|
24 |
-
#unselect_all = st.sidebar.checkbox('Unselect All', key='unselect_all')
|
25 |
-
|
26 |
-
#if unselect_all:
|
27 |
-
# st.session_state.select_all = False
|
28 |
-
|
29 |
for idx, img in enumerate(uploaded_images):
|
30 |
image = Image.open(img)
|
31 |
checkbox_key = f"{img.name}_checkbox_{idx}" # Unique key for each checkbox
|
32 |
# Display thumbnail image and checkbox in sidebar
|
33 |
st.sidebar.image(image, caption=f"{img.name} {img.size / 1024.0:.1f} KB", width=40)
|
34 |
-
|
35 |
-
selected = st.sidebar.checkbox(f"Select {img.name}", value=select_all, key=checkbox_key)
|
36 |
if selected:
|
37 |
selected_images.append(image)
|
38 |
|
39 |
if st.button("Predict Emotions") and selected_images:
|
40 |
emotions = []
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Calculate emotion statistics
|
56 |
emotion_counts = pd.Series(emotions).value_counts()
|
@@ -94,4 +113,4 @@ if st.button("Predict Emotions") and selected_images:
|
|
94 |
# Add total faces to the title
|
95 |
ax_bar.set_title(f"Emotion Distribution - Total Faces Analyzed: {total_faces}")
|
96 |
ax_bar.yaxis.set_major_locator(plt.MaxNLocator(integer=True)) # Ensure integer ticks on Y-axis
|
97 |
-
st.pyplot(fig_bar)
|
|
|
19 |
# Display thumbnail images alongside file names and sizes in the sidebar
|
20 |
selected_images = []
|
21 |
if uploaded_images:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
for idx, img in enumerate(uploaded_images):
|
23 |
image = Image.open(img)
|
24 |
checkbox_key = f"{img.name}_checkbox_{idx}" # Unique key for each checkbox
|
25 |
# Display thumbnail image and checkbox in sidebar
|
26 |
st.sidebar.image(image, caption=f"{img.name} {img.size / 1024.0:.1f} KB", width=40)
|
27 |
+
selected = st.sidebar.checkbox(f"Select {img.name}", value=False, key=checkbox_key)
|
|
|
28 |
if selected:
|
29 |
selected_images.append(image)
|
30 |
|
31 |
if st.button("Predict Emotions") and selected_images:
|
32 |
emotions = []
|
33 |
+
if len(selected_images) == 2:
|
34 |
+
# Predict emotion for each selected image using the pipeline
|
35 |
+
results = [pipe(image) for image in selected_images]
|
36 |
+
|
37 |
+
# Display images and predicted emotions side by side
|
38 |
+
col1, col2 = st.columns(2)
|
39 |
+
for i in range(2):
|
40 |
+
predicted_class = results[i][0]["label"]
|
41 |
+
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
42 |
+
emotions.append(predicted_emotion)
|
43 |
+
col = col1 if i == 0 else col2
|
44 |
+
col.image(selected_images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
45 |
+
col.write(f"Emotion Scores: {predicted_emotion}: {results[i][0]['score']:.4f}")
|
46 |
+
# Use the index to get the corresponding filename
|
47 |
+
col.write(f"Original File Name: {uploaded_images[i].name}")
|
48 |
+
|
49 |
+
# Display the keys and values of all results
|
50 |
+
st.write("Keys and Values of all results:")
|
51 |
+
col1, col2 = st.columns(2)
|
52 |
+
for i, result in enumerate(results):
|
53 |
+
col = col1 if i == 0 else col2
|
54 |
+
col.write(f"Keys and Values of results[{i}]:")
|
55 |
+
for res in result:
|
56 |
+
label = res["label"]
|
57 |
+
score = res["score"]
|
58 |
+
col.write(f"{label}: {score:.4f}")
|
59 |
+
else:
|
60 |
+
# Predict emotion for each selected image using the pipeline
|
61 |
+
results = [pipe(image) for image in selected_images]
|
62 |
+
|
63 |
+
# Display images and predicted emotions
|
64 |
+
for i, (image, result) in enumerate(zip(selected_images, results)):
|
65 |
+
predicted_class = result[0]["label"]
|
66 |
+
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
67 |
+
emotions.append(predicted_emotion)
|
68 |
+
st.image(image, caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
69 |
+
st.write(f"Emotion Scores for #{i+1} Image")
|
70 |
+
st.write(f"{predicted_emotion}: {result[0]['score']:.4f}")
|
71 |
+
# Use the index to get the corresponding filename
|
72 |
+
st.write(f"Original File Name: {uploaded_images[i].name if i < len(uploaded_images) else 'Unknown'}")
|
73 |
|
74 |
# Calculate emotion statistics
|
75 |
emotion_counts = pd.Series(emotions).value_counts()
|
|
|
113 |
# Add total faces to the title
|
114 |
ax_bar.set_title(f"Emotion Distribution - Total Faces Analyzed: {total_faces}")
|
115 |
ax_bar.yaxis.set_major_locator(plt.MaxNLocator(integer=True)) # Ensure integer ticks on Y-axis
|
116 |
+
st.pyplot(fig_bar)
|