For the complete documentation index, see llms.txt. This page is also available as Markdown.

Roboflow 2.0

Serverless Hosted API를 통해 Roboflow 2.0 semantic segmentation model을 사용합니다.

Roboflow 2.0은 DeepLabv3 기반의 semantic segmentation model입니다. Roboflow platform에서 Roboflow 2.0 모델을 학습하고 우리의 Serverless Hosted API.

자체 호스팅 배포는 다음을 참조하세요 Roboflow Inference.

코드 샘플

1

API Key를 받으세요

Roboflow 계정을 만들고, 다음에서 키를 찾으세요: Roboflow API 설정 페이지 그리고 이를 셸에서 사용할 수 있도록 설정하세요:

export ROBOFLOW_API_KEY="your-key-here"
2

종속성을 설치하세요

다음을 설치하세요 Inference SDKsupervision:

pip install inference-sdk supervision
3

모델을 실행하세요

이미 학습한 Roboflow 2.0 semantic segmentation model에 대해 inference를 실행하고, 픽셀별 class map을 디코딩한 다음, 주석이 추가된 PNG를 작성합니다. 모델은 그 {workspace}/{model-slug} ID로 호출할 수 있습니다(참조 Versions, Trainings, and Models).

응답에는 segmentation_mask (각 픽셀 값이 class ID인 base64로 인코딩된 그레이스케일 PNG이며 0 은 background이고, 그리고 class_map class ID를 class name에 매핑합니다. 이 스크립트는 이를 하나의 sv.Detections class당 한 행으로 나누므로 sv.MaskAnnotator 가 마스크를 원본 이미지에 오버레이할 수 있습니다.

import base64
import os
import cv2
import numpy as np
import requests
import supervision as sv
from inference_sdk import InferenceHTTPClient

content = requests.get("https://media.roboflow.com/quickstart/traffic.jpg").content
image = cv2.imdecode(np.frombuffer(content, np.uint8), cv2.IMREAD_COLOR)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)
# 사전 학습된 별칭 없음: 직접 모델을 학습하고 "your-project/1"을 모델 ID로 바꾸세요.
result = client.infer(image, model_id="your-project/1")
predictions = result["predictions"]

mask_bytes = base64.b64decode(predictions["segmentation_mask"])

class_map = predictions.get("class_map", {})
class_mask = cv2.imdecode(np.frombuffer(mask_bytes, np.uint8), cv2.IMREAD_GRAYSCALE)
class_mask = cv2.resize(class_mask, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)

class_ids = [cid for cid in np.unique(class_mask).tolist() if cid != 0]
if class_ids:
    masks, xyxy, names = [], [], []
    for cid in class_ids:
        binary = class_mask == cid
        rows = np.where(np.any(binary, axis=1))[0]
        cols = np.where(np.any(binary, axis=0))[0]
        xyxy.append([cols[0], rows[0], cols[-1], rows[-1]])
        masks.append(binary)
        names.append(class_map.get(str(cid), str(cid)))

    detections = sv.Detections(
        xyxy=np.array(xyxy, dtype=np.float64),
        mask=np.array(masks),
        class_id=np.array(class_ids),
        data={"class_name": np.array(names)},
    )
    annotated = sv.MaskAnnotator().annotate(image.copy(), detections)
    annotated = sv.LabelAnnotator().annotate(annotated, detections)
else:
    annotated = image

cv2.imwrite("annotated.png", annotated)
print("Saved annotated.png")

설정 api_url 을 배포 대상에 맞게 설정하세요:

  • https://serverless.roboflow.com Serverless Hosted API용입니다.

  • http://localhost:9001 로컬 Inference 서버용입니다.

  • 귀하의 Dedicated Deployment 비공개 엔드포인트용 URL입니다.

마지막 업데이트

도움이 되었나요?