[Python] PaddleOCR returning strange results when extracting text from detected regions in...

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

  1. Stack

    Stack Membro Participativo

    I'm using PaddleOCR to extract text from specific regions of an image, and I am facing an issue where sometimes the OCR results return only coordinates (like the ones below) instead of actual text, even though the region contains visible text.

    Example output of ocr.ocr() in some regions:

    [66.0, 38.0] [94.0, 38.0] [94.0, 113.0] [66.0, 113.0]
    [32.0, 44.0] [111.0, 48.0] [110.0, 76.0] [31.0, 72.0]
    [53.0, 106.0] [70.0, 106.0] [70.0, 128.0] [53.0, 128.0]
    [46.0, 27.0] [78.0, 27.0] [78.0, 52.0] [46.0, 52.0]
    [35.0, 59.0] [121.0, 57.0] [122.0, 86.0] [36.0, 88.0]
    [34.0, 53.0] [51.0, 53.0] [51.0, 78.0] [34.0, 78.0]
    [61.0, 20.0] [90.0, 19.0] [92.0, 159.0] [63.0, 160.0]
    [17.0, 43.0] [45.0, 43.0] [45.0, 117.0] [17.0, 117.0]
    [47.0, 39.0] [79.0, 39.0] [79.0, 65.0] [47.0, 65.0]


    I am passing the coordinates of the areas to PaddleOCR and attempting to extract text from them. However, sometimes I get only coordinates and no text, even though there is visible text inside the regions.

    Here is the relevant part of my code where I extract text from the bounding boxes:

    from paddleocr import PaddleOCR
    def recognize_text_from_bubbles(image, lang, detections):
    lang_mapping = {
    "Japanese": "jp",
    "English": "en",
    "Chinese": "ch"
    }
    ocr_lang = lang_mapping.get(lang, "en")
    ocr = PaddleOCR(lang=ocr_lang, use_angle_cls=True)
    recognized_text = ""
    for detection in detections:
    x, y, w, h = detection
    x = int(x) - int(w / 2)
    y = int(y) - int(h / 2)
    roi = image[y:y + int(h), x:x + int(w)]
    result = ocr.ocr(roi, cls=True)
    if result:
    for line in result:
    if len(line) > 1 and line[1]:
    candidate = line[1][0]
    recognized_text += candidate + "\n"
    else:
    print(f"Empty or invalid OCR result for line: {line}")
    else:
    print("No text detected in this region.")
    if not recognized_text:
    print("No text was recognized.")
    return recognized_text


    What I Have Tried

    1. Verified that the detection bounding boxes are correctly calculated.
    2. Tried different images with varying text styles and sizes.
    3. Tried using different language settings for OCR, but the problem persists.
    4. Added checks for None values and empty lists, but the OCR still sometimes fails to return any text.

    What could cause PaddleOCR to fail in extracting text from these regions, and how can I improve the accuracy of text recognition from these regions? Is there any preprocessing or adjustments I could apply to help the OCR perform better on the extracted regions?

    Continue reading...

Compartilhe esta Página