Ejemplo n.º 1
0
    def render_image_info(rendering_info):

        img_path = rendering_info['img_path']
        bboxes = rendering_info['bboxes']
        bboxClasses = rendering_info['boxClasses']
        output_file_name = rendering_info['output_file_name']

        if not os.path.exists(img_path):
            print('Image {} cannot be found'.format(img_path))
            return

        try:
            original_image = vis_utils.open_image(img_path)
            original_size = original_image.size
            image = vis_utils.resize_image(original_image, options.viz_size[0],
                                           options.viz_size[1])
        except Exception as e:
            print('Image {} failed to open. Error: {}'.format(img_path, e))
            return

        vis_utils.render_db_bounding_boxes(boxes=bboxes,
                                           classes=bboxClasses,
                                           image=image,
                                           original_size=original_size,
                                           label_map=label_map)
        image.save(
            os.path.join(output_dir, 'rendered_images', output_file_name))
Ejemplo n.º 2
0
def processImages(db_path,
                  output_dir,
                  image_base_dir,
                  options=None,
                  bbox_db=None):
    """
    Writes images and html to output_dir to visualize the annotations in the json file
    db_path.
    
    Returns the html filename and the bbox database.
    """

    if options is None:
        options = DbVizOptions()

    print(options.__dict__)

    os.makedirs(os.path.join(output_dir, 'rendered_images'), exist_ok=True)
    assert (os.path.isfile(db_path))
    assert (os.path.isdir(image_base_dir))

    if bbox_db is None:
        print('Loading the database...')
        bbox_db = json.load(open(db_path))
        print('...done')

    annotations = bbox_db['annotations']
    images = bbox_db['images']
    categories = bbox_db['categories']

    # Optionally remove all images without bounding boxes, *before* sampling
    if options.trim_to_images_with_bboxes:

        bHasBbox = [False] * len(annotations)
        for iAnn, ann in enumerate(annotations):
            if 'bbox' in ann:
                assert isinstance(ann['bbox'], list)
                bHasBbox[iAnn] = True
        annotationsWithBboxes = list(compress(annotations, bHasBbox))

        imageIDsWithBboxes = [x['image_id'] for x in annotationsWithBboxes]
        imageIDsWithBboxes = set(imageIDsWithBboxes)

        bImageHasBbox = [False] * len(images)
        for iImage, image in enumerate(images):
            imageID = image['id']
            if imageID in imageIDsWithBboxes:
                bImageHasBbox[iImage] = True
        imagesWithBboxes = list(compress(images, bImageHasBbox))
        images = imagesWithBboxes

    # put the annotations in a dataframe so we can select all annotations for a given image
    df_anno = pd.DataFrame(annotations)
    df_img = pd.DataFrame(images)

    # construct label map
    label_map = {}
    for cat in categories:
        label_map[int(cat['id'])] = cat['name']

    # take a sample of images
    if options.num_to_visualize is not None:
        df_img = df_img.sample(n=options.num_to_visualize,
                               random_state=options.random_seed)

    images_html = []

    # iImage = 0
    for iImage in tqdm(range(len(df_img))):

        img_id = df_img.iloc[iImage]['id']
        img_relative_path = df_img.iloc[iImage]['file_name']
        img_path = os.path.join(
            image_base_dir,
            imageFilenameToPath(img_relative_path, image_base_dir))

        if not os.path.exists(img_path):
            print('Image {} cannot be found'.format(img_path))
            continue

        annos_i = df_anno.loc[df_anno['image_id'] ==
                              img_id, :]  # all annotations on this image

        try:
            originalImage = vis_utils.open_image(img_path)
            original_size = originalImage.size
            image = vis_utils.resize_image(originalImage, options.viz_size[0],
                                           options.viz_size[1])
        except Exception as e:
            print('Image {} failed to open. Error: {}'.format(img_path, e))
            continue

        bboxes = []
        boxClasses = []

        # All the class labels we've seen for this image (with out without bboxes)
        imageCategories = set()

        annotationLevelForImage = ''

        # Iterate over annotations for this image
        # iAnn = 0; anno = annos_i.iloc[iAnn]
        for iAnn, anno in annos_i.iterrows():

            if 'sequence_level_annotation' in anno:
                bSequenceLevelAnnotation = anno['sequence_level_annotation']
                if bSequenceLevelAnnotation:
                    annLevel = 'sequence'
                else:
                    annLevel = 'image'
                if annotationLevelForImage == '':
                    annotationLevelForImage = annLevel
                elif annotationLevelForImage != annLevel:
                    annotationLevelForImage = 'mixed'

            categoryID = anno['category_id']
            categoryName = label_map[categoryID]
            if options.add_search_links:
                categoryName = categoryName.replace('"', '')
                categoryName = '<a href="https://www.bing.com/images/search?q={}">{}</a>'.format(
                    categoryName, categoryName)
            imageCategories.add(categoryName)

            if 'bbox' in anno:
                bbox = anno['bbox']
                if isinstance(bbox, float):
                    assert math.isnan(
                        bbox
                    ), "I shouldn't see a bbox that's neither a box nor NaN"
                    continue
                bboxes.append(bbox)
                boxClasses.append(anno['category_id'])

        imageClasses = ', '.join(imageCategories)

        # render bounding boxes in-place
        vis_utils.render_db_bounding_boxes(bboxes, boxClasses, image,
                                           original_size, label_map)

        file_name = '{}_gtbbox.jpg'.format(img_id.lower().split('.jpg')[0])
        file_name = file_name.replace('/', '~')
        image.save(os.path.join(output_dir, 'rendered_images', file_name))

        labelLevelString = ''
        if len(annotationLevelForImage) > 0:
            labelLevelString = ' (annotation level: {})'.format(
                annotationLevelForImage)

        images_html.append({
            'filename':
            '{}/{}'.format('rendered_images', file_name),
            'title':
            '{}<br/>{}, number of boxes: {}, class labels: {}{}'.format(
                img_relative_path, img_id, len(bboxes), imageClasses,
                labelLevelString),
            'textStyle':
            'font-family:verdana,arial,calibri;font-size:80%;text-align:left;margin-top:20;margin-bottom:5'
        })

    # ...for each image

    if options.sort_by_filename:
        images_html = sorted(images_html, key=lambda x: x['filename'])

    htmlOutputFile = os.path.join(output_dir, 'index.html')

    htmlOptions = options.htmlOptions
    htmlOptions['headerHtml'] = '<h1>Sample annotations from {}</h1>'.format(
        db_path)
    write_html_image_list(filename=htmlOutputFile,
                          images=images_html,
                          options=htmlOptions)

    print('Visualized {} images, wrote results to {}'.format(
        len(images_html), htmlOutputFile))

    return htmlOutputFile, bbox_db