Spaces:
Sleeping
Sleeping
Create faceapp.py
Browse files- faceapp.py +33 -0
faceapp.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
4 |
+
|
5 |
+
# Load the model
|
6 |
+
model_name = "trpakov/vit-face-expression"
|
7 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
8 |
+
image_processor = ViTImageProcessor.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Streamlit app
|
11 |
+
st.title("Emotion Recognition with vit-face-expression")
|
12 |
+
|
13 |
+
# Slider example
|
14 |
+
x = st.slider('Select a value')
|
15 |
+
st.write(f"{x} squared is {x * x}")
|
16 |
+
|
17 |
+
# Upload image
|
18 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png"])
|
19 |
+
|
20 |
+
if uploaded_image:
|
21 |
+
image = Image.open(uploaded_image)
|
22 |
+
inputs = image_processor(images=image, return_tensors="pt")
|
23 |
+
pixel_values = inputs.pixel_values
|
24 |
+
|
25 |
+
# Predict emotion
|
26 |
+
with torch.no_grad():
|
27 |
+
outputs = model(pixel_values)
|
28 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
29 |
+
|
30 |
+
emotion_labels = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
31 |
+
predicted_emotion = emotion_labels[predicted_class]
|
32 |
+
|
33 |
+
st.image(image, caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
|