Skip to content

Commit

Permalink
add log to ocr reader
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzaizai2k committed Oct 3, 2024
1 parent 018c11d commit c1b7f86
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ For LDAP authentication using FastAPI, explore the following resources:
2024-10-03T07:43:02.258236787Z Illegal instruction (core dumped)
```
https://github.com/PaddlePaddle/PaddleOCR/issues/11597
https://github.com/PaddlePaddle/PaddleOCR/issues/11597
https://github.com/PaddlePaddle/PaddleOCR/blob/main/doc/doc_en/multi_languages_en.md
2 changes: 1 addition & 1 deletion src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
change_stream = None
change_stream_thread = None

ocr_reader = OcrReader(config_path=config_path, translator=GoogleTranslator())
ocr_reader = OcrReader(config_path=config_path, translator=GoogleTranslator(), logger=logger)
invoice_extractor = OpenAIExtractor(config_path=config_path)


Expand Down
17 changes: 14 additions & 3 deletions src/ocr_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def __getitem__(self, item):
class OcrReader:
def __init__(self,
translator=None,
config_path:str = "config/config.yaml"
config_path:str = "config/config.yaml",
logger=None
):

self.config_path = config_path
Expand All @@ -72,6 +73,7 @@ def __init__(self,
self.target_language = self.config['target_language']
self.resize_size = self.config['resize_size']

self.logger = logger

# Load language dictionary from JSON file
with open(self.language_dict_path, 'r', encoding='utf-8') as f:
Expand Down Expand Up @@ -147,13 +149,16 @@ def get_text(self, input_data) -> dict:

src_language = self._get_lang(image)

if self.logger:
self.logger.debug(f"src_language: {src_language}")

# Initialize the PaddleOCR with the detected language
ocr = None

if (src_language in ["zh-CN","ch", "chinese_cht"]) and (self.device == 'cpu'):
if (src_language in ["zh-CN","ch", "chinese_cht", "japan"]) and (self.device == 'cpu'):
print("src_language", src_language)
print("self.device", self.device)
ocr = PaddleOCR(lang="japan", show_log=False, use_angle_cls=True,
ocr = PaddleOCR(lang="en", show_log=False, use_angle_cls=True,
cls=True,) # ocr_version='PP-OCR2', enable_mkldnn=True) #https://github.com/PaddlePaddle/PaddleOCR/issues/11597
else:
ocr = PaddleOCR(lang=src_language, show_log=False, use_angle_cls=True, cls=True, )
Expand Down Expand Up @@ -184,6 +189,10 @@ def get_text(self, input_data) -> dict:
"language": src_language,
}
data['angle'] = doc_angle

if self.logger:
self.logger.debug(f"ocr_data: {data}")

except Exception as e:
print("error", e)
data = {
Expand All @@ -193,6 +202,8 @@ def get_text(self, input_data) -> dict:
"language": "",
"angle": 0,
}
if self.logger:
self.logger.debug(f"error: {e}")
return data

def get_rotated_image(self, input_data):
Expand Down

0 comments on commit c1b7f86

Please sign in to comment.