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

ocr-document-classification

v1.3.7

Published

Document classification using tesseract.js and string-similarity-js.

Downloads

31

Readme

OCR Document Classification

Overview

The OCR Document Classification package provides a utility to classify documents based on their content. It uses OCR (Optical Character Recognition) to extract text from images and then determines the document type by matching extracted words with predefined target words using string similarity.

Installation

To install this package, use npm:

npm install ocr-document-classification

Usage

The main function exported by this package is classifyDocument. Below is a detailed guide on how to use it.

Importing the Package

import { classifyDocument } from "ocr-document-classification";
import type { documentDictionary } from "ocr-document-classification";

Function: classifyDocument

Parameters

  • file: The image file (File object) of the document to be classified.
  • options (optional): An object containing the following optional properties:
    • onProgress: A callback function to receive progress updates. It accepts a number between 0 and 100.
    • customDocumentDictionary: An object containing custom document types and their associated target words.
    • maxNumPages: A number specifying the maximum number of pages to process. Defaults to Infinity.

Returns

A Promise that resolves with an object containing:

  • classification: The determined document type.
  • text: The extracted text from the document.

Classes

There exists a couple of default classes that can be useful to classify the most common documents. As you can see there exists multiple arrays for each key. This means that every word of only ONE of the arrays needs to be found in the document after OCR. You can also add your own class my creating a customDocumentDictionary.

const defaultDocumentDictionary: documentDictionary = {
  MILITÆRBEVIS: [
    ["førstegangstjeneste", "bevis", "avtjent"],
    ["attest", "førstegangstjeneste"],
    ["fullført", "førstegangstjeneste"],
  ],
  POLITIATTEST: [["politiattest", "politidistrikt"], ["police certificate"]],
  KOMPETANSEBEVIS: [["omfatter", "opplæring", "utdanningsprogram"]],
  LEGEERKLÆRING: [["legeerklæring", "fødselsnummer"]],
  BOSTEDSATTEST: [
    ["registrerte", "opplysninger", "folkeregisteret"],
    ["bostedsattest", "bostedsadresse", "registrert"],
    ["registrert", "adressehistorikk", "folkeregisteret"],
  ],
};

Example

Here is an example of how to use the package can be used with a custom document dictionary in React:

import React, { useState, useEffect } from "react";
import { classifyDocument } from "ocr-document-classification";

function UploadClassification() {
  const [documentFile, setDocumentFile] = useState<File | null>(null);
  const [classification, setClassification] = useState("");
  const [outputText, setOutputText] = useState("");
  const [progress, setProgress] = useState(0);

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files && event.target.files[0];
    setDocumentFile(file);
  };

  const customDocumentDictionary = {
    Jobbsøknad: [["søknad", "stilling", "ledig"]],
  };

  useEffect(() => {
    console.log("Progress: ", progress);
  }, [progress]);

  useEffect(() => {
    if (documentFile) {
      classifyDocument(documentFile, {
        onProgress: setProgress,
        customDocumentDictionary: customDocumentDictionary,
      })
        .then(({ classification, text }) => {
          setClassification(classification);
          setOutputText(text);
        })
        .catch((err) => {
          console.error(err);
          setOutputText("Error during OCR processing");
        });
    }
    resetOCR();
  }, [documentFile]);

  function resetOCR() {
    setClassification("");
    setOutputText("");
    setProgress(0);
  }

  return (
    <>
      <input
        accept="image/jpeg, image/png"
        type="file"
        onChange={handleFileChange}
      />
      <div>
        <h3>Resultat av OCR</h3>
        <p>{classification ? outputText : "Laster inn ..."}</p>
        <h1>{classification}</h1>
      </div>
    </>
  );
}

export default UploadClassification;

Dependencies

This package relies on the following dependencies:

  • string-similarity-js: For calculating the similarity between strings.
  • tesseract.js: For performing OCR on the document image.
  • pdfjs-dist: For handling PDFs

LICENSE

This package is currently UNLICENSED.