0

I have been trying to convert image to text using pytesseract library. I have managed to photocapture the captcha and try to translate it, however. It can't translate the text well and makes the captcha fail, I don't know what I'm doing wrong, I've searched many sites and I can't find something specific :s. I really need help

This is my code on python:

from PIL import Image, ImageFilter
import pytesseract as pt
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

rif = 'J400607396'
browser = webdriver.Chrome('C:\\Users\\USUARIO\\Desktop\\chromedriver.exe')  -> driver on chrome
browser.get('http://contribuyente.seniat.gob.ve/BuscaRif/BuscaRif.jsp') -> page
# completar el rif y presionar el siguiente botón 
p_rif = browser.find_element_by_id('p_rif') -> input p rif
p_rif.send_keys(rif) 

# Imagen captcha

screenshot_name = "captcha.png"
browser.save_screenshot(screenshot_name)

img = Image.open("captcha.png")
area = (143, 205, 263, 235)
cropped_img = img.crop(area)
cropped_img.save('captcha.png')

img = Image.open("captcha.png")

captcha = pt.image_to_string(img,config='--psm 10 -c tessedit_char_whitelist=0123456789abcdefghijklmnopkrstuvwxyz') -> this is the problem
captcha = captcha.replace(" ", "").strip()
print(captcha)
codigo = browser.find_element_by_id('codigo')
codigo.send_keys(captcha)
nextButton = browser.find_element_by_name('busca') 
nextButton.click()
browser.close()

I need convert a image in text, is all

Captcha example

I will attach an example captcha, the captcha always changes with each update

5
  • 1
    If it were possible, what's the sense of captchas?
    – enzo
    Sep 7, 2020 at 1:07
  • I need to resolve the captchas to get more information of the rif sent Sep 7, 2020 at 1:10
  • captcha is used to block bots/scripts - and it seems it works correctly on this page. it shouldn't be easy to workaround. You can try to learn some model in Machine Learning, Deep Network which can recognize text with some extra elements. As I know tesseract use also Deep Network to recognize text and there is option to learn own model for tesseract but it may not be easy task. You can also find portals which can recognize text on captcha (but you may have to pay for it) - some of them use Deep Network + Real People.
    – furas
    Sep 7, 2020 at 1:33
  • see also Tesseract doc: Improving the quality of the output and Training Tesseract
    – furas
    Sep 7, 2020 at 1:35
  • I tested your example image directly with tesseract-ocr. I get text when I convert to grayscale, and invert to black text on white background and use --psm 7 (for text in one line). I tested also with resizing 200% (and --psm 7) but it doesn't give text.
    – furas
    Sep 7, 2020 at 1:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.