Damanger commited on
Commit
7d5465d
·
1 Parent(s): ec11250

corrigiendo app

Browse files
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -133,8 +133,8 @@ def draw_box_text(img, xyxy, text, color=(0, 255, 0)):
133
  cv2.putText(img, text, (x1 + 2, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0), 2, cv2.LINE_AA)
134
 
135
  def detect_plates_bgr(bgr, conf=0.25, iou=0.45):
136
- # imgsz 512 suele ser ~2× más rápido que 640 en CPU
137
- res = yolo.predict(bgr, conf=conf, iou=iou, imgsz=640, max_det=2, verbose=False)[0]
138
  boxes = res.boxes.xyxy.cpu().numpy() if res.boxes is not None else np.empty((0,4))
139
  confs = res.boxes.conf.cpu().numpy() if res.boxes is not None else np.empty((0,))
140
  return boxes, confs
@@ -146,25 +146,24 @@ def run_on_image_bgr(bgr, conf=0.25, iou=0.45, with_ocr=True, annotate=True, max
146
  boxes, confs = detect_plates_bgr(bgr, conf, iou)
147
 
148
  idx = np.argsort(-confs)[:max_plates]
149
- boxes = boxes[idx]
150
- confs = confs[idx]
151
 
152
  detections = []
153
  for xyxy, c in zip(boxes, confs):
154
- x1, y1, x2, y2 = expand_box(xyxy, w, h, pad_ratio=0.12) # 👈 12% borde
155
  crop = bgr[y1:y2, x1:x2]
156
  txt, s = ("", 0.0)
157
- if with_ocr and crop.size:
 
 
158
  txt, s = ocr_plate(crop)
 
159
  if annotate:
160
  label = f"{txt or 'plate'} {c:.2f}"
161
  draw_box_text(vis, (x1, y1, x2, y2), label)
162
- detections.append({
163
- "box_xyxy": [x1, y1, x2, y2],
164
- "det_conf": float(c),
165
- "ocr_text": txt,
166
- "ocr_conf": float(s),
167
- })
168
  dt_ms = int((time.time() - t0) * 1000)
169
  return vis, detections, (w, h), dt_ms
170
 
@@ -312,13 +311,9 @@ async def detect_upload(
312
 
313
  @app.on_event("startup")
314
  def _warmup():
315
- import numpy as np
316
- dummy = np.full((128, 256, 3), 255, dtype=np.uint8)
317
- try:
318
- _ = yolo.predict(dummy, conf=0.25, iou=0.45, verbose=False)
319
- except Exception as e:
320
- print("Warmup YOLO:", e)
321
- try:
322
- _ = reader.readtext(cv2.cvtColor(dummy, cv2.COLOR_BGR2GRAY), detail=0)
323
- except Exception as e:
324
- print("Warmup EasyOCR:", e)
 
133
  cv2.putText(img, text, (x1 + 2, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0), 2, cv2.LINE_AA)
134
 
135
  def detect_plates_bgr(bgr, conf=0.25, iou=0.45):
136
+ # 512 es buen sweet spot en CPU
137
+ res = yolo.predict(bgr, conf=conf, iou=iou, imgsz=512, max_det=1, verbose=False)[0]
138
  boxes = res.boxes.xyxy.cpu().numpy() if res.boxes is not None else np.empty((0,4))
139
  confs = res.boxes.conf.cpu().numpy() if res.boxes is not None else np.empty((0,))
140
  return boxes, confs
 
146
  boxes, confs = detect_plates_bgr(bgr, conf, iou)
147
 
148
  idx = np.argsort(-confs)[:max_plates]
149
+ boxes, confs = boxes[idx], confs[idx]
 
150
 
151
  detections = []
152
  for xyxy, c in zip(boxes, confs):
153
+ x1, y1, x2, y2 = expand_box(xyxy, w, h, pad_ratio=0.10)
154
  crop = bgr[y1:y2, x1:x2]
155
  txt, s = ("", 0.0)
156
+
157
+ # 👇 no gastes OCR si la caja es floja
158
+ if with_ocr and crop.size and float(c) >= 0.55:
159
  txt, s = ocr_plate(crop)
160
+
161
  if annotate:
162
  label = f"{txt or 'plate'} {c:.2f}"
163
  draw_box_text(vis, (x1, y1, x2, y2), label)
164
+
165
+ detections.append({"box_xyxy":[x1,y1,x2,y2],"det_conf":float(c),"ocr_text":txt,"ocr_conf":float(s)})
166
+
 
 
 
167
  dt_ms = int((time.time() - t0) * 1000)
168
  return vis, detections, (w, h), dt_ms
169
 
 
311
 
312
  @app.on_event("startup")
313
  def _warmup():
314
+ import numpy as np, cv2
315
+ dummy = np.zeros((512, 512, 3), dtype=np.uint8)
316
+ try: _ = yolo.predict(dummy, conf=0.25, iou=0.45, imgsz=512, verbose=False)
317
+ except Exception as e: print("Warmup YOLO:", e)
318
+ try: _ = reader.readtext(cv2.cvtColor(dummy, cv2.COLOR_BGR2GRAY), detail=0)
319
+ except Exception as e: print("Warmup EasyOCR:", e)