Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,9 +5,6 @@ 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 |
-
# 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,42 +16,27 @@ st.write(f"{x} squared is {x * x}")
|
|
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
# Predict emotion for each image using the pipeline
|
47 |
-
results = [pipe(image) for image in images]
|
48 |
-
|
49 |
-
# Display images and predicted emotions
|
50 |
-
for i, result in enumerate(results):
|
51 |
-
predicted_class = result[0]["label"]
|
52 |
-
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
53 |
-
st.image(images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
54 |
-
st.write(f"Emotion Scores for Image {i+1}:")
|
55 |
-
st.write(f"{predicted_emotion}: {result[0]['score']:.4f}")
|
56 |
-
|
57 |
-
# Display keys and values of all results for the current image
|
58 |
-
st.write(f"Keys and Values of results[{i}] (Image {i+1}):")
|
59 |
-
for key, value in result[0].items():
|
60 |
-
st.write(f"Key: {key}, Value: {value}")
|
|
|
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 |
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 |
+
# Open the uploaded images
|
20 |
+
images = [Image.open(img) for img in uploaded_images]
|
21 |
+
|
22 |
+
# Predict emotion for each image using the pipeline
|
23 |
+
results = [pipe(image) for image in images]
|
24 |
+
|
25 |
+
# Determine display style based on the number of images
|
26 |
+
num_images = len(uploaded_images)
|
27 |
+
display_style = "column" if num_images > 2 else "row"
|
28 |
+
|
29 |
+
# Display images and predicted emotions
|
30 |
+
for i, result in enumerate(results):
|
31 |
+
predicted_class = result[0]["label"]
|
32 |
+
predicted_emotion = predicted_class.split("_")[-1].capitalize()
|
33 |
+
st.image(images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|
34 |
+
|
35 |
+
# Display scores for all categories
|
36 |
+
st.write(f"Emotion Scores for Image {i+1}:")
|
37 |
+
for emotion, score in result[0]["score"].items():
|
38 |
+
st.write(f"{emotion.capitalize()}: {score:.4f}")
|
39 |
+
|
40 |
+
# If only two images and not the last image, display side by side
|
41 |
+
if num_images == 2 and i == 0:
|
42 |
+
st.write("----") # Divider between images if side by side
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|