Skip to content

Commit

Permalink
Add method
Browse files Browse the repository at this point in the history
  • Loading branch information
stellasphere committed Dec 1, 2024
1 parent 6889e33 commit 625a646
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions supervision/detection/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,102 @@ def with_nmm(
result.append(merged_detections)

return Detections.merge(result)

@classmethod
def from_gcp_vision(cls, gcp_results, size) -> Detections:
"""
Creates a Detections instance from the
[Google Cloud Cloud Vision API's](https://cloud.google.com/vision/docs)
inference result.
Args:
gcp_results (List[dict]): The output results from GCP from
the `localized_object_annotations`.
size (Tuple[int, int]): The height, then width of the input image.
Returns:
Detections: A new Detections object.
Example:
```python
>>> import supervision as sv
>>> from google.cloud import vision
>>> from PIL import Image
>>> image_path = "/content/people.jpeg"
>>> img = Image.open(image_path)
>>> client = vision.ImageAnnotatorClient()
>>> with open(image_path, "rb") as image_file:
>>> content = image_file.read()
>>> image = vision.Image(content=content)
>>> result = client.object_localization(image=image)
>>> objects = result.localized_object_annotations
>>> detections = sv.Detections.from_gcp_vision(
>>> gcp_results=objects,
>>> size=(img.height, img.width)
>>> )
```
"""
xyxys, confidences, class_ids = [], [], []

class_id_reference = {}

for object_ in gcp_results:
# bounding boxes must be in the format [x0, y0, x1, y1]
# not the polygons returned by the GCP Vision API

object_bboxes = []

for vertex in object_.bounding_poly.normalized_vertices:
object_bboxes.append([vertex.x, vertex.y])

object_bboxes = np.array(object_bboxes)

x0 = object_bboxes[:, 0].min()
y0 = object_bboxes[:, 1].min()
x1 = object_bboxes[:, 0].max()
y1 = object_bboxes[:, 1].max()

height, width = size

# normalize as image size, not 0-1
x0 *= width
y0 *= height
x1 *= width
y1 *= height

class_name = object_.name

xyxys.append([x0, y0, x1, y1])

confidences.append(object_.score)

if class_id_reference.get(class_name):
class_ids.append(class_id_reference[class_name])
else:
new_id = len(class_id_reference) + 1

class_id_reference[class_name] = new_id

class_ids.append(new_id)

id_to_class_name = {id_: name for name, id_ in class_id_reference.items()}
class_names = [id_to_class_name[class_id] for class_id in class_ids]

if len(xyxys) == 0:
return cls.empty()

return cls(
xyxy=np.array(xyxys),
class_id=np.array(class_ids),
confidence=np.array(confidences),
data={CLASS_NAME_DATA_FIELD: np.array(class_names)},
)


def merge_inner_detection_object_pair(
Expand Down

0 comments on commit 625a646

Please sign in to comment.