npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ddddocr-node

v0.4.1

Published

The Node version of DdddOcr

Downloads

666

Readme

DdddOcr Node

npm

This project is a port of the Python project DdddOcr.

The goal is to make it easy to use this trained model for text detection in JavaScript.

Docs

Installation

npm install ddddocr-node

Features

  • Basic OCR recognition capability
  • OCR probability output
  • Object detection capability

Basic OCR recognition capability

Primarily used for recognizing single-line text, where the text occupies the main portion of the image, such as common alphanumeric captchas. This project can recognize Chinese characters, English (with random case sensitivity or case constrained by specified ranges), numbers, and certain special characters.

const { DdddOcr } = require('ddddocr-node');

const ddddOcr = new DdddOcr();

const result = await ddddOcr.classification('example.jpg');
console.log(result);

This library includes two built-in OCR models, which do not switch automatically by default. You need to use setOcrMode() with parameters to switch between them.

const { DdddOcr, MODEL_TYPE } = require('ddddocr-node');

// Method 1: Enable Beta OCR mode after creating an instance
const ddddOcr = new DdddOcr();
ddddOcr.setOcrMode(MODEL_TYPE.OCR_BETA);

// Method 2: Directly enable Beta OCR mode during instance creation
const ddddOcr = new DdddOcr().setOcrMode(MODEL_TYPE.OCR_BETA);

const result = await ddddOcr.classification('example.jpg');
console.log(result);

OCR probability output

To provide more flexible control and range restriction for OCR results, the project supports setting range limitations on OCR results.

The setRanges() method restricts the returned characters.

This method accepts one parameter. If the input is of type int, it refers to a predefined character set restriction. If the input is of type string, it represents a custom character set.

For int input, please refer to the table below.

| Parameter Value | Meaning | |-----------------------|------------------------------------------------------------------------| | 0 | Pure integers 0-9 | | 1 | Pure lowercase letters a-z | | 2 | Pure uppercase letters A-Z | | 3 | Lowercase letters a-z + uppercase letters A-Z | | 4 | Lowercase letters a-z + integers 0-9 | | 5 | Uppercase letters A-Z + integers 0-9 | | 6 | Lowercase letters a-z + uppercase letters A-Z + integers 0-9 | | 7 | Default character set - lowercase a-z, uppercase A-Z, and integers 0-9 |

For string input, provide a string where each character is treated as a candidate character, e.g., "0123456789+-x/=".

const { DdddOcr, CHARSET_RANGE } = require('ddddocr-node');

const ddddOcr = new DdddOcr();
ddddOcr.setRanges(CHARSET_RANGE.NUM_CASE);

const result = await ddddOcr.classification('example.jpg');
console.log(result);

Object detection capability

The main purpose is to quickly detect the possible location of the target object in the image. Since the detected target may not necessarily be text, this function only provides the bounding box (bbox) location of the target. In object detection, we typically use a bbox (bounding box) to describe the target location. A bbox is a rectangular frame, which can be determined by the x and y coordinates of the top-left corner and the x and y coordinates of the bottom-right corner.

const { DdddOcr } = require('ddddocr-node');

const ddddOcr = new DdddOcr();

const result = await ddddOcr.detection('example.jpg');
console.log(result);

If you want to add the detected bounding box to the original image, here is an example.

const { Jimp, cssColorToHex } = require('jimp');

const { DdddOcr } = require('ddddocr-node');
const { drawRectangle } = require('ddddocr-node/core/utils/image-utils');

const ddddOcr = new DdddOcr();

const result = await ddddOcr.detection('example.jpg');

const image = await Jimp.read('example.jpg');
const color = cssColorToHex('#ff0000');
for (let i = 0; i < result.length; i++) {
    const [x1, y1, x2, y2] = result[i];

    const points = [
        { x: x1, y: y1 },
        { x: x2, y: y1 },
        { x: x2, y: y2 },
        { x: x1, y: y2 }
    ];
    drawRectangle(image, points, color);
}

image.write('output.jpg');

Futures

  • Slider detection
  • Import custom OCR training model