示例#1
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Training")
    parser.add_argument(
        "--config-file",
        default="",
        metavar="FILE",
        help="path to config file",
        type=str,
    )
    args = parser.parse_args()
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(["MODEL.DEVICE", "cpu"])

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

    dataDir = '/depudata1/coco'
    dataType = 'val2017'
    annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataType)

    coco = COCO(annFile)

    # display COCO categories and supercategories
    cats = coco.loadCats(coco.getCatIds())
    nms = [cat['name'] for cat in cats]
    print('COCO categories: \n{}\n'.format(' '.join(nms)))

    nms = set([cat['supercategory'] for cat in cats])
    print('COCO supercategories: \n{}'.format(' '.join(nms)))

    catIds = coco.getCatIds(catNms=['person'])
    imgIds = coco.getImgIds(catIds=catIds)
    # imgIds = coco.getImgIds(imgIds = [341681])
    curr_img_id = imgIds[np.random.randint(0, len(imgIds))]
    img = coco.loadImgs(curr_img_id)[0]

    image = io.imread(img['coco_url'])
    # image = load("http://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg")
    plt.imshow(image)
    predictions = coco_demo.run_on_opencv_image(image)
    plt.imshow(predictions)

    str1 = '%s.jpg' % curr_img_id
    plt.savefig(str1)
示例#2
0
def main():
    # this makes our figures bigger
    pylab.rcParams['figure.figsize'] = 20, 12

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

    #'../configs/e2e_mask_rcnn_fbnet.yaml'
    #'../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml'
    #'../configs/e2e_mask_rcnn_fbnet.yaml'

    #'../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml'

    #'../configs/e2e_mask_rcnn_fbnet.yaml'
    #'../configs/e2e_mask_rcnn_fbnet.yaml'
    #'../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", "cpu"])

    coco_demo = COCODemo(cfg,
                         min_image_size=800,
                         confidence_threshold=0.3,
                         load_weight='../Trained Model/new/model_0027500.pth')

    # from http://cocodataset.org/#explore?id=345434
    #image = load("https://www.abc.net.au/news/image/9857250-3x2-940x627.jpg")
    #imshow(image)

    # compute predictions

    #print(image)
    #image = Image.open('./0008.png').convert('RGB')
    #print(image.size)
    #'/mnt/DATA/Download/Dataset/t-less_v2/test_primesense/01/rgb/0004.png' /54
    image = imread(
        '/mnt/DATA/Download/Dataset/t-less_v2/train_primesense/30/rgb/0001.png'
    )
    #imread('./Test.jpg')

    image = np.array(image)[:, :, [2, 1, 0]]
    predictions = coco_demo.run_on_opencv_image(image)

    imshow(predictions)
示例#3
0
def main():

    cfg.merge_from_file(
        "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml")
    #cfg.merge_from_list(None)
    cfg.freeze()

    coco_demo = COCODemo(
        cfg,
        confidence_threshold=0.7,
        show_mask_heatmaps=False,
        masks_per_dim=2,
        min_image_size=224,
    )

    img = cv2.imread("test.jpg")
    composite = coco_demo.run_on_opencv_image(img)
    print(composite is None)
    cv2.imwrite("./result.jpg", composite)
示例#4
0
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)

count = 0
start = time.time()
for im_name in demo_im_names:
    img = cv2.imread(os.path.join(args.images_dir, im_name))
    count += 1
    if img is None:
        continue
    start_time = time.time()
    composite = coco_demo.run_on_opencv_image(img)
    # print(composite)
time_pytorch = (time.time() - start) / count
print("Pytorch average inference time: {:.2f}s".format(time_pytorch))

# input_data = torch.randn(1, 3, 800, 800, dtype=torch.float32, device='cuda')

# print(coco_demo.model(input_data)[0])
# output_data_pytorch = coco_demo.model(input_data).cpu().detach().numpy()

# input_names = ['input']
# output_names = ['output']
# torch.onnx.export(resnet50, input_data, 'resnet50.onnx', input_names=input_names, output_names=output_names, verbose=False, opset_version=11)
# torch.onnx.export(resnet50, input_data, 'resnet50.dynamic_shape.onnx', dynamic_axes={"input": [0, 2, 3]}, input_names=input_names, output_names=output_names, verbose=False, opset_version=11)

# #继续运行python代码前,先运行如下命令
示例#5
0
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection")
    parser.add_argument(
        "--config-file",
        default="/home/mrvain/Workspace/test/FCOS_PLUS/configs/fcos/fcos_R_50_FPN_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default="/home/mrvain/Workspace/test/fcos_r50.pth",
        metavar="FILE",
        help="path to the trained model",
    )
    parser.add_argument(
        "--images-dir",
        default="/home/mrvain/Workspace/test/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",
    )
    parser.add_argument(
        "opts",
        help="modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.merge_from_list(["MODEL.DEVICE", "cpu"])
    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.23860901594161987, 0.24108672142028809, 0.2470853328704834,
        0.2316885143518448, 0.2708061933517456, 0.23173952102661133,
        0.31990334391593933, 0.21302376687526703, 0.20151866972446442,
        0.20928964018821716, 0.3793887197971344, 0.2715213894844055,
        0.2836397588253021, 0.26449233293533325, 0.1728038638830185,
        0.314998596906662, 0.28575003147125244, 0.28987520933151245,
        0.2727000117301941, 0.23306897282600403, 0.265937477350235,
        0.32663893699645996, 0.27102580666542053, 0.29177549481391907,
        0.2043062448501587, 0.24331751465797424, 0.20752687752246857,
        0.22951272130012512, 0.22753854095935822, 0.2159966081380844,
        0.1993938684463501, 0.23676514625549316, 0.20982342958450317,
        0.18315598368644714, 0.2489681988954544, 0.24793922901153564,
        0.287187397480011, 0.23045086860656738, 0.2462811917066574,
        0.21191294491291046, 0.22845126688480377, 0.24365000426769257,
        0.22687821090221405, 0.18365581333637238, 0.2035856395959854,
        0.23478077352046967, 0.18431290984153748, 0.18184082210063934,
        0.2708037495613098, 0.2268175482749939, 0.19970566034317017,
        0.21832780539989471, 0.21120598912239075, 0.270445853471756,
        0.189377561211586, 0.2101106345653534, 0.2112293541431427,
        0.23484709858894348, 0.22701986134052277, 0.20732736587524414,
        0.1953316181898117, 0.3237660229206085, 0.3078872859477997,
        0.2881140112876892, 0.38746657967567444, 0.20038367807865143,
        0.28123822808265686, 0.2588447630405426, 0.2796839773654938,
        0.266757994890213, 0.3266656696796417, 0.25759157538414,
        0.2578003704547882, 0.17009201645851135, 0.29051828384399414,
        0.24002137780189514, 0.22378061711788177, 0.26134759187698364,
        0.1730124056339264, 0.1857597529888153
    ]

    image_filenames = os.listdir(args.images_dir)

    model = COCODemo(
        cfg,
        confidence_thresholds_for_classes=thresholds_for_classes,
        min_image_size=args.min_image_size
    )

    for image_filename in image_filenames:
        img = cv2.imread(os.path.join(args.images_dir, image_filename))
        if img is None:
            continue

        start_time = time.time()
        composite = model.run_on_opencv_image(img)
        print("{}\ttime: {:.2f}s".format(image_filename, time.time() - start_time))

        cv2.imshow(image_filename, composite)

    print("Press any keys ...")
    cv2.waitKey()
    cv2.destroyAllWindows()
示例#6
0
    # convert to BGR format
    image = np.array(pil_image)[:, :, [2, 1, 0]]
    return image

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

import cv2
# from http://cocodataset.org/#explore?id=345434
# image = load("http://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg")
image = cv2.imread('/content/drive/MyDrive/maskrcnn-benchmark/datasets/coco/test/test_images/2007_001175.jpg')
imshow(image)

# compute predictions
predictions = my_demo.run_on_opencv_image(image)
imshow(predictions)

"""## 調整weight"""

import os
import torch
import argparse
from maskrcnn_benchmark.config import cfg
from maskrcnn_benchmark.utils.c2_model_loading import load_c2_format

_d=torch.load("/content/drive/MyDrive/maskrcnn-benchmark/weights/e2e_mask_rcnn_R_50_FPN_1x.pth")
newdict=_d
def removekey(d, listofkeys):
    r=d
    for key in listofkeys:
示例#7
0
def main():
    parser = argparse.ArgumentParser(
        description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="configs/embed_mask/embed_mask_R50_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default="models/embed_mask_R50_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(
        "--out-dir",
        default="demo/output",
        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.24721044301986694, 0.2316334992647171, 0.23782534897327423,
        0.2447730302810669, 0.26833730936050415, 0.2909756898880005,
        0.22202278673648834, 0.23603129386901855, 0.19448654353618622,
        0.2009030282497406, 0.2205723077058792, 0.4426179826259613,
        0.2812938094139099, 0.23200270533561707, 0.22222928702831268,
        0.34396135807037354, 0.29865574836730957, 0.2620207965373993,
        0.23538640141487122, 0.21343813836574554, 0.23408174514770508,
        0.3619556427001953, 0.25181055068969727, 0.2753196656703949,
        0.20989173650741577, 0.256824254989624, 0.24953776597976685,
        0.2482326775789261, 0.23516853153705597, 0.3231242001056671,
        0.1875445693731308, 0.22903329133987427, 0.220603808760643,
        0.1938045769929886, 0.2102973908185959, 0.30885136127471924,
        0.21589471399784088, 0.2611836791038513, 0.27154257893562317,
        0.2536311149597168, 0.21989859640598297, 0.2741137146949768,
        0.24886088073253632, 0.20183633267879486, 0.17529579997062683,
        0.2467200607061386, 0.2103690654039383, 0.23187917470932007,
        0.28766655921936035, 0.21596665680408478, 0.24378667771816254,
        0.2806374728679657, 0.23764009773731232, 0.2884339392185211,
        0.19776469469070435, 0.29654744267463684, 0.23793953657150269,
        0.2753768265247345, 0.24718035757541656, 0.2166261523962021,
        0.22458019852638245, 0.36707887053489685, 0.29586368799209595,
        0.24396133422851562, 0.3916597068309784, 0.2478819191455841,
        0.3140171468257904, 0.23574240505695343, 0.30935078859329224,
        0.2633970379829407, 0.22616524994373322, 0.22482863068580627,
        0.25680482387542725, 0.184458926320076, 0.31002628803253174,
        0.2936173677444458, 0.2688758671283722, 0.2438362091779709,
        0.17232654988765717, 0.1869594156742096
    ]

    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.imwrite(os.path.join(args.out_dir, im_name), composite)
    print("Press any keys to exit ...")
示例#8
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 qiang.zhou <*****@*****.**>
"""

"""
from maskrcnn_benchmark.config import cfg

config_file = "./configs/e2e_mask_rcnn_R_50_FPN_1x.yaml"

cfg.merge_from_file(config_file)
cfg.merge_from_list(['MODEL.WEIGHT', "./e2e_mask_rcnn_R_50_FPN_1x.pth"])
print(cfg)

from demo.predictor import COCODemo

coco_demo = COCODemo(cfg, min_image_size=800, confidence_threshold=0.7)
import cv2
image = cv2.imread("/tmp/00000.jpg")
predictions = coco_demo.run_on_opencv_image(image)

import pdb
pdb.set_trace()
示例#9
0
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="configs/embed_mask/embed_mask_R50_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default="models/embed_mask_R50_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(
        "--out-dir",
        default="demo/output",
        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.24445024132728577, 0.2556260824203491, 0.2336651235818863, 0.26643890142440796, 0.22829005122184753,
        0.27605465054512024, 0.29680299758911133, 0.24539557099342346, 0.22566702961921692, 0.21125544607639313,
        0.3632965385913849, 0.42116600275039673, 0.29700127243995667, 0.2278410643339157, 0.2317150980234146,
        0.30244436860084534, 0.32276564836502075, 0.25707629323005676, 0.24852260947227478, 0.24491029977798462,
        0.2518414556980133, 0.35320255160331726, 0.2866332232952118, 0.2207552194595337, 0.2568267285823822,
        0.24461865425109863, 0.20570527017116547, 0.2656995356082916, 0.21232444047927856, 0.2799481451511383,
        0.18180416524410248, 0.2654014825820923, 0.262266606092453, 0.19924932718276978, 0.22213412821292877,
        0.3075449764728546, 0.2290934920310974, 0.2963321805000305, 0.23535756766796112, 0.2430417388677597,
        0.22808006405830383, 0.2716907560825348, 0.21096138656139374, 0.18565504252910614, 0.17213594913482666,
        0.2755044996738434, 0.22538238763809204, 0.22792285680770874, 0.24877801537513733, 0.23092558979988098,
        0.23993775248527527, 0.21917308866977692, 0.2535002529621124, 0.30203622579574585, 0.19476301968097687,
        0.24782243371009827, 0.22699865698814392, 0.25022363662719727, 0.23006463050842285, 0.22317998111248016,
        0.20648975670337677, 0.28253015875816345, 0.35304051637649536, 0.2882220447063446, 0.2875506281852722,
        0.21613512933254242, 0.308322936296463, 0.29409125447273254, 0.3021804690361023, 0.273112416267395,
        0.23458659648895264, 0.2998719811439514, 0.2715963125228882, 0.1898047924041748, 0.32565683126449585,
        0.25560101866722107, 0.265905499458313, 0.3087238669395447, 0.2053961306810379, 0.20331673324108124
    ]

    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.imwrite(os.path.join(args.out_dir, im_name), composite)
    print("Press any keys to exit ...")
示例#10
0
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default="/home/w/workspace/DetectionHub/configs/global_wheat.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=0.5,
        show_mask_heatmaps=args.show_mask_heatmaps,
        masks_per_dim=args.masks_per_dim,
        min_image_size=args.min_image_size,
    )

    result_dict = {}
    image_dir = '/media/w/Data/globel-wheat-detection/test'
    image_list = os.listdir(image_dir)
    for i in image_list:
        image = cv2.imread(os.path.join(image_dir, i))
        start_time = time.time()
        image = cv2.resize(image, (600, 600), cv2.INTER_CUBIC)
        composite, predictions = coco_demo.run_on_opencv_image(image)
        result = []
        for bbox, score in zip(predictions.bbox, predictions.get_field("scores")):
            bbox = bbox.cpu().numpy()
            score = score.cpu().numpy()
            bbox = resize2_1024(bbox)
            result.append(str(score))
            for b in bbox:
                result.append(str(b))

        result_dict[i.split('.')[0]] = ' '.join(result)

        print("Time: {:.2f} s / img".format(time.time() - start_time))
        # cv2.imshow("COCO detections", composite)
        # if cv2.waitKey(0) == 27:
        #     break  # esc to quit
    with open("test.csv", "w") as csvfile:
        writer = csv.writer(csvfile)

        # 先写入columns_name
        writer.writerow(["image_id", "PredictionString"])
        for key in result_dict.keys():
            writer.writerow([key, result_dict[key]])
    cv2.destroyAllWindows()
示例#11
0
def main():
    parser = argparse.ArgumentParser(description="MaskRCNN Demo")
    parser.add_argument(
        "--image_dir",
        default="",
        type=str,
        help="path to images directory",
    )
    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,
    )

    filenames = os.listdir(args.image_dir)
    for filename in filenames:
        img = cv2.imread(os.path.join(args.image_dir, filename))
        if img is None:
            continue

        visualize, prediction, tracking = coco_demo.run_on_opencv_image(img)
        coco_demo.saveResults(filename, visualize, prediction, tracking)
示例#12
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()
def main():
    parser = argparse.ArgumentParser(
        description="Multi Object Tracking Video Demo")
    parser.add_argument(
        "--video-file",
        metavar="FILE",
        help="path to video file",
    )
    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(args.video_file)

    if cam.isOpened():
        # get vcap property
        width = cam.get(cv2.CAP_PROP_FRAME_WIDTH)  # float
        height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
        fps = cam.get(cv2.CAP_PROP_FPS)
        num_frame = cam.get(cv2.CAP_PROP_FRAME_COUNT)
        print("VIDEO INFO:")
        print("Width %f, Height %f, FPS %f, Frame_number %f\n" %
              (width, height, fps, num_frame))

    current_frame = 0
    success = True

    while success:
        success, img = cam.read()
        if success:
            start_time = time.time()
            visualize, prediction, tracking = coco_demo.run_on_opencv_image(
                img, current_frame)
            print("%d / %d, processing time: %.2fs" %
                  (current_frame, num_frame, time.time() - start_time))

            coco_demo.saveResults(
                str(current_frame).zfill(6), visualize, prediction, tracking)
            current_frame += 1