Fix: workaround for drawing non-latin characters (#7686)

* Add transliteration support to draw_box_with_label function

* isort
This commit is contained in:
Sergey Krashevich
2023-11-21 05:05:51 +03:00
committed by GitHub
parent 3dd0192fe6
commit 500d369c50
3 changed files with 33 additions and 2 deletions

View File

@@ -9,10 +9,32 @@ from typing import AnyStr, Optional
import cv2
import numpy as np
from unidecode import unidecode
logger = logging.getLogger(__name__)
def transliterate_to_latin(text: str) -> str:
"""
Transliterate a given text to Latin.
This function uses the unidecode library to transliterate the input text to Latin.
It is useful for converting texts with diacritics or non-Latin characters to a
Latin equivalent.
Args:
text (str): The text to be transliterated.
Returns:
str: The transliterated text.
Example:
>>> transliterate_to_latin('frégate')
'fregate'
"""
return unidecode(text)
def draw_timestamp(
frame,
timestamp,
@@ -116,7 +138,10 @@ def draw_box_with_label(
):
if color is None:
color = (0, 0, 255)
display_text = "{}: {}".format(label, info)
try:
display_text = transliterate_to_latin("{}: {}".format(label, info))
except Exception:
display_text = "{}: {}".format(label, info)
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, thickness)
font_scale = 0.5
font = cv2.FONT_HERSHEY_SIMPLEX