Exemple #1
0
def resize(image, annotations_list, img_data, size):
    res_image = copy.deepcopy(image)
    res_image["width"] = size[1]
    res_image["height"] = size[0]
    res_ann = []

    res_img_data = wmli.resize_img(img_data, size, keep_aspect_ratio=True)
    x_scale = res_img_data.shape[1] / img_data.shape[1]
    y_scale = res_img_data.shape[0] / img_data.shape[0]

    for ann in annotations_list:
        bbox = (np.array(ann["bbox"]) *
                np.array([x_scale, y_scale, x_scale, y_scale])).astype(
                    np.int32)
        segmentation = wmli.resize_img(ann["segmentation"],
                                       size=size,
                                       keep_aspect_ratio=True,
                                       interpolation=cv2.INTER_NEAREST)
        category = copy.deepcopy(ann["category_id"])
        res_ann.append({
            "bbox": bbox,
            "segmentation": segmentation,
            "category_id": category
        })

    return res_image, res_ann, res_img_data
def trans_data(data_dir,save_dir):
    global name_to_id_dict
    wmlu.show_dict(name_to_id_dict)
    wmlu.create_empty_dir(save_dir,remove_if_exists=False)

    def name_to_id(x):
        return name_to_id_dict[x]

    ignored_labels = ["construction--barrier--ambiguous","construction--barrier--separator"]
    data = LabelMeData(label_text2id=name_to_id, shuffle=False)
    data.read_data(data_dir)
    for i,x in enumerate(data.get_items()):
        full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x

        if len(category_ids) == 0:
            print(f"Skip {full_path}")
            continue

        new_mask = odm.dense_mask_to_sparse_mask(binary_mask,category_ids,default_label=255)
        #r_base_name = wmlu.base_name(full_path)
        r_base_name = f"IMG_{i+1:05d}"
        base_name = r_base_name+".png"
        save_path = os.path.join(save_dir,base_name)
        if resize_size is not None:
            new_mask = wmli.resize_img(new_mask,resize_size,keep_aspect_ratio=True,interpolation=cv2.INTER_NEAREST)
            img  = wmli.imread(full_path)
            img = wmli.resize_img(img,resize_size,keep_aspect_ratio=True)
            img_save_path = os.path.join(save_dir,r_base_name+".jpg")
            wmli.imwrite(img_save_path,img)

        new_mask = new_mask.astype(np.uint8)
        if os.path.exists(save_path):
            print(f"WARNING: File {save_path} exists.")
        cv2.imwrite(save_path,new_mask)
        sys.stdout.write(f"\r{i}")
Exemple #3
0
def create_tf_example(image, annotations):
    global src_file_index
    image_height = image['img_height']
    image_width = image['img_width']
    img_path = image['img_path']

    if RECORD_IMG_SIZE is None:
        with tf.gfile.GFile(img_path, 'rb') as fid:
            encoded_jpg = fid.read()
    else:
        img = wmli.imread(img_path)
        img = wmli.resize_img(img, RECORD_IMG_SIZE, keep_aspect_ratio=True)
        encoded_jpg = wmli.encode_img(img)

    xmin = []
    xmax = []
    ymin = []
    ymax = []
    is_crowd = []
    category_ids = []

    for l, box in annotations:
        xmin.append(box[1])
        xmax.append(box[3])
        ymin.append(box[0])
        ymax.append(box[2])
        is_crowd.append(False)
        category_ids.append(l)

    if len(xmin) == 0:
        return None

    feature_dict = {
        'image/height': dataset_util.int64_feature(image_height),
        'image/width': dataset_util.int64_feature(image_width),
        'image/filename': dataset_util.bytes_feature(img_path.encode('utf8')),
        'image/encoded': dataset_util.bytes_feature(encoded_jpg),
        'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),
        'image/object/class/label':
        dataset_util.int64_list_feature(category_ids),
        'image/object/is_crowd': dataset_util.int64_list_feature(is_crowd),
    }
    example = tf.train.Example(features=tf.train.Features(
        feature=feature_dict))
    return example
Exemple #4
0
def show_anchor_box(img_file,boxes,size=None):
    nr = boxes.shape[0]
    classes = []
    scores = []
    for i in range(nr):
        classes.append(0)
        scores.append(1)
    img = wmli.imread(img_file)
    if size is not None:
        img = wmli.resize_img(img,(size[1],size[0]))

    odv.bboxes_draw_on_img(img, classes, scores, boxes)
    plt.figure(figsize=(10,10))
    plt.imshow(img)
    plt.show()
    return img
Exemple #5
0
    dataset.read_data("/home/wj/ai/mldata/boesemantic")
    save_dir = wmlu.home_dir("ai/tmp/boe_images2")
    wmlu.create_empty_dir(save_dir,remove_if_exists=False)
    color_map = fill_colormap_and_names("/home/wj/ai/mldata/mapillary_vistas/config_v2.0.json")
    def text_fn(l):
        if l in ID_TO_READABLE_NAME:
            return ID_TO_READABLE_NAME[l]
        else:
            return "NA"
    def color_fn(l):
        return color_map[l*3:l*3+3]

    legend_img = draw_legend(list(ID_TO_NAME.keys()),text_fn,img_size=(2448,300),color_fn=color_fn)
    for ifn,img,mask in dataset.get_items():
        base_name = wmlu.base_name(ifn)
        wmlu.safe_copy(ifn,save_dir)
        rgb_mask = convert_semantic_to_rgb(mask,color_map,True)
        if rgb_mask.shape[0] != legend_img.shape[0]:
            legend_img = wmli.resize_img(legend_img,(legend_img.shape[1],rgb_mask.shape[0]))
        rgb_mask = np.concatenate([rgb_mask,legend_img],axis=1)
        mask_path = os.path.join(save_dir,base_name+".png")
        wmli.imwrite(mask_path,rgb_mask)

        mask_image = draw_semantic_on_image(img,mask,color_map,ignored_label=255)
        mask_image = np.concatenate([mask_image,legend_img],axis=1)
        mask_image_path = os.path.join(save_dir,base_name+"1.png")
        wmli.imwrite(mask_image_path,mask_image)



Exemple #6
0
        text_to_id[t] = i + 1
        id_to_text[i + 1] = t

    text_to_id[" "] = 69

    def name_to_id(name):
        return text_to_id[name]

    data = PascalVOCData(label_text2id=name_to_id, shuffle=True)
    data.read_data("/home/vghost/ai/mldata2/ocrdata/rdatasv20/train")
    MIN_IMG_SIZE = 768
    for x in data.get_items():
        full_path, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x
        img = wmli.imread(full_path)
        if img.shape[0] < MIN_IMG_SIZE or img.shape[1] < MIN_IMG_SIZE:
            img = wmli.resize_img(img, [MIN_IMG_SIZE, MIN_IMG_SIZE],
                                  keep_aspect_ratio=True)

        def text_fn(classes, scores):
            return id_to_text[classes]

        odv.bboxes_draw_on_imgv2(img=img,
                                 classes=category_ids,
                                 scores=None,
                                 bboxes=boxes,
                                 color_fn=None,
                                 text_fn=text_fn,
                                 thickness=2,
                                 show_text=True,
                                 fontScale=0.8)
        plt.figure()
        plt.imshow(img)