From 698d76fe4123a796aefc9b6c67e8caa6c5247c1d Mon Sep 17 00:00:00 2001 From: Dax Frost Date: Thu, 13 Mar 2025 13:18:32 -0500 Subject: [PATCH] Update easyocr.ts to support JSON payloads bigger than buffer size on read_text commands --- src/easyocr.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/easyocr.ts b/src/easyocr.ts index ed6ea42..8c8e8ea 100644 --- a/src/easyocr.ts +++ b/src/easyocr.ts @@ -50,6 +50,9 @@ export class EasyOCR { const commandString = `${command} ${args.join(' ')}\n`; this.pythonProcess!.stdin!.write(commandString); + // adding this to collect partial JSON payloads, to support larger more complex image files than the data buffer response supports (JSON will be incomplete) + let fullResult = ''; + const onData = (data: Buffer) => { const result = data.toString().trim(); try { @@ -57,7 +60,25 @@ export class EasyOCR { this.pythonProcess!.stdout!.removeListener('data', onData); resolve(parsedResult); } catch (error) { - reject(new Error(`Failed to parse Python output: ${error}`)); + // handle commands for read_text of images differently if parsing fails + if (command === 'read_text') { + // add to the payload + fullResult += result; + try { + // try parse again in case it's complete + const parsedResult = JSON.parse(fullResult); + // if it is complete, we can finish the process + this.pythonProcess.stdout.removeListener('data', onData); + resolve(parsedResult); + } + catch (error) { + // it's incomplete, let's try again + console.log("waiting for full result... ", fullResult); + } + } else { + // all other errors go here + reject(new Error(`Failed to parse Python output: ${error}`)); + } } }; @@ -95,4 +116,4 @@ export class EasyOCR { } } -export default EasyOCR; \ No newline at end of file +export default EasyOCR;