Add transliteration support to draw_box_with_label function

This commit is contained in:
Sergey Krashevich 2023-08-24 19:28:03 +03:00
parent f682203f9d
commit 15f63f70c1
No known key found for this signature in database
GPG Key ID: 625171324E7D3856
3 changed files with 33 additions and 2 deletions

View File

@ -23,6 +23,7 @@ scipy == 1.10.*
norfair == 2.2.*
setproctitle == 1.3.*
ws4py == 0.5.*
unidecode == 1.3.*
# Openvino Library - Custom built with MYRIAD support
openvino @ https://github.com/NateMeyer/openvino-wheels/releases/download/multi-arch_2022.3.1/openvino-2022.3.1-1-cp39-cp39-manylinux_2_31_x86_64.whl; platform_machine == 'x86_64'
openvino @ https://github.com/NateMeyer/openvino-wheels/releases/download/multi-arch_2022.3.1/openvino-2022.3.1-1-cp39-cp39-linux_aarch64.whl; platform_machine == 'aarch64'

View File

@ -5,7 +5,7 @@ import numpy as np
from norfair.drawing.color import Palette
from norfair.drawing.drawer import Drawer
from frigate.util.image import intersection
from frigate.util.image import intersection, transliterate_to_latin
from frigate.video import (
get_cluster_boundary,
get_cluster_candidates,
@ -80,6 +80,11 @@ class TestRegion(unittest.TestCase):
assert len(cluster_candidates) == 2
def test_transliterate_to_latin(self):
self.assertEqual(transliterate_to_latin("frégate"), "fregate")
self.assertEqual(transliterate_to_latin("utilité"), "utilite")
self.assertEqual(transliterate_to_latin("imágé"), "image")
def test_cluster_boundary(self):
boxes = [(100, 100, 200, 200), (215, 215, 325, 325)]
boundary_boxes = [

View File

@ -6,6 +6,7 @@ from abc import ABC, abstractmethod
from multiprocessing import shared_memory
from string import printable
from typing import AnyStr, Optional
from unidecode import unidecode
import cv2
import numpy as np
@ -13,6 +14,27 @@ import numpy as np
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