Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz-roboflow committed May 20, 2024
1 parent 0196c48 commit 125570b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 44 deletions.
44 changes: 27 additions & 17 deletions inference/core/interfaces/http/orjson_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ def serialise_sv_detections(detections: sv.Detections) -> List[Dict[str, Any]]:
polygon = sv.mask_to_polygons(mask=mask)
detection_dict[POLYGON_KEY] = []
for x, y in polygon[0]:
detection_dict[POLYGON_KEY].append({
X_KEY: float(x),
Y_KEY: float(y),
})
detection_dict[POLYGON_KEY].append(
{
X_KEY: float(x),
Y_KEY: float(y),
}
)
if tracker_id is not None:
detection_dict[TRACKER_ID_KEY] = int(tracker_id)
if "class_name" in data:
Expand All @@ -158,23 +160,31 @@ def serialise_sv_detections(detections: sv.Detections) -> List[Dict[str, Any]]:
detection_dict[DETECTION_ID_KEY] = str(data[DETECTION_ID_KEY])
if PARENT_ID_KEY in data:
detection_dict[PARENT_ID_KEY] = str(data[PARENT_ID_KEY])
if KEYPOINTS_CLASS_ID_KEY in data \
and KEYPOINTS_CLASS_NAME_KEY in data \
and KEYPOINTS_CONFIDENCE_KEY in data \
and KEYPOINTS_XY_KEY in data:
if (
KEYPOINTS_CLASS_ID_KEY in data
and KEYPOINTS_CLASS_NAME_KEY in data
and KEYPOINTS_CONFIDENCE_KEY in data
and KEYPOINTS_XY_KEY in data
):
kp_class_id = data[KEYPOINTS_CLASS_ID_KEY]
kp_class_name = data[KEYPOINTS_CLASS_NAME_KEY]
kp_confidence = data[KEYPOINTS_CONFIDENCE_KEY]
kp_xy = data[KEYPOINTS_XY_KEY]
detection_dict[KEYPOINTS_KEY] = []
for keypoint_class_id, keypoint_class_name, keypoint_confidence, (x, y) \
in zip(kp_class_id, kp_class_name, kp_confidence, kp_xy):
detection_dict[KEYPOINTS_KEY].append({
KEYPOINTS_CLASS_ID_KEY: int(keypoint_class_id),
KEYPOINTS_CLASS_NAME_KEY: str(keypoint_class_name),
KEYPOINTS_CONFIDENCE_KEY: float(keypoint_confidence),
X_KEY: float(x),
Y_KEY: float(y),
})
for (
keypoint_class_id,
keypoint_class_name,
keypoint_confidence,
(x, y),
) in zip(kp_class_id, kp_class_name, kp_confidence, kp_xy):
detection_dict[KEYPOINTS_KEY].append(
{
KEYPOINTS_CLASS_ID_KEY: int(keypoint_class_id),
KEYPOINTS_CLASS_NAME_KEY: str(keypoint_class_name),
KEYPOINTS_CONFIDENCE_KEY: float(keypoint_confidence),
X_KEY: float(x),
Y_KEY: float(y),
}
)
serialized_detections.append(detection_dict)
return serialized_detections
2 changes: 1 addition & 1 deletion inference/core/workflows/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
CLASS_ID_KEY = "class_id"
CLASS_NAME_KEY = "class"
POLYGON_KEY = "points"
TRACKER_ID_KEY = "tracker_id"
TRACKER_ID_KEY = "tracker_id"
25 changes: 15 additions & 10 deletions inference/core/workflows/core_steps/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ def convert_to_sv_detections(
batch_of_detections: List[sv.Detections] = []
for p in predictions:
detections = sv.Detections.from_inference(p)
parent_ids = [
d.get(PARENT_ID_KEY, "")
for d in p[predictions_key]
]
parent_ids = [d.get(PARENT_ID_KEY, "") for d in p[predictions_key]]
detection_ids = [
d.get(DETECTION_ID_KEY, str(uuid.uuid4))
for d in p[predictions_key]
d.get(DETECTION_ID_KEY, str(uuid.uuid4)) for d in p[predictions_key]
]
detections[DETECTION_ID_KEY] = np.array(detection_ids)
detections[PARENT_ID_KEY] = np.array(parent_ids)
Expand All @@ -94,14 +90,23 @@ def add_keypoints_to_detections(
np.array([k.get(KEYPOINTS_CLASS_ID_KEY, "") for k in keypoints])
)
keypoints_confidences.append(
np.array([k.get(KEYPOINTS_CONFIDENCE_KEY, "") for k in keypoints], dtype=np.float64)
np.array(
[k.get(KEYPOINTS_CONFIDENCE_KEY, "") for k in keypoints],
dtype=np.float64,
)
)
keypoints_xy.append(
np.array([k.get(KEYPOINTS_XY_KEY, [-1, -1]) for k in keypoints], dtype=np.float64)
np.array(
[k.get(KEYPOINTS_XY_KEY, [-1, -1]) for k in keypoints], dtype=np.float64
)
)
detections[KEYPOINTS_CLASS_NAME_KEY] = np.array(keypoints_class_names, dtype="object")
detections[KEYPOINTS_CLASS_NAME_KEY] = np.array(
keypoints_class_names, dtype="object"
)
detections[KEYPOINTS_CLASS_ID_KEY] = np.array(keypoints_class_ids, dtype="object")
detections[KEYPOINTS_CONFIDENCE_KEY] = np.array(keypoints_confidences, dtype="object")
detections[KEYPOINTS_CONFIDENCE_KEY] = np.array(
keypoints_confidences, dtype="object"
)
detections[KEYPOINTS_XY_KEY] = np.array(keypoints_xy, dtype="object")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,7 @@ def _post_process_result(
predictions=predictions,
classes_to_accept=class_filter,
)
predictions = attach_parent_info(
images=images,
predictions=predictions
)
predictions = attach_parent_info(images=images, predictions=predictions)
return anchor_prediction_detections_in_parent_coordinates(
image=images,
predictions=predictions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ def _post_process_result(
prediction_type="classification",
)
predictions = attach_parent_info(
images=images,
predictions=predictions,
nested_key=None
images=images, predictions=predictions, nested_key=None
)
return predictions
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,5 @@ def _post_process_result(
prediction_type="classification",
)
return attach_parent_info(
images=images,
predictions=predictions,
nested_key=None
images=images, predictions=predictions, nested_key=None
)
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def _post_process_result(
) -> List[Dict[str, Union[sv.Detections, Any]]]:
batch_of_detections = convert_to_sv_detections(predictions)
for prediction, detections in zip(predictions, batch_of_detections):
detections["data"] = np.array([
p.get("data", "") for p in prediction["predictions"]
])
detections["data"] = np.array(
[p.get("data", "") for p in prediction["predictions"]]
)
prediction["predictions"] = detections
converted_predictions = attach_prediction_type_info(
predictions=predictions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def _post_process_result(
) -> List[Dict[str, Union[sv.Detections, Any]]]:
batch_of_detections = convert_to_sv_detections(predictions)
for prediction, detections in zip(predictions, batch_of_detections):
detections["data"] = np.array([
p.get("data", "") for p in prediction["predictions"]
])
detections["data"] = np.array(
[p.get("data", "") for p in prediction["predictions"]]
)
prediction["predictions"] = detections
converted_predictions = attach_prediction_type_info(
predictions=predictions,
Expand Down

0 comments on commit 125570b

Please sign in to comment.