Download code/routeAB/engine/fastcand.py from Raphael2099/chujian-ocr-sources: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/datasets/Raphael2099/chujian-ocr-sources/resolve/main/code/routeAB/engine/fastcand.py
- Command line
-
hf download hf://datasets/Raphael2099/chujian-ocr-sources/code/routeAB/engine/fastcand.py
-
curl -L -o fastcand.py https://huggingface.co/datasets/Raphael2099/chujian-ocr-sources/resolve/main/code/routeAB/engine/fastcand.py
3.2 kB
| """Coarse-to-fine accelerator for glyph_candidates. | |
| Same objective as the pilot engine (argmax of the full-plate NCC over the | |
| s0*[0.78,3.2] grid); only the *search* is staged: | |
| stage 1 the whole plate, decimated D=3, all 33 scales -> top locations | |
| stage 2 FULL RESOLUTION NCC in a window around each surviving location, | |
| over a +-5-step scale bracket | |
| Every reported score/position is a genuine full-resolution NCC value. Accepted | |
| only after it reproduced the exhaustive full-resolution picks (see val_fast.py). | |
| """ | |
| import numpy as np | |
| from engine import ImgCtx, resize_tpl, top_peaks | |
| D = 3 # decimation for stage 1 | |
| NLOC = 8 # locations carried into stage 2 | |
| SBRK = 5 # +- scale grid steps refined at each location | |
| MARG = 3 * D + 8 | |
| def decimate(img, d=D): | |
| H, W = img.shape | |
| Hs, Ws = H // d, W // d | |
| return img[:Hs * d, :Ws * d].reshape(Hs, d, Ws, d).mean(axis=(1, 3)) | |
| def coarse_ctx(img, d=D): | |
| return ImgCtx(decimate(img, d)), d | |
| def candidates(img, cctx, d, gpil, scales, npeak=4, ncand=12): | |
| H, W = img.shape | |
| gw, gh = gpil.size | |
| # ---- stage 1: decimated full-plate sweep | |
| loc = [] | |
| for j, s in enumerate(scales): | |
| t = resize_tpl(gpil, s / d) | |
| if t.shape[0] > cctx.H or t.shape[1] > cctx.W: | |
| continue | |
| m = cctx.ncc(t) | |
| if m is None: | |
| continue | |
| for v, x, y in top_peaks(m, t.shape[0], t.shape[1], npeak): | |
| loc.append((v, (x + t.shape[1] / 2.) * d, (y + t.shape[0] / 2.) * d, j)) | |
| if not loc: | |
| return [] | |
| loc.sort(key=lambda c: -c[0]) | |
| picks = [] | |
| for v, cx, cy, j in loc: | |
| tw = gw * scales[j]; th = gh * scales[j] | |
| if all(not (abs(cx - p[1]) < .45 * tw and abs(cy - p[2]) < .45 * th) for p in picks): | |
| picks.append((v, cx, cy, j)) | |
| if len(picks) >= NLOC: | |
| break | |
| # ---- stage 2: full-resolution refinement in a window per location | |
| out = [] | |
| for v, cx, cy, j in picks: | |
| j0, j1 = max(0, j - SBRK), min(len(scales) - 1, j + SBRK) | |
| twm = int(round(gw * scales[j1])) + 1 | |
| thm = int(round(gh * scales[j1])) + 1 | |
| x0 = max(0, int(cx - twm / 2. - MARG)); x1 = min(W, int(cx + twm / 2. + MARG) + 1) | |
| y0 = max(0, int(cy - thm / 2. - MARG)); y1 = min(H, int(cy + thm / 2. + MARG) + 1) | |
| win = np.ascontiguousarray(img[y0:y1, x0:x1]) | |
| if win.shape[0] < 8 or win.shape[1] < 8: | |
| continue | |
| wc = ImgCtx(win) | |
| for k in range(j0, j1 + 1): | |
| t = resize_tpl(gpil, scales[k]) | |
| m = wc.ncc(t) | |
| if m is None: | |
| continue | |
| i = int(np.argmax(m)); py, px = divmod(i, m.shape[1]) | |
| out.append((float(m[py, px]), x0 + int(px), y0 + int(py), | |
| int(t.shape[1]), int(t.shape[0]), float(scales[k]))) | |
| out.sort(key=lambda c: -c[0]) | |
| keep = [] | |
| for c in out: | |
| cx, cy = c[1] + c[3] / 2., c[2] + c[4] / 2. | |
| if all(not (abs(cx - (k[1] + k[3] / 2.)) < .45 * k[3] and | |
| abs(cy - (k[2] + k[4] / 2.)) < .45 * k[4]) for k in keep): | |
| keep.append(c) | |
| if len(keep) >= ncand: | |
| break | |
| return keep | |