A comprehensive Python-based CAPTCHA solving toolkit that combines image preprocessing, OCR (Optical Character Recognition), and web automation to solve various types of text-based CAPTCHAs.
This tool is intended for educational purposes, security research, and legitimate testing only.
- Use only on systems you own or have explicit permission to test
- Respect website terms of service and rate limits
- Do not use for malicious activities or to bypass security measures
- The developers are not responsible for misuse of this software
- Multi-source CAPTCHA extraction: From files, URLs, and web elements
- Advanced image preprocessing: Noise reduction, enhancement, and optimization
- Robust OCR engine: Multiple Tesseract configurations for better accuracy
- Web automation support: Selenium integration for form submission
- Batch processing: Handle multiple CAPTCHAs efficiently
- Comprehensive logging: Detailed processing information and debugging
- Flexible configuration: Customizable settings for different CAPTCHA types
- Performance monitoring: Statistics and confidence scoring
- Python 3.7 or higher
- Tesseract OCR engine
- Chrome/Firefox browser (for Selenium)
See requirements.txt for the complete list. Key dependencies include:
requests- HTTP requestsbeautifulsoup4- HTML parsingPillow- Image processingpytesseract- OCR engine interfaceselenium- Web automationopencv-python- Advanced image processingnumpy- Numerical operations
Windows:
# Download and install from: https://github.com/UB-Mannheim/tesseract/wiki
# Or using chocolatey:
choco install tesseractLinux (Ubuntu/Debian):
sudo apt-get update
sudo apt-get install tesseract-ocr
sudo apt-get install libtesseract-devmacOS:
brew install tesseractgit clone <repository-url>
cd Captcha_Solver
pip install -r requirements.txtfrom captcha_solver import CAPTCHASolver
solver = CAPTCHASolver()
print(solver.test_components())from captcha_solver import CAPTCHASolver
# Initialize solver
solver = CAPTCHASolver()
# Solve from image file
result = solver.solve_from_file("captcha.png")
print(f"CAPTCHA solved: {result}")
# Solve from URL
result = solver.solve_from_url("https://example.com/captcha-page")
print(f"CAPTCHA solved: {result}")from captcha_solver import CAPTCHASolver, Config
# Custom configuration
config = {
"tesseract": {
"config": "--psm 8 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"timeout": 30
},
"preprocessing": {
"steps": ["grayscale", "denoise", "threshold", "enhance"]
},
"ocr": {
"confidence_threshold": 70
}
}
solver = CAPTCHASolver(config=config)
result = solver.solve_from_file("difficult_captcha.png", save_processed=True)
print(f"Result: {result}, Confidence: {solver.get_confidence():.1f}%")from selenium import webdriver
from captcha_solver import CAPTCHASolver, FormSubmitter
# Setup WebDriver
driver = webdriver.Chrome()
solver = CAPTCHASolver()
submitter = FormSubmitter(driver)
try:
# Navigate to page with CAPTCHA
driver.get("https://example.com/login")
# Find CAPTCHA element
captcha_element = driver.find_element("css selector", "img.captcha")
# Solve CAPTCHA
result = solver.solve_from_element(captcha_element, driver)
# Submit form
form_data = {
"username": "your_username",
"password": "your_password",
"captcha": result
}
success = submitter.submit_form(form_data, "#login-form")
print(f"Form submitted: {success}")
finally:
driver.quit()from pathlib import Path
from captcha_solver import CAPTCHASolver
solver = CAPTCHASolver()
# Process all images in a directory
image_dir = Path("captcha_images")
image_paths = list(image_dir.glob("*.png"))
results = solver.solve_batch(image_paths, save_processed=True)
# Print results
for result in results:
print(f"{result['path']}: {result['result']} ({result['confidence']:.1f}%)")
# Get statistics
stats = solver.get_statistics()
print(f"Success rate: {stats['success_rate']:.1f}%")Captcha_Solver/
βββ captcha_solver/
β βββ __init__.py # Package initialization
β βββ config.py # Configuration management
β βββ solver.py # Main solver class
β βββ extractor.py # CAPTCHA image extraction
β βββ preprocessor.py # Image preprocessing
β βββ ocr.py # OCR handling
β βββ submitter.py # Form submission
β βββ utils.py # Utility functions
βββ examples/
β βββ basic_usage.py # Basic examples
β βββ advanced_config.py # Advanced configuration
β βββ selenium_example.py # Selenium integration
β βββ batch_processing.py # Batch processing
βββ tests/
β βββ test_solver.py # Main solver tests
β βββ test_preprocessor.py # Preprocessing tests
β βββ test_ocr.py # OCR tests
βββ docs/
β βββ configuration.md # Configuration guide
β βββ preprocessing.md # Preprocessing guide
β βββ troubleshooting.md # Common issues
βββ requirements.txt # Python dependencies
βββ setup.py # Package setup
βββ README.md # This file
βββ caotcha me.py # Original implementation
The solver supports extensive configuration through YAML files or Python dictionaries:
# config.yaml
tesseract:
path: "/usr/bin/tesseract" # Auto-detected if not specified
config: "--psm 8 --oem 3"
timeout: 30
language: "eng"
preprocessing:
steps:
- "grayscale"
- "denoise"
- "threshold"
- "enhance"
grayscale:
method: "weighted" # "average", "weighted", "luminosity"
denoise:
method: "gaussian" # "gaussian", "median", "bilateral"
kernel_size: 3
threshold:
method: "adaptive" # "binary", "adaptive", "otsu"
block_size: 11
c_value: 2
ocr:
confidence_threshold: 60
character_whitelist: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
multiple_configs: true
selenium:
implicit_wait: 10
page_load_timeout: 30
screenshot_on_error: true
extraction:
default_selectors:
- "img[src*='captcha']"
- ".captcha img"
- "#captcha"
timeout: 10
retry_attempts: 3The image preprocessor supports various enhancement techniques:
- Grayscale conversion: Multiple algorithms for optimal contrast
- Noise reduction: Gaussian, median, and bilateral filtering
- Thresholding: Binary, adaptive, and Otsu methods
- Morphological operations: Opening, closing, erosion, dilation
- Enhancement: Contrast adjustment, sharpening, histogram equalization
- Skew correction: Automatic rotation correction
- Character segmentation: Individual character isolation
Tesseract OCR can be fine-tuned for different CAPTCHA types:
# Numeric only CAPTCHAs
config = "--psm 8 -c tessedit_char_whitelist=0123456789"
# Alphanumeric CAPTCHAs
config = "--psm 8 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# Single word CAPTCHAs
config = "--psm 8 --oem 3"
# Multiple words
config = "--psm 6 --oem 3"solver = CAPTCHASolver()
# Process some CAPTCHAs...
# Get detailed statistics
stats = solver.get_statistics()
print(f"Total processed: {stats['total_processed']}")
print(f"Success rate: {stats['success_rate']:.1f}%")
print(f"Average confidence: {stats['average_confidence']:.1f}%")
# Get system information
info = solver.get_system_info()
print(f"Component tests: {info['component_tests']}")-
Tesseract not found
TesseractNotFoundError: tesseract is not installed- Install Tesseract OCR and ensure it's in PATH
- Or specify path in configuration
-
Low accuracy
- Try different preprocessing steps
- Adjust OCR configuration
- Use character whitelisting
- Check image quality and size
-
Selenium WebDriver issues
- Update WebDriver to match browser version
- Use webdriver-manager for automatic management
- Check browser compatibility
solver = CAPTCHASolver(log_level="DEBUG")
result = solver.solve_from_file("captcha.png", save_processed=True)
# Check processed image
processed_img = solver.get_processed_image()
processed_img.show() # Display processed image
# Check confidence
print(f"Confidence: {solver.get_confidence():.1f}%")This project is licensed under the MIT License - see the LICENSE file for details.
- Tesseract OCR - OCR engine
- OpenCV - Computer vision library
- Selenium - Web automation framework
- Pillow - Python imaging library
Remember: Use this tool responsibly and ethically. Always respect website terms of service and applicable laws.