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: """