File size: 2,429 Bytes
5d58062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import io
from fastapi import FastAPI, UploadFile, File, Query, HTTPException
from PIL import Image
from typing import Literal

# импорты из наших модулей
from src.api.schemas import PredictionResponse
from src.ml.predictor import DefectPredictor

app = FastAPI(
    title="PowerLine Defect Detection API",
    description="API для детекции дефектов ЛЭП (YOLO OBB)",
    version="1.0.0"
)

# предиктор подгрузит модели только при первом запросе
predictor = DefectPredictor()

@app.get("/")
def health_check():
    return {
        "status": "ok", 
        "version": "1.0.0", 
        "models_available": list(predictor.weights_map.keys())
    }

@app.post("/predict", response_model=PredictionResponse)
async def predict_endpoint(

    file: UploadFile = File(...),

    # параметр выбора модели

    model_type: Literal["fast", "accurate"] = Query("fast", description="Выбор модели: fast (YOLO-S) или accurate (YOLO-L)"),

    # параметр порога уверенности (от 0.0 до 1.0)

    conf_threshold: float = Query(0.4, ge=0.0, le=1.0, description="Порог уверенности (Confidence Threshold)")

):
    """

    Принимает изображение и возвращает найденные объекты (OBB полигоны).

    """
    # валидация файла
    if not file.content_type.startswith("image/"):
        raise HTTPException(status_code=400, detail="Файл должен быть изображением")

    try:
        # чтение картинки
        image_bytes = await file.read()
        image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
        
        # передаем параметры в ML модуль 
        detections = predictor.predict(
            image=image, 
            model_key=model_type, 
            conf_threshold=conf_threshold
        )
        
        # формирование ответа
        return {
            "filename": file.filename,
            "image_size": [image.width, image.height],
            "model_used": model_type,
            "detections": detections
        }

    except Exception as e:
        print(f"Error processing image: {e}")
        raise HTTPException(status_code=500, detail=str(e))