mirror of https://github.com/open-mmlab/mmocr.git
parent
36e92ebe70
commit
b863bbca5e
|
@ -1,6 +1,6 @@
|
||||||
import cv2
|
|
||||||
import imgaug
|
import imgaug
|
||||||
import imgaug.augmenters as iaa
|
import imgaug.augmenters as iaa
|
||||||
|
import mmcv
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from mmdet.core.mask import PolygonMasks
|
from mmdet.core.mask import PolygonMasks
|
||||||
|
@ -145,7 +145,7 @@ class EastRandomCrop:
|
||||||
padded_img = np.zeros(
|
padded_img = np.zeros(
|
||||||
(self.target_size[1], self.target_size[0], img.shape[2]),
|
(self.target_size[1], self.target_size[0], img.shape[2]),
|
||||||
img.dtype)
|
img.dtype)
|
||||||
padded_img[:h, :w] = cv2.resize(
|
padded_img[:h, :w] = mmcv.imresize(
|
||||||
img[crop_y:crop_y + crop_h, crop_x:crop_x + crop_w], (w, h))
|
img[crop_y:crop_y + crop_h, crop_x:crop_x + crop_w], (w, h))
|
||||||
|
|
||||||
# for bboxes
|
# for bboxes
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import math
|
import math
|
||||||
|
|
||||||
import cv2
|
|
||||||
import mmcv
|
import mmcv
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
@ -91,8 +90,8 @@ class ResizeOCR:
|
||||||
if dst_max_width is not None:
|
if dst_max_width is not None:
|
||||||
valid_ratio = min(1.0, 1.0 * new_width / dst_max_width)
|
valid_ratio = min(1.0, 1.0 * new_width / dst_max_width)
|
||||||
resize_width = min(dst_max_width, new_width)
|
resize_width = min(dst_max_width, new_width)
|
||||||
img_resize = cv2.resize(results['img'],
|
img_resize = mmcv.imresize(results['img'],
|
||||||
(resize_width, dst_height))
|
(resize_width, dst_height))
|
||||||
resize_shape = img_resize.shape
|
resize_shape = img_resize.shape
|
||||||
pad_shape = img_resize.shape
|
pad_shape = img_resize.shape
|
||||||
if new_width < dst_max_width:
|
if new_width < dst_max_width:
|
||||||
|
@ -102,13 +101,13 @@ class ResizeOCR:
|
||||||
pad_val=self.img_pad_value)
|
pad_val=self.img_pad_value)
|
||||||
pad_shape = img_resize.shape
|
pad_shape = img_resize.shape
|
||||||
else:
|
else:
|
||||||
img_resize = cv2.resize(results['img'],
|
img_resize = mmcv.imresize(results['img'],
|
||||||
(new_width, dst_height))
|
(new_width, dst_height))
|
||||||
resize_shape = img_resize.shape
|
resize_shape = img_resize.shape
|
||||||
pad_shape = img_resize.shape
|
pad_shape = img_resize.shape
|
||||||
else:
|
else:
|
||||||
img_resize = cv2.resize(results['img'],
|
img_resize = mmcv.imresize(results['img'],
|
||||||
(dst_max_width, dst_height))
|
(dst_max_width, dst_height))
|
||||||
resize_shape = img_resize.shape
|
resize_shape = img_resize.shape
|
||||||
pad_shape = img_resize.shape
|
pad_shape = img_resize.shape
|
||||||
|
|
||||||
|
@ -286,10 +285,10 @@ class RandomPaddingOCR:
|
||||||
random_padding_bottom = round(
|
random_padding_bottom = round(
|
||||||
np.random.uniform(0, self.max_ratio[3]) * ori_height)
|
np.random.uniform(0, self.max_ratio[3]) * ori_height)
|
||||||
|
|
||||||
img = np.copy(results['img'])
|
padding = (random_padding_left, random_padding_top,
|
||||||
img = cv2.copyMakeBorder(img, random_padding_top,
|
random_padding_right, random_padding_bottom)
|
||||||
random_padding_bottom, random_padding_left,
|
img = mmcv.impad(results['img'], padding=padding, padding_mode='edge')
|
||||||
random_padding_right, cv2.BORDER_REPLICATE)
|
|
||||||
results['img'] = img
|
results['img'] = img
|
||||||
results['img_shape'] = img.shape
|
results['img_shape'] = img.shape
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import math
|
import math
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
|
import mmcv
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import Polygon as plg
|
import Polygon as plg
|
||||||
import torchvision.transforms as transforms
|
import torchvision.transforms as transforms
|
||||||
|
@ -587,7 +588,7 @@ class RandomRotatePolyInstances:
|
||||||
(h_ind, w_ind) = (np.random.randint(0, h * 7 // 8),
|
(h_ind, w_ind) = (np.random.randint(0, h * 7 // 8),
|
||||||
np.random.randint(0, w * 7 // 8))
|
np.random.randint(0, w * 7 // 8))
|
||||||
img_cut = img[h_ind:(h_ind + h // 9), w_ind:(w_ind + w // 9)]
|
img_cut = img[h_ind:(h_ind + h // 9), w_ind:(w_ind + w // 9)]
|
||||||
img_cut = cv2.resize(img_cut, (canvas_size[1], canvas_size[0]))
|
img_cut = mmcv.imresize(img_cut, (canvas_size[1], canvas_size[0]))
|
||||||
mask = cv2.warpAffine(
|
mask = cv2.warpAffine(
|
||||||
mask,
|
mask,
|
||||||
rotation_matrix, (canvas_size[1], canvas_size[0]),
|
rotation_matrix, (canvas_size[1], canvas_size[0]),
|
||||||
|
@ -670,7 +671,7 @@ class SquareResizePad:
|
||||||
t_w = self.target_size if h <= w else int(w * self.target_size / h)
|
t_w = self.target_size if h <= w else int(w * self.target_size / h)
|
||||||
else:
|
else:
|
||||||
t_h = t_w = self.target_size
|
t_h = t_w = self.target_size
|
||||||
img = cv2.resize(img, (t_w, t_h))
|
img = mmcv.imresize(img, (t_w, t_h))
|
||||||
return img, (t_h, t_w)
|
return img, (t_h, t_w)
|
||||||
|
|
||||||
def square_pad(self, img):
|
def square_pad(self, img):
|
||||||
|
@ -685,7 +686,7 @@ class SquareResizePad:
|
||||||
(h_ind, w_ind) = (np.random.randint(0, h * 7 // 8),
|
(h_ind, w_ind) = (np.random.randint(0, h * 7 // 8),
|
||||||
np.random.randint(0, w * 7 // 8))
|
np.random.randint(0, w * 7 // 8))
|
||||||
img_cut = img[h_ind:(h_ind + h // 9), w_ind:(w_ind + w // 9)]
|
img_cut = img[h_ind:(h_ind + h // 9), w_ind:(w_ind + w // 9)]
|
||||||
expand_img = cv2.resize(img_cut, (pad_size, pad_size))
|
expand_img = mmcv.imresize(img_cut, (pad_size, pad_size))
|
||||||
if h > w:
|
if h > w:
|
||||||
y0, x0 = 0, (h - w) // 2
|
y0, x0 = 0, (h - w) // 2
|
||||||
else:
|
else:
|
||||||
|
@ -758,7 +759,7 @@ class RandomScaling:
|
||||||
scales = self.size * 1.0 / max(h, w) * aspect_ratio
|
scales = self.size * 1.0 / max(h, w) * aspect_ratio
|
||||||
scales = np.array([scales, scales])
|
scales = np.array([scales, scales])
|
||||||
out_size = (int(h * scales[1]), int(w * scales[0]))
|
out_size = (int(h * scales[1]), int(w * scales[0]))
|
||||||
image = cv2.resize(image, out_size[::-1])
|
image = mmcv.imresize(image, out_size[::-1])
|
||||||
|
|
||||||
results['img'] = image
|
results['img'] = image
|
||||||
results['img_shape'] = image.shape
|
results['img_shape'] = image.shape
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from mmocr.models.builder import CONVERTORS
|
from mmocr.models.builder import CONVERTORS
|
||||||
|
from mmocr.utils import list_from_file
|
||||||
|
|
||||||
|
|
||||||
@CONVERTORS.register_module()
|
@CONVERTORS.register_module()
|
||||||
|
@ -36,10 +37,10 @@ class NerConvertor:
|
||||||
assert self.max_len > 2
|
assert self.max_len > 2
|
||||||
assert self.annotation_type in ['bio', 'bioes']
|
assert self.annotation_type in ['bio', 'bioes']
|
||||||
|
|
||||||
lines = open(vocab_file, encoding='utf-8').readlines()
|
vocabs = list_from_file(vocab_file)
|
||||||
self.vocab_size = len(lines)
|
self.vocab_size = len(vocabs)
|
||||||
for i in range(len(lines)):
|
for idx, vocab in enumerate(vocabs):
|
||||||
self.word2ids.update({lines[i].rstrip(): i})
|
self.word2ids.update({vocab: idx})
|
||||||
|
|
||||||
if self.annotation_type == 'bio':
|
if self.annotation_type == 'bio':
|
||||||
self.label2id_dict, self.id2label, self.ignore_id = \
|
self.label2id_dict, self.id2label, self.ignore_id = \
|
||||||
|
|
Loading…
Reference in New Issue