From ef52dfff053b176f82142cf2cdf790ad3e9058e2 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sun, 15 Sep 2024 04:36:03 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20=20from=5Feasyocr=20detecti?= =?UTF-8?q?on=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- supervision/config.py | 1 + supervision/detection/core.py | 38 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/supervision/config.py b/supervision/config.py index b18d2e20..357ff2d4 100644 --- a/supervision/config.py +++ b/supervision/config.py @@ -1,2 +1,3 @@ CLASS_NAME_DATA_FIELD = "class_name" ORIENTED_BOX_COORDINATES = "xyxyxyxy" +TEXT_DATA_FIELD = "text_data" diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 06995f90..54cae9a7 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -6,7 +6,11 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Union import numpy as np -from supervision.config import CLASS_NAME_DATA_FIELD, ORIENTED_BOX_COORDINATES +from supervision.config import ( + CLASS_NAME_DATA_FIELD, + ORIENTED_BOX_COORDINATES, + TEXT_DATA_FIELD, +) from supervision.detection.lmm import ( LMM, from_florence_2, @@ -843,6 +847,38 @@ class Detections: raise ValueError(f"Unsupported LMM: {lmm}") + @classmethod + def from_easyocr(cls, easyocr_results: list) -> Detections: + """ + Create a Detections object from the + [EasyOCR](https://github.com/JaidedAI/EasyOCR) inference result. + + Args: + easyocr_results (List): The output Results instance from EasyOCR + + Returns: + Detections: A new Detections object. + + Example: + ```python + import supervision as sv + import easyocr + + reader = easyocr.Reader(['en']) + results = reader.readtext() + detections = sv.Detections.from_easyocr(results) + ``` + """ + bbox = np.array([result[0] for result in easyocr_results]) + xyxy = np.hstack((np.min(bbox, axis=1), np.max(bbox, axis=1))) + + return cls( + xyxy=xyxy, + confidence=np.array([result[2] for result in easyocr_results]), + class_id=np.arange(len(xyxy)), + data={TEXT_DATA_FIELD: np.array([result[1] for result in easyocr_results])}, + ) + @classmethod def empty(cls) -> Detections: """