Esempio n. 1
0
 def load_train(i):
     if i % 100 == 0:
         print('dataset', i, '/', len(lines))
     line = lines[i]
     if line[0] == 'test_cls':
         prs, gts = line[1:]
         prs, gts = json.loads(prs), json.loads(gts)
         image = cv2.imread(
             os.path.join(settings.TEST_IMAGE_DIR, prs['file_name']))
         assert image.shape == (prs['height'], prs['width'], 3)
         for pr, gt in zip(prs['proposals'], gts['ground_truth']):
             cropped = predictions2html.crop(image, pr['adjusted_bbox'],
                                             64)
             all[i].append([cropped, gt['attributes']])
     else:
         anno = json.loads(line[1].strip())
         image = cv2.imread(
             os.path.join(
                 settings.TRAINVAL_IMAGE_DIR if line[0]
                 in {'train', 'val'} else settings.TEST_IMAGE_DIR,
                 anno['file_name']))
         assert image.shape == (anno['height'], anno['width'], 3)
         for char in anno_tools.each_char(anno):
             if not char['is_chinese']:
                 continue
             cropped = predictions2html.crop(image,
                                             char['adjusted_bbox'], 64)
             all[i].append([cropped, char['attributes']])
Esempio n. 2
0
def get_polygons():
    lines = []
    with open(settings.TRAIN) as f:
        lines += [('train', s) for s in f.read().splitlines()]
    with open(settings.VAL) as f:
        lines += [('val', s) for s in f.read().splitlines()]
    with open(settings.TEST_CLASSIFICATION) as f1, open(
            settings.TEST_CLASSIFICATION_GT) as f2:
        prs = f1.read().splitlines()
        gts = f2.read().splitlines()
        lines += [('test_cls', pr, gt) for pr, gt in zip(prs, gts)]
    with open(settings.TEST_DETECTION_GT) as f:
        lines += [('test_det', s) for s in f.read().splitlines()]
    polygons = []
    for i, line in enumerate(lines):
        if line[0] == 'test_cls':
            prs = line[1]
            prs = json.loads(prs.strip())
            for pr in prs['proposals']:
                polygons.append(pr['polygon'])
        else:
            anno = json.loads(line[1].strip())
            for char in anno_tools.each_char(anno):
                if not char['is_chinese']:
                    continue
                polygons.append(char['polygon'])
    return polygons
Esempio n. 3
0
def main():
    # load from downloads
    with open('../data/annotations/downloads/info.json') as f:
        full_data_list = json.load(f)
    with open('../data/annotations/downloads/val.jsonl') as f:
        val = f.read().splitlines()

    # make infomation of data list
    data_list = {
        'train': full_data_list['train'],
        'val': [],
        'test_cls': full_data_list['val'],
        'test_det': full_data_list['val'],
    }
    with open(settings.DATA_LIST, 'w') as f:
        json.dump(data_list, f, indent=2)

    # copy training set
    shutil.copy('../data/annotations/downloads/train.jsonl', settings.TRAIN)

    # create empty validation set
    with open(settings.VAL, 'w') as f:
        pass

    # create testing set for classification
    with open(settings.TEST_CLASSIFICATION,
              'w') as f, open(settings.TEST_CLASSIFICATION_GT, 'w') as fgt:
        for line in val:
            anno = json.loads(line.strip())
            proposals = []
            gt = []
            for char in anno_tools.each_char(anno):
                if not char['is_chinese']:
                    continue
                proposals.append({
                    'adjusted_bbox': char['adjusted_bbox'],
                    'polygon': char['polygon']
                })
                gt.append({
                    'text': char['text'],
                    'attributes': char['attributes'],
                    'size': char['adjusted_bbox'][-2:]
                })
            anno.pop('annotations')
            anno.pop('ignore')
            anno['proposals'] = proposals
            f.write(common_tools.to_jsonl(anno))
            f.write('\n')
            anno.pop('proposals')
            anno['ground_truth'] = gt
            fgt.write(common_tools.to_jsonl(anno))
            fgt.write('\n')

    # create testing set for detection
    shutil.copy('../data/annotations/downloads/val.jsonl',
                settings.TEST_DETECTION_GT)
Esempio n. 4
0
 def load_train(i):
     if i % 100 == 0:
         print('trainval', i, '/', len(lines))
     anno = json.loads(lines[i].strip())
     image = misc.imread(
         os.path.join(settings.TRAINVAL_IMAGE_DIR, anno['file_name']))
     assert image.shape == (anno['height'], anno['width'], 3)
     for char in anno_tools.each_char(anno):
         if not char['is_chinese']:
             continue
         cropped = crop(image, char['adjusted_bbox'])
         train[i].append([cropped, char['text']])
Esempio n. 5
0
 def crop_once(line, write_images):
     anno = json.loads(line.strip())
     image_id = anno['image_id']
     all = []
     for char in anno_tools.each_char(anno):
         if not char['is_chinese']:
             continue
         cate_id = text2cate[char['text']]
         if cate_id >= settings.NUM_CHAR_CATES:
             cate_id = settings.NUM_CHAR_CATES
         all.append((char['adjusted_bbox'], cate_id))
     if write_images:
         image = cv2.imread(
             os.path.join(settings.TRAINVAL_IMAGE_DIR, anno['file_name']))
         assert image.shape == imshape
         for o in anno['ignore']:
             poly = (np.array(o['polygon'])).astype(np.int32)
             cv2.fillConvexPoly(image, poly, (128, 128, 128))
     cropped_list = list()
     for o in darknet_tools.get_crop_bboxes(imshape, cropshape,
                                            cropoverlap):
         xlo = o['xlo']
         xhi = xlo + cropshape[1]
         ylo = o['ylo']
         yhi = ylo + cropshape[0]
         labels = []
         for bbox, cate_id in all:
             x, y, w, h = bbox
             if x > xhi or x + w < xlo or y > yhi or y + h < ylo:
                 continue
             bbox = ((x + w / 2 - xlo) / cropshape[1],
                     (y + h / 2 - ylo) / cropshape[0], w / cropshape[1],
                     h / cropshape[0])
             if 0.5 < in_image_ratio(bbox):
                 labels.append((bbox, cate_id))
         if 0 < len(labels):
             basename = '{}_{}'.format(image_id, o['name'])
             cropped_file_name = os.path.join(settings.TRAINVAL_CROPPED_DIR,
                                              '{}.jpg'.format(basename))
             cropped_xml_name = os.path.join(settings.TRAINVAL_CROPPED_DIR,
                                             '{}.xml'.format(basename))
             cropped_list.append('{} {}'.format(cropped_file_name,
                                                cropped_xml_name))
             if write_images:
                 cropped = image[ylo:yhi, xlo:xhi]
                 cv2.imwrite(cropped_file_name, cropped)
                 with open(
                         os.path.join(settings.TRAINVAL_CROPPED_DIR,
                                      '{}.xml'.format(basename)), 'w') as f:
                     write_xml(labels, cropshape, f)
     return cropped_list
Esempio n. 6
0
def crop_no_concat():
    assert six.PY3
    random.seed(0)

    belongs = defaultdict(list)
    with open(settings.TRAIN) as f:
        lines = f.read().splitlines()
    with open(settings.VAL) as f:
        lines += f.read().splitlines()
    for line in lines:
        anno = json.loads(line.strip())
        i = 0
        for char in anno_tools.each_char(anno):
            if not char['is_chinese']:
                continue
            for attr in settings.ATTRIBUTES:
                if attr in char['attributes']:
                    belongs[attr].append((anno['image_id'], anno['file_name'],
                                          char['adjusted_bbox'], i))
                else:
                    belongs['not-{}'.format(attr)].append(
                        (anno['image_id'], anno['file_name'],
                         char['adjusted_bbox'], i))
            i += 1

    for attr, imgset in sorted(belongs.items()):
        imgset = random.sample(imgset, 200)
        root = os.path.join(settings.ATTR_SAMPLE_DIR, attr)
        resized_root = os.path.join(settings.ATTR_SAMPLE_DIR,
                                    '{}-resized'.format(attr))
        if not os.path.isdir(root):
            os.makedirs(root)
        if not os.path.isdir(resized_root):
            os.makedirs(resized_root)
        for i, (image_id, file_name, bbox, idx_in_img) in enumerate(imgset):
            if i % 100 == 0:
                print(attr, i, '/', len(imgset))
            image = cv2.imread(
                os.path.join(settings.TRAINVAL_IMAGE_DIR, file_name))
            cropped_file_path = os.path.join(
                root, '{}_{}.png'.format(image_id, idx_in_img))
            resized_file_path = os.path.join(
                resized_root, '{}_{}.png'.format(image_id, idx_in_img))
            cropped = predictions2html.crop(image, bbox, float('inf'))
            resized = cv2.resize(cropped, (32, 32))
            cv2.imwrite(cropped_file_path, cropped)
            cv2.imwrite(resized_file_path, resized)
Esempio n. 7
0
 def gt2array(gt, draw_ignore):
     color = '#f00'
     color_ignore = '#ff0'
     a = list()
     for char in anno_tools.each_char(gt):
         if char['is_chinese']:
             a.append({
                 'bbox': char['adjusted_bbox'],
                 'text': char['text'],
                 'color': color
             })
     if draw_ignore:
         for char in gt['ignore']:
             a.append({
                 'bbox': char['bbox'],
                 'text': '',
                 'color': color_ignore
             })
     return a
Esempio n. 8
0
 def gt2array(gt):
     color = '#0f0'
     a = list()
     for char in anno_tools.each_char(gt):
         if char['is_chinese']:
             a.append({
                 'polygon': char['polygon'],
                 'text': char['text'],
                 'color': color,
                 'fontsize': 10
             })
     for char in gt['ignore']:
         a.append({
             'polygon': char['polygon'],
             'text': '',
             'color': '#ff0',
             'fontsize': 10
         })
     return a
Esempio n. 9
0
def main():
    counts = defaultdict(int)
    with open(settings.TRAIN) as f, open(settings.VAL) as f2:
        for line in f.read().splitlines() + f2.read().splitlines():
            anno = json.loads(line.strip())
            for char in anno_tools.each_char(anno):
                if char['is_chinese']:
                    text = char['text']
                    assert 1 == len(text)
                    counts[text] += 1
    cates = [{
        'text': k,
        'trainval': v,
    } for k, v in counts.items()]
    cates.sort(key=lambda o: (-o['trainval'], o['text']))
    for i, o in enumerate(cates):
        o['cate_id'] = i
    with codecs.open(settings.CATES, 'w', 'utf-8') as f:
        json.dump(cates,
                  f,
                  ensure_ascii=False,
                  allow_nan=False,
                  indent=2,
                  sort_keys=True)
Esempio n. 10
0
    def dt2array(dtobj, gtobj, draw_ignore, draw_proposal):
        iou_thresh = settings.IOU_THRESH
        charset = set()
        proposal = False

        def in_size(_):
            return True

        dt = dtobj['detections']
        dt.sort(
            key=lambda o: -o['score']
        )  # sort must be stable, otherwise mAP will be slightly different
        dt = [(o['bbox'], o.get('text'), o['score']) for o in dt]

        ig = [(o['bbox'], None) for o in gtobj['ignore']]
        gt = []
        for char in anno_tools.each_char(gtobj):
            if char['is_chinese']:
                charset.add(char['text'])
                gt.append(
                    (char['adjusted_bbox'], char['text'], char['attributes']))

        dt_matches = [[] for i in range(len(dt))]
        dt_ig = [False] * len(dt)
        for i_dt, dtchar in enumerate(dt):
            for i_gt, gtchar in enumerate(gt):
                if proposal or dtchar[1] == gtchar[1]:
                    miou = eval_tools.iou(dtchar[0], gtchar[0])
                    if miou > iou_thresh:
                        dt_matches[i_dt].append((i_gt, miou))
            for igchar in ig:
                miou = eval_tools.a_in_b(dtchar[0], igchar[0])
                if miou > iou_thresh:
                    dt_ig[i_dt] = True
        for matches in dt_matches:
            matches.sort(
                key=lambda t: -t[1]
            )  # sort must be stable, otherwise you shoule use key=lambda t: (-t[1], t[0])

        dt_matched = [
            0 if in_size(o[0]) and False == b else 2
            for o, b in zip(dt, dt_ig)
        ]
        gt_taken = [(0, None) if in_size(o[0]) else (2, None) for o in gt]
        for i_dt, matches in enumerate(dt_matches):
            for i_gt, _ in matches:
                if 1 != dt_matched[i_dt] and 1 != gt_taken[i_gt][0]:
                    if 0 == gt_taken[i_gt][0]:
                        dt_matched[i_dt] = 1
                        gt_taken[i_gt] = (1, i_dt)
                    else:
                        dt_matched[i_dt] = 2

        a = list()
        minscore = 1.
        colormap = {0: '#ff0', 1: '#0f0', 2: '#0ff'}
        for i in range(len(dt)):
            if len(a) >= len(gt):
                break
            bbox, text, score = dt[i]
            taken = dt_matched[i]
            if 2 != taken or draw_ignore:
                flag = True
                for o in a:
                    if settings.IOU_THRESH < eval_tools.a_in_b(
                            bbox, o['bbox']
                    ) or settings.IOU_THRESH < eval_tools.a_in_b(
                            o['bbox'], bbox):
                        flag = False
                if flag:
                    a.append({
                        'bbox': bbox,
                        'text': text or '■',
                        'color': colormap[taken]
                    })
                    minscore = score
        if draw_proposal:
            for o in sorted(dtobj['proposals'], key=lambda o: -o['score']):
                bbox, score = o['bbox'], o['score']
                if score >= minscore:
                    s = 0
                    for igbbox, _ in ig:
                        s += eval_tools.a_in_b(bbox, igbbox)
                    for o in a:
                        s += max(eval_tools.a_in_b(o['bbox'], bbox),
                                 eval_tools.a_in_b(bbox, o['bbox']))
                    if s <= settings.IOU_THRESH:
                        a.append({'bbox': bbox, 'text': '', 'color': '#00f'})
        return list(reversed(a))
Esempio n. 11
0
def main():
    allowed_filename = get_allowed_filename()
    basename2cnt = dict()
    imgid2anno = dict()
    with codecs.open(settings.OVERALL_ANNOTATIONS, 'r', 'utf-8') as f:
        for line in f:
            image_anno = json.loads(line.strip())
            image_id = image_anno['image_id']
            if image_id[:3] == '204':
                logo_bbox = [810., 1800., 460., 100.]
                image_anno['ignore'].append({
                    'bbox': logo_bbox,
                    'polygon': bbox2poly(logo_bbox),
                })
            else:
                logo_bbox = None
            for i, blk in enumerate(image_anno['annotations']):
                image_anno['annotations'][i] = list(
                    filter(
                        is_legal_char(
                            (image_anno['height'], image_anno['width'], 3),
                            logo_bbox), blk))
            image_anno['annotations'] = list(
                filter(lambda a: a, image_anno['annotations']))
            direction = image_id[0]
            onew = image_id[1:]
            oold = mapper.new2old[onew]
            complete_basename = '{}.{}'.format(oold, direction)
            if image_id[
                    1:3] != '04' and complete_basename not in allowed_filename:
                continue
            is_chinese = 0
            is_not_chinese = 0
            for char in anno_tools.each_char(image_anno):
                if char['is_chinese']:
                    is_chinese += 1
                else:
                    is_not_chinese += 1
            if 0 == is_chinese:
                continue
            basename2cnt[complete_basename] = [is_chinese, is_not_chinese]
            imgid2anno[image_id] = image_anno
    streets = []
    current_street = []

    def feature(basename):
        assert 25 == len(basename)
        cityid = basename[:4]
        streetid = basename[4:8]
        dateid = basename[8:14]
        timeid = basename[14:23]
        direction = basename[-1]
        return (cityid, streetid, dateid, timeid, direction)

    def like(n1, n2):
        f1, f2 = feature(n1), feature(n2)
        return (f1[0], f1[1], f1[2]) == (f2[0], f2[1], f2[2])

    for i, basename in enumerate(sorted(basename2cnt.keys())):
        if i == 0 or like(last_basename, basename):
            current_street.append(basename)
        else:
            streets.append(current_street)
            current_street = [basename]
        last_basename = basename
    streets.append(current_street)

    def count_chinese(a):
        return sum([basename2cnt[basename][0] for basename in a])

    np.random.seed(0)
    train = []
    tests = [[], [], []]
    requires = [101000, 101000, 50500]

    basenames = sorted(mapper.old2new.keys())
    for i, st in enumerate(streets):
        ntest = 100
        nval = 50
        ntrainval = 1200
        ninterval = 5
        if ntest * 2 + ninterval + ntrainval < len(st):
            while 0 < len(st):
                j = np.random.randint(0, 2)
                tests[j] += st[0:ntest]
                tests[1 - j] += st[ntest:ntest * 2]
                lastname = st[ntest * 2 - 1].split('.')[0]
                lastpos = bisect.bisect(basenames, lastname) - 1
                assert basenames[lastpos] == lastname
                st = st[ntest * 2:]
                j = 0
                while True:
                    name = st[j].split('.')[0]
                    pos = bisect.bisect(basenames, name) - 1
                    assert basenames[pos] == name
                    if pos - lastpos <= ninterval:
                        j += 1
                    else:
                        break
                st = st[j:]
                hi = ntrainval
                if len(st) < ntrainval * (math.sqrt(5) +
                                          1) / 2 + ntest * 2 + ninterval * 2:
                    hi = len(st)
                j = np.random.randint(0, 2)
                train += st[j * nval:hi - nval + j * nval]
                tests[2] += st[0:j * nval] + st[hi - nval + j * nval:hi]
                lastname = st[hi - 1].split('.')[0]
                st = st[hi:]
                if 0 < len(st):
                    lastpos = bisect.bisect(basenames, lastname) - 1
                    j = 0
                    while True:
                        name = st[j].split('.')[0]
                        pos = bisect.bisect(basenames, name) - 1
                        assert (basenames[pos] == name)
                        if pos - lastpos <= ninterval:
                            j += 1
                        else:
                            break
                    st = st[j:]
            streets[i] = []
    counts = [count_chinese(test) for test in tests]

    while (np.array(counts) < np.array(requires)).any():
        i = np.random.randint(0, len(streets))
        j = np.random.randint(0, len(requires))
        if counts[j] >= requires[j]:
            continue
        st = streets[i]
        if 300 < len(st):
            continue
        tests[j] += streets.pop(i)
        counts[j] = count_chinese(tests[j])
    for st in streets:
        train += st

    print('train', len(train), count_chinese(train))
    print('val', len(tests[2]), count_chinese(tests[2]))
    print('test_1', len(tests[0]), count_chinese(tests[0]))
    print('test_2', len(tests[1]), count_chinese(tests[1]))

    def basename2imgid(bn):
        a, direction = bn.split('.')
        return '{}{}'.format(direction, mapper.old2new[a])

    train = sorted(map(basename2imgid, train))
    val = sorted(map(basename2imgid, tests[2]))
    test_1 = sorted(map(basename2imgid, tests[0]))
    test_2 = sorted(map(basename2imgid, tests[1]))

    def toinfo(anno):
        keys = list(anno.keys())
        keys.remove('annotations')
        keys.remove('ignore')
        return {k: anno[k] for k in keys}

    with open(settings.DATA_LIST, 'w') as f:

        def g(ds):
            return [toinfo(imgid2anno[imgid]) for imgid in ds]

        r = {
            'train': g(train),
            'val': g(val),
            'test_cls': g(test_1),
            'test_det': g(test_2)
        }
        json.dump(r,
                  f,
                  ensure_ascii=True,
                  allow_nan=False,
                  indent=2,
                  sort_keys=True)

    with open(settings.TRAIN, 'w') as f:
        for imgid in train:
            f.write(common_tools.to_jsonl(imgid2anno[imgid]))
            f.write('\n')
    with open(settings.VAL, 'w') as f:
        for imgid in val:
            f.write(common_tools.to_jsonl(imgid2anno[imgid]))
            f.write('\n')
    with open(settings.TEST_CLASSIFICATION,
              'w') as f, open(settings.TEST_CLASSIFICATION_GT, 'w') as fgt:
        for imgid in test_1:
            anno = imgid2anno[imgid]
            proposals = []
            gt = []
            for char in anno_tools.each_char(anno):
                if not char['is_chinese']:
                    continue
                proposals.append({
                    'adjusted_bbox': char['adjusted_bbox'],
                    'polygon': char['polygon']
                })
                gt.append({
                    'text': char['text'],
                    'attributes': char['attributes'],
                    'size': char['adjusted_bbox'][-2:]
                })
            anno.pop('annotations')
            anno.pop('ignore')
            anno['proposals'] = proposals
            f.write(common_tools.to_jsonl(anno))
            f.write('\n')
            anno.pop('proposals')
            anno['ground_truth'] = gt
            fgt.write(common_tools.to_jsonl(anno))
            fgt.write('\n')
    with open(settings.TEST_DETECTION_GT, 'w') as f:
        for imgid in test_2:
            f.write(common_tools.to_jsonl(imgid2anno[imgid]))
            f.write('\n')
Esempio n. 12
0
def main():
    all = defaultdict(int)
    n = 0
    with open(settings.TRAIN) as f1, open(settings.VAL) as f2:
        for line in f1.read().splitlines() + f2.read().splitlines():
            anno = json.loads(line.strip())
            for char in anno_tools.each_char(anno):
                if char['is_chinese']:
                    k = 0
                    n += 1
                    for attr in char['attributes']:
                        k |= 2**settings.ATTRIBUTES.index(attr)
                    for szname, (lo, hi) in settings.SIZE_RANGES:
                        if lo <= max(char['adjusted_bbox'][2:]) < hi:
                            all['trainval', szname, k] += 1
    with open(settings.TEST_CLASSIFICATION_GT) as f2:
        for line in f2:
            gts = json.loads(line.strip())
            for char in gts['ground_truth']:
                k = 0
                for attr in char['attributes']:
                    k |= 2**settings.ATTRIBUTES.index(attr)
                for szname, (lo, hi) in settings.SIZE_RANGES:
                    if lo <= max(char['size']) < hi:
                        all['test', szname, k] += 1
    with open(settings.TEST_DETECTION_GT) as f:
        for line in f:
            anno = json.loads(line.strip())
            for char in anno_tools.each_char(anno):
                if char['is_chinese']:
                    k = 0
                    for attr in char['attributes']:
                        k |= 2**settings.ATTRIBUTES.index(attr)
                    for szname, (lo, hi) in settings.SIZE_RANGES:
                        if lo <= max(char['adjusted_bbox'][2:]) < hi:
                            all['test', szname, k] += 1

    def check(k, attr_id):
        if attr_id < len(settings.ATTRIBUTES):
            return int(k) & 2**attr_id
        else:
            return 0 == int(k) & 2**(attr_id - len(settings.ATTRIBUTES))

    def trans(attr_id):
        if attr_id < len(settings.ATTRIBUTES):
            return settings.ATTRIBUTES[attr_id]
        else:
            return r'$\sim${}'.format(
                settings.ATTRIBUTES[attr_id - len(settings.ATTRIBUTES)])

    for i in range(2 * len(settings.ATTRIBUTES) - 1):
        for j in range(i + 1, 2 * len(settings.ATTRIBUTES)):
            if j == i + len(settings.ATTRIBUTES):
                continue
            s = r'{}\!\!\!\!\! & \& & \!\!\!\!\!{}'.format(trans(i), trans(j))
            for szname, (lo, hi) in settings.SIZE_RANGES:
                n_train = n_test = 0
                for k, v in all.items():
                    if check(k[2], i) and check(k[2], j):
                        if ('trainval', szname) == k[:2]:
                            n_train += v
                        if ('test', szname) == k[:2]:
                            n_test += v
                s += ' & {} / {}'.format(
                    n_train, '{:5d}'.format(n_test).replace(' ', r'\,\,\,'))
            s += r' \\'
            print(s)
Esempio n. 13
0
def main():
    most_freq = defaultdict(lambda: {'trainval': 0, 'test': 0})
    num_char = defaultdict(lambda: {'trainval': 0, 'test': 0})
    num_uniq_char = defaultdict(lambda: {'trainval': 0, 'test': 0})
    num_image = {'trainval': 0, 'test': 0}
    sum_chinese = {'trainval': 0, 'test': 0}
    sum_not_chinese = {'trainval': 0, 'test': 0}
    sum_ignore = {'trainval': 0, 'test': 0}
    longsizes = {'trainval': list(), 'test': list()}
    attrs = {
        szname: {attr: 0
                 for attr in settings.ATTRIBUTES + ['__all__']}
        for szname, _ in settings.SIZE_RANGES
    }
    with open(settings.TRAIN) as f, open(settings.VAL) as f2:
        for line in f.read().splitlines() + f2.read().splitlines():
            anno = json.loads(line.strip())
            num = 0
            uniq = set()
            for char in anno_tools.each_char(anno):
                if char['is_chinese']:
                    most_freq[char['text']]['trainval'] += 1
                    num += 1
                    uniq.add(char['text'])
                    sum_chinese['trainval'] += 1
                    longsize = max(char['adjusted_bbox'][2],
                                   char['adjusted_bbox'][3])
                    longsizes['trainval'].append(longsize)
                    for szname, szrange in settings.SIZE_RANGES:
                        if szrange[0] <= longsize < szrange[1]:
                            for attr in char['attributes']:
                                attrs[szname][attr] += 1
                            attrs[szname]['__all__'] += 1
                else:
                    sum_not_chinese['trainval'] += 1
            assert 0 < len(uniq)
            num_char[num]['trainval'] += 1
            num_uniq_char[len(uniq)]['trainval'] += 1
            num_image['trainval'] += 1
            sum_ignore['trainval'] += len(anno['ignore'])
    with open(settings.TEST_CLASSIFICATION_GT) as f:
        for line in f:
            gt = json.loads(line.strip())['ground_truth']
            num = 0
            uniq = set()
            for char in gt:
                most_freq[char['text']]['test'] += 1
                num += 1
                uniq.add(char['text'])
                sum_chinese['test'] += 1
                longsize = max(*char['size'])
                longsizes['test'].append(longsize)
                for szname, szrange in settings.SIZE_RANGES:
                    if szrange[0] <= longsize < szrange[1]:
                        for attr in char['attributes']:
                            attrs[szname][attr] += 1
                        attrs[szname]['__all__'] += 1
            assert 0 < len(uniq)
            num_char[num]['test'] += 1
            num_uniq_char[len(uniq)]['test'] += 1
            num_image['test'] += 1
    with open(settings.TEST_DETECTION_GT) as f:
        for line in f:
            anno = json.loads(line.strip())
            num = 0
            uniq = set()
            for char in anno_tools.each_char(anno):
                if char['is_chinese']:
                    most_freq[char['text']]['test'] += 1
                    num += 1
                    uniq.add(char['text'])
                    sum_chinese['test'] += 1
                    longsizes['test'].append(
                        max(char['adjusted_bbox'][2],
                            char['adjusted_bbox'][3]))
                    for szname, szrange in settings.SIZE_RANGES:
                        if szrange[0] <= longsize < szrange[1]:
                            for attr in char['attributes']:
                                attrs[szname][attr] += 1
                            attrs[szname]['__all__'] += 1
                else:
                    sum_not_chinese['test'] += 1
            assert 0 < len(uniq)
            num_char[num]['test'] += 1
            num_uniq_char[len(uniq)]['test'] += 1
            num_image['test'] += 1
            sum_ignore['test'] += len(anno['ignore'])
    most_freq = [{
        'text': k,
        'trainval': v['trainval'],
        'test': v['test'],
    } for k, v in most_freq.items()]
    most_freq.sort(key=lambda o: (-o['trainval'] - o['test'], o['text']))
    print('10_most_frequent_characters')
    for i, o in enumerate(most_freq[:10]):
        print(i + 1, o['text'], o['trainval'], o['test'])
    print('over_all')
    print('uniq_chinese', len(most_freq))
    print('num_image', num_image['trainval'], num_image['test'])
    print('sum_chinese', sum_chinese['trainval'], sum_chinese['test'])
    print('sum_not_chinese', sum_not_chinese['trainval'],
          sum_not_chinese['test'])
    print('sum_ignore', sum_ignore['trainval'], sum_ignore['test'])

    with codecs.open(settings.STAT_FREQUENCY, 'w', 'utf-8') as f:
        json.dump(most_freq, f, ensure_ascii=False, indent=2)

    # most_freq
    meta = most_freq[:50]
    data = [
        [
            {
                'legend': 'training set',
                'data': [o['trainval'] for o in meta],
            },
            {
                'legend': 'testing set',
                'data': [o['test'] for o in meta],
            },
        ],
    ]
    labels = [o['text'] for o in meta]
    with plt.style.context({
            'figure.subplot.left': .06,
            'figure.subplot.right': .98,
            'figure.subplot.top': .96,
            'pdf.fonttype': 42,
    }):
        plt.figure(figsize=(10, 3))
        plt.xlim((0, len(labels) + 1))
        plt.grid(which='major', axis='y', linestyle='dotted')
        plot_tools.draw_bar(data, labels, xticks_font_fname=get_chinese_ttf())
        plt.savefig(os.path.join(settings.PLOTS_DIR, 'stat_most_freq.pdf'))
        plt.close()

    # num_char
    meta = [num_char[i] for i in range(1, 61)]
    data = [
        [
            {
                'legend': 'training set',
                'data': [o['trainval'] for o in meta],
            },
            {
                'legend': 'testing set',
                'data': [o['test'] for o in meta],
            },
        ],
    ]
    labels = [i + 1 if (i + 1) % 10 == 0 else None for i, _ in enumerate(meta)]
    with plt.style.context({
            'figure.subplot.left': .14,
            'figure.subplot.right': .96,
            'figure.subplot.bottom': .16,
            'figure.subplot.top': .96,
            'pdf.fonttype': 42,
    }):
        plt.figure(figsize=(5, 3))
        plt.xlim((0, len(labels) + 1))
        plt.grid(which='major', axis='y', linestyle='dotted')
        plot_tools.draw_bar(data, labels)
        plt.xlabel('number of character instances')
        plt.ylabel('number of images')
        plt.savefig(os.path.join(settings.PLOTS_DIR, 'stat_num_char.pdf'))
        plt.close()

    # num_uniq_char
    meta = [num_uniq_char[i] for i in range(1, 61)]
    data = [
        [
            {
                'legend': 'training set',
                'data': [o['trainval'] for o in meta],
            },
            {
                'legend': 'testing set',
                'data': [o['test'] for o in meta],
            },
        ],
    ]
    labels = [i + 1 if (i + 1) % 10 == 0 else None for i, _ in enumerate(meta)]
    with plt.style.context({
            'figure.subplot.left': .14,
            'figure.subplot.right': .96,
            'figure.subplot.bottom': .16,
            'figure.subplot.top': .96,
            'pdf.fonttype': 42,
    }):
        plt.figure(figsize=(5, 3))
        plt.xlim((0, len(labels) + 1))
        plt.grid(which='major', axis='y', linestyle='dotted')
        plot_tools.draw_bar(data, labels)
        plt.xlabel('number of character categories')
        plt.ylabel('number of images')
        plt.savefig(os.path.join(settings.PLOTS_DIR, 'stat_num_uniq_char.pdf'))
        plt.close()

    # instance size
    longsizes['trainval'].sort()
    longsizes['test'].sort()
    ranges = list(range(0, 65, 8))
    data = [
        [
            {
                'legend':
                'training set',
                'data': [
                    bisect.bisect_left(longsizes['trainval'], hi) -
                    bisect.bisect_left(longsizes['trainval'], lo)
                    for lo, hi in zip(ranges, ranges[1:] + [float('inf')])
                ],
            },
            {
                'legend':
                'testing set',
                'data': [
                    bisect.bisect_left(longsizes['test'], hi) -
                    bisect.bisect_left(longsizes['test'], lo)
                    for lo, hi in zip(ranges, ranges[1:] + [float('inf')])
                ],
            },
        ],
    ]
    labels = [
        '{}-{}'.format(lo, hi) for lo, hi in zip(ranges, ranges[1:] + [''])
    ]
    with plt.style.context({
            'figure.subplot.left': .12,
            'figure.subplot.right': .96,
            'figure.subplot.bottom': .10,
            'figure.subplot.top': .96,
            'pdf.fonttype': 42,
    }):
        plt.figure(figsize=(6, 3))
        plt.xlim((0, len(labels) + 1))
        plt.grid(which='major', axis='y', linestyle='dotted')
        plot_tools.draw_bar(data, labels)
        plt.savefig(os.path.join(settings.PLOTS_DIR, 'stat_instance_size.pdf'))
        plt.close()

    # attributes percentage
    data = [[{
        'legend':
        szname,
        'data': [
            attrs[szname][attr] / attrs[szname]['__all__'] * 100
            for attr in settings.ATTRIBUTES
        ],
    }] for szname, szrange in settings.SIZE_RANGES]
    labels = settings.ATTRIBUTES
    with plt.style.context({
            'figure.subplot.left': .12,
            'figure.subplot.right': .96,
            'figure.subplot.bottom': .10,
            'figure.subplot.top': .96,
            'pdf.fonttype': 42,
    }):
        plt.figure(figsize=(6, 3))
        plt.xlim((.3, .7 + len(labels)))
        plt.grid(which='major', axis='y', linestyle='dotted')
        plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(
            '{:.0f}%'.format))
        plot_tools.draw_bar(data, labels, width=.18)
        plt.savefig(os.path.join(settings.PLOTS_DIR, 'stat_attributes.pdf'))
        plt.close()
Esempio n. 14
0
# sys.path.append('C:\\Users\\flavi\\PycharmProjects\\ctw-traffic-signs')
import settings

from pythonapi import anno_tools
with open(settings.TRAIN_AUGMENTED) as f:
    lines = f.readlines()
    for line in lines:
        anno = json.loads(line)
        if anno['image_id'] == "0500223":
            to_show = anno
            break
path = os.path.join(settings.TRAINVAL_AUGMENTED_DIR,
                    to_show['image_id'] + '.png')
assert os.path.exists(path), 'file not exists: {}'.format(path)
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(16, 16))
ax = plt.gca()
plt.imshow(img)

for instance in anno_tools.each_char(anno):
    color = (0, 1, 0) if instance['is_chinese'] else (1, 0, 0)
    ax.add_patch(patches.Polygon(instance['polygon'], fill=False, color=color))
'''
for ignore in anno['ignore']:
    color = (1, 1, 0)
    ax.add_patch(patches.Polygon(ignore['polygon'], fill=False, color=color))
'''
plt.show()