From 15f63f70c1809388813e62a178fbe6246589204c Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Thu, 24 Aug 2023 19:28:03 +0300 Subject: [PATCH] Add transliteration support to draw_box_with_label function --- docker/main/requirements-wheels.txt | 1 + frigate/test/test_video.py | 7 ++++++- frigate/util/image.py | 27 ++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docker/main/requirements-wheels.txt b/docker/main/requirements-wheels.txt index d25f53653..322d53a46 100644 --- a/docker/main/requirements-wheels.txt +++ b/docker/main/requirements-wheels.txt @@ -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' diff --git a/frigate/test/test_video.py b/frigate/test/test_video.py index 99736f658..c0bdf9dfe 100644 --- a/frigate/test/test_video.py +++ b/frigate/test/test_video.py @@ -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 = [ diff --git a/frigate/util/image.py b/frigate/util/image.py index d5eaaf885..c31d2b441 100644 --- a/frigate/util/image.py +++ b/frigate/util/image.py @@ -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