Neural Network Model for solving captchas in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
9.4KB

  1. import tensorflow as tf
  2. from tensorflow.keras.models import load_model
  3. from imutils import paths
  4. import numpy as np
  5. import cv2
  6. import pickle
  7. from tratar_captcha import tratar_base64
  8. import base64
  9. tamanho_imagem = (35, 50)
  10. modelo = load_model("modelo_treinado.keras")
  11. # Posições fixas para o corte de cada caractere
  12. posicoes_corte = [(6, 40), (56, 90), (111, 144), (161, 197), (214, 250), (265, 297)]
  13. def quebrar_captcha_base64(base64_string):
  14. if base64_string.startswith('data:image/png;base64,'):
  15. base64_string = base64_string.replace('data:image/png;base64,', '')
  16. with open("rotulos_modelo.dat", "rb") as arquivo_tradutor:
  17. lb = pickle.load(arquivo_tradutor)
  18. imagem_tratada = tratar_base64(base64_string)
  19. # Aplicar um limiar para binarizar a imagem
  20. _, imagem_bin = cv2.threshold(imagem_tratada, 128, 255, cv2.THRESH_BINARY_INV)
  21. imagem_final = cv2.merge([imagem_bin] * 3) # Convertendo de volta para RGB para desenhar os retângulos coloridos
  22. # Processar cada região de corte definida
  23. previsao = []
  24. for x1, x2 in posicoes_corte:
  25. imagem_caractere = imagem_bin[20:-15, x1:x2] # Cortar a região do caractere
  26. imagem_caractere = cv2.bitwise_not(imagem_caractere)
  27. imagem_caractere = cv2.resize(imagem_caractere, tamanho_imagem)
  28. imagem_caractere = np.expand_dims(imagem_caractere, axis=2)
  29. imagem_caractere = np.expand_dims(imagem_caractere, axis=0)
  30. letra_prevista = modelo.predict(imagem_caractere)
  31. letra_prevista = lb.inverse_transform(letra_prevista)[0]
  32. previsao.append(letra_prevista)
  33. texto_previsao = "".join(previsao)
  34. print(texto_previsao)
  35. return texto_previsao
  36. # Exemplo de uso
  37. if __name__ == "__main__":
  38. # Aqui você deve substituir por uma string base64 real
  39. base64_string = "data:image/png;base64, /9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKD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