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