Using ddddocr SDK for Captcha Recognition in Python
This article introduces the open‑source ddddocr SDK, demonstrates how to install it and use it in Python to automatically solve three common captcha types—slider, click‑based, and alphanumeric—providing code examples and result explanations for each.
Introduction
When crawling websites that require login, you often encounter captchas such as simple letter codes, slider puzzles, or click‑based image selections. These are long‑standing challenges for web scrapers. The ddddocr SDK ("ddddocr brother OCR") is a free, open‑source solution that can handle all of them.
Installation
The SDK automatically installs the appropriate version for your environment. It requires Python 3.9 or lower.
<code>pip install ddddocr</code>Usage
ddddocr can recognize three types of captchas. The following sections demonstrate each type.
Slider Captcha
The example uses Douban's slider captcha. The slider piece is stored in hycdn.png and the background with the slot in background.jpg .
<code>import ddddocr
det = ddddocr.DdddOcr(det=False, ocr=False)
with open('hycdn.png', 'rb') as f:
target_bytes = f.read()
with open('background.jpg', 'rb') as f:
background_bytes = f.read()
res = det.slide_match(target_bytes, background_bytes, simple_target=True)
print(res)
</code>Sample output:
<code>{'target_y': 0, 'target': [486, 126, 622, 262]}</code>Click‑Based Captcha
This example uses a click‑based captcha from NetEase login.
<code>det = ddddocr.DdddOcr(det=True)
with open('eb.jpg', 'rb') as f:
image = f.read()
poses = det.detection(image)
im = cv2.imread('eb.jpg')
for box in poses:
x1, y1, x2, y2 = box
im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
cv2.imwrite('result.jpg', im)
</code>The resulting image shows the detected click positions.
Alphanumeric Captcha
The image is taken from a Google search.
<code>ocr = ddddocr.DdddOcr(old=True)
with open('z1.jpg', 'rb') as f:
image = f.read()
res = ocr.classification(image)
print(res)
</code>Sample output:
<code>3n3d
8342
</code>Conclusion
ddddocr makes solving captchas simple and accessible, allowing developers who are not familiar with OpenCV, PyTorch, or TensorFlow to quickly bypass login verification challenges. Users are encouraged to share other effective OCR tools in the comments.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.