Skip to content

Commit

Permalink
fix: Error of iFlytek OCR when image has no words
Browse files Browse the repository at this point in the history
  • Loading branch information
ccslykx committed Jun 13, 2023
1 parent 036154d commit 22b2a87
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/interfaces_ocr/iflytek.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,15 @@ export async function ocr(base64, lang, setText, id) {
body: { type: 'Text', payload: JSON.stringify(request_body) }
});

if (!res.ok) {
if (!res.ok && (id === ocrID || id === 'translate')) {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}

let data = res['data'];
if (!data) {
if (!data && (id === ocrID || id === 'translate')) {
throw `Result data not found\nResult:\n${JSON.stringify(res)}`;
}
let res_payload = data['payload'];
if (!res_payload) {
if (!res_payload && (id === ocrID || id === 'translate')) {
throw `Result payload not found\nResult:\n${JSON.stringify(res)}`;
}

Expand All @@ -99,14 +98,17 @@ export async function ocr(base64, lang, setText, id) {
let text_json = JSON.parse(text_string);
let return_content = ''; // 最终结果


let pages = text_json['pages'];
for (let page of pages) {
let lines = page['lines'];
if (!lines) { continue; }
for (let line of lines) {
let words = line['words'];
if (!words) { continue; }
for (let word of words) {
return_content += word['content'] + ' ';
let content = word['content'];
if (!content) { continue; }
else { return_content += content + ' '; }
}
return_content += '\n'
}
Expand Down
7 changes: 4 additions & 3 deletions src/interfaces_ocr/iflytek_intsig.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,23 @@ export async function ocr(base64, lang, setText, id) {
});

// 处理结果
if (!res.ok) {
if (!res.ok && (id === ocrID || id === 'translate')) {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
let data = res['data'];
if (!data) {
if (!data && (id === ocrID || id === 'translate')) {
throw `Result data not found\nResult:\n${JSON.stringify(res)}`;
}
let res_payload = data['payload'];
if (!res_payload) {
if (!res_payload && (id === ocrID || id === 'translate')) {
throw `Result payload not found\nResult:\n${JSON.stringify(res)}`;
}

let text = CryptoJS.enc.Base64.parse(res_payload['recognizeDocumentRes']['text']); // Base64解码
let text_string = CryptoJS.enc.Utf8.stringify(text);
let text_json = JSON.parse(text_string);
let return_content = text_json['whole_text']; // 最终结果
if (!return_content) { return_content = ''; }

if (id === ocrID || id === 'translate') {
setText(return_content);
Expand Down

0 comments on commit 22b2a87

Please sign in to comment.