File size: 958 Bytes
d1de587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)