Repository navigation
Expand file tree
/
Copy pathdebug_mode.py
More file actions
47 lines (36 loc) · 1.67 KB
/
Copy pathdebug_mode.py
File metadata and controls
47 lines (36 loc) · 1.67 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
"""Example: Run one Image to Text task with sanitized debug logging enabled.
What it does:
This example reads a bundled CAPTCHA image, enables debug mode explicitly,
solves it with ImageToTextTask and CaptchaClient.solve(), and prints the
recognized text after the diagnostic events and SOLVE SUMMARY.
How it works:
STEP-01 - Configure CaptchaClient with debug=True and read the bundled image.
STEP-02 - Encode the image and create ImageToTextTask.
STEP-03 - Call solve(), which creates and polls the task while logging safely.
STEP-04 - Inspect the sanitized logs and print the recognized text.
Prerequisites:
Set the CAPTCHA_API_KEY environment variable or add it to the repository .env.
This example creates a real, paid CAPTCHA task.
"""
import os
import sys
from base64 import b64encode
try:
from dotenv import load_dotenv
except ModuleNotFoundError as exc:
if exc.name != "dotenv":
raise
load_dotenv = None
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
sys.path.append(repo_root)
if load_dotenv is not None:
load_dotenv(os.path.join(repo_root, ".env"))
from captcha_solver_api import CaptchaClient
from captcha_solver_api.tasks import ImageToTextTask
api_key = os.getenv("CAPTCHA_API_KEY", "YOUR_API_KEY")
image_path = os.path.join(repo_root, "examples", "assets", "captcha-digits.png")
with open(image_path, "rb") as image_file:
image_body = b64encode(image_file.read()).decode("utf-8")
with CaptchaClient(api_key, debug=True) as client:
solution = client.solve(ImageToTextTask(body=image_body, numeric=1, minLength=4, maxLength=6))
print(f"Recognized text: {solution['text']}")