import cv2
import numpy as np
import easyocr
import time

reader = easyocr.Reader(['en'], gpu=False)

def test_ocr(image_text, img_data):
    # Upscale 3x to give CRAFT detector enough spatial resolution
    upscaled = cv2.resize(img_data, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
    # Pad to make it look like a printed document page
    padded = cv2.copyMakeBorder(upscaled, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[255, 255, 255])
    
    t0 = time.time()
    results = reader.readtext(padded, allowlist="0123456789")
    tf = time.time()
    
    print(f"[{image_text}] Result: {results} (took {tf-t0:.2f}s)")

# Generate a tiny "37" (white on black)
img_37 = np.zeros((30, 40), dtype=np.uint8)
img_37[5:10, 10:20] = 255 # Top of "3"
img_37[12:15, 10:20] = 255 # Mid of "3"
img_37[20:25, 10:20] = 255 # Bot of "3"
img_37[5:10, 25:35] = 255 # Top of "7"
img_37[5:25, 30:35] = 255 # Right of "7"

# Invert it so it's black text on white background (what OCR expects)
img_37_inv = cv2.bitwise_not(img_37)

test_ocr("Tiny 37 Synth", img_37_inv)
