File size: 11,011 Bytes
24927f2
1444ed5
c9e715d
1444ed5
 
f16d0cc
 
1444ed5
5d99b0b
1444ed5
 
 
af9ef77
 
 
f16d0cc
c9e715d
 
 
 
 
 
 
62aa642
24927f2
 
 
 
 
 
 
 
62aa642
 
d0ee279
af9ef77
 
 
 
62aa642
1444ed5
 
 
24927f2
 
 
af9ef77
24927f2
1444ed5
 
 
ec11250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1444ed5
 
ec11250
 
 
 
 
 
1444ed5
 
ec11250
 
 
 
 
 
 
 
 
 
 
 
1444ed5
c9e715d
ec11250
c9e715d
 
1444ed5
 
 
 
 
ec11250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1444ed5
 
 
 
 
 
 
 
 
7d5465d
 
1444ed5
 
 
 
c9e715d
1444ed5
 
 
 
c9e715d
 
7d5465d
c9e715d
1444ed5
 
7d5465d
ec11250
1444ed5
7d5465d
 
 
1444ed5
7d5465d
1444ed5
 
ec11250
7d5465d
 
 
1444ed5
 
 
 
 
 
 
 
 
f16d0cc
 
 
 
 
 
 
 
 
 
 
 
1444ed5
 
f16d0cc
 
 
 
 
 
 
 
1444ed5
f16d0cc
 
 
 
 
 
 
 
1444ed5
 
 
 
f16d0cc
1444ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f16d0cc
1444ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d99b0b
 
 
 
1444ed5
 
 
5d99b0b
 
 
f16d0cc
1444ed5
f16d0cc
5d99b0b
 
1444ed5
 
 
 
 
 
 
 
 
 
 
 
5d99b0b
 
 
7d5465d
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os, io, base64, urllib.request, ssl, time, json, pathlib
from typing import Optional, List
import numpy as np, cv2
from ultralytics import YOLO
import easyocr
from PIL import Image
import pillow_heif

from fastapi import FastAPI, HTTPException, File, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field

os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics")
os.environ.setdefault("HF_HOME", "/tmp/.cache/huggingface")
os.makedirs(os.environ["YOLO_CONFIG_DIR"], exist_ok=True)

os.environ.setdefault("OMP_NUM_THREADS", "2")
os.environ.setdefault("OPENBLAS_NUM_THREADS", "2")
os.environ.setdefault("MKL_NUM_THREADS", "2")
import torch
torch.set_num_threads(2)


TMP_DIR = "/tmp"
paths = [
    f"{TMP_DIR}/Ultralytics",
    f"{TMP_DIR}/.EasyOCR",
    f"{TMP_DIR}/.EasyOCR/user_network",
    f"{TMP_DIR}/mplconfig",
]
for p in paths:
    os.makedirs(p, exist_ok=True)

from huggingface_hub import hf_hub_download

# --- PESOS COMPATIBLES ULTRALYTICS (YOLOv11) ---
REPO_ID = "morsetechlab/yolov11-license-plate-detection"
FILENAME = "license-plate-finetune-v1n.pt"  # o v1s/v1m/v1l/v1x
WEIGHTS = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)

yolo = YOLO(WEIGHTS)

# EasyOCR con GPU si está disponible
reader = easyocr.Reader(
    ['en'],
    gpu=torch.cuda.is_available(),
    model_storage_directory="/tmp/.EasyOCR",
)

ALLOW = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def clamp(v, lo, hi): 
    return max(lo, min(hi, v))

def expand_box(xyxy, w, h, pad_ratio=0.10):
    x1, y1, x2, y2 = [int(v) for v in xyxy]
    bw, bh = x2 - x1, y2 - y1
    px, py = int(bw * pad_ratio), int(bh * pad_ratio)
    nx1 = clamp(x1 - px, 0, w - 1)
    ny1 = clamp(y1 - py, 0, h - 1)
    nx2 = clamp(x2 + px, 0, w - 1)
    ny2 = clamp(y2 + py, 0, h - 1)
    return nx1, ny1, nx2, ny2

def ensure_min_size(img_bgr, target_long=320):
    h, w = img_bgr.shape[:2]
    m = max(h, w)
    if m < target_long:
        scale = target_long / float(m)
        nh, nw = int(round(h * scale)), int(round(w * scale))
        img_bgr = cv2.resize(img_bgr, (nw, nh), interpolation=cv2.INTER_CUBIC)
    return img_bgr

def preproc_adaptive(plate_bgr):
    img = ensure_min_size(plate_bgr)           # asegura tamaño
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 7, 50, 50)
    th = cv2.adaptiveThreshold(
        gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 5
    )
    # opcional: cerrar huecos finos
    k = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    th = cv2.morphologyEx(th, cv2.MORPH_CLOSE, k, iterations=1)
    return th

def preproc_clahe_otsu(plate_bgr):
    img = ensure_min_size(plate_bgr)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
    eq = clahe.apply(gray)
    _, th = cv2.threshold(eq, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    k = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    th = cv2.morphologyEx(th, cv2.MORPH_CLOSE, k, iterations=1)
    return th

def read_easy(img, allow=ALLOW):
    out = reader.readtext(img, detail=1, allowlist=allow)
    cands = []
    for _, text, score in out:
        t = "".join(c for c in (text or "").upper() if c in allow)
        if len(t) >= 4:
            cands.append((t, float(score)))
    if not cands:
        return "", 0.0
    cands.sort(key=lambda x: (x[1], len(x[0])), reverse=True)
    return cands[0]

def preprocess_for_ocr(plate_bgr):
    img = plate_bgr.copy()
    h, w = img.shape[:2]
    if max(h, w) < 160:
        img = cv2.resize(img, (w*2, h*2), interpolation=cv2.INTER_CUBIC)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 7, 50, 50)
    th = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                               cv2.THRESH_BINARY, 31, 5)
    return th

def ocr_plate(plate_bgr):
    # 1) adaptativa
    t, s = read_easy(preproc_adaptive(plate_bgr))
    if t:
        return t, s
    # 2) CLAHE + Otsu (fallback)
    return read_easy(preproc_clahe_otsu(plate_bgr))

def draw_box_text(img, xyxy, text, color=(0, 255, 0)):
    x1, y1, x2, y2 = [int(v) for v in xyxy]
    cv2.rectangle(img, (x1,y1), (x2,y2), color, 2)
    if text:
        tsize = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)[0]
        cv2.rectangle(img, (x1, y1 - tsize[1] - 6), (x1 + tsize[0] + 4, y1), color, -1)
        cv2.putText(img, text, (x1 + 2, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0), 2, cv2.LINE_AA)

def detect_plates_bgr(bgr, conf=0.25, iou=0.45):
    # 512 es buen sweet spot en CPU
    res = yolo.predict(bgr, conf=conf, iou=iou, imgsz=512, max_det=1, verbose=False)[0]
    boxes = res.boxes.xyxy.cpu().numpy() if res.boxes is not None else np.empty((0,4))
    confs = res.boxes.conf.cpu().numpy() if res.boxes is not None else np.empty((0,))
    return boxes, confs

def run_on_image_bgr(bgr, conf=0.25, iou=0.45, with_ocr=True, annotate=True, max_plates=1):
    h, w = bgr.shape[:2]
    vis = bgr.copy()
    t0 = time.time()
    boxes, confs = detect_plates_bgr(bgr, conf, iou)

    idx = np.argsort(-confs)[:max_plates]
    boxes, confs = boxes[idx], confs[idx]

    detections = []
    for xyxy, c in zip(boxes, confs):
        x1, y1, x2, y2 = expand_box(xyxy, w, h, pad_ratio=0.10)
        crop = bgr[y1:y2, x1:x2]
        txt, s = ("", 0.0)

        # 👇 no gastes OCR si la caja es floja
        if with_ocr and crop.size and float(c) >= 0.55:
            txt, s = ocr_plate(crop)

        if annotate:
            label = f"{txt or 'plate'} {c:.2f}"
            draw_box_text(vis, (x1, y1, x2, y2), label)

        detections.append({"box_xyxy":[x1,y1,x2,y2],"det_conf":float(c),"ocr_text":txt,"ocr_conf":float(s)})

    dt_ms = int((time.time() - t0) * 1000)
    return vis, detections, (w, h), dt_ms

def bgr_to_jpeg_base64(bgr):
    ok, buf = cv2.imencode(".jpg", bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
    if not ok:
        return None
    return base64.b64encode(buf.tobytes()).decode("ascii")

def pil_to_bgr(pil_img: Image.Image) -> np.ndarray:
    if pil_img.mode not in ("RGB", "RGBA"):
        pil_img = pil_img.convert("RGB")
    arr = np.array(pil_img)
    if arr.ndim == 2:
        arr = np.stack([arr]*3, axis=-1)
    if arr.shape[2] == 4:
        arr = arr[:, :, :3]
    return cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)

def decode_bytes_to_bgr(data: bytes, content_type: str = "") -> np.ndarray:
    # 1) OpenCV rápido
    arr = np.frombuffer(data, np.uint8)
    bgr = cv2.imdecode(arr, cv2.IMREAD_COLOR)
    if bgr is not None:
        return bgr
    # 2) Fallback PIL (con HEIC soportado por pillow-heif)
    try:
        with Image.open(io.BytesIO(data)) as im:
            return pil_to_bgr(im)
    except Exception as e:
        raise ValueError(f"No pude decodificar la imagen ({content_type}): {e}")

def load_image_from_url(url: str) -> np.ndarray:
    ssl._create_default_https_context = ssl._create_unverified_context
    req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    with urllib.request.urlopen(req, timeout=20) as r:
        data = r.read()
    return decode_bytes_to_bgr(data, content_type=r.headers.get("Content-Type",""))

def load_image_from_b64(b64_or_data_url: str) -> np.ndarray:
    s = b64_or_data_url
    if s.startswith("data:"):
        s = s.split(",", 1)[1]
    raw = base64.b64decode(s)
    return decode_bytes_to_bgr(raw, content_type="base64")

# --- FastAPI ---
app = FastAPI(title="Plates API (HF Space)")

ALLOWED = [
    "http://localhost:5173", "http://127.0.0.1:5173",
    "https://www.omar-cruz.com", "https://omar-cruz.com",
]
app.add_middleware(
    CORSMiddleware,
    allow_origins=ALLOWED,
    allow_origin_regex=r"^https?://([a-z0-9-]+\.)*hf\.space$",
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)

class Detection(BaseModel):
    box_xyxy: List[int]
    det_conf: float
    ocr_text: str = ""
    ocr_conf: float = 0.0

class DetectResponse(BaseModel):
    detections: List[Detection]
    count: int
    width: int
    height: int
    time_ms: int
    annotated_image_b64: Optional[str] = None

class DetectRequest(BaseModel):
    image_url: Optional[str] = None
    image_b64: Optional[str] = None
    conf: float = Field(0.25, ge=0.05, le=0.95)
    iou: float  = Field(0.45, ge=0.1, le=0.9)
    ocr: bool = True
    return_image: bool = False

@app.get("/")
def health():
    return {
        "status": "ok",
        "service": "plates-api",
        "model": os.path.basename(WEIGHTS),
        "ocr_gpu": torch.cuda.is_available(),
        "allow_origins": ALLOWED,
    }

@app.post("/detect", response_model=DetectResponse)
def detect(req: DetectRequest):
    try:
        if not req.image_url and not req.image_b64:
            raise HTTPException(400, "Proporciona 'image_url' o 'image_b64'.")

        bgr = load_image_from_url(req.image_url) if req.image_url else load_image_from_b64(req.image_b64)
        vis, dets, (w, h), dt_ms = run_on_image_bgr(
            bgr, conf=req.conf, iou=req.iou, with_ocr=req.ocr, annotate=req.return_image
        )
        b64 = bgr_to_jpeg_base64(vis) if req.return_image else None
        return DetectResponse(
            detections=dets, count=len(dets), width=w, height=h, time_ms=dt_ms,
            annotated_image_b64=b64
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(500, f"Error procesando la imagen: {e}")

@app.post("/detect_upload", response_model=DetectResponse)
async def detect_upload(
    image: UploadFile = File(...),
    conf: float = Form(0.25),
    iou: float = Form(0.45),
    ocr: bool = Form(True),
    return_image: bool = Form(False),
):
    try:
        data = await image.read()
        if not data:
            raise HTTPException(400, "Archivo vacío.")

        bgr = decode_bytes_to_bgr(data, content_type=image.content_type or image.filename)
        if bgr is None:
            # si llega aquí es que ni cv2 ni PIL pudieron
            raise HTTPException(415, f"Formato no soportado: {image.content_type or image.filename}")

        vis, dets, (w, h), dt_ms = run_on_image_bgr(
            bgr, conf=conf, iou=iou, with_ocr=ocr, annotate=return_image
        )
        b64 = bgr_to_jpeg_base64(vis) if return_image else None
        return DetectResponse(
            detections=dets, count=len(dets), width=w, height=h, time_ms=dt_ms,
            annotated_image_b64=b64
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(500, f"Error procesando la imagen: {e}")

@app.on_event("startup")
def _warmup():
    import numpy as np, cv2
    dummy = np.zeros((512, 512, 3), dtype=np.uint8)
    try: _ = yolo.predict(dummy, conf=0.25, iou=0.45, imgsz=512, verbose=False)
    except Exception as e: print("Warmup YOLO:", e)
    try: _ = reader.readtext(cv2.cvtColor(dummy, cv2.COLOR_BGR2GRAY), detail=0)
    except Exception as e: print("Warmup EasyOCR:", e)