File size: 1,239 Bytes
a60d0d2
0347dd6
 
 
 
 
 
 
 
 
a60d0d2
765cf0f
a60d0d2
 
 
 
 
 
0347dd6
 
 
a60d0d2
 
 
 
 
 
 
0347dd6
a60d0d2
0347dd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import json
import subprocess

from IdentifyModel.cardModel import parse_id_card
from Plan.AiLLM import extract_entities


def pytesseractJs_recognition(validation_type, image, temp_path, file_name, language):
    try:
        # 使用 subprocess 執行 JavaScript 代碼,傳遞語言參數
        result = subprocess.run(
            ['node', 'node_app/pytesseractJsOCR.js', image, language, temp_path + file_name],
            capture_output=True,
            text=True
        )
        # print(result.stdout)  # 打印子進程的標準輸出
        print(' ###### result.stderr:' + result.stderr)  # 打印子進程的錯誤輸出

        with open(temp_path + file_name, 'r') as file:
            out_ocr_text = file.read()
            entities = extract_entities(out_ocr_text)

        parsed_result = parse_id_card(out_ocr_text, validation_type, entities)
        print(' ###### parsed_result:' + str(parsed_result))
        # 確保返回的數據是有效的 JSON
        json_result = json.dumps(parsed_result)
        print(' ###### json_result:' + str(json_result))
        return json_result
    except Exception as e:
        print(' ###### [ERROR] pytesseractJs_recognition Exception:' + str(e))
        return str(e)