提交 4369552e 编写于 作者: 文幕地方's avatar 文幕地方

add in and out box_format

上级 a8efe28f
...@@ -86,7 +86,8 @@ Train: ...@@ -86,7 +86,8 @@ Train:
loc_reg_num: *loc_reg_num loc_reg_num: *loc_reg_num
max_text_length: *max_text_length max_text_length: *max_text_length
- TableBoxEncode: - TableBoxEncode:
box_format: *box_format in_box_format: *box_format
out_box_format: *box_format
- ResizeTableImage: - ResizeTableImage:
max_len: 488 max_len: 488
- NormalizeImage: - NormalizeImage:
...@@ -121,7 +122,8 @@ Eval: ...@@ -121,7 +122,8 @@ Eval:
loc_reg_num: *loc_reg_num loc_reg_num: *loc_reg_num
max_text_length: *max_text_length max_text_length: *max_text_length
- TableBoxEncode: - TableBoxEncode:
box_format: *box_format in_box_format: *box_format
out_box_format: *box_format
- ResizeTableImage: - ResizeTableImage:
max_len: 488 max_len: 488
- NormalizeImage: - NormalizeImage:
......
...@@ -90,7 +90,8 @@ Train: ...@@ -90,7 +90,8 @@ Train:
- PaddingTableImage: - PaddingTableImage:
size: [480, 480] size: [480, 480]
- TableBoxEncode: - TableBoxEncode:
box_format: *box_format in_box_format: *box_format
out_box_format: *box_format
- NormalizeImage: - NormalizeImage:
scale: 1./255. scale: 1./255.
mean: [0.5, 0.5, 0.5] mean: [0.5, 0.5, 0.5]
...@@ -126,7 +127,8 @@ Eval: ...@@ -126,7 +127,8 @@ Eval:
- PaddingTableImage: - PaddingTableImage:
size: [480, 480] size: [480, 480]
- TableBoxEncode: - TableBoxEncode:
box_format: *box_format in_box_format: *box_format
out_box_format: *box_format
- NormalizeImage: - NormalizeImage:
scale: 1./255. scale: 1./255.
mean: [0.5, 0.5, 0.5] mean: [0.5, 0.5, 0.5]
......
...@@ -749,28 +749,35 @@ class TableMasterLabelEncode(TableLabelEncode): ...@@ -749,28 +749,35 @@ class TableMasterLabelEncode(TableLabelEncode):
class TableBoxEncode(object): class TableBoxEncode(object):
def __init__(self, box_format='xyxy', **kwargs): def __init__(self, in_box_format='xyxy', out_box_format='xyxy', **kwargs):
assert box_format in ['xywh', 'xyxy', 'xyxyxyxy'] assert box_format in ['xywh', 'xyxy', 'xyxyxyxy']
self.box_format = box_format self.in_box_format = in_box_format
self.out_box_format = out_box_format
def __call__(self, data): def __call__(self, data):
img_height, img_width = data['image'].shape[:2] img_height, img_width = data['image'].shape[:2]
bboxes = data['bboxes'] bboxes = data['bboxes']
if self.box_format == 'xywh' and bboxes.shape[1] == 4: if self.in_box_format != self.out_box_format:
if self.out_box_format == 'xywh':
if self.in_box_format == 'xyxyxyxy':
bboxes = self.xyxyxyxy2xywh(bboxes)
elif self.in_box_format == 'xyxy':
bboxes = self.xyxy2xywh(bboxes) bboxes = self.xyxy2xywh(bboxes)
bboxes[:, 0::2] /= img_width bboxes[:, 0::2] /= img_width
bboxes[:, 1::2] /= img_height bboxes[:, 1::2] /= img_height
data['bboxes'] = bboxes data['bboxes'] = bboxes
return data return data
def xyxyxyxy2xywh(self, boxes):
new_bboxes = np.zeros([len(bboxes), 4])
new_bboxes[:, 0] = bboxes[:, 0::2].min() # x1
new_bboxes[:, 1] = bboxes[:, 1::2].min() # y1
new_bboxes[:, 2] = bboxes[:, 0::2].max() - new_bboxes[:, 0] # w
new_bboxes[:, 3] = bboxes[:, 1::2].max() - new_bboxes[:, 1] # h
return new_bboxes
def xyxy2xywh(self, bboxes): def xyxy2xywh(self, bboxes):
"""
Convert coord (x1,y1,x2,y2) to (x,y,w,h).
where (x1,y1) is top-left, (x2,y2) is bottom-right.
(x,y) is bbox center and (w,h) is width and height.
:param bboxes: (x1, y1, x2, y2)
:return:
"""
new_bboxes = np.empty_like(bboxes) new_bboxes = np.empty_like(bboxes)
new_bboxes[:, 0] = (bboxes[:, 0] + bboxes[:, 2]) / 2 # x center new_bboxes[:, 0] = (bboxes[:, 0] + bboxes[:, 2]) / 2 # x center
new_bboxes[:, 1] = (bboxes[:, 1] + bboxes[:, 3]) / 2 # y center new_bboxes[:, 1] = (bboxes[:, 1] + bboxes[:, 3]) / 2 # y center
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册