feat: ✨ from_easyocr detection added

Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
This commit is contained in:
Onuralp SEZER
2024-09-15 04:36:03 +03:00
parent ade8976eaf
commit ef52dfff05
2 changed files with 38 additions and 1 deletions
+1
View File
@@ -1,2 +1,3 @@
CLASS_NAME_DATA_FIELD = "class_name"
ORIENTED_BOX_COORDINATES = "xyxyxyxy"
TEXT_DATA_FIELD = "text_data"
+37 -1
View File
@@ -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(<SOURCE_IMAGE_PATH>)
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:
"""