Spaces:
Running
Running
Speed up screenshot OCR by downscaling before tesseract
Browse files50 unmodified lines51525354555657585960616263646566676869587071727374637576777838 unmodified lines11711811912012112212312412512612712810810911011111211312913013113213313413513613750 unmodified lines# OCR via tesseract subprocess# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
OCR_MAX_DIM = 1500 # downscale before tesseract for speed; boxes scaled back
def run_tesseract(image: np.ndarray, min_conf: int = 30) -> list[tuple]: """Call `tesseract` CLI, parse TSV output, return (x, y, w, h) boxes.""" h, w = image.shape[:2] scale = 1.0 ocr_image = image if max(h, w) > OCR_MAX_DIM: scale = OCR_MAX_DIM / float(max(h, w)) new_w = max(1, int(round(w * scale))) new_h = max(1, int(round(h * scale))) ocr_image = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA)
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False) try: Image.fromarray(image).save(tmp.name) Image.fromarray(ocr_image).save(tmp.name) result = subprocess.run( ["tesseract", tmp.name, "stdout", "--psm", "3", "tsv"], capture_output=True, text=True, timeout=30, timeout=60, ) except FileNotFoundError: print("[screenshot] tesseract binary not found")38 unmodified lines continue if conf < min_conf: continue if scale != 1.0: inv = 1.0 / scale boxes.append(( int(round(int(cols[idx_left]) * inv)), int(round(int(cols[idx_top]) * inv)), int(round(int(cols[idx_width]) * inv)), int(round(int(cols[idx_height]) * inv)), )) else: boxes.append(( int(cols[idx_left]), int(cols[idx_top]), int(cols[idx_width]), int(cols[idx_height]), )) boxes.append(( int(cols[idx_left]), int(cols[idx_top]), int(cols[idx_width]), int(cols[idx_height]), )) return boxes
app/screenshot.py+29-8