forked from whai362/PSENet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicdar2015_test_loader.py
70 lines (55 loc) · 2.09 KB
/
icdar2015_test_loader.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# dataloader add 3.0 scale
# dataloader add filer text
import numpy as np
from PIL import Image
from torch.utils import data
import util
import cv2
import random
import torchvision.transforms as transforms
import torch
ic15_root_dir = './data/ICDAR2015/Challenge4/'
ic15_test_data_dir = ic15_root_dir + 'ch4_test_images/'
ic15_test_gt_dir = ic15_root_dir + 'ch4_test_localization_transcription_gt/'
random.seed(123456)
def get_img(img_path):
try:
img = cv2.imread(img_path)
img = img[:, :, [2, 1, 0]]
except Exception as e:
print img_path
raise
return img
def scale(img, long_size=2240):
h, w = img.shape[0:2]
scale = long_size * 1.0 / max(h, w)
img = cv2.resize(img, dsize=None, fx=scale, fy=scale)
return img
class IC15TestLoader(data.Dataset):
def __init__(self, part_id=0, part_num=1, long_size=2240):
data_dirs = [ic15_test_data_dir]
self.img_paths = []
for data_dir in data_dirs:
img_names = util.io.ls(data_dir, '.jpg')
img_names.extend(util.io.ls(data_dir, '.png'))
img_paths = []
for idx, img_name in enumerate(img_names):
img_path = data_dir + img_name
img_paths.append(img_path)
self.img_paths.extend(img_paths)
part_size = len(self.img_paths) / part_num
l = part_id * part_size
r = (part_id + 1) * part_size
self.img_paths = self.img_paths[l:r]
self.long_size = long_size
def __len__(self):
return len(self.img_paths)
def __getitem__(self, index):
img_path = self.img_paths[index]
img = get_img(img_path)
scaled_img = scale(img, self.long_size)
scaled_img = Image.fromarray(scaled_img)
scaled_img = scaled_img.convert('RGB')
scaled_img = transforms.ToTensor()(scaled_img)
scaled_img = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(scaled_img)
return img[:, :, [2, 1, 0]], scaled_img