import tensorflow as tf
import numpy as np
def convert_to_xywh(boxes):
# 取前两列(取所有的行)
print(boxes[..., :2])
return tf.concat(
[(boxes[..., :2] + boxes[..., 2:]) / 2.0, boxes[..., 2:] - boxes[..., :2]],
axis=-1,
)
box = [[13, 45, 32, 67], [34, 56, 18, 23]]
boxes = np.asarray(box)
convert_to_xywh(boxes=boxes)
# print(boxes)
输出结果:
[[13 45]
[34 56]]
<tf.Tensor: shape=(2, 4), dtype=float64, numpy=
array([[ 22.5, 56. , 19. , 22. ],
[ 26. , 39.5, -16. , -33. ]])>
可以看见[…, n:]这种操作是取列的操作,并且将列取出来仍然是一个张量