[Python] Gibberish output for PyTesseract OCR

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"
    def extract_text(image):
    gray = image.convert('L')
    enhancer = ImageEnhance.Contrast(gray)
    enhanced_image = enhancer.enhance(2)
    enhanced_image = enhanced_image.filter(ImageFilter.GaussianBlur(radius=.3))
    enhanced_image.save("processed_image.png")
    text = pytesseract.image_to_string(enhanced_image, config='--psm 12')
    print(f'{text=}')
    return text

    @interactions.slash_command("predict", description="Predict the location of an uploaded image")
    @interactions.slash_option("image", "Upload an image", opt_type=interactions.OptionType.ATTACHMENT, required=True)
    async def predict(ctx: interactions.SlashContext, image: interactions.Attachment):
    await ctx.defer()
    url = image.url
    image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
    enhancer = ImageEnhance.Contrast(image)
    enhanced_image = enhancer.enhance(1.3)

    # Convert the enhanced image to a tensor and ensure values are in the range [0, 1]
    transform = transforms.Compose([
    transforms.ToTensor()
    ])
    image_tensor = transform(enhanced_image).unsqueeze(0).to(device)

    extracted_text = extract_text(enhanced_image)


    I am not sure why my pytesseract is returning gibberish, I am unsure why getting these outputs. [​IMG] That results in the following output:

    text = "m ms\n\nnu MvInIA6=Fmcem'x4Ir;u\nq Dnvuvauou 53523\n\n3 mm nyzenq795nx3I)1r>cmePmoe:.x\n- 531463 my\n\n1; I92I)xl2I)0,IMHz\n\n"


    I have tried changing the config, changing the filters, removing the filters, etc. I have updated pytesseract via pip, I am not sure what else there is to do. Anything help would be appreciated.

    Continue reading...

Compartilhe esta Página