-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecaptcha_classification.py
More file actions
100 lines (80 loc) · 2.96 KB
/
recaptcha_classification.py
File metadata and controls
100 lines (80 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import base64
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# CapSolver API configuration
CAPSOLVER_API_KEY = os.getenv("CAPSOLVER_API_KEY")
CREATE_TASK_URL = "https://api.capsolver.com/createTask"
# Question types for ReCaptcha classification
QUESTION_TYPES = {
"cars": "/m/0k4j",
"motorcycles": "/m/04_sv",
"taxis": "/m/0pg52",
"buses": "/m/01bjv",
"traffic_lights": "/m/015qff",
"bicycles": "/m/0199g",
"boats": "/m/019jd",
"crosswalks": "/m/014xcs",
"fire_hydrants": "/m/01pns0",
"parking_meters": "/m/015qbp",
"stairs": "/m/01lynh",
"chimneys": "/m/01jk_4",
"bridges": "/m/015kr",
"tractors": "/m/07j7r",
"palm_trees": "/m/0cdl1",
"mountains": "/m/09d_r",
"hills": "/m/0cnMx"
}
def solve_recaptcha_classification(image_path=None, image_base64=None, question="cars"):
"""
Classify ReCaptcha v2 image challenges.
Args:
image_path: Path to the captcha image file
image_base64: Base64 encoded image (alternative to image_path)
question: Type of object to identify (see QUESTION_TYPES)
Returns:
Solution containing object indices or hasObject flag
"""
# Get base64 image
if image_path:
with open(image_path, "rb") as f:
image_base64 = base64.b64encode(f.read()).decode("utf-8")
if not image_base64:
raise ValueError("Either image_path or image_base64 must be provided")
# Convert question name to code if needed
question_code = QUESTION_TYPES.get(question, question)
payload = {
"clientKey": CAPSOLVER_API_KEY,
"task": {
"type": "ReCaptchaV2Classification",
"image": image_base64,
"question": question_code
}
}
# ReCaptchaV2Classification returns result directly (no polling needed)
response = requests.post(CREATE_TASK_URL, json=payload)
result = response.json()
if result.get("errorId") != 0:
raise Exception(f"Failed to solve: {result.get('errorDescription')}")
return result.get("solution", {})
def main():
if not CAPSOLVER_API_KEY:
print("Error: CAPSOLVER_API_KEY not found in .env file")
print("Please create a .env file with your API key:")
print("CAPSOLVER_API_KEY=your_api_key_here")
return
print("ReCaptcha v2 Image Classification")
print("\nAvailable question types:")
for name, code in QUESTION_TYPES.items():
print(f" {name}: {code}")
print("\nExample usage:")
print(' solution = solve_recaptcha_classification(image_path="captcha.png", question="cars")')
print(' # or')
print(' solution = solve_recaptcha_classification(image_base64="...", question="/m/0k4j")')
print("\nResponse format:")
print(" Multi-object: {'type': 'multi', 'objects': [0, 1, 2], 'size': 3}")
print(" Single-object: {'type': 'single', 'hasObject': True, 'size': 1}")
if __name__ == "__main__":
main()