File size: 1,013 Bytes
bea65b2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
---
license: apache-2.0
---
```python
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification
import torch
from rich import print
image_path = "./OIP.jpeg"
image = Image.open(image_path)
model_name = "Abhaykoul/emo-face-rec"
processor = AutoImageProcessor.from_pretrained(model_name)
model = AutoModelForImageClassification.from_pretrained(model_name)
inputs = processor(images=image, return_tensors="pt")
# Make a prediction
with torch.no_grad():
outputs = model(**inputs)
predicted_class_id = outputs.logits.argmax(-1).item()
predicted_emotion = model.config.id2label[predicted_class_id]
confidence_scores = torch.nn.functional.softmax(outputs.logits, dim=-1)
scores = {model.config.id2label[i]: score.item() for i, score in enumerate(confidence_scores[0])}
# Print the results
print(f"Predicted emotion: {predicted_emotion}")
print("\nConfidence scores for all emotions:")
for emotion, score in scores.items():
print(f"{emotion}: {score:.4f}")
``` |