Example #1
0
def main():
    parser = init()
    args = parser.parse_args()
    # update the config options with the config file
    cfg.merge_from_file(args.config_file)
    # manual override some options
    cfg.merge_from_list(["MODEL.DEVICE", "cpu"])
    # cfg.merge_from_list(["MODEL.MASK_ON", False])
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )
    if args.weight is not None:
        checkpoint = torch.load(args.weight)
        load_state_dict(coco_demo.model, checkpoint.pop("model"))
        del checkpoint
    start_time = time.time()
    img = cv2.imread(args.img)
    composite = coco_demo.run_on_opencv_image(img)
    print("Time: {:.2f} s / img".format(time.time() - start_time))
    #     plt.figure()
    plt.imshow(composite)
    plt.savefig("result.jpg")
Example #2
0
def main():
    args = parser.parse_args()
    cfg.merge_from_file(args.config)
    cfg.merge_from_list(["MODEL.DEVICE", "cuda:0"])

    coco_demo = COCODemo(
        cfg,
        min_image_size=2048,
        confidence_threshold=0.7,
    )

    image = load_from_file(args.data)
    batch_id = 1
    elapsed = 0.
    totaltime = 0.
    while True:
        try:
            start_time = time.time()
            predictions = coco_demo.run_on_opencv_image(image)
            elapsed += time.time() - start_time
            time.sleep(args.interval)
            totaltime += time.time() - start_time
            if batch_id % args.freq == 0:
                print(
                    "Batch #{}: inference time: {:>3.3f}s, per image: {:>3.4f}s, total time: {:>3.3f}s inference percent: {:>3.0f}%"
                    .format(batch_id, elapsed, elapsed / args.freq, totaltime,
                            100. * elapsed / totaltime))
                if args.output:
                    if not os.path.exists(args.output):
                        os.makedirs(args.output)
                    filename = os.path.join(
                        args.output, 'prediction_{}.jpg'.format(batch_id))
                    print("=> Saving prediction to {}".format(filename))
                    cv2.imwrite(filename, predictions)
                elapsed = 0.
                totaltime = 0.
            batch_id += 1
        except KeyboardInterrupt:
            print("Interrupted.")
            break
Example #3
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Inference")
    parser.add_argument('--config', default='/output/tf_dir/config.yml')
    parser.add_argument('--model_weights',
                        default='/output/tf_dir/model_final.pth')
    parser.add_argument('--input_root', default='/input0/train_0712/images/')
    parser.add_argument('--output_dir', default='./predictions')
    args = parser.parse_args()
    # update the config options with the config file
    cfg.merge_from_file(args.config)
    # manual override some options
    cfg.merge_from_list(["MODEL.DEVICE", "cpu"])

    coco_demo = COCODemo(cfg,
                         min_image_size=800,
                         confidence_threshold=0.7,
                         weight_loading=args.model_weights)
    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)
    for img_name in tqdm(os.listdir(args.input_root)[:10]):
        img = load(os.path.join(args.input_root, img_name))
        predictions = coco_demo.run_on_opencv_image(img)
        cv2.imwrite('%s/%s' % (args.output_dir, img_name), predictions)
Example #4
0
def remove_background(input_path):
    config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"

    # update the config options with the config file
    cfg.merge_from_file(config_file)
    # manual override some options
    cfg.merge_from_list(["MODEL.DEVICE", "cuda"])

    image = cv2.imread(input_path, cv2.IMREAD_COLOR)

    coco_demo = COCODemo(
        cfg,
        min_image_size=800,
        confidence_threshold=0.7,
    )
    predictions, isfind = coco_demo.run_on_opencv_image(image,
                                                        category=3,
                                                        is_crop=True)

    img = resize2SquareKeepingAspectRation(predictions, 256, cv2.INTER_AREA)

    final_path = "temp_no_background.png"
    cv2.imwrite(final_path, img)
    return final_path
Example #5
0
    image = np.array(pil_image)[:, :, [2, 1, 0]]
    return image


def imshow(img):
    plt.imshow(img[:, :, [2, 1, 0]])
    plt.axis("off")


config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"
config_file = "../configs/caffe2/e2e_mask_rcnn_R_101_FPN_1x_caffe2.yaml"

# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cpu"])

coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.7,
)
# load image and then run prediction
#image = load("http://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg")

image = Image.open('/media/HDD_4TB/MSCOCO/images/train2017/000000579043.jpg')
image = np.array(image)[:, :, [2, 1, 0]]

predictions = coco_demo.run_on_opencv_image(image)

imshow(predictions)
Example #6
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default=
        "configs/centermask/centermask_M_v2_FPN_lite_res600_ms_bs32_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default="centermask-lite-M-v2-ms-bs32-1x.pth",
        metavar="FILE",
        help="path to the trained model",
    )
    parser.add_argument(
        "--conf_th",
        type=float,
        default=0.2,
        help="confidence_threshold",
    )
    parser.add_argument('--display_text',
                        default=False,
                        type=str2bool,
                        help='Whether or not to display text (class [score])')
    parser.add_argument(
        '--display_scores',
        default=False,
        type=str2bool,
        help='Whether or not to display scores in addition to classes')
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )
    parser.add_argument('--input',
                        type=str,
                        default='datasets/coco/test2017',
                        help='images to infer. Must not use with --image_dir')
    parser.add_argument('--output_dir',
                        default='demo/results/CenterMask-Lite-M-v2',
                        help='directory to save demo results')

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.MODEL.WEIGHT = args.weights

    cfg.freeze()

    # The following per-class thresholds are computed by maximizing
    # per-class f-measure in their precision-recall curve.
    # Please see compute_thresholds_for_classes() in coco_eval.py for details.

    # demo_im_names = os.listdir(args.images_dir)

    # # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(cfg,
                         confidence_threshold=args.conf_th,
                         display_text=args.display_text,
                         display_scores=args.display_scores)

    # for im_name in demo_im_names:
    #     img = cv2.imread(os.path.join(args.images_dir, im_name))
    #     if img is None:
    #         continue
    #     start_time = time.time()
    #     composite = coco_demo.run_on_opencv_image(img)
    #     print("{}\tinference time: {:.2f}s".format(im_name, time.time() - start_time))
    #     cv2.imshow(im_name, composite)
    # print("Press any keys to exit ...")
    # cv2.waitKey()
    # cv2.destroyAllWindows()

    if os.path.isfile(args.input):
        imglist = [args.input]
    else:
        imglist = glob(os.path.join(args.input, '*'))

    num_images = len(imglist)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    for i in range(num_images):
        print('file', i)
        if os.path.splitext(imglist[i])[1] in ['.mp4', '.wmv', '.avi']:
            cap = cv2.VideoCapture(imglist[i])
            image_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            image_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

            # scale = get_target_scale(image_width, image_height, 800, 1333)
            video = cv2.VideoWriter(
                os.path.join(args.output_dir, os.path.basename(imglist[i])),
                cv2.VideoWriter_fourcc(*'mp4v'), 25.0,
                (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                 int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
            cnt = 0
            while True:
                cnt += 1
                # Capture frame-by-frame
                ret, img = cap.read()

                if ret:
                    start_time = time.time()
                    composite = coco_demo.run_on_opencv_image(img)
                    print("{} frame \tinference time: {:.2f}s".format(
                        cnt,
                        time.time() - start_time))
                    video.write(composite)
                else:
                    break
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            cap.release()
            video.release()
            print('Result file number {} saved'.format(i))
        elif os.path.splitext(imglist[i])[1] in [
                '.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm'
        ]:
            img = cv2.imread(imglist[i])
            assert img is not None
            im_name, _ = os.path.splitext(os.path.basename(imglist[i]))
            print(f"{im_name} processing...")
            start_time = time.time()
            composite = coco_demo.run_on_opencv_image(img)
            print("{}\tinference time: {:.2f}s".format(
                im_name,
                time.time() - start_time))
            save_path = os.path.join(args.output_dir, f'{im_name}_result.jpg')
            cv2.imwrite(save_path, composite)

        else:
            continue
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Image Dir Demo")
    parser.add_argument(
        "--image-dir",
        required=True,
        help="path to image dir",
    )
    parser.add_argument(
        "--output-dir",
        required=True,
        help="path to output dir",
    )
    parser.add_argument(
        "--config-file",
        default="configs/e2e_faster_rcnn_R_50_FPN_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--templete",
        default='coco',
        help="Categories templete for pretty print",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=800,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
        categories_templete=args.templete,
    )

    annotation_dir = os.path.join(args.output_dir, 'annotations')
    vis_dir = os.path.join(args.output_dir, 'vis')
    if not os.path.exists(annotation_dir):
        os.makedirs(annotation_dir)
        # os.makedirs(vis_dir)

    for f in os.listdir(args.image_dir):
        if f.endswith('.png') or f.endswith('.jpg') or f.endswith('.jpeg'):
            image_fn = os.path.join(args.image_dir, f)
            img = cv2.imread(image_fn)
            start_time = time.time()
            prediction = coco_demo.compute_prediction(img)
            json_str = coco_demo.prediction_to_json_str(f, prediction)
            ofs = open(os.path.join(annotation_dir, f + '.json'), 'w')
            ofs.write(json_str)
            ofs.close()
            composite = coco_demo.run_on_opencv_image(img)
            print("Time: {:.2f} s / img".format(time.time() - start_time))
            cv2.imshow("COCO detections", composite)
            # cv2.imwrite(os.path.join(vis_dir, f+'.png'), composite)
            if cv2.waitKey(1) == 27:
                break  # esc to quit
    cv2.destroyAllWindows()
Example #8
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="configs/fcos/fcos_R_50_FPN_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default="FCOS_R_50_FPN_1x.pth",
        metavar="FILE",
        help="path to the trained model",
    )
    parser.add_argument(
        "--images-dir",
        default="demo/images",
        metavar="DIR",
        help="path to demo images directory",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=800,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.MODEL.WEIGHT = args.weights

    cfg.freeze()

    # The following per-class thresholds are computed by maximizing
    # per-class f-measure in their precision-recall curve.
    # Please see compute_thresholds_for_classes() in coco_eval.py for details.
    thresholds_for_classes = [
        0.49211737513542175, 0.49340692162513733, 0.510103702545166,
        0.4707475006580353, 0.5197340250015259, 0.5007652044296265,
        0.5611110329627991, 0.4639902412891388, 0.4778415560722351,
        0.43332818150520325, 0.6180170178413391, 0.5248752236366272,
        0.5437473654747009, 0.5153843760490417, 0.4194680452346802,
        0.5640717148780823, 0.5087228417396545, 0.5021755695343018,
        0.5307778716087341, 0.4920770823955536, 0.5202335119247437,
        0.5715234279632568, 0.5089765191078186, 0.5422378778457642,
        0.45138806104660034, 0.49631351232528687, 0.4388565421104431,
        0.47193753719329834, 0.47037890553474426, 0.4791252017021179,
        0.45699411630630493, 0.48658522963523865, 0.4580649137496948,
        0.4603237509727478, 0.5243804454803467, 0.5235602855682373,
        0.48501554131507874, 0.5173789858818054, 0.4978085160255432,
        0.4626562297344208, 0.48144686222076416, 0.4889853894710541,
        0.4749937951564789, 0.42273756861686707, 0.47836390137672424,
        0.48752328753471375, 0.44069987535476685, 0.4241463541984558,
        0.5228247046470642, 0.4834112524986267, 0.4538525640964508,
        0.4730372428894043, 0.471712201833725, 0.5180512070655823,
        0.4671719968318939, 0.46602892875671387, 0.47536996006965637,
        0.487352192401886, 0.4771934747695923, 0.45533207058906555,
        0.43941256403923035, 0.5910647511482239, 0.554875910282135,
        0.49752360582351685, 0.6263655424118042, 0.4964958727359772,
        0.5542593002319336, 0.5049241185188293, 0.5306999087333679,
        0.5279538035392761, 0.5708096623420715, 0.524990975856781,
        0.5187852382659912, 0.41242220997810364, 0.5409807562828064,
        0.48504579067230225, 0.47305455803871155, 0.4814004898071289,
        0.42680642008781433, 0.4143834114074707
    ]

    demo_im_names = os.listdir(args.images_dir)

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_thresholds_for_classes=thresholds_for_classes,
        min_image_size=args.min_image_size)

    for im_name in demo_im_names:
        img = cv2.imread(os.path.join(args.images_dir, im_name))
        if img is None:
            continue
        start_time = time.time()
        composite = coco_demo.run_on_opencv_image(img)
        print("{}\tinference time: {:.2f}s".format(im_name,
                                                   time.time() - start_time))
        cv2.imshow(im_name, composite)
    print("Press any keys to exit ...")
    cv2.waitKey()
    cv2.destroyAllWindows()
Example #9
0
import cv2
import time

from maskrcnn_benchmark.config import cfg
from predictor import COCODemo

config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"

# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cuda"])

coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.7,
)
# load image and then run prediction
image = cv2.imread('demo_e2e_mask_rcnn_X_101_32x8d_FPN_1x.png')

start = time.time()
for i in range(100):
    print(i)
    coco_demo.run_on_opencv_image(image)

print('time taken: {}'.format(time.time() - start))
Example #10
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    # added for video
    video_path = './1.MP4'
    cam = cv2.VideoCapture(video_path)
    fps = 30
    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
    # video_writer = cv2.VideoWriter(filename='./result.avi', fourcc=fourcc, fps=fps, frameSize=(640,480))
    video_writer = cv2.VideoWriter(filename='./result2.avi',
                                   fourcc=cv2.VideoWriter_fourcc(*'MJPG'),
                                   fps=fps,
                                   frameSize=(853, 480))
    while True:
        start_time = time.time()
        ret_val, img = cam.read()
        if img is None:
            break

        composite = coco_demo.run_on_opencv_image(img)
        # cv2.imwrite('a.jpg', composite)
        print("Time: {:.2f} s / img".format(time.time() - start_time))

        # cv2.imshow("COCO detections", composite)
        # if cv2.waitKey(1) == 27:
        #     break  # esc to quit

        # added by zhangyongtao

        composite = cv2.resize(composite, (853, 480))
        video_writer.write(composite)
Example #11
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/MAL_X-101-FPN_e2e.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=800,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)

    ## model file
    cfg.MODEL.WEIGHT = '../outputs/models/model_0025000.pth'
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    start_time = time.time()
    img = cv2.imread('000000000139.jpg')
    composite = coco_demo.run_on_opencv_image(img)
    print("Time: {:.2f} s / img".format(time.time() - start_time))
    cv2.imwrite('output_000000000139.jpg', composite)
Example #12
0
    pathParts = filePath.split('/')
    fileName = pathParts[-1]
    city = pathParts[-2]
    name, ext = fileName.split('.')

    fileName_occluded_instanceSegmented = name + "_ois." + ext
    fileName_filled_IS = name + "_fis." + ext

    image_occluded = load(filePath)

    # Hallucinate the occlusions in the loaded images
    inpainted_image = generateInpaintedImage(args, netG, filePath)
    inpainted_image = np.array(inpainted_image)[:, :, [2, 1, 0]]

    # Feed images to the Mask R-CNN and produce instance segmentation annotations.
    predictions_occluded = coco_demo.run_on_opencv_image(image_occluded)
    predictions_filled = coco_demo.run_on_opencv_image(inpainted_image)

    predictions_occluded = Image.fromarray(predictions_occluded[:, :,
                                                                [2, 1, 0]])
    predictions_filled = Image.fromarray(predictions_filled[:, :, [2, 1, 0]])

    # If you would like to save the output of the instance segmentation then
    # Set the 'save_output' argument to True.
    if args.save_output:
        pathParts[-3] = pathParts[-3] + '_is_256'
        segmented_folder = '/'.join(pathParts[:-2])

        creatDirectoryIfNotExists(segmented_folder)
        city_folder = segmented_folder + '/' + city
        creatDirectoryIfNotExists(city_folder)
)

# file_root = "/root/mobilenet_maskrcnn/datasets/coco/val2014/"
file_root = "/root/mobilenet_maskrcnn/demo/yzmtest/"
file_list = os.listdir(file_root)

now = time.strftime("%Y-%m-%d-%H:%M:%s", time.localtime(time.time()))

# fileimg = "/home/heqing/maskrcnn-benchmark/demo/" + now
fileimg = "/root/mobilenet_maskrcnn/demo/" + now
os.makedirs(fileimg)
save_out = fileimg + "/"
for img_name in file_list:
    img_path = file_root + img_name
    image = cv2.imread(img_path)
    predictions = coco_demo.run_on_opencv_image(
        image)  # demo时将POSTPROCESS_MASKS关掉

    save_path = save_out + img_name
    cv2.imwrite(save_path, predictions)
    # cv2.namedWindow("img", cv2.WINDOW_NORMAL)
    # cv2.imshow("img", predictions)
    # cv2.waitKey(1)

# coco = COCO(annFile)
# catNms = ['crazing', 'inclusion', 'patches', 'pitted_surface', 'rolled-in_scale', 'scratches']
# catIds_1 = coco.getCatIds(catNms=catNms)
# with open(annFile, 'r', encoding='UTF-8') as f:
#     dataset = json.load(f)
#     for img in tqdm(dataset['images']):
#         img_name = img['file_name']
#         img_path = file_root + img_name
Example #14
0
from maskrcnn_benchmark.config import cfg
from predictor import COCODemo
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

# config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"
config_file = "/home/SelfDriving/maskrcnn/maskrcnn-benchmark/configs/e2e_faster_rcnn_R_101_FPN_1x.yaml"
# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cpu"])
cfg.MODEL.WEIGHT = "/data6/SRIP_SelfDriving/Outputs/model_final.pth"

coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.7,
)
# load image and then run prediction
image = Image.open('test2.jpg').convert("RGB")
predictions = coco_demo.run_on_opencv_image(np.array(image))
plt.imshow(predictions)
plt.axis('off')
plt.savefig('prediction.png')
Example #15
0
def main():
    parser = argparse.ArgumentParser(description="DPNet Demo")
    parser.add_argument(
        "--config-file",
        default="configs/e2e_mask_rcnn_R_101_FPN_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--images_dir",
        required=True,
        type=str,
        help="path to images file",
    )
    parser.add_argument(
        "--save_dir",
        default='rpc_results',
        type=str,
        help="path to images file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=800,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()
    #509 391 788 670
    #564 391
    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=0.9,  #args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )
    if not os.path.exists(args.save_dir):
        os.mkdir(args.save_dir)
    '''
	img = cv2.imread('test_img/112.jpg')
	composite = coco_demo.run_on_opencv_image(img, False)

	cv2.imwrite(os.path.join(args.save_dir, '111.png'), composite)
	'''
    #images = load_obj('../../test_img/imageres_ori_0.obj')
    ori_images = load_obj('test_img/imageres_ori_0.obj')
    #images = images * 255
    #images = images.astype(np.uint8)
    #images = images.transpose(0,2,3,1)
    ori_images = ori_images * 255
    ori_images = ori_images.astype(np.uint8)
    ori_images = ori_images.transpose(0, 2, 3, 1)
    '''
	for i, img in enumerate(images):
		print('fake_', i)
		composite = coco_demo.run_on_opencv_image(img,False)
		cv2.imwrite(os.path.join(args.save_dir, '0_' + str(i) + '.png'), composite)
	'''
    for i, img in enumerate(ori_images):
        print('ori_', i)
        composite = coco_demo.run_on_opencv_image(img, False)
        cv2.imwrite(os.path.join(args.save_dir, 'ori_0_' + str(i) + '.png'),
                    composite)
Example #16
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="configs/fcos/fcos_syncbn_bs32_c128_MNV2_FPN_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default="FCOS_syncbn_bs32_c128_MNV2_FPN_1x.pth",
        metavar="FILE",
        help="path to the trained model",
    )
    parser.add_argument(
        "--images-dir",
        default="demo/images",
        metavar="DIR",
        help="path to demo images directory",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=800,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.MODEL.WEIGHT = args.weights

    cfg.freeze()

    # The following per-class thresholds are computed by maximizing
    # per-class f-measure in their precision-recall curve.
    # Please see compute_thresholds_for_classes() in coco_eval.py for details.
    thresholds_for_classes = [
        0.4923645853996277, 0.4928510785102844, 0.5040897727012634,
        0.4912887513637543, 0.5016880631446838, 0.5278812646865845,
        0.5351834893226624, 0.5003424882888794, 0.4955945909023285,
        0.43564629554748535, 0.6089804172515869, 0.666087806224823,
        0.5932040214538574, 0.48406165838241577, 0.4062422513961792,
        0.5571075081825256, 0.5671307444572449, 0.5268378257751465,
        0.5112953186035156, 0.4647842049598694, 0.5324517488479614,
        0.5795850157737732, 0.5152440071105957, 0.5280804634094238,
        0.4791383445262909, 0.5261335372924805, 0.4906163215637207,
        0.523737907409668, 0.47027698159217834, 0.5103300213813782,
        0.4645252823829651, 0.5384289026260376, 0.47796186804771423,
        0.4403403103351593, 0.5101461410522461, 0.5535093545913696,
        0.48472103476524353, 0.5006796717643738, 0.5485560894012451,
        0.4863888621330261, 0.5061569809913635, 0.5235867500305176,
        0.4745445251464844, 0.4652363359928131, 0.4162440598011017,
        0.5252017974853516, 0.42710989713668823, 0.4550687372684479,
        0.4943239390850067, 0.4810051918029785, 0.47629663348197937,
        0.46629616618156433, 0.4662836790084839, 0.4854755401611328,
        0.4156557023525238, 0.4763634502887726, 0.4724511504173279,
        0.4915047585964203, 0.5006274580955505, 0.5124194622039795,
        0.47004589438438416, 0.5374764204025269, 0.5876904129981995,
        0.49395060539245605, 0.5102297067642212, 0.46571290493011475,
        0.5164387822151184, 0.540651798248291, 0.5323763489723206,
        0.5048757195472717, 0.5302401781082153, 0.48333442211151123,
        0.5109739303588867, 0.4077408015727997, 0.5764586925506592,
        0.5109297037124634, 0.4685552418231964, 0.5148998498916626,
        0.4224434792995453, 0.4998510777950287
    ]

    demo_im_names = os.listdir(args.images_dir)

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_thresholds_for_classes=thresholds_for_classes,
        min_image_size=args.min_image_size)
    i = 0
    for im_name in demo_im_names:
        img = cv2.imread(os.path.join(args.images_dir, im_name))
        if img is None:
            continue
        start_time = time.time()
        composite = coco_demo.run_on_opencv_image(img)
        i = i + 1
        print(i)
        print("{}\tinference time: {:.2f}s".format(im_name,
                                                   time.time() - start_time))
Example #17
0
    confidence_threshold=0.3,
)

if __name__ == '__main__':
    frame_path = './Data/Frames'
    feat_path = './Data/roi_feat_fc'
    vid_names = os.listdir(frame_path)

    roi_feats_h5 = h5py.File(os.path.join(feat_path, 'roi_feats.h5'), 'w')
    roi_box_h5 = h5py.File(os.path.join(feat_path, 'roi_box.h5'), 'w')

    for cnt, vid in enumerate(vid_names):
        print('{}/({}, {})'.format(cnt, opt.start_idx, opt.end_idx))
        curr_fr_path = os.path.join(frame_path, vid)

        for i in range(14, 15):
            img = cv2.imread(os.path.join(curr_fr_path, str(i) + '.jpg'))
            result, top_preds, top_roi_feats = coco_demo.run_on_opencv_image(
                img)
            if top_roi_feats.shape[0] > 0:
                roi_feats_h5.create_dataset(vid,
                                            data=top_roi_feats.numpy(),
                                            dtype='f4')
                roi_box_h5.create_dataset(vid,
                                          data=top_preds.bbox.numpy(),
                                          dtype='f4')

    print('done')
    roi_feats_h5.close()
    roi_box_h5.close()
Example #18
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        # default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml",
        default="../configs/e2e_mask_rcnn_R_50_FPN_wolf.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    save_file = os.path.join("..", cfg.OUTPUT_DIR, "last_checkpoint")
    # print("save_file: " ,save_file)

    try:
        with open(save_file, "r") as f:
            last_saved = f.read()
            last_saved = last_saved.strip()
            last_saved = os.path.join("..", last_saved)
    except IOError:
        # if file doesn't exist, maybe because it has just been
        # deleted by a separate process
        last_saved = None

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(cfg,
                         confidence_threshold=args.confidence_threshold,
                         show_mask_heatmaps=args.show_mask_heatmaps,
                         masks_per_dim=args.masks_per_dim,
                         min_image_size=args.min_image_size,
                         weight_loading=last_saved)

    train_images = glob("../../dataset/robot_images/*.jpg")
    test_images = glob("../../dataset/tripod_images/*.jpg")

    start_time = time.time()
    rand_img = random.choice(train_images)
    print('images: {}'.format(rand_img))
    img = cv2.imread(rand_img)

    composite = coco_demo.run_on_opencv_image(img)
    print("Time: {:.2f} s / img".format(time.time() - start_time))
    cv2.imshow("COCO detections", composite)

    cv2.waitKey(0)
# for img_path in img_list:
#     image = cv2.imread(img_path) # imread returns BGR output
#     predictions = demo.run_on_opencv_image(image)
#     predictions = predictions[:, :, ::-1]
#     save_path = os.path.join(args.vis_dir, os.path.basename(img_path).rstrip('.%s' % args.postfix) + '.png')
#     if not os.path.isdir(os.path.dirname(save_path)):
#         os.makedirs(os.path.dirname(save_path))
#     # imsave(save_path, predictions) # imsave is assuming RGB input
#     cv2.imwrite(save_path, predictions[:, :, ::-1])

# loop inside a dataset
img_list = glob.glob(args.data_dir + '/*.%s' % args.postfix)
rand_idx = np.arange(len(img_list))
np.random.shuffle(rand_idx)
img_list = np.array(img_list)[rand_idx[0:args.img_num]].tolist()
im_list, im_cap = fu.initHTML(len(img_list), 1)
for img_cursor, img_path in tqdm(enumerate(img_list), total=len(img_list)):
    img = cv2.imread(img_path)  # imread returns BGR output
    predictions = demo.run_on_opencv_image(img)
    img_name = os.path.basename(img_path).rstrip('.%s' % args.postfix) + '.png'
    save_path = os.path.join(args.vis_dir, 'imgs', img_name)
    if not os.path.isdir(os.path.dirname(save_path)):
        os.makedirs(os.path.dirname(save_path))
    cv2.imwrite(save_path, predictions)
    im_list[img_cursor][0] = os.path.relpath(save_path, args.vis_dir)
    im_cap[img_cursor][0] = img_name

html_path = os.path.join(args.vis_dir, 'vis.html')
fu.writeHTML(html_path, im_list, im_cap)
print('Done.')
Example #20
0
if os.path.isdir(dir_save):
    shutil.rmtree(dir_save)

if os.path.isdir(dir_pred_save):
    shutil.rmtree(dir_pred_save)

os.makedirs(dir_save)
os.makedirs(dir_pred_save)

for root, dirs, filenames in os.walk(dir_coco_test):
    filenames.sort()

    for ind, name in enumerate(filenames):
        if not name.endswith('.jpg'):
            continue

        img_path = os.path.join(root, name)
        img = cv2.imread(img_path)
        fname_base = name.split(".")[0]
        masked_image = coco_demo.run_on_opencv_image(
            img,
            os.path.join(dir_pred_save, "%s_vector_results.json" % fname_base),
            dir_coco_test)

        if masked_image is None:
            continue

        path_save = os.path.join(dir_save, name)
        cv2.imwrite(path_save, masked_image)
Example #21
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default=
        "../configs/caffe2/e2e_mask_rcnn_X-152-32x8d-FPN-IN5k_1.44x_caffe2.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.9,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )
    """
    cam = cv2.VideoCapture("./test.mp4")
    for i in range(10):
        start_time = time.time()
        ret_val, img = cam.read()
        composite = coco_demo.run_on_opencv_image(img)
        print("Time: {:.2f} s / img".format(time.time() - start_time))
        # cv2.imshow("COCO detections", composite)
        cv2.imwrite("./output_mask/photo_{}.jpg".format(i), composite)
        # if cv2.waitKey(1) == 27:
        #     break  # esc to quit
    # cv2.destroyAllWindows()
    """
    testdata_path = "./test_data"
    test_images = [
        f for f in os.listdir(testdata_path) if
        os.path.isfile(os.path.join(testdata_path, f)) and f.endswith('.jpg')
    ]
    samples = sorted(test_images)
    print(samples)
    for i in range(len(samples)):
        filename = samples[i]
        print('Start processing image: {}'.format(filename))
        img = cv2.imread(os.path.join(testdata_path, filename))
        composite = coco_demo.run_on_opencv_image(img)
        print(img.shape)
        print(composite.shape)
        cv2.imwrite("./output_mask1/photo_{}.jpg".format(i), composite)
Example #22
0
def main():
    parser = argparse.ArgumentParser(description="DPNet Demo")
    parser.add_argument(
        "--config-file",
        default="configs/predict.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--images_dir",
        required=True,
        type=str,
        help="path to images file",
    )
    parser.add_argument(
        "--save_dir",
        default='rpc_results',
        type=str,
        help="path to images file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=512,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=0.7,  #args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )
    if not os.path.exists(args.save_dir):
        os.mkdir(args.save_dir)

    image_paths = glob.glob(os.path.join(args.images_dir, '*.png'))

    test = False
    dic = {}
    pic_path = '/media/dsg3/datasets/rpc/train2019/'
    pic_info = "../../dataset.txt"
    name2label = get_pic_and_label(pic_path, pic_info)
    patched = True
    #patch = load_obj('/media/dsg3/caobowen/result/correct2/patch_pkl/134_.pkl')
    for image_path in tqdm(image_paths):
        img = cv2.imread(image_path)
        if img is None:
            print(image_path)
            continue
        if test:
            composite, labels = coco_demo.run_on_opencv_image(img, test)
        else:
            if patched:

                composite = coco_demo.run_on_opencv_image(img, test)
        name = image_path.split('/')[-1]
        if test:
            if name[0] != 'f':
                real_label = name2label[name]
                if not dic.__contains__(name):
                    dic[name] = {}
                dic[name]['y'] = real_label
                dic[name]['real_label'] = labels
            else:
                if not dic.__contains__(name[5:]):
                    dic[name[5:]] = {}
                dic[name[5:]]['fake_label'] = labels
        cv2.imwrite(os.path.join(args.save_dir, os.path.basename(image_path)),
                    composite)
Example #23
0
def dataloader(img):
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/e2e_keypoint_rcnn_R_50_FPN_1x_caffe2.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
            "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    pred=coco_demo.run_on_opencv_image(img)

    keypoints=pred.get_field("keypoints")

    feature=keypoints.get_field("feature")

    return keypoints.keypoints,feature
Example #24
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
        weight_loading='best_model/model_best.pth',
    )

    images = files_with_ext('test', '.jpg')

    for image in images:

        start_time = time.time()
        img = cv2.imread(image)
        #        ret_val, img = cam.read()
        composite = coco_demo.run_on_opencv_image(img)
        print("Time: {:.2f} s / img".format(time.time() - start_time))
        output_path = os.path.join('test/' + 'output_' +
                                   os.path.basename(image))
        print(output_path)
        #    cv2.imshow("COCO detections", composite)
        cv2.imwrite(output_path, composite)
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
            "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )
    parser.add_argument(
        "--img",
        type=str,
        help="path to the target image",
        default=None,
    )
    parser.add_argument(
        "--output_dir",
        type=str,
        help="path to the target image",
        default=None,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    if not ((os.path.exists(args.img) or os.path.isdir(args.img))):
        assert(0), "Image or Dir: {} not found.".format(args.img)

    if not os.path.isdir(args.output_dir):
        os.mkdir(args.output_dir)

    if os.path.isdir(args.img):
        imgset = []
        imgset.extend(glob.glob(os.path.join(args.img, '*.jpg')))
        imgset.extend(glob.glob(os.path.join(args.img, '*.JPG')))
        imgset.extend(glob.glob(os.path.join(args.img, '*.png')))
        imgset.extend(glob.glob(os.path.join(args.img, '*.PNG')))
        random.shuffle(imgset) 
        for iximg, img in enumerate(imgset):
            start_time = time.time()
            imgr = cv2.imread(img)
            composite = coco_demo.run_on_opencv_image(imgr)
            print("Time: {:.2f} s / img".format(time.time() - start_time))
            composite = cv2.resize(composite, (1280, 720))
            cv2.imwrite(args.output_dir+os.path.basename(img), composite)
    else:
        start_time = time.time()
        img = cv2.imread(args.img)
        composite = coco_demo.run_on_opencv_image(img)
        print("Time: {:.2f} s / img".format(time.time() - start_time))
        composite = cv2.resize(composite, (1280, 720))
        cv2.imshow("COCO detections", composite)
        cv2.waitKey(0)
Example #26
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/keypoints_R_101_FPN.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    #
    results = []
    print("testing ...")
    val_rootpath = './../datasets/linemod/ape_train'
    with open('val.txt', 'r') as fp:

        val_imgs = fp.readlines()

        # print(val_imgs)

        fp.close()

        for imgname in val_imgs:
            per_dict = {}
            imgname = imgname.replace('\n', '').split('/')[-1]
            imgpath = os.path.join(val_rootpath, imgname)
            print(imgname)
            img = cv2.imread(imgpath)
            # print(img)
            _, box, score, kpts = coco_demo.run_on_opencv_image(img)
            np_box = box.cpu().numpy()
            np_score = score.cpu().numpy()
            per_dict[imgname] = [
                np_box.tolist(),
                np_score.tolist(),
                kpts.tolist()
            ]
            results.append(per_dict)

    # to save results

    json_dict = {}
    json_dict["ape"] = results

    with open('./result.json', 'w') as f:
        json.dump(json_dict, f)
        f.close()

    return True
Example #27
0
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        # default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml",
        default="../configs/e2e_mask_rcnn_R_50_FPN_wolf.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
            "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    save_file = os.path.join("..", cfg.OUTPUT_DIR, "last_checkpoint")
    # print("save_file: " ,save_file)

    try:
        with open(save_file, "r") as f:
            last_saved = f.read()
            last_saved = last_saved.strip()
            last_saved = os.path.join("..", last_saved)
    except IOError:
        # if file doesn't exist, maybe because it has just been
        # deleted by a separate process
        last_saved = None

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
        weight_loading=last_saved
    )

    # cam = cv2.VideoCapture('/dev/C922')
    # cam = cv2.VideoCapture('/dev/C930E')
    cam = cv2.VideoCapture('../../dataset/robot_video/wolf_robot_cam-158.avi')  #0 56 158
    # cam = cv2.VideoCapture('../../dataset/tripod_video/wolf_tripod_cam-0.avi')
    
    while True:
        start_time = time.time()
        ret_val, img = cam.read()
        
        if ret_val:
            composite = coco_demo.run_on_opencv_image(img)
            print("Time: {:.2f} s / img".format(time.time() - start_time))
            print("FPS: ", 1.0 / (time.time() - start_time)) # FPS = 1 / time to process loop
            cv2.imshow("COCO detections", composite)
        else:
            break

        k = cv2.waitKey(1)
        if k == 27 or k == ord('q'):
            print('Exit with code 0')
            break  # esc to quit
    cv2.destroyAllWindows()
Example #28
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default=
        "./configs/yolonet/yolonet_mask_R-101-FPN_2x_adjust_std011_ms.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.5,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=416,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )
    files = os.listdir(
        "/home/stirfryrabbit/Projects/Research_Project/sheepCount/retinamask/train_test_data/coco-style/val2017"
    )
    # print(files)
    # cam = cv2.VideoCapture(0)
    # while True:
    for file in files:
        img = cv2.imread(
            "/home/stirfryrabbit/Projects/Research_Project/sheepCount/retinamask/train_test_data/coco-style/val2017/{}"
            .format(file))
        start_time = time.time()
        # ret_val, img = cam.read()
        composite = coco_demo.run_on_opencv_image(img)
        print("Time: {:.2f} s / img".format(time.time() - start_time))
        cv2.imshow("COCO detections", composite)
        if cv2.waitKey(1000) == 27:
            break  # esc to quit
    cv2.destroyAllWindows()
Example #29
0
def main(val_path, obj_id):
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/keypoints_R_101_FPN.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
             "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    print("testing ...")

    # val_path='./../datasets/linemod/ape_train'
    # val_list=glob.glob(val_path+'/*.png')
    # val_label=os.path.join(val_path,'val.txt')

    # metric

    val_img_list = glob.glob(val_path + '/test_data/*.png')

    K = np.array([[572.4114, 0., 325.2611],
                  [0., 573.57043, 242.04899],
                  [0., 0., 1.]])

    add_v = 0
    rep_v = 0
    length = 0

    for imgpath in val_img_list[:]:
        per_dict = {}
        img = cv2.imread(imgpath)
        imgname = imgpath.split('/')[-1]

        img = cv2.imread(imgpath)
        try:
            # print(imgname)
            labels, box, score, kpts = coco_demo.run_on_opencv_image(img)

            labels_np = labels.cpu().numpy()
            np_kpts = kpts
            ind = np.where(labels_np == obj_id)

            if len(ind) == 0:
                continue
            obj_kpts = np_kpts[ind[0][0]]  # [8,3]
            add, rep = error_cp(val_path, obj_id, imgname, K, obj_kpts)
        except:
            continue

        length += 1
        # print(length)
        if add:
            add_v += 1
        if rep:
            rep_v += 1

    print("ADD metric:{}".format(add_v / length))
    print("REP metric:{}".format(rep_v / length))

    return True
Example #30
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="configs/test.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
        "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    cam = cv2.VideoCapture(0)
    while True:
        start_time = time.time()
        ret_val, img = cam.read()
        composite = coco_demo.run_on_opencv_image(img)
        print("Time: {:.2f} s / img".format(time.time() - start_time))
        cv2.imshow("COCO detections", composite)
        if cv2.waitKey(1) == 27:
            break  # esc to quit
    cv2.destroyAllWindows()
Example #31
0
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.7,
        help="Minimum score for the prediction to be shown",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=224,
        help="Smallest size of the image to feed to the model. "
            "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "--show-mask-heatmaps",
        dest="show_mask_heatmaps",
        help="Show a heatmap probability for the top masks-per-dim masks",
        action="store_true",
    )
    parser.add_argument(
        "--masks-per-dim",
        type=int,
        default=2,
        help="Number of heatmaps per dimension to show",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.freeze()

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_threshold=args.confidence_threshold,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    cam = cv2.VideoCapture(0)
    while True:
        start_time = time.time()
        ret_val, img = cam.read()
        composite = coco_demo.run_on_opencv_image(img)
        print("Time: {:.2f} s / img".format(time.time() - start_time))
        cv2.imshow("COCO detections", composite)
        if cv2.waitKey(1) == 27:
            break  # esc to quit
    cv2.destroyAllWindows()