create_crowd_anno.py 2.95 KB
Newer Older
wofmanaf's avatar
wofmanaf committed
1
import argparse
zhe chen's avatar
zhe chen committed
2
3
import concurrent.futures
import json
wofmanaf's avatar
wofmanaf committed
4
5
6
import os
import pickle as pkl
import random
zhe chen's avatar
zhe chen committed
7

wofmanaf's avatar
wofmanaf committed
8
import mmcv
zhe chen's avatar
zhe chen committed
9
10
11
import numpy as np
from PIL import Image

wofmanaf's avatar
wofmanaf committed
12
13
14
15
16
17
18
19
20

def parse_args():
    parser = argparse.ArgumentParser(description='Generate MMDetection Annotations for Crowdhuman-like dataset')
    parser.add_argument('--dataset', help='dataset name', type=str)
    parser.add_argument('--dataset-split', help='dataset split, e.g. train, val', type=str)

    args = parser.parse_args()
    return args.dataset, args.dataset_split

zhe chen's avatar
zhe chen committed
21

wofmanaf's avatar
wofmanaf committed
22
23
24
25
26
27
28
def load_func(fpath):
    assert os.path.exists(fpath)
    with open(fpath, 'r') as fid:
        lines = fid.readlines()
    records = [json.loads(line.strip('\n')) for line in lines]
    return records

zhe chen's avatar
zhe chen committed
29

wofmanaf's avatar
wofmanaf committed
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def decode_annotations(records, dataset_path):
    rec_ids = list(range(len(records)))
    img_list = []
    ann_list = []
    ann_id = 1
    for idx, rec_id in enumerate(rec_ids):
        img_id = records[rec_id]['ID']
        img_url = dataset_path + 'Images/' + img_id + '.jpg'
        assert os.path.exists(img_url)
        im = Image.open(img_url)
        im_w, im_h = im.width, im.height

        gt_box = records[rec_id]['gtboxes']
        gt_box_len = len(gt_box)
        img_dict = dict(
            file_name=img_id + '.jpg',
            height=im_h,
            width=im_w,
            id=idx
        )
        img_list.append(img_dict)
        for ii in range(gt_box_len):
            each_data = gt_box[ii]
            x, y, w, h = each_data['fbox']

            if w <= 0 or h <= 0:
                continue
            # x1 = x; y1 = y; x2 = x + w; y2 = y + h

            valid_bbox = [x, y, w, h]
            if each_data['tag'] == 'person':
                tag = 1
            else:
                tag = -2
            if 'extra' in each_data:
                if 'ignore' in each_data['extra']:
                    if each_data['extra']['ignore'] != 0:
                        tag = -2
            ann_dict = dict(
                area=w * h,
                iscrowd=1 if tag == -2 else 0,
                image_id=idx,
                bbox=[x, y, w, h],
                category_id=1,
                id=ann_id,
                # ignore=1 if tag == -2 else 1,
            )
            ann_id += 1
            ann_list.append(ann_dict)
    cate_list = [{'supercategory': 'none', 'id': 1, 'name': 'person'}]
    json_dict = dict(
        images=img_list,
        annotations=ann_list,
        categories=cate_list
    )
    return json_dict

zhe chen's avatar
zhe chen committed
87
88

if __name__ == '__main__':
wofmanaf's avatar
wofmanaf committed
89
90
91
92
93
94
    dataset_name, dataset_type = parse_args()
    dataset_path = 'data/%s/' % dataset_name
    ch_file_path = dataset_path + 'annotations/annotation_%s.odgt' % dataset_type
    json_file_path = dataset_path + 'annotations/annotation_%s.json' % dataset_type

    records = load_func(ch_file_path)
zhe chen's avatar
zhe chen committed
95
    print('Loading Annotations Done')
wofmanaf's avatar
wofmanaf committed
96
97
98

    json_dict = decode_annotations(records, dataset_path)

zhe chen's avatar
zhe chen committed
99
    print('Parsing Bbox Number: %d' % len(json_dict['annotations']))
wofmanaf's avatar
wofmanaf committed
100
    mmcv.dump(json_dict, json_file_path)