def visualize_result(data, preds, args):
    colors = loadmat('data/color150.mat')['colors']
    (img, seg, info) = data

    # segmentation
    seg_color = colorEncode(seg, colors)

    # prediction
    pred_color = colorEncode(preds, colors)

    # aggregate images and save
    im_vis = np.concatenate((img, seg_color, pred_color),
                            axis=1).astype(np.uint8)

    img_name = info.split('/')[-1]
    cv2.imwrite(os.path.join(args.result,
                img_name.replace('.jpg', '.png')), im_vis)
Ejemplo n.º 2
0
    def segmentation_frame(self, img, frame_id, stamp):
        #image = cv2.resize(image, (height, width))
        ori_height, ori_width, _ = img.shape
        print ('ori image ', ori_height, ori_width)

        # to avoid rounding in network
        target_height = self.round2nearest_multiple(ori_height, self.padding_constant)
        target_width = self.round2nearest_multiple(ori_width, self.padding_constant)

        # resize
        image = cv2.resize(img.copy(), (target_width, target_height))

        pub_image = CvBridge().cv2_to_imgmsg(image, "bgr8")
        pub_image.header.frame_id = frame_id
        pub_image.header.stamp = stamp
        self.image_raw_pub.publish(pub_image)

        # image transform
        image = self.img_transform(image).cuda()

        image = torch.unsqueeze(image, 0)

        #image = torch.tensor(image).

        print ('image ', image.shape)

        #segSize = (image.shape[2],image.shape[3])
        segSize = (target_height, target_width)
        scores = torch.zeros(1, args.num_class, segSize[0], segSize[1])
        scores = async_copy_to(scores, args.gpu)
        feed_dict = {}
        feed_dict['img_data'] = image
        # forward pass
        pred_tmp = self.segmentation_module(feed_dict, segSize=segSize)
        scores = scores + pred_tmp
        _, pred = torch.max(scores, dim=1)
        pred = as_numpy(pred.squeeze(0).cpu())
        print ('pred ', pred.shape)
        pred_color = colorEncode(pred, colors).astype(np.uint8)
        pub_image = CvBridge().cv2_to_imgmsg(pred_color, "bgr8")
        pub_image.header.frame_id = frame_id
        pub_image.header.stamp = stamp
        self.image_pub.publish(pub_image)
Ejemplo n.º 3
0
def visualize_result(data, pred, args):
    (img, info) = data

    # prediction
    #pred = pred + 1
    pred_color = colorEncode(pred, colors,mode="BGR")
    
	
    #print(pred.shape)
    #cv2.imwrite("gray.png",pred)
#     pred = cv2.cvtColor(pred, cv2.COLOR_GRAY2RGB)

    # aggregate images and save
    im_vis = np.concatenate((img, pred_color),
                            axis=1).astype(np.uint8) #pred_color
	
    img_name = info.split('/')[-1]
    cv2.imwrite(os.path.join(args.result,
                img_name.replace('.jpg', '.png')), im_vis)
def visualize_test_result(img, pred, args):
    colors = loadmat('data/color150.mat')['colors']
    # recover image
    img = img[0]
    pred = pred.data.cpu()[0]
    for t, m, s in zip(img,
                       [0.485, 0.456, 0.406],
                       [0.229, 0.224, 0.225]):
        t.mul_(s).add_(m)
    img = (img.numpy().transpose((1, 2, 0)) * 255).astype(np.uint8)

    # prediction
    pred_ = np.argmax(pred.numpy(), axis=0)
    pred_color = colorEncode(pred_, colors)

    # aggregate images and save
    im_vis = np.concatenate((img, pred_color), axis=1).astype(np.uint8)
    imsave(os.path.join(args.result,
                        os.path.basename(args.test_img) + '.png'),
           im_vis)
Ejemplo n.º 5
0
def main(cfg, gpu, visualise, resolution):
    torch.cuda.set_device(gpu)
    segmentation_module = module_init(cfg)

    print('Successfully initialised segmentation module')
    # Load color map
    colors = loadmat('data/matlab.mat')['colors']

    # Setup the webcam
    cap = cv2.VideoCapture(0)
    width, height = STD_DIMENSIONS[resolution]
    set_resolution(cap, width, height)
    print('Successfully set up camera')

    fps = 0

    while True:
        fps += 1
        ret, frame = cap.read()  #Capture each frame
        # Convert to numpy.ndarray
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Hand segmentation API
        pred = hand_segmentation(frame, segmentation_module)

        # Visualisation
        if visualise:
            pred_color = colorEncode(pred, colors).astype(np.uint8)
            im_vis = np.concatenate((frame, pred_color), axis=1)
            im_vis = cv2.cvtColor(im_vis, cv2.COLOR_RGB2BGR)
            cv2.imshow("INFERENCE", im_vis)
        else:
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            cv2.imshow("INFERENCE", frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyWindow("INFERENCE")
Ejemplo n.º 6
0
def visualize_result(img_dir, pred):
    #
    img = cv2.imread(img_dir)
    colors = loadmat('demo/color150.mat')['colors']
    names = {
        1: "耕地",
        2: "林地",
        3: "草地",
        4: "道路",
        5: "城镇建设用地",
        6: "农村建设用地",
        7: "工业用地",
        8: "构筑物",
        9: "水域",
        10: "裸地"
    }
    # print predictions in descending order
    pred = np.int32(pred)
    pixs = pred.size
    uniques, counts = np.unique(pred, return_counts=True)
    #
    for idx in np.argsort(counts)[::-1]:
        name = names[uniques[idx] + 1]
        ratio = counts[idx] / pixs * 100
        if ratio > 0.1:
            print("  {}: {:.2f}%".format(name, ratio))

    # colorize prediction
    pred_color = colorEncode(pred, colors).astype(np.uint8)

    # aggregate images and save
    #print(pred_color.shape)
    #pred_color=cv2.resize(pred_color,(256,256))
    im_vis = np.concatenate((img, pred_color), axis=1)

    #
    #img_name=image_demo_dir.split('/')[-1]
    save_dir, name = os.path.split(img_dir)
    Image.fromarray(im_vis).save('demo/256x256_deeplab_44.png')
Ejemplo n.º 7
0
    def visualize_result(self, data, pred, cfg):
        (img, info) = data

        # print predictions in descending order
        pred = np.int32(pred)
        pixs = pred.size
        uniques, counts = np.unique(pred, return_counts=True)
        print("Predictions in [{}]:".format(info))
        for idx in np.argsort(counts)[::-1]:
            name = names[uniques[idx] + 1]
            ratio = counts[idx] / pixs * 100
            if ratio > 0.1:
                print("  {}: {:.2f}%".format(name, ratio))

        # colorize prediction
        pred_color = colorEncode(pred, colors).astype(np.uint8)

        # aggregate images and publish
        im_vis = np.concatenate((img, pred_color), axis=1)

        img_msg = self.bridge.cv2_to_imgmsg(im_vis, encoding='rgb8')
        self.seg_pub.publish(img_msg)
Ejemplo n.º 8
0
def visualize_result(data, pred, cfg):
    (img, info) = data

    # print predictions in descending order
    pred = np.int32(pred)
    pixs = pred.size
    uniques, counts = np.unique(pred, return_counts=True)
    print("Predictions in [{}]:".format(info))
    for idx in np.argsort(counts)[::-1]:
        name = names[uniques[idx] + 1]
        ratio = counts[idx] / pixs * 100
        if ratio > 0.1:
            print("  {}: {:.2f}%".format(name, ratio))

    # colorize prediction
    pred_color = colorEncode(pred, colors).astype(np.uint8)

    # aggregate images and save
    im_vis = np.concatenate((img, pred_color), axis=1)

    img_name = info.split('/')[-1]
    Image.fromarray(im_vis).save(
        os.path.join(cfg.TEST.result, img_name.replace('.jpg', '.png')))
Ejemplo n.º 9
0
def encode_save_image(arr, path):
    # encode array
    img_color = colorEncode(arr, colors)

    Image.fromarray(img_color).save(path)
Ejemplo n.º 10
0
def save_img(img, pred):
    pred[pred != 2] = 0
    pred[pred == 2] = 1
    pred = np.int32(pred)
    res_img = colorEncode(pred, colors).astype(np.uint8)
    Image.fromarray(res_img).save(os.path.join(cwd, RES_FILE_NAME))
Ejemplo n.º 11
0
        img_resized = torch.unsqueeze(img_resized, 0)
        img_resized_list.append(img_resized)
    input = dict()
    input['img_ori'] = img.copy()
    input['img_data'] = [x.contiguous() for x in img_resized_list]

    segSize = (img.shape[0],img.shape[1])

    with torch.no_grad():
        pred = torch.zeros(1, args.num_class, segSize[0], segSize[1])
        for timg in img_resized_list:
            feed_dict = dict()
            feed_dict['img_data'] = timg.cuda()
            feed_dict = async_copy_to(feed_dict, args.gpu_id)
            # forward pass
            pred_tmp = segmentation_module(feed_dict, segSize=segSize)
            pred = pred + pred_tmp.cpu() / len(args.imgSize)

        _, preds = torch.max(pred, dim=1)
        preds = as_numpy(preds.squeeze(0))

    # prediction
    pred_color = colorEncode(preds, colors)

    # aggregate images and save
    blended = overlay(img,pred_color)
    im_vis = np.concatenate((img, pred_color, blended), axis=1).astype(np.uint8)
    cv2.imwrite(ovf, im_vis)
    cv2.imwrite(of, preds)