feat: selezione immagini da cartella IMAGES_DIR via .env
- .env con IMAGES_DIR=Test - server: _load_env legge .env senza dip extra - GET /images lista file, POST /load_from_folder carica per nome - frontend: file picker sostituiti con 2 select popolati all avvio Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ Endpoint:
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
@@ -21,6 +22,30 @@ from fastapi.responses import HTMLResponse, Response
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
def _load_env(root: Path) -> None:
|
||||
"""Legge .env in root e popola os.environ (no override se già set)."""
|
||||
f = root / ".env"
|
||||
if not f.exists():
|
||||
return
|
||||
for line in f.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
k, v = line.split("=", 1)
|
||||
k = k.strip(); v = v.strip().strip('"').strip("'")
|
||||
os.environ.setdefault(k, v)
|
||||
|
||||
|
||||
# Root progetto (parent di pm2d/)
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
_load_env(PROJECT_ROOT)
|
||||
|
||||
_images_dir_raw = os.environ.get("IMAGES_DIR", "Test")
|
||||
IMAGES_DIR = Path(_images_dir_raw)
|
||||
if not IMAGES_DIR.is_absolute():
|
||||
IMAGES_DIR = PROJECT_ROOT / IMAGES_DIR
|
||||
|
||||
from pm2d.line_matcher import LineShapeMatcher, Match
|
||||
from pm2d.auto_tune import auto_tune
|
||||
|
||||
@@ -259,6 +284,39 @@ def index():
|
||||
return HTMLResponse(html_path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
@app.get("/images")
|
||||
def list_images():
|
||||
"""Lista file immagine nella cartella configurata in IMAGES_DIR."""
|
||||
if not IMAGES_DIR.is_dir():
|
||||
return {"dir": str(IMAGES_DIR), "files": []}
|
||||
exts = {".png", ".jpg", ".jpeg", ".bmp", ".tif", ".tiff"}
|
||||
files = sorted(
|
||||
p.name for p in IMAGES_DIR.iterdir()
|
||||
if p.is_file() and p.suffix.lower() in exts
|
||||
)
|
||||
return {"dir": str(IMAGES_DIR), "files": files}
|
||||
|
||||
|
||||
class LoadFolderReq(BaseModel):
|
||||
filename: str
|
||||
|
||||
|
||||
@app.post("/load_from_folder", response_model=UploadResp)
|
||||
def load_from_folder(req: LoadFolderReq):
|
||||
"""Carica immagine dalla cartella IMAGES_DIR per nome file."""
|
||||
name = req.filename
|
||||
if "/" in name or ".." in name:
|
||||
raise HTTPException(400, "nome file non valido")
|
||||
path = IMAGES_DIR / name
|
||||
if not path.is_file():
|
||||
raise HTTPException(404, f"file non trovato: {name}")
|
||||
img = cv2.imread(str(path), cv2.IMREAD_COLOR)
|
||||
if img is None:
|
||||
raise HTTPException(400, "immagine non leggibile")
|
||||
iid = _store_image(img)
|
||||
return UploadResp(id=iid, width=img.shape[1], height=img.shape[0])
|
||||
|
||||
|
||||
@app.post("/upload", response_model=UploadResp)
|
||||
async def upload(file: UploadFile = File(...)):
|
||||
data = await file.read()
|
||||
|
||||
Reference in New Issue
Block a user