|
from typing import Dict, List, Any |
|
from PIL import Image |
|
from io import BytesIO |
|
from transformers import pipeline |
|
import base64 |
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
self.pipeline = pipeline(task="zero-shot-object-detection",model=path, device = 0 ) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
images (:obj:`string`) |
|
candiates (:obj:`list`) |
|
Return: |
|
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82} |
|
""" |
|
inputs = data.pop("inputs", data) |
|
|
|
|
|
image = Image.open(BytesIO(base64.b64decode(inputs['image']))) |
|
|
|
|
|
detector = self.pipeline(images=[image], candidate_labels=inputs["candidate_labels"]) |
|
return detector |