Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code with autopep8 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
with open('requirements.txt', encoding="utf-8-sig") as f:
requirements = f.readlines()


def readme():
with open('README.md', encoding="utf-8-sig") as f:
README = f.read()
return README


setup(
name='easyocr',
packages=['easyocr'],
Expand Down
6 changes: 4 additions & 2 deletions trainer/craft/config/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

CONFIG_PATH = os.path.dirname(__file__)


def load_yaml(config_name):

with open(os.path.join(CONFIG_PATH, config_name)+ '.yaml') as file:
with open(os.path.join(CONFIG_PATH, config_name) + '.yaml') as file:
config = yaml.safe_load(file)

return config


class DotDict(dict):
def __getattr__(self, k):
try:
Expand All @@ -34,4 +36,4 @@ def get(self, k, default=None):
return self[k]
except KeyError:
return default
return super().get(k, default=default)
return super().get(k, default=default)
7 changes: 6 additions & 1 deletion trainer/craft/data/boxEnlarge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ def pointAngle(Apoint, Bpoint):
angle = (Bpoint[1] - Apoint[1]) / ((Bpoint[0] - Apoint[0]) + 10e-8)
return angle


def pointDistance(Apoint, Bpoint):
return math.sqrt((Bpoint[1] - Apoint[1])**2 + (Bpoint[0] - Apoint[0])**2)


def lineBiasAndK(Apoint, Bpoint):

K = pointAngle(Apoint, Bpoint)
B = Apoint[1] - K*Apoint[0]
return K, B


def getX(K, B, Ypoint):
return int((Ypoint-B)/K)


def sidePoint(Apoint, Bpoint, h, w, placehold, enlarge_size):

K, B = lineBiasAndK(Apoint, Bpoint)
Expand All @@ -43,6 +47,7 @@ def sidePoint(Apoint, Bpoint, h, w, placehold, enlarge_size):
y1 = min(h, Apoint[1] + YaxisIncreaseDistance)
return int(x1), int(y1)


def enlargebox(box, h, w, enlarge_size, horizontal_text_bool):

if not horizontal_text_bool:
Expand All @@ -62,4 +67,4 @@ def enlargebox(box, h, w, enlarge_size, horizontal_text_bool):
x3, y3 = sidePoint(center, Cpoint, h, w, 'rightBottom', enlarge_size)
x4, y4 = sidePoint(Dpoint, center, h, w, 'leftBottom', enlarge_size)
newcharbox = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
return newcharbox
return newcharbox
23 changes: 15 additions & 8 deletions trainer/craft/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ def __init__(
self.sample = sample
if self.sample != -1:
random.seed(0)
self.idx = random.sample(range(0, len(self.img_names)), self.sample)
self.idx = random.sample(
range(0, len(self.img_names)), self.sample)

self.pre_crop_area = []

def augment_image(
self, image, region_score, affinity_score, confidence_mask, word_level_char_bbox
):
augment_targets = [image, region_score, affinity_score, confidence_mask]
augment_targets = [image, region_score,
affinity_score, confidence_mask]

if self.aug.random_scale.option:
augment_targets, word_level_char_bbox = random_scale(
Expand Down Expand Up @@ -100,7 +102,8 @@ def augment_image(
)

elif self.aug.random_crop.version == "random_crop":
augment_targets = random_crop(augment_targets, self.output_size,)
augment_targets = random_crop(
augment_targets, self.output_size,)

else:
assert "Undefined RandomCrop version"
Expand Down Expand Up @@ -185,7 +188,8 @@ def __getitem__(self, index):
confidence_mask,
)

region_score = self.resize_to_half(region_score, interpolation=cv2.INTER_CUBIC)
region_score = self.resize_to_half(
region_score, interpolation=cv2.INTER_CUBIC)
affinity_score = self.resize_to_half(
affinity_score, interpolation=cv2.INTER_CUBIC
)
Expand Down Expand Up @@ -284,7 +288,7 @@ def make_gt_score(self, index):

for i in range(len(words)):
length_of_word = len(words[i])
word_bbox = all_char_bbox[char_idx : char_idx + length_of_word]
word_bbox = all_char_bbox[char_idx: char_idx + length_of_word]
assert len(word_bbox) == length_of_word
char_idx += length_of_word
word_bbox = np.array(word_bbox)
Expand Down Expand Up @@ -425,7 +429,8 @@ def load_data(self, index):
self.net, self.gpu, image, word_bboxes[i], words[i], img_name=img_name
)

cv2.fillPoly(confidence_mask, [np.int32(_word_bboxes[i])], confidence)
cv2.fillPoly(confidence_mask, [
np.int32(_word_bboxes[i])], confidence)
do_care_words.append(words[i])
word_level_char_bbox.append(pseudo_char_bbox)
horizontal_text_bools.append(horizontal_text_bool)
Expand Down Expand Up @@ -512,8 +517,10 @@ def load_saved_gt_score(self, index):
saved_cf_mask_path = os.path.join(
self.saved_gt_dir, f"res_img_{query_idx}_cf_mask_thresh_0.6.jpg"
)
region_score = cv2.imread(saved_region_scores_path, cv2.IMREAD_GRAYSCALE)
affinity_score = cv2.imread(saved_affi_scores_path, cv2.IMREAD_GRAYSCALE)
region_score = cv2.imread(
saved_region_scores_path, cv2.IMREAD_GRAYSCALE)
affinity_score = cv2.imread(
saved_affi_scores_path, cv2.IMREAD_GRAYSCALE)
confidence_mask = cv2.imread(saved_cf_mask_path, cv2.IMREAD_GRAYSCALE)

region_score = cv2.resize(region_score, (img_w, img_h))
Expand Down
15 changes: 9 additions & 6 deletions trainer/craft/data/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def generate_gaussian_map(self):
gaussian_map = (gaussian_map / np.max(gaussian_map)).astype(np.float32)

gaussian_map_color = (gaussian_map * 255).astype(np.uint8)
gaussian_map_color = cv2.applyColorMap(gaussian_map_color, cv2.COLORMAP_JET)
gaussian_map_color = cv2.applyColorMap(
gaussian_map_color, cv2.COLORMAP_JET)
return gaussian_map, gaussian_map_color

def generate_circle_mask(self):
Expand Down Expand Up @@ -73,7 +74,8 @@ def four_point_transform(self, bbox):
)

M = cv2.getPerspectiveTransform(init_points, bbox)
warped_gaussian_map = cv2.warpPerspective(self.gaussian_map, M, (width, height))
warped_gaussian_map = cv2.warpPerspective(
self.gaussian_map, M, (width, height))
return warped_gaussian_map, width, height

def add_gaussian_map_to_score_map(
Expand All @@ -97,7 +99,8 @@ def add_gaussian_map_to_score_map(
"""

map_h, map_w = score_map.shape
bbox = enlargebox(bbox, map_h, map_w, enlarge_size, horizontal_text_bool)
bbox = enlargebox(bbox, map_h, map_w, enlarge_size,
horizontal_text_bool)

# If any one point of character bbox is out of range, don't put in on map
if np.any(bbox < 0) or np.any(bbox[:, 0] > map_w) or np.any(bbox[:, 1] > map_h):
Expand All @@ -113,15 +116,15 @@ def add_gaussian_map_to_score_map(

try:
bbox_area_of_image = score_map[
bbox_top : bbox_top + height, bbox_left : bbox_left + width,
bbox_top: bbox_top + height, bbox_left: bbox_left + width,
]
high_value_score = np.where(
warped_gaussian_map > bbox_area_of_image,
warped_gaussian_map,
bbox_area_of_image,
)
score_map[
bbox_top : bbox_top + height, bbox_left : bbox_left + width,
bbox_top: bbox_top + height, bbox_left: bbox_left + width,
] = high_value_score

except Exception as e:
Expand Down Expand Up @@ -189,4 +192,4 @@ def generate_affinity(

if len(all_affinity_bbox) > 0:
all_affinity_bbox = np.concatenate(all_affinity_bbox, axis=0)
return affinity_map, all_affinity_bbox
return affinity_map, all_affinity_bbox
21 changes: 14 additions & 7 deletions trainer/craft/data/imgaug.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
def rescale(img, bboxes, target_size=2240):
h, w = img.shape[0:2]
scale = target_size / max(h, w)
img = cv2.resize(img, dsize=None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
img = cv2.resize(img, dsize=None, fx=scale, fy=scale,
interpolation=cv2.INTER_CUBIC)
bboxes = bboxes * scale
return img, bboxes

Expand All @@ -25,13 +26,16 @@ def random_resize_crop_synth(augment_targets, size):
confidence_mask = Image.fromarray(confidence_mask)

short_side = min(image.size)
i, j, h, w = RandomCrop.get_params(image, output_size=(short_side, short_side))
i, j, h, w = RandomCrop.get_params(
image, output_size=(short_side, short_side))

image = resized_crop(
image, i, j, h, w, size=(size, size), interpolation=InterpolationMode.BICUBIC
image, i, j, h, w, size=(
size, size), interpolation=InterpolationMode.BICUBIC
)
region_score = resized_crop(
region_score, i, j, h, w, (size, size), interpolation=InterpolationMode.BICUBIC
region_score, i, j, h, w, (size,
size), interpolation=InterpolationMode.BICUBIC
)
affinity_score = resized_crop(
affinity_score,
Expand Down Expand Up @@ -76,17 +80,20 @@ def random_resize_crop(

else:
if random.random() < threshold:
i, j, h, w = RandomResizedCrop.get_params(image, scale=scale, ratio=ratio)
i, j, h, w = RandomResizedCrop.get_params(
image, scale=scale, ratio=ratio)
else:
i, j, h, w = RandomResizedCrop.get_params(
image, scale=(1.0, 1.0), ratio=(1.0, 1.0)
)

image = resized_crop(
image, i, j, h, w, size=(size, size), interpolation=InterpolationMode.BICUBIC
image, i, j, h, w, size=(
size, size), interpolation=InterpolationMode.BICUBIC
)
region_score = resized_crop(
region_score, i, j, h, w, (size, size), interpolation=InterpolationMode.BICUBIC
region_score, i, j, h, w, (size,
size), interpolation=InterpolationMode.BICUBIC
)
affinity_score = resized_crop(
affinity_score,
Expand Down
24 changes: 16 additions & 8 deletions trainer/craft/data/pseudo_label/make_charbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def __init__(self, watershed_param, vis_test_dir, pseudo_vis_opt, gaussian_build

def crop_image_by_bbox(self, image, box, word):
w = max(
int(np.linalg.norm(box[0] - box[1])), int(np.linalg.norm(box[2] - box[3]))
int(np.linalg.norm(box[0] - box[1])
), int(np.linalg.norm(box[2] - box[3]))
)
h = max(
int(np.linalg.norm(box[0] - box[3])), int(np.linalg.norm(box[1] - box[2]))
int(np.linalg.norm(box[0] - box[3])
), int(np.linalg.norm(box[1] - box[2]))
)
try:
word_ratio = h / w
Expand Down Expand Up @@ -105,8 +107,10 @@ def visualize_pseudo_label(
_watershed_box = np.int32(watershed_box)
_pseudo_char_bbox = np.int32(pseudo_char_bbox)

region_score_color = cv2.applyColorMap(np.uint8(region_score), cv2.COLORMAP_JET)
region_score_color = cv2.resize(region_score_color, (word_img_w, word_img_h))
region_score_color = cv2.applyColorMap(
np.uint8(region_score), cv2.COLORMAP_JET)
region_score_color = cv2.resize(
region_score_color, (word_img_w, word_img_h))

for box in _watershed_box:
cv2.polylines(
Expand All @@ -118,7 +122,8 @@ def visualize_pseudo_label(

for box in _pseudo_char_bbox:
cv2.polylines(
np.uint8(word_img_cp2), [np.reshape(box, (-1, 1, 2))], True, (255, 0, 0)
np.uint8(word_img_cp2), [np.reshape(
box, (-1, 1, 2))], True, (255, 0, 0)
)

# NOTE: Just for visualize, put gaussian map on char box
Expand Down Expand Up @@ -180,7 +185,8 @@ def split_word_equal_gap(self, word_img_w, word_img_h, word):
continue
left = j * width_per_char
right = (j + 1) * width_per_char
bbox = np.array([[left, 0], [right, 0], [right, height], [left, height]])
bbox = np.array(
[[left, 0], [right, 0], [right, height], [left, height]])
bboxes.append(bbox)

bboxes = np.array(bboxes, np.float32)
Expand Down Expand Up @@ -238,7 +244,8 @@ def build_char_box(self, net, gpu, image, word_bbox, word, img_name=""):
confidence = self.get_confidence(real_char_len, len(pseudo_char_bbox))

if confidence <= 0.5:
pseudo_char_bbox = self.split_word_equal_gap(word_img_w, word_img_h, word)
pseudo_char_bbox = self.split_word_equal_gap(
word_img_w, word_img_h, word)
confidence = 0.5

if self.pseudo_vis_opt and self.flag:
Expand All @@ -258,6 +265,7 @@ def build_char_box(self, net, gpu, image, word_bbox, word, img_name=""):
pseudo_char_bbox[i][None, :, :], M_inv
)

pseudo_char_bbox = self.clip_into_boundary(pseudo_char_bbox, image.shape)
pseudo_char_bbox = self.clip_into_boundary(
pseudo_char_bbox, image.shape)

return pseudo_char_bbox, confidence, horizontal_text_bool
Loading