|
--- |
|
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}") |
|
|
|
``` |