UNPKG

ppu-paddle-ocr

Version:

Lightweight, probably the fastest PaddleOCR SDK in TypeScript. Runs anywhere JavaScript runs: Node.js, Bun, Deno, mobile react-native, web browsers, and browser extensions. Docker & CLI supported. The official SDK is browser-only. Accurate text detection

2 lines 1.74 kB
export let BLANK_INDEX=0;export let UNK_TOKEN="<unk>";export let MIN_CROP_WIDTH=8;export function ctcGreedyDecode(logits,sequenceLength,numClasses,charDict){let dictLen=charDict.length;let lastDictIndex=dictLen-1;let decodedText="";let lastCharIndex=-1;let confidenceSum=0;let confidenceCount=0;for(let t=0;t<sequenceLength;t++){let base=t*numClasses;let maxProb=logits[base]??-1/0;let maxIndex=0;for(let c=1;c<numClasses;c++){let prob=logits[base+c]??-1/0;if(prob>maxProb){maxProb=prob;maxIndex=c}}if(maxIndex===BLANK_INDEX||maxIndex===lastCharIndex){lastCharIndex=maxIndex;continue}if(maxIndex>=0&&maxIndex<dictLen){let char=charDict[maxIndex]??"";if(maxIndex===lastDictIndex){if(char!==UNK_TOKEN){decodedText+=" ";confidenceSum+=maxProb;confidenceCount++}}else{decodedText+=char;confidenceSum+=maxProb;confidenceCount++}}lastCharIndex=maxIndex}let confidence=confidenceCount>0?confidenceSum/confidenceCount:0;return{text:decodedText,confidence}}export function decodeResults(outputTensor,charactersDictionary,numClassesFromShape,verbose=false){let outputData=outputTensor.data;let outputShape=outputTensor.dims;let sequenceLength=outputShape[1];let numClasses=outputShape[2]??numClassesFromShape;if(!charactersDictionary){return{text:"",confidence:0}}let dict=charactersDictionary;if(charactersDictionary.length===numClasses-1){dict=["",...charactersDictionary]}else if(numClasses!==charactersDictionary.length&&verbose){console.warn(`Warning: Model output classes (${numClasses}) does not match dictionary length (${charactersDictionary.length}). Consider using our model & dictionary catalogue at https://github.com/PT-Perkasa-Pilar-Utama/ppu-paddle-ocr-models.`)}return ctcGreedyDecode(outputData,sequenceLength,numClasses,dict)}