CoolFace
Apppublic

aieye/emotion_classifier_tutorial

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
0likes
inference.py34 linesDownload Raw Back to utils
1# import requests2 3# API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"4# headers = {"Authorization": "Bearer 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"}5 6 7# def query(filename):8#     with open(filename, "rb") as f:9#         data = f.read()10#     response = requests.post(API_URL, headers=headers, data=data)11#     return response.json()12 13 14from PIL import Image15from transformers import CLIPProcessor, CLIPModel16 17model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")18processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")19 20 21def query(filename):22    image = Image.open(filename)23    inputs = processor(24        text=["Happy", "Sad", "Surprised", "Angry", "Disgusted", "Neutral", "Fearful"],25        images=image,26        return_tensors="pt",27        padding=True,28    )29    outputs = model(**inputs)30    logits_per_image = outputs.logits_per_image31    probs = logits_per_image.softmax(dim=1)32    output = [{"label": label, "score": float(score)} for label, score in zip(["Happy", "Sad", "Surprised", "Angry", "Disgusted", "Neutral", "Fearful"], probs[0])]33    return output34