|
from ultralytics import YOLO |
|
import cv2 |
|
import numpy as np |
|
import base64 |
|
from flask import Response |
|
|
|
|
|
model = YOLO("best.pt") |
|
|
|
def handler(event, context): |
|
""" |
|
Nuclio HTTP 트리거 핸들러 |
|
:param event: HTTP 요청 이벤트 |
|
:param context: Nuclio 컨텍스트 |
|
:return: YOLOv8 추론 결과 |
|
""" |
|
try: |
|
|
|
image_data = base64.b64decode(event.body) |
|
nparr = np.frombuffer(image_data, np.uint8) |
|
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
|
|
|
|
|
results = model(img) |
|
|
|
|
|
response_data = results.pandas().xyxy[0].to_json(orient="records") |
|
return context.Response(body=response_data, content_type="application/json", status_code=200) |
|
except Exception as e: |
|
return context.Response(body=str(e), content_type="text/plain", status_code=500) |
|
|