Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/easyocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,35 @@ 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 {
const parsedResult = JSON.parse(result);
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}`));
}
}
};

Expand Down Expand Up @@ -95,4 +116,4 @@ export class EasyOCR {
}
}

export default EasyOCR;
export default EasyOCR;