Views
No views yet
1from transformers import ViTImageProcessor, ViTForImageClassification
2from PIL import Image
3import torch
4
5# Load model and processor
6processor = ViTImageProcessor.from_pretrained('abhilash88/face-emotion-detection')
7model = ViTForImageClassification.from_pretrained('abhilash88/face-emotion-detection')
8
9# Load and preprocess image
10image = Image.open('path_to_your_image.jpg')
11inputs = processor(image, return_tensors="pt")
12
13# Make prediction
14with torch.no_grad():
15 outputs = model(**inputs)
16 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
17 predicted_class = torch.argmax(predictions, dim=-1).item()
18
19# Emotion classes
20emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
21predicted_emotion = emotions[predicted_class]
22confidence = predictions[0][predicted_class].item()
23
24print(f"Predicted Emotion: {predicted_emotion} ({confidence:.2f})")






Epoch 1: Loss: 0.917, Accuracy: 66.90%
Epoch 2: Loss: 0.609, Accuracy: 69.32%
Epoch 3: Loss: 0.316, Accuracy: 71.55%1@misc{face-emotion-detection,
2 author = {Abhilash},
3 title = {ViT Face Emotion Detection},
4 year = {2025},
5 publisher = {Hugging Face},
6 howpublished = {https://huggingface.co/abhilash88/face-emotion-detection}
7}