from ultralytics import YOLO import cv2 import numpy as np import base64 from flask import Response # YOLOv8 모델 로드 model = YOLO("best.pt") def handler(event, context): """ Nuclio HTTP 트리거 핸들러 :param event: HTTP 요청 이벤트 :param context: Nuclio 컨텍스트 :return: YOLOv8 추론 결과 """ try: # HTTP 요청에서 이미지 추출 image_data = base64.b64decode(event.body) nparr = np.frombuffer(image_data, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # YOLOv8으로 추론 실행 results = model(img) # 결과를 JSON 형식으로 반환 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)