[Python] Improving OCR Accuracy for Digits Inside Circles from Vector PDF Images (AutoCAD...

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on an OCR task where I need to detect and read numbers that are inside circles. The original data comes from .dwg (AutoCAD) files, and the PDFs I use are not scanned — they’re exported directly from AutoCAD, so they contain clean, vector-based text and shapes.

    Here's my pipeline:


    • Convert PDF pages to images using a high resolution (up to 1200 DPI). V1.png


    • Detect all circles using Hough Circle Transform (they all have roughly the same radius).


    • Slice each region of interest (ROI) around the detected circles.


    • Preprocess the ROI (grayscale + thresholding).


    • Run Tesseract OCR on each ROI with config --psm 7 digits.

    Despite this, I can’t seem to get the OCR accuracy above 80%. Many numbers are either misread or not detected at all, even though the input seems quite clean.

    img = cv2.imread("V1.png")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Detect circles
    circles = cv2.HoughCircles(
    gray,
    cv2.HOUGH_GRADIENT,
    dp=1.2,
    minDist=20,
    param1=50,
    param2=30,
    minRadius=52,
    maxRadius=56
    )

    rod_positions = []
    if circles is not None:
    circles = np.round(circles[0, :]).astype("int")
    for (x, y, r) in circles:
    rod_positions.append((x, y, r))

    rod_labels = []
    for (x, y, r) in rod_positions:
    patch_size_x = int(0.9 * r) - 1
    patch_size_y = r // 2 - 1
    roi = gray[y - patch_size_y:y + patch_size_y, x - patch_size_x:x + patch_size_x]

    _, roi_thresh = cv2.threshold(roi, 150, 255, cv2.THRESH_BINARY_INV)
    config = '--psm 7 digits'
    number = pytesseract.image_to_string(roi_thresh, config=config)
    rod_labels.append(number.strip())


    What I’ve tried:


    • Increased PDF-to-image resolution (300 → 600 → 1200 DPI) — minor improvements.


    • Various Tesseract configs (--psm 6, --psm 10, etc.) — --psm 7 with digits seems best.


    • Different preprocessing methods (e.g., adaptive thresholding, dilation, erosion).

    What I'm trying to achieve:


    • High OCR accuracy (ideally >95%) for digits inside circles.


    • Generate a database to train a Neural Network

    Questions:


    • Given that the source is a vector PDF, should I be using a completely different approach (e.g., PDF parsing or font-based extraction)?


    • Are there preprocessing tricks or OCR config options that could improve digit recognition here?


    • Would training a custom Tesseract model help — or is that overkill for such clean, uniform data?

    Any tips or suggestions would be hugely appreciated!

    Continue reading...

Compartilhe esta Página