This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathutils.py
More file actions
55 lines (48 loc) · 1.89 KB
/
utils.py
File metadata and controls
55 lines (48 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright 2022 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utility functions for keypoint transformation."""
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use KerasCV, please install TensorFlow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from keras_cv.src.api_export import keras_cv_export
H_AXIS = -3
W_AXIS = -2
@keras_cv_export("keras_cv.keypoint.filter_out_of_image")
def filter_out_of_image(keypoints, image):
"""Discards keypoints if falling outside of the image.
Args:
keypoints: a, possibly ragged, 2D (ungrouped), 3D (grouped)
keypoint data in the 'xy' format.
image: a 3D tensor in the HWC format.
Returns:
tf.RaggedTensor: a 2D or 3D ragged tensor with at least one
ragged rank containing only keypoint in the image.
"""
image_shape = tf.cast(tf.shape(image), keypoints.dtype)
mask = tf.math.logical_and(
tf.math.logical_and(
keypoints[..., 0] >= 0, keypoints[..., 0] < image_shape[W_AXIS]
),
tf.math.logical_and(
keypoints[..., 1] >= 0, keypoints[..., 1] < image_shape[H_AXIS]
),
)
masked = tf.ragged.boolean_mask(keypoints, mask)
if isinstance(masked, tf.RaggedTensor):
return masked
return tf.RaggedTensor.from_tensor(masked)