8000 GitHub - areb0s/ocr: High accurate text detection (OCR) Javascript/Typescript library that runs on Node.js, Browser, React Native and C++. Based on PaddleOCR and ONNX runtime · GitHub
[go: up one dir, main page]

Skip to content
areb0sPublic
forked from gutenye/ocr

About

High accurate text detection (OCR) Javascript/Typescript library that runs on Node.js, Browser, React Native and C++. Based on PaddleOCR and ONNX runtime

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

@areb0s/ocr

Fork of @gutenye/ocr with ImageBitmap support

OCR Javascript library for Browser with extended image input support

Based on PaddleOCR and ONNX Runtime, supports PP-OCRv4 model

What's New in This Fork

  • ImageBitmap support: Pass ImageBitmap directly to ocr.detect()
  • HTMLImageElement support: Pass <img> elements directly
  • HTMLCanvasElement support: Pass <canvas> elements directly
  • HTMLVideoElement support: Capture current frame from <video> elements

Getting Started

Browser

bun add @areb0s/ocr-browser
import Ocr from '@areb0s/ocr-browser'

const ocr = await Ocr.create({
  models: {
    detectionPath: '/assets/ch_PP-OCRv4_det_infer.onnx',
    recognitionPath: '/assets/ch_PP-OCRv4_rec_infer.onnx',
    dictionaryPath: '/assets/ppocr_keys_v1.txt'
  }
})

// URL string (original)
const result = await ocr.detect('/image.jpg')

// ImageBitmap (NEW)
const bitmap = await createImageBitmap(file)
const result = await ocr.detect(bitmap)

// HTMLImageElement (NEW)
const img = document.querySelector('img')
const result = await ocr.detect(img)

// HTMLCanvasElement (NEW)
const canvas = document.querySelector('canvas')
const result = await ocr.detect(canvas)

// HTMLVideoElement - captures current frame (NEW)
const video = document.querySelector('video')
const result = await ocr.detect(video)

// Raw pixel data (original)
const result = await ocr.detect({
  data: imageData.data,  // Uint8ClampedArray
  width: 800,
  height: 600
})

API Reference

Ocr.create({
  models?: {
    detectionPath: string
    recognitionPath: string
    dictionaryPath: string
  },
  isDebug?: boolean
}): Promise<Ocr>

// Browser - Extended input types
ocr.detect(
  image: string                 // URL or data URL
        | ImageBitmap           // from createImageBitmap()
        | HTMLImageElement      // <img> element
        | HTMLCanvasElement     // <canvas> element
        | HTMLVideoElement      // <video> element (current frame)
        | {                     // Raw pixel data
            data: Uint8Array | Uint8ClampedArray,
            width: number,
            height: number
          }
): Promise<{
  texts: TextLine[],
  resizedImageWidth: number,
  resizedImageHeight: number
}>

TextLine {
  text: string
  mean: number
  box?: number[][]
}

ImageRaw Static Methods

For advanced use cases, you can use ImageRaw directly:

import { ImageRaw } from '@areb0s/ocr-browser'

// Universal factory method
const imageRaw = await ImageRaw.from(input)

// Specific converters
const imageRaw = await ImageRaw.fromImageBitmap(bitmap)
const imageRaw = await ImageRaw.fromHTMLImageElement(img)
const imageRaw = await ImageRaw.fromHTMLCanvasElement(canvas)
const imageRaw = await ImageRaw.fromHTMLVideoElement(video)

// Type guards
ImageRaw.isImageBitmap(input)
ImageRaw.isHTMLImageElement(input)
ImageRaw.isHTMLCanvasElement(input)
ImageRaw.isHTMLVideoElement(input)
ImageRaw.isImageRawData(input)

Memory Management

When using ImageBitmap, the library automatically calls bitmap.close() after conversion to free GPU/CPU memory. You don't need to manage this manually.

const bitmap = await createImageBitmap(file)
await ocr.detect(bitmap)  // bitmap is automatically closed after use
// bitmap is now invalid - don't reuse it

If you need to reuse the bitmap, create a new one for each OCR call.

Credits

This is a fork of @gutenye/ocr by Guten Ye.

Original project features:

  • High accuracy OCR based on PaddleOCR PP-OCRv4 model
  • ONNX Runtime for cross-platform inference
  • Support for Node.js, Browser, React Native, and C++

License

MIT - See LICENSE for details.

About

High accurate text detection (OCR) Javascript/Typescript library that runs on Node.js, Browser, React Native and C++. Based on PaddleOCR and ONNX runtime

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0