Luke
commited on
Commit
·
a60d0d2
1
Parent(s):
d7fedda
no message
Browse files- Plan/pytesseractJsOCR.py +17 -4
Plan/pytesseractJsOCR.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import subprocess
|
2 |
|
3 |
from IdentifyModel.cardModel import parse_id_card
|
@@ -7,12 +8,24 @@ from Plan.AiLLM import extract_entities
|
|
7 |
def pytesseractJs_recognition(validation_type, image, temp_path, file_name, language):
|
8 |
try:
|
9 |
# 使用 subprocess 執行 JavaScript 代碼,傳遞語言參數
|
10 |
-
subprocess.run(
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
with open(temp_path + file_name, 'r') as file:
|
14 |
out_ocr_text = file.read()
|
15 |
entities = extract_entities(out_ocr_text)
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
except Exception as e:
|
|
|
18 |
return str(e)
|
|
|
1 |
+
import json
|
2 |
import subprocess
|
3 |
|
4 |
from IdentifyModel.cardModel import parse_id_card
|
|
|
8 |
def pytesseractJs_recognition(validation_type, image, temp_path, file_name, language):
|
9 |
try:
|
10 |
# 使用 subprocess 執行 JavaScript 代碼,傳遞語言參數
|
11 |
+
result = subprocess.run(
|
12 |
+
['node', 'pytesseractJsOCR.js', image, language, temp_path + file_name],
|
13 |
+
capture_output=True,
|
14 |
+
text=True
|
15 |
+
)
|
16 |
+
# print(result.stdout) # 打印子進程的標準輸出
|
17 |
+
print(' ###### result.stderr:' + result.stderr) # 打印子進程的錯誤輸出
|
18 |
+
|
19 |
with open(temp_path + file_name, 'r') as file:
|
20 |
out_ocr_text = file.read()
|
21 |
entities = extract_entities(out_ocr_text)
|
22 |
+
|
23 |
+
parsed_result = parse_id_card(out_ocr_text, validation_type, entities)
|
24 |
+
print(' ###### parsed_result:' + str(parsed_result))
|
25 |
+
# 確保返回的數據是有效的 JSON
|
26 |
+
json_result = json.dumps(parsed_result)
|
27 |
+
print(' ###### json_result:' + str(json_result))
|
28 |
+
return json_result
|
29 |
except Exception as e:
|
30 |
+
print(' ###### [ERROR] pytesseractJs_recognition Exception:' + str(e))
|
31 |
return str(e)
|