Master Double-Digit OCR with ddddocr: Deep Learning Library for PHP & Python
This article introduces ddddocr, an open‑source deep‑learning OCR library for recognizing double‑digit numbers, explains its background, key features, installation steps, and provides detailed PHP examples for basic OCR, target detection, and slider detection functionalities.
Introduction
ddddocr (Deep Double‑Digital Digits OCR) is an open‑source library that uses deep learning to recognize double‑digit numbers in images. It offers both training and inference capabilities, allowing developers to integrate accurate double‑digit OCR into their applications.
Background
In computer vision, recognizing numbers is common, but traditional methods struggle with two‑digit numbers because of visual similarity and overlapping features. ddddocr addresses this by combining convolutional neural networks (CNN) and recurrent neural networks (RNN) to achieve high accuracy on double‑digit tasks.
Features and Advantages
Deep Learning : Utilises CNN and RNN models for precise double‑digit recognition.
Open Source : Free to use, modify, and distribute, encouraging community contributions.
High Accuracy : Deep‑learning approach overcomes the limitations of traditional algorithms.
Flexibility : Provides both training and prediction APIs, allowing custom model training for various use cases.
Installation
pip install ddddocrBasic OCR Usage
Single‑Line Text Recognition
Designed for images where text occupies the main area, such as alphanumeric captchas. Supports Chinese, English (case‑sensitive or forced case), digits, and some special characters.
Example file:
demo01.php <?php
/**
* @desc Basic OCR capability
* @help https://github.com/sml2h3/ddddocr
* @author Tinywan (ShaoBo Wan)
*/
declare(strict_types=1);
$ddd = PyCore::import('ddddocr');
$file = '../demo01.png';
$imgBytes = PyCore::bytes(file_get_contents($file));
$ocr = $ddd->DdddOcr(show_ad: false);
$res = $ocr->classification($imgBytes);
echo $res . PHP_EOL;Input image:
demo01.pngOutput:
# php demo01.php
2bghzAnother example image:
demo02.pngOutput:
# php demo01.php
8A62N1The library includes two OCR models; you can switch models by passing the beta:true parameter during initialization.
// Switch to the second OCR model
$ocr = $ddd->DdddOcr(beta:true);When using the second model, the output may include additional promotional text and data source references.
Target Detection Capability
This feature quickly detects possible object bounding boxes in an image. It returns bbox coordinates (x1, y1, x2, y2) without performing OCR on the detected regions.
Example file:
demo03.php <?php
/**
* @desc Target detection capability
* @help https://github.com/sml2h3/ddddocr
* @author Tinywan (ShaoBo Wan)
*/
declare(strict_types=1);
$operator = PyCore::import("operator");
$builtins = PyCore::import("builtins");
$ddddocr = PyCore::import('ddddocr');
$cv2 = PyCore::import('cv2');
// Initialize with detection enabled, OCR disabled
$det = $ddddocr->DdddOcr(det:true, show_ad:false);
$f = PyCore::open('./demo04.png', "rb")->__enter__();
try { $image = $f->read(); } finally { $f->__exit__(); }
$bboxes = $det->detection($image);
PyCore::print($bboxes);
$im = $cv2->imread('./demo04.png');
foreach ($bboxes as $bbox) {
[$x1, $y1, $x2, $y2] = $bbox;
$im = $cv2->rectangle($im, [$x1, $y1], [$x2, $y2], color:[0,0,255], thickness:2);
}
$cv2->imwrite("./result.jpg", $im);Input image:
demo04.pngOutput (bounding boxes):
# php demo03.php
[[55, 344, 90, 378], [26, 345, 58, 379], [0, 345, 29, 377]]Result image with drawn boxes:
result.jpgIf OCR functionality is not needed, initialize with ocr:false. To enable detection only, set det:true.
Slider Detection
The slider detection feature is implemented with OpenCV algorithms rather than AI. It can be disabled by setting ocr:false or det:false during initialization.
More information: https://github.com/sml2h3/ddddocr?tab=readme-ov-file
Related Resources
phpy – smooth integration of Python ecosystem into PHP
EasyOCR – simple and powerful OCR tool for PHP
ModelScope – open‑source large model platform
ModelScope training guide
Vision Transformer (ViT) implementation with phpy
phpy breakthrough for AI programming in PHP
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
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.
