Skip to content

Commit

Permalink
Merge pull request #1704 from roboflow/fix/from_inference_to_support_…
Browse files Browse the repository at this point in the history
…deployed_roboflow_results

from_inference now supports roboflow results without json()
  • Loading branch information
LinasKo authored Dec 4, 2024
2 parents ae8e6b2 + fac1581 commit c416474
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions supervision/detection/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from contextlib import suppress
from dataclasses import dataclass, field
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

Expand Down Expand Up @@ -606,8 +605,10 @@ def from_inference(cls, roboflow_result: Union[dict, Any]) -> Detections:
detections = sv.Detections.from_inference(result)
```
"""
with suppress(AttributeError):
if hasattr(roboflow_result, "dict"):
roboflow_result = roboflow_result.dict(exclude_none=True, by_alias=True)
elif hasattr(roboflow_result, "json"):
roboflow_result = roboflow_result.json()
xyxy, confidence, class_id, masks, trackers, data = process_roboflow_result(
roboflow_result=roboflow_result
)
Expand Down
6 changes: 3 additions & 3 deletions supervision/keypoint/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from contextlib import suppress
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union

Expand Down Expand Up @@ -204,9 +203,10 @@ def from_inference(cls, inference_result: Union[dict, Any]) -> KeyPoints:
"You can retrieve it like so: inference_result = model.infer(image)[0]"
)

with suppress(AttributeError):
if hasattr(inference_result, "dict"):
inference_result = inference_result.dict(exclude_none=True, by_alias=True)

elif hasattr(inference_result, "json"):
inference_result = inference_result.json()
if not inference_result.get("predictions"):
return cls.empty()

Expand Down

0 comments on commit c416474

Please sign in to comment.