示例#1
0
    def make_chip(self, img_name):
        image = cv2.imread(osp.join(self.img_dir, img_name))
        height, width = image.shape[:2]
        img_id = osp.splitext(osp.basename(img_name))[0]
        # mask_path = ""
        mask_path = osp.join(self.mask_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        region_box = utils.region_postprocess(region_box, contours,
                                              (mask_w, mask_h))
        region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                      (width, height))
        region_box = utils.generate_crop_region(region_box, (width, height))
        try:
            region_box = np.vstack(
                (region_box, np.array([0, 0, width - 1, height - 1])))
        except:
            print("empty box")

        if args.show:
            utils.show_image(image, region_box)

        chip_loc = self.write_chip_and_anno(image, img_id, region_box)

        return len(region_box), chip_loc
示例#2
0
    def make_chip(self, sample, imgset):
        image = cv2.imread(sample['image'])
        height, width = sample['height'], sample['width']
        img_id = osp.splitext(osp.basename(sample['image']))[0]

        mask_path = osp.join(self.segmentation_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        region_box = utils.region_postprocess(region_box, contours,
                                              (mask_w, mask_h))
        region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                      (width, height))
        region_box = utils.generate_crop_region(region_box, (width, height))

        if args.show:
            utils.show_image(image, region_box)
        # if imgset == 'train':
        #     region_box = np.vstack((region_box, np.array([0, 0, width-1, height-1])))

        gt_bboxes, gt_cls = sample['bboxes'], sample['cls']

        chip_gt_list, chip_label_list, neglect_list = self.generate_region_gt(
            region_box, gt_bboxes, gt_cls)
        chip_loc = self.write_chip_and_anno(image, img_id, imgset, region_box,
                                            chip_gt_list, chip_label_list,
                                            neglect_list)

        return chip_loc
示例#3
0
def _boxvis(img, gt_box_list):
    img1 = img.copy()
    mask = np.zeros_like(img1, dtype=np.uint8)
    chip_mask = np.zeros((30, 30, 1), dtype=np.uint8)
    vis_mask = np.zeros_like(img1, dtype=np.uint8)

    for box in gt_box_list:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 4)
        mask[box[1]:box[3], box[0]:box[2], :] = 255

        xmin, ymin, xmax, ymax = np.floor(box / 2048 * 30).astype(np.int)
        chip_mask[ymin:ymax + 1, xmin:xmax + 1] = 1

    mask_box = utils.generate_box_from_mask(chip_mask)
    for box in mask_box:
        xmin, ymin, xmax, ymax = np.floor(box / 30 * 2048).astype(np.int)
        vis_mask[ymin:ymax + 1, xmin:xmax + 1] = 255

    cv2.imwrite('vis1.jpg', img1)
    cv2.imwrite('vis2.png', vis_mask)

    plt.subplot(1, 2, 1)
    plt.imshow(img1[:, :, [2, 1, 0]])
    plt.subplot(1, 2, 2)
    plt.imshow(vis_mask)
    plt.show()
    cv2.waitKey()
示例#4
0
    def make_chip(self, img_name):
        image = cv2.imread(osp.join(self.img_dir, img_name))
        height, width = image.shape[:2]
        img_id = osp.splitext(osp.basename(img_name))[0]
        # mask_path = ""
        mask_path = osp.join(self.mask_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        # # utils.show_image(mask, np.array(region_box))
        region_box = utils.generate_crop_region(region_box, mask, (mask_w, mask_h), (width, height), self.gbm, args.aim)
        # # utils.show_image(mask, np.array(region_box))
        region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height))

        # if len(region_box) == 0:
        #     region_box = np.array([[0, 0, width, height]])
        # else:
        #     region_box = np.vstack((region_box, [0, 0, width, height]))

        if args.show:
            utils.show_image(image[..., ::-1], np.array(region_box))

        chip_loc = self.write_chip_and_anno(image, img_id, region_box)

        return chip_loc
示例#5
0
def main():
    if not os.path.exists(dest_datadir):
        os.mkdir(dest_datadir)
        os.mkdir(image_dir)
        os.makedirs(list_dir)
        os.mkdir(anno_dir)

    train_list = glob.glob(src_traindir + '/*.jpg')
    random.shuffle(train_list)
    train_list, val_list = train_list[:-2000], train_list[-2000:]
    test_list = glob.glob(src_testdir + '/*.jpg')
    test_list = [os.path.basename(x)[:-4] for x in test_list]

    chip_loc = {}
    chip_name_list = []
    for imgid in tqdm(test_list):
        origin_img = cv2.imread(os.path.join(src_testdir, '%s.jpg' % imgid))
        mask_img = cv2.imread(os.path.join(mask_path, '%s.png' % imgid),
                              cv2.IMREAD_GRAYSCALE)

        height, width = mask_img.shape[:2]
        mask_box = utils.generate_box_from_mask(mask_img)
        mask_box = list(
            map(utils.resize_box, mask_box, [(width, height)] * len(mask_box),
                [(3200, 1800)] * len(mask_box)))

        chip_list = mask_chip(mask_box, (3200, 1800))
        # utils._boxvis(cv2.resize(mask_img, (2048, 2048)), chip_list, origin_img)
        # cv2.waitKey(0)

        for i, chip in enumerate(chip_list):
            chip_img = origin_img[chip[1]:chip[3], chip[0]:chip[2], :].copy()
            # chip_img = cv2.resize(chip_img, (416, 416), cv2.INTER_AREA)
            chip_name = '%s_%d' % (imgid, i)
            cv2.imwrite(os.path.join(image_dir, '%s.jpg' % chip_name),
                        chip_img)
            chip_name_list.append(chip_name)

            chip_info = {'loc': chip}
            chip_loc[chip_name] = chip_info

    # write test txt
    with open(os.path.join(list_dir, 'test.txt'), 'w') as f:
        f.writelines([x + '\n' for x in chip_name_list])
        print('write txt.')

    # write chip loc json
    with open(os.path.join(anno_dir, 'test_chip.json'), 'w') as f:
        json.dump(chip_loc, f)
        print('write json.')
示例#6
0
def main():
    label_df = pd.read_csv(annos_path)

    label_object = []
    detect_object = []
    mask_object = []
    undetected_img = []
    pixel_num = []
    for raw_file in tqdm(glob(mask_path + '/*.png')):
        img_name = os.path.basename(raw_file)
        imgid = os.path.splitext(img_name)[0]
        label_file = os.path.join(label_path, img_name)
        image_file = os.path.join(image_path, imgid + '.jpg')

        mask_img = cv.imread(raw_file, cv.IMREAD_GRAYSCALE)
        height, width = mask_img.shape[:2]

        pixel_num.append(np.sum(mask_img))

        label_box = utils.get_label_box(label_df, imgid + '.jpg')
        mask_box = utils.generate_box_from_mask(mask_img)
        mask_box = list(
            map(utils.resize_box, mask_box, [(width, height)] * len(mask_box),
                [(3200, 1800)] * len(mask_box)))
        # mask_box = utils.enlarge_box(mask_box, (3200, 1800), ratio=1)
        # _boxvis(mask_img, mask_box)

        count = 0
        for box1 in label_box:
            for box2 in mask_box:
                if utils.overlap(box2, box1):
                    count += 1
                    break

        label_object.append(len(label_box))
        detect_object.append(count)
        mask_object.append(len(mask_box))
        if len(label_box) != count:
            undetected_img.append(imgid)

    print('recall: %f' % (np.sum(detect_object) / np.sum(label_object)))
    print('cost avg: %f, std: %f' % (np.mean(pixel_num), np.std(pixel_num)))
    print('detect box avg: %f, std %d' %
          (np.mean(mask_object), np.std(mask_object)))
示例#7
0
    def make_chip(self, sample, imgset):
        # get image and mask informations
        image = cv2.imread(sample['image'])
        height, width = sample['height'], sample['width']
        img_id = osp.splitext(osp.basename(sample['image']))[0]
        mask_path = osp.join(self.segmentation_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        region_box = utils.generate_crop_region(region_box, mask,
                                                (mask_w, mask_h),
                                                (width, height), self.gbm,
                                                args.aim)
        region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                      (width, height))

        # make tiling
        if args.tiling and imgset != "val":
            tiling = utils.add_tiling((width, height))
            for pattern in tiling:
                if utils.iou_calc1(pattern, region_box).max() < 0.85:
                    region_box = np.vstack((region_box, tiling))

        if args.show:
            utils.show_image(image[..., ::-1], np.array(region_box))

        # get box and class
        gt_bboxes, gt_cls = sample['bboxes'], sample['cls']

        # generate chip annotations and writer chip image
        chip_gt_list, chip_label_list, neglect_list = self.generate_region_gt(
            region_box, gt_bboxes, gt_cls)
        chip_loc = self.write_chip_and_anno(image, img_id, region_box,
                                            chip_gt_list, chip_label_list,
                                            neglect_list, imgset)

        return chip_loc
def _worker(img_path, dataset):
    imgid = os.path.basename(img_path)[:-4]
    image = cv2.imread(img_path)
    height, width = image.shape[:2]

    mask_path = os.path.join(segmentation_dir, imgid + '_region.png')
    mask_img = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
    mask_h, mask_w = mask_img.shape[:2]
    region_box, contours = utils.generate_box_from_mask(mask_img)
    region_box = utils.region_postprocess(region_box, contours,
                                          (mask_w, mask_h))
    region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                  (width, height))
    region_box = utils.generate_crop_region(region_box, (width, height))
    region_box = np.vstack((region_box, np.array([0, 0, width - 1,
                                                  height - 1])))

    gt_boxes, labels = dataset.get_gtbox(img_path)

    chip_list, chip_gt_list, chip_label_list = generate_region_gt(
        (width, height), region_box, gt_boxes, labels)
    chip_loc = write_chip_and_anno(image, imgid, chip_list, chip_gt_list,
                                   chip_label_list)
    return len(chip_list), chip_loc
示例#9
0
def _vis(img_path, dataset):
    img = cv2.imread(img_path)
    height, width = img.shape[:2]

    img_id = os.path.basename(img_path)[:-4]
    pred_mask_path = os.path.join(pred_mask_dir, img_id + '.png')
    label_mask_path = os.path.join(segmentation_dir, img_id + '_region.png')
    pred_mask = cv2.imread(pred_mask_path) * 255
    label_mask = cv2.imread(label_mask_path) * 255

    # bounding box
    img1 = img.copy()
    gt_box_list, _ = dataset.get_gtbox(img_path)
    for box in gt_box_list:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 4)
        cv2.putText(img1, str((box[2], box[3])), (box[0], box[3]),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4)
    cv2.putText(img1, str((width, height)), (100, 100),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4)
    label_region_box, _ = utils.generate_box_from_mask(label_mask[:, :, 0])
    print('box', gt_box_list)
    print('region', label_region_box)
    label_region_box = utils.resize_box(label_region_box, (40, 30),
                                        (width, height))
    for box in label_region_box:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 5)

    # region box
    img2 = img.copy()
    mask_h, mask_w = pred_mask.shape[:2]
    region_box, contours = utils.generate_box_from_mask(pred_mask[:, :, 0])
    resize_region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                         (width, height))
    for box in resize_region_box:
        cv2.rectangle(img2, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 5)

    # region postprocess
    img3 = img.copy()
    new_regions = utils.region_postprocess(region_box, contours,
                                           (mask_w, mask_h))
    resize_region_box = utils.resize_box(new_regions, (mask_w, mask_h),
                                         (width, height))
    # new_regions = utils.generate_crop_region(resize_region_box, (width, height))
    for box in resize_region_box:
        cv2.rectangle(img3, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 5)

    img4 = img.copy()
    # resize_region_box = utils.resize_box(temp, (mask_w, mask_h), (width, height))
    new_regions = utils.generate_crop_region(resize_region_box,
                                             (width, height))
    for box in new_regions:
        cv2.rectangle(img4, (box[0], box[1]), (box[2], box[3]), (255, 0, 0),
                      10)

    plt.subplot(1, 1, 1)
    plt.imshow(img1[:, :, [2, 1, 0]])
    # plt.subplot(2, 3, 2); plt.imshow(img2[:, :, [2,1,0]])
    # plt.subplot(2, 3, 3); plt.imshow(img3[:, :, [2,1,0]])
    # plt.subplot(2, 3, 4); plt.imshow(label_mask[:, :, [2,1,0]])
    # plt.subplot(2, 3, 5); plt.imshow(pred_mask[:, :, [2,1,0]])
    # plt.subplot(2, 3, 6); plt.imshow(img4[:, :, [2,1,0]])

    plt.show()
    cv2.waitKey(0)
示例#10
0
    mask_object = []
    undetected_img = []
    pixel_num = []
    for img_path in tqdm(val_list, ncols=80):
        img_name = os.path.basename(img_path)
        raw_file = os.path.join(mask_path, img_name[:-4]+'.png')
        
        img = cv2.imread(img_path)
        height, width = img.shape[:2]
        mask_img = cv2.imread(raw_file, cv2.IMREAD_GRAYSCALE)
        mask_h, mask_w = mask_img.shape[:2]
        
        pixel_num.append(np.sum(mask_img))

        label_box, _ = dataset.get_gtbox(img_path)
        region_box, contours = utils.generate_box_from_mask(mask_img)
        region_box = utils.region_postprocess(region_box, contours, (mask_w, mask_h))
        region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height))
        region_box = utils.generate_crop_region(region_box, (width, height))

        count = 0
        for box1 in label_box:
            for box2 in region_box:
                if utils.overlap(box2, box1):
                    count += 1
                    break

        label_object.append(len(label_box))
        detect_object.append(count)
        mask_object.append(len(region_box))
        if len(label_box) != count:
示例#11
0
def _vis(img_path, dataset):
    img = cv2.imread(img_path)
    height, width = img.shape[:2]

    img_id = os.path.basename(img_path)[:-4]
    pred_mask_path = os.path.join(pred_mask_dir, img_id+'.png')
    label_mask_path = os.path.join(segmentation_dir, img_id+'_region.png')
    pred_mask = cv2.imread(pred_mask_path) * 255
    label_mask = cv2.imread(label_mask_path) * 255

    # bounding box
    img1 = img.copy()
    gt_box_list, _ = dataset.get_gtbox(img_path)
    for box in gt_box_list:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 127, 0), 3)
    #     cv2.putText(img1, str((box[2], box[3])), (box[0], box[3]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 4)
    # cv2.putText(img1, str((width, height)), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 4)
    # label_region_box, _ = utils.generate_box_from_mask(label_mask[:, :, 0])
    # label_region_box = utils.resize_box(label_region_box, (40, 30), (width, height))
    # for box in label_region_box:
    #     cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 5)

    # region box
    img2 = img.copy()
    mask_h, mask_w = pred_mask.shape[:2]
    region_box, contours = utils.generate_box_from_mask(pred_mask[:, :, 0])
    resize_region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height))
    for box in resize_region_box:
        cv2.rectangle(img2, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 4)

    # region postprocess
    img3 = img.copy()
    new_regions = utils.region_postprocess(region_box, contours, (mask_w, mask_h))
    resize_region_box = utils.resize_box(new_regions, (mask_w, mask_h), (width, height))
    # new_regions = utils.generate_crop_region(resize_region_box, (width, height))
    for box in resize_region_box:
        cv2.rectangle(img3, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 4)

    img4 = img.copy()
    # resize_region_box = utils.resize_box(temp, (mask_w, mask_h), (width, height))
    new_regions = utils.generate_crop_region(resize_region_box, (width, height))
    chip_list = []
    for box in new_regions:
        cv2.rectangle(img4, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 7)
        chip_list.append(img[box[1]:box[3], box[0]:box[2], :].copy())

    # heat map
    img5 = img.copy().astype(np.float64)
    pred_mask_path = os.path.join(pred_mask_dir, img_id+'_heat.png')
    heat_mask = cv2.imread(pred_mask_path, cv2.IMREAD_GRAYSCALE)
    heat_mask = cv2.resize(heat_mask, (width, height), interpolation=cv2.INTER_NEAREST)
    img5[:, :, 2] += heat_mask / 255 * 150
    

    plt.subplot(2, 3, 1); plt.imshow(img1[:, :, [2,1,0]])
    plt.subplot(2, 3, 2); plt.imshow(img2[:, :, [2,1,0]])
    plt.subplot(2, 3, 3); plt.imshow(img3[:, :, [2,1,0]])
    plt.subplot(2, 3, 4); plt.imshow(label_mask[:, :, [2,1,0]])
    plt.subplot(2, 3, 5); plt.imshow(pred_mask[:, :, [2,1,0]])
    plt.subplot(2, 3, 6); plt.imshow(img4[:, :, [2,1,0]])

    dirname = os.path.join(pred_mask_dir, os.path.pardir)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'image.jpg'), img)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'bbox.jpg'), img1)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'brec.jpg'), img2)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'post_process.jpg'), img3)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'label_mask.jpg'), label_mask)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'pred_mask.jpg'), pred_mask)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'result.jpg'), img4)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'heat.jpg'), img5)
    for i, chip in enumerate(chip_list):
        cv2.imwrite(os.path.join(dirname, 'virtualization', 'chip_%d.jpg' % i), chip)

    
    plt.show()
    cv2.waitKey(0)