Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ from transformers import pipeline
|
|
5 |
# Create an image classification pipeline with scores
|
6 |
pipe = pipeline("image-classification", model="trpakov/vit-face-expression", top_k=None)
|
7 |
|
|
|
|
|
|
|
8 |
# Streamlit app
|
9 |
st.title("Emotion Recognition with vit-face-expression")
|
10 |
|
@@ -16,24 +19,32 @@ st.write(f"{x} squared is {x * x}")
|
|
16 |
uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
|
17 |
|
18 |
if st.button("Predict Emotions") and uploaded_images:
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Create an image classification pipeline with scores
|
6 |
pipe = pipeline("image-classification", model="trpakov/vit-face-expression", top_k=None)
|
7 |
|
8 |
+
# Define emotion labels
|
9 |
+
emotion_labels = ["Neutral", "Sad", "Angry", "Surprised", "Happy"]
|
10 |
+
|
11 |
# Streamlit app
|
12 |
st.title("Emotion Recognition with vit-face-expression")
|
13 |
|
|
|
19 |
uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
|
20 |
|
21 |
if st.button("Predict Emotions") and uploaded_images:
|
22 |
+
if len(uploaded_images) == 2:
|
23 |
+
# Open the uploaded images
|
24 |
+
images = [Image.open(img) for img in uploaded_images]
|
25 |
+
|
26 |
+
# Predict emotion for each image using the pipeline
|
27 |
+
results = [pipe(image) for image in images]
|
28 |
+
|
29 |
+
# Display images and predicted emotions side by side
|
30 |
+
col1, col2 = st.columns(2)
|
31 |
+
for i in range(2):
|
32 |
+
predicted_class = results[i][0]["label"]
|
33 |
+
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
34 |
+
col = col1 if i == 0 else col2
|
35 |
+
col.image(images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
36 |
+
col.write(f"Emotion Scores: {predicted_emotion}: {results[i][0]['score']:.4f}")
|
37 |
+
else:
|
38 |
+
# Open the uploaded images
|
39 |
+
images = [Image.open(img) for img in uploaded_images]
|
40 |
+
|
41 |
+
# Predict emotion for each image using the pipeline
|
42 |
+
results = [pipe(image) for image in images]
|
43 |
+
|
44 |
+
# Display images and predicted emotions
|
45 |
+
for i, result in enumerate(results):
|
46 |
+
predicted_class = result[0]["label"]
|
47 |
+
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
48 |
+
st.image(images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
49 |
+
st.write(f"Emotion Scores for Image {i+1}:")
|
50 |
+
st.write(f"{predicted_emotion}: {result[0]['score']:.4f}")
|