|
from typing import Dict, List, Any |
|
import supervision as sv |
|
import urllib.request |
|
import numpy as np |
|
import cv2 |
|
import base64 |
|
from inference_sdk import InferenceHTTPClient |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, key): |
|
pass |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
inputs = data.get("inputs") |
|
isurl = inputs.get("isurl") |
|
path = inputs.get("path") |
|
key = inputs.get("key") |
|
|
|
print("load image") |
|
CLIENT = InferenceHTTPClient(api_url="https://detect.roboflow.com", api_key=key) |
|
print("get client") |
|
if isurl: |
|
req = urllib.request.urlopen(path) |
|
arr = np.asarray(bytearray(req.read()), dtype=np.uint8) |
|
img = cv2.imdecode(arr, -1) |
|
else: |
|
img = cv2.imread(path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("start model detection") |
|
result = CLIENT.infer(path, model_id="clothing-detection-s4ioc/6") |
|
detections = sv.Detections.from_inference(result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Data processing") |
|
if detections.confidence.size == 0: |
|
return "Not Found" |
|
else: |
|
x1, y1, x2, y2 = ( |
|
int(detections.xyxy[0][0]), |
|
int(detections.xyxy[0][1]), |
|
int(detections.xyxy[0][2]), |
|
int(detections.xyxy[0][3]), |
|
) |
|
clothes = img[y1:y2, x1:x2] |
|
retval, buffer = cv2.imencode(".jpg", clothes) |
|
|
|
jpg_as_text = base64.b64encode(buffer).decode("utf-8") |
|
|
|
return jpg_as_text |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|