コード例 #1
0
def test_dict_iteration():
    d = {"a": 1, "b": 2, "c": 3}
    assert not isinstance(iteritems(d), list)
    assert not isinstance(iterkeys(d), list)
    assert not isinstance(itervalues(d), list)
    assert set(iteritems(d)) == set(d.items())
    assert set(iterkeys(d)) == set(d.keys())
    assert set(itervalues(d)) == set(d.values())
コード例 #2
0
def test_dict_iteration():
    d = {'a': 1, 'b': 2, 'c': 3}
    assert not isinstance(iteritems(d), list)
    assert not isinstance(iterkeys(d), list)
    assert not isinstance(itervalues(d), list)
    assert set(iteritems(d)) == set(d.items())
    assert set(iterkeys(d)) == set(d.keys())
    assert set(itervalues(d)) == set(d.values())
コード例 #3
0
def test_dict_iteration():
    d = {'a': 1, 'b': 2, 'c': 3}
    assert not isinstance(iteritems(d), list)
    assert not isinstance(iterkeys(d), list)
    assert not isinstance(itervalues(d), list)
    assert set(iteritems(d)) == set(d.items())
    assert set(iterkeys(d)) == set(d.keys())
    assert set(itervalues(d)) == set(d.values())
コード例 #4
0
ファイル: coco2pascal.py プロジェクト: witzou/Python_Practice
def create_annotations(dbpath, subset, dst):
    annotations_path = path(
        dbpath).expand() / 'annotations/instances_{}2014.json'.format(subset)
    #images_path = path(dbpath).expand() / 'images/{}2014'.format(subset)
    images_path = path(dbpath).expand() / '{}2014'.format(
        subset)  #clw note:这里图片直接放在2014val下,前面没有images
    #         另外subset这里一般是val或者train,见文件名
    categories, instances = get_instances(annotations_path)
    dst = path(dst).expand()

    for i, instance in enumerate(instances):
        instances[i]['category_id'] = categories[instance['category_id']]

    for name, group in iteritems(groupby('file_name', instances)):
        img = imread(images_path / name)
        if img.ndim == 3:
            out_name = rename(name)
            annotation = root('VOC2014', '{}.jpg'.format(out_name),
                              group[0]['height'], group[0]['width'])
            for instance in group:
                annotation.append(instance_to_xml(instance))
            etree.ElementTree(annotation).write(dst /
                                                '{}.xml'.format(out_name))
            #print out_name
            print(out_name)  #clw modify: 看来之前是py2.7
        else:
            #print instance['file_name']
            print(instance['file_name'])  #clw modify: 看来之前是py2.7
コード例 #5
0
def create_annotations(dbpath, subset, dst):
    '''

    :param dbpath:   root path of coco dataset
    :param subset:  'train' or  'val'
    :param dst:     where to save transfered result
    :return:
    '''
    annotations_path = dbpath + '/annotations_trainval2014/annotations/instances_{}2014.json'.format(
        subset)
    images_path = dbpath + '/images/{}2014'.format(subset)
    categories, instances = get_instances(annotations_path)

    if not os.path.exists(dst):
        os.makedirs(dst)

    for i, instance in enumerate(instances):
        instances[i]['category_id'] = categories[instance['category_id']]

    for name, group in iteritems(groupby('file_name', instances)):
        print("image_path is %s , name is %s " % (images_path, name))
        img = imread(images_path + "/" + name)
        if img.ndim == 3:
            annotation = root(images_path, name, group[0]['height'],
                              group[0]['width'])
            for instance in group:
                annotation.append(instance_to_xml(instance))
            etree.ElementTree(annotation).write(
                dst + '/{}.xml'.format(name.split(".")[0]))
            print(name)
        else:
            print(instance['file_name'])
コード例 #6
0
def create_annotations(dbpath, subset, dst):
    first_part = path(dbpath).expand()
    last_part = 'annotations/instances_{}2014.json'.format(subset)
    annotations_path = first_part / last_part
    images_path = first_part / 'images/{}2014'.format(subset)
    categories, instances = get_instances(annotations_path)
    dst = path(dst).expand()

    for i, instance in enumerate(instances):
        instances[i]['category_id'] = categories[instance['category_id']]

    for name, group in iteritems(groupby('file_name', instances)):
        img = imread(images_path / name)
        if img.ndim == 3:
            out_name = rename(name)
            annotation = root('VOC2014', '{}.jpg'.format(out_name),
                              group[0]['height'], group[0]['width'])
            for instance in group:
                annotation.append(instance_to_xml(instance))
            etree.ElementTree(annotation).write(
                dst / '{}.xml'.format(out_name)
            )
            print(out_name)
        else:
            print(instance['file_name'])
コード例 #7
0
ファイル: coco2voc.py プロジェクト: yssyss1/yolo-v2
def change_annotations(data_path, subset, destination_path):
    if not os.path.exists(data_path):
        raise FileNotFoundError("{} path is not exist".format(data_path))

    os.makedirs(destination_path, exist_ok=True)

    annotation_path = os.path.join(
        data_path, "annotations/instances_{}2014.json".format(subset))
    image_path = os.path.join(data_path, "images/{}2014".format(subset))

    if not os.path.exists(annotation_path):
        raise FileNotFoundError(
            "{} annotation is not exist".format(annotation_path))

    if not os.path.exists(image_path):
        raise FileNotFoundError("{} image is not exist".format(image_path))

    categories, instances = create_annotation_instance(annotation_path)

    for i, instance in enumerate(instances):
        instances[i]["category_id"] = categories[instance["category_id"]]

    for name, group in tqdm(iteritems(groupby("file_name", instances)),
                            desc="Create annotation xml files"):
        out_name = name.split(".")[-2]
        img = imread(os.path.join(image_path, name))

        if img.ndim == 3:
            annotation = xml_root("{}.jpg".format(out_name),
                                  group[0]["height"], group[0]["width"])

            for instance in group:
                annotation.append(instance_to_xml(instance))
            etree.ElementTree(annotation).write(
                os.path.join(destination_path, "{}.xml".format(out_name)))
コード例 #8
0
def create_annotations(coco_annotation, dst):
    categories, instances = get_instances(coco_annotation)
    if not os.path.exists(dst):
        os.makedirs(dst)
        print('Create a new folder as xml folder: {}'.format(dst))
    dst = path(dst).expand()

    for i, instance in enumerate(instances):
        instances[i]['category_id'] = categories[instance['category_id']]

    for name, group in iteritems(groupby('file_name', instances)):
        out_name = rename(name)
        annotation = root('VOC2014', '{}.png'.format(out_name),
                          group[0]['height'], group[0]['width'])
        for instance in group:
            annotation.append(instance_to_xml(instance))

        print(out_name)
        etree.ElementTree(annotation).write(dst / '{}.xml'.format(out_name))
コード例 #9
0
def create_annotations(coco_annotation, dst='annotations_voc'):

    os.makedirs(dst, exist_ok=True)

    categories, instances = get_instances(coco_annotation)
    '''
        About categories: Dictionary where the keys are the categories IDs and the values are tha categories names.
        About instances: Tuple of dictionaries containing information of the annotations and its respective images.
        NOTE: There is one instance for every annotation, not image.
    '''

    dst = os.path.abspath(dst)
    '''
        Modifying the category ID to show an string instead of a number.
        The string corresponds to the name of the category.
    '''
    for i, instance in tqdm(enumerate(instances), desc="rewriting categories"):
        instances[i]['category_id'] = categories[instance['category_id']]

    for name, group in tqdm(iteritems(groupby('file_name', instances)),
                            total=len(groupby('file_name', instances)),
                            desc="processing annotations"):
        '''
            About name: the image path
            About group: the image informations
        '''

        img = imread(os.path.abspath(name))
        if img.ndim == 3:
            out_name = rename(name)
            image_folder, image_name = os.path.split(out_name)
            annotation = root(image_folder, '{}.jpg'.format(image_name),
                              group[0]['height'], group[0]['width'])
            for instance in group:
                annotation.append(instance_to_xml(instance))

            # Exporting XML to destination folder
            destination_file = "{}.xml".format(out_name)
            _, destination_file = os.path.split(destination_file)
            xml_file = etree.ElementTree(annotation)
            xml_file.write(os.path.join(dst, destination_file))
コード例 #10
0
def create_annotations(dbpath, subset, dst):
    annotations_path = path(dbpath).expand(
    ) / 'annotations_json/instances_{}2012.json'.format(subset)
    images_path = path(dbpath).expand() / 'images'
    categories, instances = get_instances(annotations_path)
    dst = path(dst).expand()

    for i, instance in enumerate(instances):
        instances[i]['category_id'] = categories[instance['category_id']]

    for name, group in iteritems(groupby('file_name', instances)):
        if Path(images_path + "/" + name).exists():
            img = imread(images_path / name)
            if img.ndim == 3:
                out_name = rename(name)
                annotation = root('VOC2012', '{}.jpg'.format(out_name),
                                  group[0]['height'], group[0]['width'])
                for instance in group:
                    if (instance['category_id'] == 'person'):
                        annotation.append(instance_to_xml(instance))
                etree.ElementTree(annotation).write(dst /
                                                    '{}.xml'.format(out_name))
コード例 #11
0
def create_annotations(dbpath, subset, dst):
    annotations_path = Path(dbpath).expand() / '{}_Text.json'.format(subset)
    images_path = 'path to JPEG Images containing Images'
    categories, instances = get_instances(annotations_path)

    dst = Path(dst).expand()

    #for i, instance in enumerate(instances):
    #if i==10:
    #    print(instance)
    #print(instances[i]['category'])
    #print([instance['class']])
    #instances[i]['category_id'] = instance['class']
    #instances[i]['category_id'] = categories[instance['category_id']]
    #if i==10:
    #    print(instance)
    #print(instance['class'])
    #instances[i][1] = 'Text'

    #exit()
    for name, group in iteritems(groupby('file_name', instances)):
        images_path = 'path to JPEG Images containing Images'
        images_path = images_path + '/' + name
        print(images_path)
        img = imread(images_path)
        if img.ndim == 3:
            out_name = rename(name)
            annotation = root('VOC2012', '{}.jpg'.format(out_name),
                              group[0]['height'], group[0]['width'])
            for instance in group:
                annotation.append(instance_to_xml(instance))
            etree.ElementTree(annotation).write(dst /
                                                '{}.xml'.format(out_name))
            print(out_name)
        else:
            print(instance['file_name'])