Se CaptchaAI retornar respostas incorretas para CAPTCHAs de imagem, o problema quase sempre está no envio da imagem - e não na solução. Veja como diagnosticar e corrigir isso.
Causas comuns de respostas erradas
| Causa | Frequência | Correção |
|---|---|---|
| Imagem cortada incorretamente | Muito comum | Capture o elemento CAPTCHA completo |
| Baixa resolução/compressão | Comum | Envie imagem de maior qualidade |
| Codificação de imagem errada | Comum | Verifique a codificação base64 |
| Dica de idioma /type ausente | Ocasional | Adicione language ou textinstructions |
| imagem | Ocasional | Capture uma nova imagem antes de resolver |
Correção 1: verifique a qualidade da imagem antes do envio
import base64
from io import BytesIO
from PIL import Image
def validate_captcha_image(image_path):
"""Check image quality before submitting to CaptchaAI."""
img = Image.open(image_path)
width, height = img.size
issues = []
# Minimum resolution
if width < 50 or height < 20:
issues.append(f"Too small: {width}x{height}px (min 50x20)")
# Check if mostly blank
pixels = list(img.getdata())
if img.mode == "RGB":
white_count = sum(1 for p in pixels if p[0] > 250 and p[1] > 250 and p[2] > 250)
else:
white_count = sum(1 for p in pixels if p > 250)
blank_ratio = white_count / len(pixels)
if blank_ratio > 0.95:
issues.append(f"Image appears blank ({blank_ratio:.0%} white)")
# File size check
img_bytes = BytesIO()
img.save(img_bytes, format="PNG")
size_kb = img_bytes.tell() / 1024
if size_kb < 1:
issues.append(f"File too small ({size_kb:.1f} KB) — may be empty")
if size_kb > 600:
issues.append(f"File too large ({size_kb:.0f} KB) — submit under 600 KB")
return issues
issues = validate_captcha_image("captcha.png")
if issues:
for issue in issues:
print(f"WARNING: {issue}")
else:
print("Image quality OK")
Correção 2: codificação Base64 correta
import base64
def encode_captcha(image_path):
"""Properly encode a CAPTCHA image to base64."""
with open(image_path, "rb") as f:
raw = f.read()
encoded = base64.b64encode(raw).decode("ascii")
# Verify round-trip
decoded = base64.b64decode(encoded)
assert decoded == raw, "Base64 encoding corrupted the image"
return encoded
# WRONG — encoding a file path string
bad = base64.b64encode(b"captcha.png").decode() # Encodes filename, not image!
# CORRECT — encoding file contents
with open("captcha.png", "rb") as f:
good = base64.b64encode(f.read()).decode()
Correção 3: pré-processamento de imagem
from PIL import Image, ImageFilter, ImageEnhance
from io import BytesIO
import base64
def preprocess_captcha(image_path):
"""Improve image quality for better OCR accuracy."""
img = Image.open(image_path)
# Convert to RGB if needed
if img.mode != "RGB":
img = img.convert("RGB")
# Upscale small images
width, height = img.size
if width < 200:
scale = 200 / width
img = img.resize(
(int(width * scale), int(height * scale)),
Image.LANCZOS,
)
# Increase contrast
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(1.5)
# Sharpen
img = img.filter(ImageFilter.SHARPEN)
# Convert to PNG bytes
buffer = BytesIO()
img.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode()
Correção 4: adicionar dicas de tipo e idioma
import requests
def solve_image(api_key, image_base64, **hints):
"""Submit image CAPTCHA with quality hints."""
data = {
"key": api_key,
"method": "base64",
"body": image_base64,
"json": 1,
}
# Add optional hints for better accuracy
if "language" in hints:
data["language"] = hints["language"] # 0=default, 1=Cyrillic, 2=Latin
if "textinstructions" in hints:
data["textinstructions"] = hints["textinstructions"]
if "numeric" in hints:
data["numeric"] = hints["numeric"] # 1=digits only, 2=letters only
if "min_len" in hints:
data["min_len"] = hints["min_len"]
if "max_len" in hints:
data["max_len"] = hints["max_len"]
resp = requests.post("https://ocr.captchaai.com/in.php", data=data, timeout=30)
return resp.json()
# Example: Digits-only CAPTCHA, 4-6 characters
result = solve_image(
"YOUR_API_KEY",
encoded_image,
numeric=1,
min_len=4,
max_len=6,
)
# Example: Case-sensitive text
result = solve_image(
"YOUR_API_KEY",
encoded_image,
textinstructions="Case-sensitive, enter exactly as shown",
)
Correção 5: capturar elemento CAPTCHA completo
from selenium import webdriver
from selenium.webdriver.common.by import By
import base64
def capture_captcha_element(driver, selector):
"""Screenshot only the CAPTCHA element, not the full page."""
element = driver.find_element(By.CSS_SELECTOR, selector)
# Element screenshot (better than page crop)
png_bytes = element.screenshot_as_png
# Verify it's not empty
if len(png_bytes) < 500:
raise ValueError("Screenshot too small — element may not be visible")
return base64.b64encode(png_bytes).decode()
# Usage
driver = webdriver.Chrome()
driver.get("https://example.com")
image_b64 = capture_captcha_element(driver, "img#captchaImage")
Correção 6: lidar com CAPTCHAs Dynamic/Rotating
import time
def solve_with_fresh_image(driver, api_key, captcha_selector):
"""Capture and solve CAPTCHA immediately to avoid expiry."""
# Wait for CAPTCHA to load fully
time.sleep(2)
# Capture fresh
element = driver.find_element(By.CSS_SELECTOR, captcha_selector)
png_bytes = element.screenshot_as_png
body = base64.b64encode(png_bytes).decode()
# Submit immediately
resp = requests.post("https://ocr.captchaai.com/in.php", data={
"key": api_key,
"method": "base64",
"body": body,
"json": 1,
}, timeout=30)
result = resp.json()
if result.get("status") != 1:
raise RuntimeError(result.get("request"))
task_id = result["request"]
# Poll — image CAPTCHAs solve fast
time.sleep(5)
for _ in range(12):
resp = requests.get("https://ocr.captchaai.com/res.php", params={
"key": api_key, "action": "get",
"id": task_id, "json": 1,
}, timeout=15)
data = resp.json()
if data.get("status") == 1:
return data["request"]
if data["request"] != "CAPCHA_NOT_READY":
raise RuntimeError(data["request"])
time.sleep(3)
raise TimeoutError("Image solve timeout")
Lista de verificação de solução de problemas
| Sintoma | Diagnóstico | Correção |
|---|---|---|
| A resposta é um jargão | Codificação Base64 errada | Verifique a codificação de ida e volta |
| A resposta está próxima, mas errada | Baixa qualidade de imagem | Pré-processamento: sofisticado, nítido, contraste |
| A resposta tem contagem de caracteres errada | Dicas de comprimento ausentes | Adicionar parâmetros min_len/max_len |
| A resposta mistura letras/digits | Dica de tipo ausente | Adicione numérico=1 ou numérico=2 |
| Resposta vazia retornada | imagem | Valide a imagem antes de enviar |
| Resposta correta, mas o site rejeita | Sensibilidade a maiúsculas e minúsculas | Adicione instruções de texto para o caso |
Perguntas frequentes
Quão preciso é CaptchaAI para CAPTCHAs de imagem?
Com imagens enviadas corretamente, CaptchaAI suporta mais de 27.500 tipos de CAPTCHA com alta precisão. A maioria das falhas se deve à má qualidade da imagem ou a parâmetros incorretos.
Devo pré-processar as imagens antes de enviar?
Somente se a imagem original for de baixa qualidade. CaptchaAI lida bem com imagens CAPTCHA padrão sem pré-processamento. Aumentar a escala de imagens muito pequenas e aumentar o contraste pode ajudar em casos extremos.
Posso relatar respostas erradas?
Sim. Use o endpoint reportbad com o ID da tarefa para relatar respostas incorretas. Isso ajuda a aprimorar a precisão e pode creditar sua conta.
Guias Relacionados
- Resolva imagens com precisão -experimente CaptchaAI.*