Esempio n. 1
0
def main():
    args = docopt(__doc__)
    data_root = args["<src>"]
    data_output = args["<dst>"]
    os.makedirs(data_output, exist_ok=True)

    dataset = sorted(glob.glob(osp.join(data_root, "*/*.jpg")))

    def handle(iname):
        prefix = osp.split(iname)[1].replace(".jpg", "")
        im = cv2.imread(iname)
        mat = loadmat(iname.replace(".jpg", "LinesAndVP.mat"))
        lines = np.array(mat["lines"]).reshape(-1, 2, 2)
        path = osp.join(data_output, prefix)
        save_heatmap(f"{path}", im[::, ::], lines)
        print(f"Finishing {path}")

    parmap(handle, dataset)
Esempio n. 2
0
def main():
    # args = docopt(__doc__)
    data_root = "/home/jasmeet/Documents/AdvanceComputerVision/YorkUrbanDB"

    data_output = "/home/jasmeet/Documents/AdvanceComputerVision/YorkUrbanDB-lcnn"

    os.makedirs(data_output, exist_ok=True)

    dataset = sorted(glob.glob(osp.join(data_root, "*/*.jpg")))

    def handle(iname):
        prefix = osp.split(iname)[1].replace(".jpg", "")
        im = cv2.imread(iname)
        mat = loadmat(iname.replace(".jpg", "LinesAndVP.mat"))
        lines = np.array(mat["lines"]).reshape(-1, 2, 2)
        path = osp.join(data_output, prefix)
        save_heatmap(f"{path}", im[::, ::], lines)
        print(f"Finishing {path}")

    parmap(handle, dataset)
Esempio n. 3
0
def main():
    # args = docopt(__doc__)
    data_root = "/home/jasmeet/Documents/AdvanceComputerVision/PPGnet-dataset/outdoor/"
    data_output = "/home/jasmeet/Documents/AdvanceComputerVision/PPGnet-dataset/outdoor/ppgnetDataset-lccn-outdoor-labels/"

    os.makedirs(data_output, exist_ok=True)

    dataset = sorted(glob.glob(osp.join(data_root, "images/*.jpg")))

    def handle(iname):
        prefix = osp.split(iname)[1].replace(".jpg", "")
        im = cv2.imread(iname)
        lblpath = data_root+'new-labels/'+prefix+".npz"
        # mat = loadmat(lblpath)
        mat = np.load(lblpath)
        lines = np.array(mat["lines"]).reshape(-1,2,2)
        print(lines)
        path = osp.join(data_output, prefix)
        save_heatmap(f"{path}", im[::, ::], lines)
        print(f"Finishing {path}")

    parmap(handle, dataset)
Esempio n. 4
0
def main():
    args = docopt(__doc__)
    data_root = args["<src>"]
    data_output = args["<dst>"]

    os.makedirs(data_output, exist_ok=True)
    for batch in ["train", "valid"]:
        anno_file = os.path.join(data_root, f"{batch}.json")

        with open(anno_file, "r") as f:
            dataset = json.load(f)

        def handle(data):
            im = cv2.imread(os.path.join(data_root, "images",
                                         data["filename"]))
            prefix = data["filename"].split(".")[0]
            lines = np.array(data["lines"]).reshape(-1, 2, 2)
            os.makedirs(os.path.join(data_output, batch), exist_ok=True)

            lines0 = lines.copy()
            lines1 = lines.copy()
            lines1[:, :, 0] = im.shape[1] - lines1[:, :, 0]
            lines2 = lines.copy()
            lines2[:, :, 1] = im.shape[0] - lines2[:, :, 1]
            lines3 = lines.copy()
            lines3[:, :, 0] = im.shape[1] - lines3[:, :, 0]
            lines3[:, :, 1] = im.shape[0] - lines3[:, :, 1]

            path = os.path.join(data_output, batch, prefix)
            save_heatmap(f"{path}_0", im[::, ::], lines0)
            if batch != "valid":
                save_heatmap(f"{path}_1", im[::, ::-1], lines1)
                save_heatmap(f"{path}_2", im[::-1, ::], lines2)
                save_heatmap(f"{path}_3", im[::-1, ::-1], lines3)
            print("Finishing", os.path.join(data_output, batch, prefix))

        parmap(handle, dataset, 16)
def main():
    args = docopt(__doc__)
    data_root = args["<src>"]
    data_output = args["<dst>"]

    os.makedirs(data_output, exist_ok=True)
    for batch in ["train", "valid"]:
        filelist = glob.glob(f"{data_root}/{batch}/*.xml")
        filelist.sort()
        def handle(xmlname):
            iname = xmlname.replace("xml", "jpg")
            image = io.imread(iname).astype(np.float32)[:, :, :3]
            image_size = image.shape
            prefix = xmlname.split(".")[-2].split('/')[-1]
            os.makedirs(os.path.join(data_output, batch), exist_ok=True)
            path = os.path.join(data_output, batch, prefix)
            try:
                tree = ET.parse(xmlname)
                root = tree.getroot()
            except:
                with open(xmlname) as f:
                    xml=f.read()
                root = ET.fromstring("<root>" + xml + "</root>")
            original_annotation={}
            for child_of_root in root.iter(tag='gate_corners'):
                corners = []
                top_left=child_of_root.find('top_left').text.split(',')
                assert len(top_left)==2
                original_annotation[0]=[image_size[0]-float(top_left[1]),float(top_left[0])]

                top_right = child_of_root.find('top_right').text.split(',')
                assert len(top_right) == 2
                original_annotation[1] = [image_size[0] - float(top_right[1]), float(top_right[0])]

                bottom_right = child_of_root.find('bottom_right').text.split(',')
                assert len(bottom_right) == 2
                original_annotation[2] = [image_size[0] - float(bottom_right[1]), float(bottom_right[0])]

                bottom_left = child_of_root.find('bottom_left').text.split(',')
                assert len(bottom_left) == 2
                original_annotation[3] = [image_size[0] - float(bottom_left[1]), float(bottom_left[0])]

                center = child_of_root.find('center').text.split(',')
                assert len(center) == 2
                original_annotation['center'] = [image_size[0] - float(center[1]), float(center[0])]

                annotation=recalculate(original_annotation,image_size)



            save_heatmap(f"{path}_0", image[::, ::], annotation)
            if batch != "valid":
                annotation1={}
                for i in annotation:
                    annotation1[i[0],image_size[1]-i[1]]=[[j[0],image_size[1]-j[1]] for j in annotation[i]]
                if not save_heatmap(f"{path}_1", image[::, ::-1], annotation1):
                    return

                annotation2={}
                for i in annotation:
                    annotation2[image_size[0]-i[0], i[1]]=[[image_size[0]-j[0], j[1]] for j in annotation[i]]
                if not save_heatmap(f"{path}_2", image[::-1, ::], annotation2):
                    return

                annotation3 ={}
                for i in annotation:
                    annotation3[image_size[0]-i[0], image_size[1]-i[1]]=[[image_size[0]-j[0], image_size[1]-j[1]] for j in annotation[i]]
                if not save_heatmap(f"{path}_3", image[::-1, ::-1], annotation3):
                    return
            print("Finishing", os.path.join(data_output, batch, prefix))

        parmap(handle, filelist, 1)
Esempio n. 6
0
def main():
    args = docopt(__doc__)
    data_root = args["<src>"]
    data_output = args["<dst>"]

    os.makedirs(data_output, exist_ok=True)
    if not os.path.exists(os.path.join(data_root, 'train')):
        print('train set not exists')
    if not os.path.exists(os.path.join(data_root, 'valid')):
        print('valid set not exists')
    for batch in ["train", "valid"]:
        filelist = glob.glob(f"{data_root}/{batch}/*.xml")
        filelist.sort()

        def handle(xmlname):
            iname = xmlname.replace("xml", "jpg")
            image = io.imread(iname).astype(np.float32)[:, :, :3]
            image_size = image.shape  # [416,416]
            prefix = xmlname.split(".")[-2].split('/')[-1]
            os.makedirs(os.path.join(data_output, batch), exist_ok=True)
            path = os.path.join(data_output, batch, prefix)
            try:
                tree = ET.parse(xmlname)
                root = tree.getroot()
            except:
                with open(xmlname) as f:
                    xml = f.read()
                root = ET.fromstring("<root>" + xml + "</root>")
            annotation = {}
            for child_class_name in root.iter(tag='object'):

                child_class = child_class_name.find('name').text
                if child_class not in annotation:
                    annotation[child_class] = {}
                for child_of_root in child_class_name.iter(tag='gate_corners'):

                    original_annotation = {}
                    top_left = child_of_root.find('top_left').text.split(',')
                    assert len(top_left) == 2
                    # convert to following coordinate system
                    # |
                    # x
                    # ↓---y--→
                    original_annotation['top_left'] = convert(
                        top_left, image_size)

                    top_right = child_of_root.find('top_right').text.split(',')
                    assert len(top_right) == 2
                    original_annotation['top_right'] = convert(
                        top_right, image_size)

                    bottom_right = child_of_root.find(
                        'bottom_right').text.split(',')
                    assert len(bottom_right) == 2
                    original_annotation['bottom_right'] = convert(
                        bottom_right, image_size)

                    bottom_left = child_of_root.find('bottom_left').text.split(
                        ',')
                    assert len(bottom_left) == 2
                    original_annotation['bottom_left'] = convert(
                        bottom_left, image_size)

                    center = child_of_root.find('center').text.split(',')
                    assert len(center) == 2
                    original_annotation['center'] = convert(center, image_size)

                    newcenter, newcorner = recalculate(original_annotation,
                                                       image_size)
                    annotation[child_class][newcenter] = newcorner

            assert len(annotation.keys()) <= 1  # only one class right now
            if len(annotation.keys()) == 0:
                obj_class = None
                annotation = []
            else:
                obj_class = list(annotation.keys())[0]
                annotation = annotation[obj_class]

            image_size = [i - 1 for i in image_size
                          ]  # to avoid index out of range error

            save_heatmap(f"{path}_0", image[::, ::], annotation)
            save_gt_txt(f"{path}_0", annotation, obj_class, image.shape)
            if batch != "valid":
                annotation1 = {}
                for i in annotation:
                    annotation1[i[0], image_size[1] -
                                i[1]] = [[j[0], image_size[1] - j[1]]
                                         for j in annotation[i]]
                if not save_heatmap(f"{path}_1", image[::, ::-1], annotation1):
                    return
                else:
                    save_gt_txt(f"{path}_1", annotation1, obj_class,
                                image.shape)

                annotation2 = {}
                for i in annotation:
                    annotation2[image_size[0] - i[0],
                                i[1]] = [[image_size[0] - j[0], j[1]]
                                         for j in annotation[i]]
                if not save_heatmap(f"{path}_2", image[::-1, ::], annotation2):
                    return
                else:
                    save_gt_txt(f"{path}_2", annotation2, obj_class,
                                image.shape)

                annotation3 = {}
                for i in annotation:
                    annotation3[image_size[0] - i[0],
                                image_size[1] - i[1]] = [[
                                    image_size[0] - j[0], image_size[1] - j[1]
                                ] for j in annotation[i]]
                if not save_heatmap(f"{path}_3", image[::-1, ::-1],
                                    annotation3):
                    return
                else:
                    save_gt_txt(f"{path}_3", annotation3, obj_class,
                                image.shape)
            print("Finishing", os.path.join(data_output, batch, prefix))

        parmap(handle, filelist, 1)
Esempio n. 7
0
def main():
    gts = sorted(glob.glob(GT))
    afm = sorted(glob.glob(AFM))
    wf = sorted(glob.glob(WF))
    img = sorted(glob.glob(IMGS))

    prefix = "/data/lcnn/wirebase/myplot/"
    os.makedirs(osp.join(prefix, "GT"), exist_ok=True)
    os.makedirs(osp.join(prefix, "LSD"), exist_ok=True)
    os.makedirs(osp.join(prefix, "AFM"), exist_ok=True)
    os.makedirs(osp.join(prefix, "WF"), exist_ok=True)
    os.makedirs(osp.join(prefix, "LL"), exist_ok=True)

    def draw(args):
        i, (wf_name, gt_name, afm_name, img_name) = args
        img = cv2.imread(img_name, 0)
        lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_ADV)
        lsd_line, _, _, lsd_score = lsd.detect(img)
        lsd_line = lsd_line.reshape(-1, 2, 2)
        lsd_score = lsd_score.flatten()
        img = cv2.imread(img_name)[:, :, ::-1]

        with np.load(gt_name) as fgt:
            gt_line = fgt["lpos"][:, :, :2]
            gt_line[:, :, 0] *= img.shape[0] / 128
            gt_line[:, :, 1] *= img.shape[1] / 128

        with np.load(afm_name) as fafm:
            afm_line = fafm["lines"].reshape(-1, 2, 2)[:, :, ::-1]

        wf_line = scipy.io.loadmat(wf_name)["lines"].reshape(-1, 2, 2)
        wf_line = wf_line[:, :, ::-1]

        plt.figure("GT")
        imshow(img)
        for a, b in gt_line - 0.5:
            plt.plot([a[1], b[1]], [a[0], b[0]], color="orange", linewidth=0.5)
            plt.scatter(a[1],
                        a[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
            plt.scatter(b[1],
                        b[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
        plt.savefig(osp.join(prefix, "GT", f"{i:05}"), dpi=3000, bbox_inches=0)
        plt.close()

        plt.figure("LSD")
        imshow(img)
        for a, b in lsd_line[:, :, ::-1] - 0.5:
            plt.plot([a[1], b[1]], [a[0], b[0]], color="orange", linewidth=0.5)
            plt.scatter(a[1],
                        a[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
            plt.scatter(b[1],
                        b[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
        plt.savefig(osp.join(prefix, "LSD", f"{i:05}"),
                    dpi=3000,
                    bbox_inches=0)
        plt.close()

        plt.figure("AFM")
        imshow(img)
        for a, b in afm_line - 0.5:
            plt.plot([a[1], b[1]], [a[0], b[0]], color="orange", linewidth=0.5)
            plt.scatter(a[1],
                        a[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
            plt.scatter(b[1],
                        b[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
        plt.savefig(osp.join(prefix, "AFM", f"{i:05}"),
                    dpi=3000,
                    bbox_inches=0)
        plt.close()

        plt.figure("WF")
        imshow(img)
        for a, b in wf_line - 0.5:
            plt.plot([a[1], b[1]], [a[0], b[0]], color="orange", linewidth=0.5)
            plt.scatter(a[1],
                        a[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
            plt.scatter(b[1],
                        b[0],
                        color="#33FFFF",
                        s=1.2,
                        edgecolors="none",
                        zorder=5)
        plt.savefig(osp.join(prefix, "WF", f"{i:05}"), dpi=3000, bbox_inches=0)
        plt.close()

    parmap(draw, enumerate(zip(wf, gts, afm, img)))
Esempio n. 8
0
def main():
    args = docopt(__doc__)

    files = sorted(glob.glob(osp.join(args["<input-dir>"], "*.npz")))
    inames = sorted(glob.glob("data/wireframe/valid-images/*.jpg"))
    gts = sorted(glob.glob("data/wireframe/valid/*.npz"))
    prefix = args["<output-dir>"]

    inputs = list(zip(files, inames, gts))
    thresholds = list(map(float, args["--thresholds"].split(",")))

    def handle(allname):
        fname, iname, gtname = allname
        print("Processing", fname)
        im = cv2.imread(iname)
        with np.load(fname) as f:
            lines = f["lines"]
            scores = f["score"]
        with np.load(gtname) as f:
            gtlines = f["lpos"][:, :, :2]
        gtlines[:, :, 0] *= im.shape[0] / 128
        gtlines[:, :, 1] *= im.shape[1] / 128
        for i in range(1, len(lines)):
            if (lines[i] == lines[0]).all():
                lines = lines[:i]
                scores = scores[:i]
                break

        lines[:, :, 0] *= im.shape[0] / 128
        lines[:, :, 1] *= im.shape[1] / 128
        diag = (im.shape[0]**2 + im.shape[1]**2)**0.5

        for threshold in thresholds:
            nlines, nscores = process(lines, scores, diag * threshold, 0,
                                      False)

            outdir = osp.join(prefix, f"{threshold:.3f}".replace(".", "_"))
            os.makedirs(outdir, exist_ok=True)
            npz_name = osp.join(outdir, osp.split(fname)[-1])

            PLTOPTS = {
                "color": "#33FFFF",
                "s": 1.2,
                "edgecolors": "none",
                "zorder": 5
            }
            if args["--plot"]:
                # plot gt
                imshow(im[:, :, ::-1])
                for (a, b) in gtlines:
                    plt.plot([a[1], b[1]], [a[0], b[0]],
                             c="orange",
                             linewidth=0.5)
                    plt.scatter(a[1], a[0], *PLTOPTS)
                    plt.scatter(b[1], b[0], *PLTOPTS)
                plt.savefig(npz_name.replace(".npz", ".png"),
                            dpi=500,
                            bbox_inches=0)

                thres = [0.97, 0.98, 0.99]
                for i, t in enumerate(thres):
                    imshow(im[:, :, ::-1])
                    for (a, b), s in zip(nlines[nscores > t],
                                         nscores[nscores > t]):
                        plt.plot([a[1], b[1]], [a[0], b[0]],
                                 c=c(s),
                                 linewidth=0.5)
                        plt.scatter(a[1], a[0], *PLTOPTS)
                        plt.scatter(b[1], b[0], *PLTOPTS)
                    plt.savefig(npz_name.replace(".npz", f"_{i}.png"),
                                dpi=500,
                                bbox_inches=0)

            nlines[:, :, 0] *= 128 / im.shape[0]
            nlines[:, :, 1] *= 128 / im.shape[1]
            np.savez_compressed(npz_name, lines=nlines, score=nscores)

    parmap(handle, inputs, 12)
def main():
    # args = docopt(__doc__)

    filespath = '/home/jasmeet/Documents/AdvanceComputerVision/PPGnet-dataset/files-outdoor'
    predpath = '/home/jasmeet/Documents/AdvanceComputerVision/PPGnet-dataset/files-outdoor/GT-outddor'

    files = sorted(glob.glob(osp.join(predpath, "*.npz")))
    inames = sorted(glob.glob(osp.join(filespath, "*.png")))
    gts = sorted(glob.glob(osp.join(filespath, "*.npz")))

    prefix = '/home/jasmeet/Documents/AdvanceComputerVision/PPGnet-dataset/files-outdoor/out'

    inputs = list(zip(files, inames, gts))
    # thresholds = list(map(float, args["--thresholds"].split(",")))
    plot = True
    thresholds = [0.006, 0.010, 0.015]

    def handle(allname):
        fname, iname, gtname = allname
        print("Processing", fname)
        im = cv2.imread(iname)
        with np.load(fname) as f:
            lines = f["lines"]
            scores = f["score"]
        with np.load(gtname) as f:
            gtlines = f["lpos"][:, :, :2]
        gtlines[:, :, 0] *= im.shape[0] / 128
        gtlines[:, :, 1] *= im.shape[1] / 128
        for i in range(1, len(lines)):
            if (lines[i] == lines[0]).all():
                lines = lines[:i]
                scores = scores[:i]
                break

        lines[:, :, 0] *= im.shape[0] / 128
        lines[:, :, 1] *= im.shape[1] / 128
        diag = (im.shape[0]**2 + im.shape[1]**2)**0.5

        for threshold in thresholds:
            nlines, nscores = postprocess(lines, scores, diag * threshold, 0,
                                          False)

            outdir = osp.join(prefix, f"{threshold:.3f}".replace(".", "_"))
            os.makedirs(outdir, exist_ok=True)
            npz_name = osp.join(outdir, osp.split(fname)[-1])

            if plot:
                # plot gt
                imshow(im[:, :, ::-1])
                for (a, b) in gtlines:
                    plt.plot([a[1], b[1]], [a[0], b[0]],
                             c="orange",
                             linewidth=0.5)
                    plt.scatter(a[1], a[0], **PLTOPTS)
                    plt.scatter(b[1], b[0], **PLTOPTS)
                plt.savefig(npz_name.replace(".npz", ".png"),
                            dpi=500,
                            bbox_inches=0)

                thres = [0.96, 0.97, 0.98, 0.99]
                for i, t in enumerate(thres):
                    imshow(im[:, :, ::-1])
                    for (a, b), s in zip(nlines[nscores > t],
                                         nscores[nscores > t]):
                        plt.plot([a[1], b[1]], [a[0], b[0]],
                                 c=c(s),
                                 linewidth=0.5)
                        plt.scatter(a[1], a[0], **PLTOPTS)
                        plt.scatter(b[1], b[0], **PLTOPTS)
                    plt.savefig(npz_name.replace(".npz", f"_{i}.png"),
                                dpi=500,
                                bbox_inches=0)

            nlines[:, :, 0] *= 128 / im.shape[0]
            nlines[:, :, 1] *= 128 / im.shape[1]
            np.savez_compressed(npz_name, lines=nlines, score=nscores)

    parmap(handle, inputs, 12)