chore(ocr): proceed to OCR without rotation when OSD fails in TesseractOcrCliModel

This commit is contained in:
Clément Doumouro 2025-05-19 18:30:54 +02:00
parent d73c3eb071
commit 4c8f1caba3

View File

@ -119,13 +119,12 @@ class TesseractOcrCliModel(BaseOcrModel):
cmd += [ifilename, "stdout", "tsv"] cmd += [ifilename, "stdout", "tsv"]
_log.info("command: {}".format(" ".join(cmd))) _log.info("command: {}".format(" ".join(cmd)))
proc = Popen(cmd, stdout=PIPE, stderr=DEVNULL) output = subprocess.run(cmd, stdout=PIPE, stderr=DEVNULL, check=True)
output, _ = proc.communicate()
# _log.info(output) # _log.info(output)
# Decode the byte string to a regular string # Decode the byte string to a regular string
decoded_data = output.decode("utf-8") decoded_data = output.stdout.decode("utf-8")
# _log.info(decoded_data) # _log.info(decoded_data)
# Read the TSV file generated by Tesseract # Read the TSV file generated by Tesseract
@ -188,9 +187,8 @@ class TesseractOcrCliModel(BaseOcrModel):
cmd = [self.options.tesseract_cmd] cmd = [self.options.tesseract_cmd]
cmd.append("--list-langs") cmd.append("--list-langs")
_log.info("command: {}".format(" ".join(cmd))) _log.info("command: {}".format(" ".join(cmd)))
proc = Popen(cmd, stdout=PIPE, stderr=DEVNULL) output = subprocess.run(cmd, stdout=PIPE, stderr=DEVNULL, check=True)
output, _ = proc.communicate() decoded_data = output.stdout.decode("utf-8")
decoded_data = output.decode("utf-8")
df_list = pd.read_csv(io.StringIO(decoded_data), header=None) df_list = pd.read_csv(io.StringIO(decoded_data), header=None)
self._tesseract_languages = df_list[0].tolist()[1:] self._tesseract_languages = df_list[0].tolist()[1:]
@ -231,16 +229,18 @@ class TesseractOcrCliModel(BaseOcrModel):
) as image_file: ) as image_file:
fname = image_file.name fname = image_file.name
high_res_image.save(image_file) high_res_image.save(image_file)
doc_orientation = 0
try: try:
df_osd = self._perform_osd(fname) df_osd = self._perform_osd(fname)
doc_orientation = _parse_orientation(df_osd)
except subprocess.CalledProcessError as exc: except subprocess.CalledProcessError as exc:
# Here we just log the error and proceed to OCR in the
# hope OCR will succeed while OSD failed
_log.error( _log.error(
"OSD failed for: %s with error:\n %s", "OSD failed for: %s with error:\n %s",
image_file, image_file,
exc.stderr, exc.stderr,
) )
continue
doc_orientation = _parse_orientation(df_osd)
if doc_orientation != 0: if doc_orientation != 0:
high_res_image = high_res_image.rotate( high_res_image = high_res_image.rotate(
-doc_orientation, expand=True -doc_orientation, expand=True