/
Zolaize
/
AutoRus_mini
Обзор
Документация
Войти
/
Zolaize
/
AutoRus_mini
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/main.py
50 строк
2 KB
Zolaize
upload files
13 дек 2025, 00:24
13 дек 2025, 00:24
cddcc8b
Код
Авторство
О чём код?
from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.responses import FileResponse from pathlib import Path import os from app.detector import load_model, process_file app = FastAPI(title="Vehicle Detection API", version="0.1") UPLOAD_DIR = Path("static/uploads") RESULT_DIR = Path("static/results") MODEL_PATH = "models/MyMv1.pt" os.makedirs(UPLOAD_DIR, exist_ok=True) os.makedirs(RESULT_DIR, exist_ok=True) model, device = load_model(MODEL_PATH) @app.post("/detect/") async def detect_vehicles(file: UploadFile = File(...)): file_ext = file.filename.split(".")[-1].lower() input_path = UPLOAD_DIR / f"{file.filename}" with open(input_path, 'wb') as f: content = await file.read() f.write(content) output_path = RESULT_DIR / f"result_{file.filename}" try: detections = process_file(model, device, input_path, output_path) except Exception as e: raise HTTPException(status_code=400, detail=str(e)) return { "type": "image" if file_ext in {"jpg", "jpeg", "png"} else "video", "detections": detections, "result_url": f"/results/{output_path.name}" } @app.get("/results/{filename}") async def get_result_file(filename: str): file_path = RESULT_DIR / filename if not file_path.exists(): raise HTTPException(status_code=404, detail="File not found") return FileResponse(file_path) @app.get("/") async def root(): return {"message": "Vehicle Detection API is running!"}