|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image |
|
from transformers import CLIPProcessor, CLIPModel |
|
|
|
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14") |
|
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14") |
|
|
|
|
|
def query(filename): |
|
image = Image.open(filename) |
|
inputs = processor( |
|
text=["Happy", "Sad", "Surprised", "Angry", "Disgusted", "Neutral", "Fearful"], |
|
images=image, |
|
return_tensors="pt", |
|
padding=True, |
|
) |
|
outputs = model(**inputs) |
|
logits_per_image = outputs.logits_per_image |
|
probs = logits_per_image.softmax(dim=1) |
|
output = [{"label": label, "score": float(score)} for label, score in zip(["Happy", "Sad", "Surprised", "Angry", "Disgusted", "Neutral", "Fearful"], probs[0])] |
|
return output |
|
|