Пример #1
0
def load_files():
    # Root directory of the project
    ROOT_DIR = os.path.abspath("../")

    # Import Mask RCNN
    sys.path.append(ROOT_DIR)  # To find local version of the library
    # Import COCO config
    sys.path.append(os.path.join(ROOT_DIR,
                                 "samples/coco/"))  # To find local version
    import coco

    # Directory to save logs and trained model
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Directory of images to run detection on
    IMAGE_DIR = os.path.join(ROOT_DIR, "images")

    class InferenceConfig(coco.CocoConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()
    # config.display()

    return MODEL_DIR, config, COCO_MODEL_PATH
Пример #2
0
def captureVideo():
    # We use a K80 GPU with 24GB memory, which can fit 3 images.
    ROOT_DIR = os.getcwd()
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    VIDEO_DIR = os.path.join(ROOT_DIR, "videos")
    VIDEO_SAVE_DIR = os.path.join(VIDEO_DIR, "save")
    WEAPON_MODEL_PATH = "mask_rcnn_weapon_cfg_0009.h5"
    if not os.path.exists(WEAPON_MODEL_PATH):
        utils.download_trained_weights(WEAPON_MODEL_PATH)

    config = PredictionConfig()
    config.display()

    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(WEAPON_MODEL_PATH, by_name=True)
    class_names = ['BG', 'weapon']

    capture = cv2.VideoCapture('video/trailer.mkv')
    try:
        if not os.path.exists(VIDEO_SAVE_DIR):
            os.makedirs(VIDEO_SAVE_DIR)
    except OSError:
        print('Error: Creating directory of data')
    frames = []
    frame_count = 0
    # these 2 lines can be removed if you dont have a 1080p camera.
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

    while True:
        ret, frame = capture.read()
        # Bail out when the video file ends
        if not ret:
            break

        # Save each frame of the video to a list
        frame_count += 1
        frames.append(frame)
        print('frame_count :{0}'.format(frame_count))
        if len(frames) == config.BATCH_SIZE:
            results = model.detect(frames, verbose=0)
            print('Predicted')
            for i, item in enumerate(zip(frames, results)):
                frame = item[0]
                r = item[1]
                frame = display_instances(frame, r['rois'], r['masks'],
                                          r['class_ids'], class_names,
                                          r['scores'])
                if frame is not None:
                    name = '{0}.jpg'.format(frame_count + i -
                                            config.BATCH_SIZE)
                    name = os.path.join(VIDEO_SAVE_DIR, name)
                    cv2.imwrite(name, frame)
                    print('writing to file:{0}'.format(name))
            # Clear the frames array to start the next batch
            frames = []

    capture.release()
Пример #3
0
 def __init__(self):
     if not os.path.exists(self.COCO_MODEL_PATH):
         utils.download_trained_weights(self.COCO_MODEL_PATH)
     config = InferenceConfig()
     config.display()
     with tf.device(self.DEVICE):
         self.model = modellib.MaskRCNN(mode="inference",
                                        model_dir=self.MODEL_DIR,
                                        config=config)
     self.model.load_weights(self.COCO_MODEL_PATH, by_name=True)
     self.biggest_object = 0
     self.object_area = 0
     self.output = {}
Пример #4
0
def person_blocker(args):

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR, config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    image = imageio.imread(args.image)

    # Create masks for all objects
    results = model.detect([image], verbose=0)
    r = results[0]

    # Filter masks to only the selected objects
    objects = np.array(args.objects)

    # Object IDs:
    if np.all(np.chararray.isnumeric(objects)):
        object_indices = objects.astype(int)
    # Types of objects:
    else:
        selected_class_ids = np.flatnonzero(np.in1d(get_class_names(),
                                                    objects))
        object_indices = np.flatnonzero(
            np.in1d(r['class_ids'], selected_class_ids))

    mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2)
    mask_selected = mask_selected.astype(np.uint8)

    # Dilate Mask
    mask_selected = dilate_mask(mask_selected)

    # Replace object masks with noise
    # image_masked = image.copy()
    # mask = create_mask_color(image, [255, 255, 255])
    # image_masked[mask_selected > 0] = mask[mask_selected > 0]

    # imageio.imwrite(args.output, image_masked)
    mask_selected[mask_selected > 0] = 255
    mask_selected = 255 - mask_selected
    imageio.imwrite(args.output, mask_selected)
def continueTrainingCoCo(epochs):
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "model", "mask_rcnn_coco.h5")
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    config = train_config()
    config.display()

    dataset = ShapesDataset(-1, 0, -1)
    dataset.load_imgs(TRAIN_PATH)
    dataset.prepare()

    #print("Image Count: {}".format(len(dataset.image_ids)))
    #print("Class Count: {}".format(dataset.num_classes))
    for i, info in enumerate(dataset.class_info):
        print("{:3}. {:50}".format(i, info['name']))

    dataset_valid = ShapesDataset(-1, 1, 1)
    dataset_valid.load_imgs(VALID_PATH)
    dataset_valid.prepare()

    model = modellib.MaskRCNN(mode="training",
                              config=config,
                              model_dir=MODEL_DIR)

    model_path = model.find_last()[1]
    if model_path:
        print("Loading weights from " + model_path)
        model.load_weights(model_path, by_name=True)
    elif config.TRAIN_FROM_COCO == 1:
        print('train from CoCo')
        model.load_weights(COCO_MODEL_PATH,
                           by_name=True,
                           exclude=[
                               "mrcnn_class_logits", "mrcnn_bbox_fc",
                               "mrcnn_bbox", "mrcnn_mask"
                           ])
    else:
        print('train from scrash')

    print('start training')
    model.train(
        dataset,
        dataset_valid,
        learning_rate=config.LEARNING_RATE,
        epochs=epochs,
        #layers="heads",
        layers="all",
        argument=config.ARGUMENT)
Пример #6
0
    def __init__(self, config):
        K.clear_session()

        if not os.path.exists(COCO_MODEL_PATH):
            utils.download_trained_weights(COCO_MODEL_PATH)

        class InferenceConfig(coco.CocoConfig):
            GPU_COUNT = 1
            IMAGES_PER_GPU = 1

        config = InferenceConfig()

        self.model = modellib.MaskRCNN(mode="inference",
                                       model_dir=MODEL_DIR,
                                       config=config)
        self.model.load_weights(COCO_MODEL_PATH, by_name=True)
Пример #7
0
def person_blocker(args):

    # If the output path does not exist, create it
    if not os.path.exists(args.write_output):
        os.makedirs(args.write_output)

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    # Start reading the frames
    reader = imageio.get_reader(args.image)
    vlen = len(reader)

    for i, image in enumerate(reader):

        # Output frame number
        print(str(i) + "/" + str(vlen))

        # Create masks for all objects
        results = model.detect([image], verbose=0)
        r = results[0]

        position_ids = [''.format(x) for x in range(r['class_ids'].shape[0])]

        outstr = (args.write_output + "/labeled{:06d}.png").format(i)
        visualize_labelmod.display_instances(
            image,
            r['rois'],
            r['masks'],
            r['class_ids'],
            get_class_names(),
            scores=r['scores'],
            selected_class=args.selected_class,
            outname=outstr)
Пример #8
0
    def __init__(self, items=None):
        """Initialize the Coco Model

        Arguments
        ---------
        items: list-like
            Only items from this list will be annotated, and the rest will be discarded.
            If not provided, all masks are generated.
        """
        super().__init__(name="Coco",
                         config=InferenceConfig(),
                         model_dir=MODEL_DIR,
                         class_names=class_names)
        # Download COCO trained weights from Releases if needed
        if not os.path.exists(COCO_MODEL_PATH):
            utils.download_trained_weights(COCO_MODEL_PATH)
        self.items = items or class_names
Пример #9
0
def load_model():
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    return model
Пример #10
0
def load_mask_rcnn():

    ROOT_DIR = os.getcwd()

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")

    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class InferenceConfig(coco.CocoConfig):

        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()
    config.display()

    DEVICE = "/cpu:0"
    with tf.device(DEVICE):
        model = modellib.MaskRCNN(mode="inference",
                                  model_dir=MODEL_DIR,
                                  config=config)

    model.load_weights(COCO_MODEL_PATH, by_name=True)

    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
        'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
        'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
        'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
        'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]

    return model
Пример #11
0
def load_weights(model, _config, init_with_override=None):

    init_with = _config.init_with if init_with_override is None else init_with_override  # imagenet, coco, or last

    if init_with == "imagenet":
        model.load_weights(model.get_imagenet_weights(), by_name=True)
    elif init_with == "coco":
        if not os.path.exists(_config.COCO_MODEL_PATH):
            utils.download_trained_weights(_config.COCO_MODEL_PATH)

        # Load weights trained on MS COCO, but skip layers that
        # are different due to the different number of classes
        # See README for instructions to download the COCO weights
        model.load_weights(_config.COCO_MODEL_PATH,
                           by_name=True,
                           exclude=[
                               "mrcnn_class_logits", "mrcnn_bbox_fc",
                               "mrcnn_bbox", "mrcnn_mask"
                           ])
    elif init_with == "last":
        # Load the last model you trained and continue training
        model.load_weights(model.find_last()[1], by_name=True)

    return model
Пример #12
0
    import time
    import balloon
    import utils
    import model as modellib
    #socket binding to unity binding to socket 5555
    import zmq
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind("tcp://*:5555")

    ROOT_DIR = os.getcwd()
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    SODA_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_soda_can.h5")
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    if not os.path.exists(SODA_WEIGHTS_PATH):
        utils.download_trained_weights(SODA_WEIGHTS_PATH)

    config = balloon.BalloonConfig()

    class InferenceConfig(config.__class__):
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()
    config.display()

    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)

    model.load_weights(SODA_WEIGHTS_PATH, by_name=True)
Пример #13
0
def person_blocker(args):

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR, config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    image = imageio.imread(filename)

    # Create masks for all objects
    results = model.detect([image], verbose=0)
    r = results[0]

    if args.labeled:
        position_ids = ['[{}]'.format(x)
                        for x in range(r['class_ids'].shape[0])]
        visualize.display_instances(image, r['rois'],
                                    r['masks'], r['class_ids'],
                                    get_class_names(), position_ids)
        sys.exit()

    # Filter masks to only the selected objects
    objects = np.array(args.objects)

    # Object IDs:
    if np.all(np.chararray.isnumeric(objects)):
        object_indices = objects.astype(int)
    # Types of objects:
    else:
        selected_class_ids = np.flatnonzero(np.in1d(get_class_names(),
                                                    objects))
        object_indices = np.flatnonzero(
            np.in1d(r['class_ids'], selected_class_ids))

    mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2)

    # Replace object masks with noise
    mask_color = string_to_rgb_triplet(args.color)
    image_masked = image.copy()
    noisy_color = create_noisy_color(image, mask_color)
    image_masked[mask_selected > 0] = noisy_color[mask_selected > 0]

    img_dir = '/Volumes/seagate/projects/person-blocker/images2/'
    img_pre = 'person_blocked_{0}'.format(strftime("%Y%m%d%H%M%S",gmtime()))
    img_name = img_dir + img_pre + '.png'
    gif_name = img_dir + img_pre + '.gif'

    imageio.imwrite(img_name, image_masked)

    # Create GIF. The noise will be random for each frame,
    # which creates a "static" effect
    # this works great, but takes some time and produces 7+ meg file
    #images = [image_masked]
    #num_images = 10   # should be a divisor of 30
    #
    #for _ in range(num_images - 1):
    #    new_image = image.copy()
    #    noisy_color = create_noisy_color(image, mask_color)
    #    new_image[mask_selected > 0] = noisy_color[mask_selected > 0]
    #    images.append(new_image)
    #
    #imageio.mimsave(gif_name, images, fps=30., subrectangles=True)

    # print json
    try:
      # Create unique image name
      uniqueid = 'person_uuid_{0}_{1}'.format(strftime("%Y%m%d%H%M%S%f",gmtime()),uuid.uuid4())
      host = os.uname()[1]
      currenttime= strftime("%Y-%m-%d %H:%M:%S",gmtime())
      ipaddress = IP_address()
      end = time.time()
      row = { 'uuid': uniqueid, 'runtime': str(round(end - start)), 'host': host, 'ts': currenttime, 'ipaddress': ipaddress, 'imagefilename': img_pre, 'originalfilename': filename }
      print( json.dumps(row) )

    except:
      print("{\"message\": \"Failed to run\"}")
Пример #14
0
def seg_main():
    """
        test everything
    """
    import os
    import sys
    import coco
    import utils
    import model as modellib

    ROOT_DIR = os.getcwd()
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class InferenceConfig(coco.CocoConfig):
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()
    config.display()

    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)
    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
        'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
        'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
        'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
        'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]

    capture = bridge.imgmsg_to_cv2(data, "bgr8")

    # these 2 lines can be removed if you dont have a 1080p camera.
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
    ret, frame = capture.read()

    frame = cv2.resize(frame, (480 * 2, 340 * 2))

    results = model.detect([frame], verbose=0)
    r = results[0]
    frame = display_instances(frame, r['rois'], r['masks'], r['class_ids'],
                              class_names, r['scores'])

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

    publisher(capture)

    capture.release()
    cv2.destroyAllWindows()
Пример #15
0
            NUM_CLASSES = nr_classes
        config = TacoTestConfig()
    config.display()

    # Create model
    if args.command == "train":
        model = MaskRCNN(mode="training", config=config, model_dir=DEFAULT_LOGS_DIR)
    else:
        model = MaskRCNN(mode="inference", config=config, model_dir=DEFAULT_LOGS_DIR)

    # Select weights file to load
    if args.model.lower() == "coco":
        model_path = COCO_MODEL_PATH
        # Download weights file
        if not os.path.exists(model_path):
            utils.download_trained_weights(model_path)
    elif args.model.lower() == "last":
        # Find last trained weights
        model_path = model.find_last()[1]
    elif args.model.lower() == "imagenet":
        # Start from ImageNet trained weights
        model_path = model.get_imagenet_weights()
    else:
        model_path = args.model

    # Load weights
    if args.model.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
        model.load_weights(model_path, by_name=True, exclude=[
            "mrcnn_class_logits", "mrcnn_bbox_fc",
Пример #16
0
def main():
    # Root directory of the project
    ROOT_DIR = os.getcwd()

    # Directory to save logs and trained model
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class InferenceConfig(coco.CocoConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 8  # 8 is maximum for single P100.

    config = InferenceConfig()
    config.display()

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)

    # Load weights trained on MS-COCO
    model.load_weights(COCO_MODEL_PATH, by_name=True)
    print("Successfully load coco pretrained model.")

    # COCO Class names
    # Index of the class in the list is its ID. For example, to get ID of
    # the teddy bear class, use: class_names.index('teddy bear')
    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
        'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
        'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
        'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
        'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]

    # read scene
    info_dir = "../../detrac/DETRAC-Train-Annotations-XML/"
    video_dir = "../../detrac/Insight-MVT_Annotation_Train/"
    save_dir = "../../detrac/train_detect_output/"
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)

    videonames = [x for x in os.listdir(video_dir) if x.startswith("MVI")]
    print(videonames)
    batch_size = config.GPU_COUNT * config.IMAGES_PER_GPU
    for videoname in videonames:
        print("Processing scene {}...".format(videoname))
        video_files = sorted([
            img for img in os.listdir(os.path.join(video_dir, videoname))
            if img.endswith(".jpg")
        ])
        video_info = parse_scene_info(
            os.path.join(info_dir, videoname + ".xml"))
        #vid = imageio.get_reader(video_file,  'ffmpeg')
        if video_info["weather"] == "night":
            print("Skipping {} due to night.".format(videoname))
            continue

        save_pkl_dir = os.path.join(save_dir, videoname)
        if not os.path.isdir(save_pkl_dir):
            os.makedirs(save_pkl_dir)
            start_point = 0  # set the start frame for detection
        else:
            start_point = len(os.listdir(save_pkl_dir))

        # tqdm progress bar
        pbar = tqdm.tqdm(total=len(video_files) - start_point)
        for fnum in range(start_point, len(video_files), batch_size):
            batch_data = []
            for i in range(batch_size):
                if fnum + i < len(video_files):
                    batch_data.append(
                        cv2.imread(
                            os.path.join(video_dir, videoname,
                                         video_files[fnum + i])))
                else:
                    batch_data.append(np.zeros((960, 540, 3)))
            #batch_data = np.array(batch_data)
            results = model.detect(batch_data, verbose=0)
            for i, r in enumerate(results):
                ## transform mask to contours
                print("frame {} detects {} objects".format(
                    fnum + i, r['rois'].shape[0]))
                contours = []
                for m in r['masks'].transpose(2, 0, 1):
                    contours.append(mask_to_contours(m.astype('uint8')))
                r['contours'] = contours
                r.pop(
                    'masks', None
                )  # This will return my_dict[key] if key exists in the dictionary, and None otherwise.
                ## save a single pkl for each frame
                if fnum + i < len(video_files):
                    with open(
                            os.path.join(
                                save_pkl_dir,
                                video_files[fnum + i].replace(".jpg", "") +
                                ".pkl"), "wb") as f:
                        pickle.dump(r, f, protocol=2)

                pbar.update(1)
        pbar.close()
Пример #17
0
def person_blocker(args):

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
    COCO_MODEL_PATH = args.model or os.path.join(SCRIPT_DIR,
                                                 "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(SCRIPT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    image = imageio.imread(args.infile)

    # Create masks for all objects
    results = model.detect([image], verbose=0)
    r = results[0]

    dirname = os.path.dirname(args.infile)
    basename = os.path.basename(args.infile)
    barename = os.path.splitext(basename)[0]

    if args.objects is None:
        # now paste it all together
        outfile = os.path.join(dirname, "labels_{}.png".format(barename))

        position_ids = [
            '[{}]'.format(x) for x in range(r['class_ids'].shape[0])
        ]
        print("Saving: {}".format(outfile))
        visualize.display_instances(image,
                                    r['rois'],
                                    r['masks'],
                                    r['class_ids'],
                                    get_class_names(),
                                    position_ids,
                                    outfile=outfile)
        sys.exit()

    # Object IDs:
    indices_list = []
    objects_list = list(args.objects.split(","))
    for objects_entry in objects_list:
        if np.chararray.isnumeric(objects_entry):
            indices_list += [objects_entry]

        else:
            selected_class_ids = np.flatnonzero(
                np.in1d(get_class_names(), [objects_entry]))
            indices_list += np.flatnonzero(
                np.in1d(r['class_ids'], selected_class_ids)).tolist()
    object_indices = np.asarray(indices_list).astype(int)

    mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2)

    # Replace object masks with noise
    mask_colors = list(map(string_to_rgb_triplet, args.colors.split(",")))
    if args.bgcolor.lower() != "none":
        bg_color = string_to_rgb_triplet(args.bgcolor)
        image_masked = np.full(shape=(image.shape[0], image.shape[1], 3),
                               fill_value=bg_color).astype(np.uint8)
    else:
        image_masked = image.copy()

    noisy_color = create_noisy_color(image, mask_colors[0])
    image_masked[mask_selected > 0] = noisy_color[mask_selected > 0]

    outfile = os.path.join(dirname, "mask_{}.png".format(barename))
    print("Saving: {}".format(outfile))
    imageio.imwrite(outfile, image_masked)
Пример #18
0
    def __init__(self):
        """Initialize a MaskRCNNNode

        Parameters
        ----------

        Returns
        ----------
        - <obj>: MaskRCNNNode
            A Mask RCNN instantiation.

        """
        self._cv_bridge = CvBridge()
        self._class_names = rospy.get_param('~class_names', CLASS_NAMES)
        self._class_colors = visualize.random_colors(len(CLASS_NAMES))
        self._visualization = rospy.get_param('~visualization', True)

        #
        # network configuration
        #
        config = InferenceConfig()
        config.display()

        #
        # model in inference mode
        #
        self._model = modellib.MaskRCNN(mode="inference",
                                        model_dir="",
                                        config=config)

        #
        # mutual exclusive lock for handling image messages.
        #
        self._last_msg = None
        self._msg_lock = threading.Lock()

        #
        # publisher
        #
        self._detection_pub = rospy.Publisher('~detection',
                                              mask_rcnn_ros.msg.Detection,
                                              queue_size=1)
        self._vis_pub = rospy.Publisher('~visualization',
                                        sensor_msgs.msg.Image,
                                        queue_size=1)

        self._publish_rate = rospy.get_param('~publish_rate', 100)

        #
        # tensorflow graph
        #
        self._graph = tf.get_default_graph()

        #
        # Load weights trained on MS-COCO, download COCO trained weights from
        # releases if needed
        #
        model_path = rospy.get_param('~model_path', COCO_MODEL_PATH)
        if model_path == COCO_MODEL_PATH and not os.path.exists(
                COCO_MODEL_PATH):
            utils.download_trained_weights(COCO_MODEL_PATH)

        self._model.load_weights(model_path, by_name=True)
def main(args):
    # Root directory of the project
    ROOT_DIR = os.getcwd()

    # Directory to save logs and trained model
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    if not os.path.exists(MODEL_DIR):
        os.mkdir(MODEL_DIR)

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(MODEL_DIR, "mask_rcnn_coco.h5")
    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # data path
    DATA_PATH = args.data_path

    # Get train and test IDs
    data_ids = next(os.walk(DATA_PATH))[1]

    # create validation ids
    train_ids, val_ids = train_test_split(data_ids, test_size=0.1)

    # dataset name
    dataset_name = os.path.basename(args.data_path)

    # dataset config
    config = DSBConfig()
    config.NAME = dataset_name
    config.STEPS_PER_EPOCH = int(
        len(data_ids) / (config.GPU_COUNT * config.IMAGES_PER_GPU))
    config.VALIDATION_STEPS = int(
        len(val_ids) / (config.GPU_COUNT * config.IMAGES_PER_GPU))
    config.display()

    # dataset
    # Training dataset
    dataset_train = DSBDataset()
    dataset_train.load_bowl(args.data_path)
    dataset_train.prepare()

    # Validation dataset
    dataset_val = DSBDataset()
    dataset_val.load_bowl(args.data_path)
    dataset_val.prepare()

    # Create model in training mode
    model = modellib.MaskRCNN(mode="training",
                              config=config,
                              model_dir=MODEL_DIR)

    if args.init_with == "imagenet":
        model.load_weights(model.get_imagenet_weights(), by_name=True)
    elif args.init_with == "coco":
        # Load weights trained on MS COCO, but skip layers that
        # are different due to the different number of classes
        # See README for instructions to download the COCO weights
        model.load_weights(COCO_MODEL_PATH,
                           by_name=True,
                           exclude=[
                               "mrcnn_class_logits", "mrcnn_bbox_fc",
                               "mrcnn_bbox", "mrcnn_mask"
                           ])
    elif args.init_with == "last":
        # Load the last model you trained and continue training
        model.load_weights(model.find_last()[1], by_name=True)
    else:
        print("wrong mode")

    # Train the head branches
    # Passing layers="heads" freezes all layers except the head
    # layers. You can also pass a regular expression to select
    # which layers to train by name pattern.
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=args.epochs,
                layers='heads')
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=args.epochs * 2,
                layers='4+')
    # Fine tune all layers
    # Passing layers="all" trains all layers. You can also
    # pass a regular expression to select which layers to
    # train by name pattern.
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=args.epochs * 3,
                layers="all")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE / 10,
                epochs=args.epochs * 4,
                layers="all")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE / 100,
                epochs=args.epochs * 5,
                layers="all")
Пример #20
0
if __name__ == '__main__':

    ROOT_DIR = os.path.abspath("../")

    sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
    sys.path.append(os.path.join(ROOT_DIR, "mrcnn/"))  # To find local version

    import coco
    import utils
    import model as modellib

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    options = {
        'threshold': 0.5,  # 阈值
        'origin': 'sample_video/gakki_cut2_origin.flv',  # 原始视频
        'danmu': 'sample_video/gakki_cut2_danmu.flv',  # 弹幕视频
        'saveVideo': True,
        'output': 'output/gakki_cut2_2.avi',
        'keep': -1,  # 处理多少秒,-1代表处理完整视频
    }

    class InferenceConfig(coco.CocoConfig):
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
        DETECTION_MIN_CONFIDENCE = options.get('threshold')
Пример #21
0
def person_blocker(args):
    
    # If the output path does not exist, create it
    if not os.path.exists(args.write_output):
        os.makedirs(args.write_output)
    
    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR, config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    # Start reading the frames
    reader = imageio.get_reader(args.image)
    vlen = len(reader)
    
    for i, image in enumerate(reader):
    
        # Output frame number
        print(str(i)+"/"+str(vlen))
        
        # Create masks for all objects
        results = model.detect([image], verbose=0)
        r = results[0]

#         if args.labeled:
#             position_ids = ['[{}]'.format(x)
#                             for x in range(r['class_ids'].shape[0])]
#             visualize.display_instances(image, r['rois'],
#                                         r['masks'], r['class_ids'],
#                                         get_class_names(), position_ids)
#             sys.exit()

        # Filter masks to only the selected objects
        objects = np.array(args.objects)

        # Object IDs:
        if np.all(np.chararray.isnumeric(objects)):
            object_indices = objects.astype(int)
        # Types of objects:
        else:
            selected_class_ids = np.flatnonzero(np.in1d(get_class_names(),
                                                        objects))
            object_indices = np.flatnonzero(
                np.in1d(r['class_ids'], selected_class_ids))

        mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2)

        # Replace object masks with noise
        mask_color = string_to_rgb_triplet(args.color)
        image_masked = image.copy()
        noisy_color = create_noisy_color(image, mask_color)
        image_masked[mask_selected > 0] = noisy_color[mask_selected > 0]

        # Write number of detected objects
        n_persons = sum(r['class_ids'] == 1)
        img = Image.fromarray(image_masked.astype(np.uint8), 'RGB')
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype("arial.ttf", 60)
        draw.text((0, 0), "N="+str(n_persons), fill=(255,0,0), font = font)

#         plt.imshow(image_masked.astype(np.uint8))
#         plt.text(0, 0, "N = " + str(n_persons), fontsize=20, color = "red")
        
        # Save masked frame
        #plt.show()
        outstr = (args.write_output + "/frame{:06d}.jpg").format(i)
        img.save(outstr)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input_name",
                        type=str,
                        required=True,
                        help="Input image name")
    parser.add_argument("--output_name",
                        type=str,
                        required=True,
                        help="Output image name")

    args = parser.parse_args()

    # Root directory of the project
    ROOT_DIR = os.getcwd()

    # Directory to save logs and trained model
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    # Download COCO trained weights from Releases if needed
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class InferenceConfig(coco.CocoConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)

    # Load weights trained on MS-COCO
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
        'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
        'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
        'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
        'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush', 'bottle',
        'microphone'
    ]

    # Load image
    image = skimage.io.imread(args.input_name)

    # Run detection
    results = model.detect([image], verbose=1)

    # Visualize results
    r = results[0]

    # Blur backround of the image and save it with output_name
    visualize.display_blurred(image,
                              r['rois'],
                              r['masks'],
                              r['class_ids'],
                              class_names,
                              r['scores'],
                              name=args.output_name)
Пример #23
0
def test_simple(params):
    """Test function."""
    import os
    import sys
    import coco
    import utils

    from mrcnn.config import Config

    ROOT_DIR = os.getcwd()
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    COCO = os.path.normpath(os.getcwd() + os.sep + os.pardir)
    COCO = os.path.join(COCO +os.sep,"Mask RCNN" )
    COCO_MODEL_PATH = os.path.join(COCO, "mask_rcnn_coco.h5")

    #print(COCO_MODEL_PATH)

    if os.path.isdir('Detection') is False:
        os.mkdir('Detection')
    DET_DIR = os.path.join(ROOT_DIR, "Detection")
    DET_PATH = DET_DIR + os.sep

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class CocoConfig(Config):
        """Configuration for training on MS COCO.
        Derives from the base Config class and overrides values specific
        to the COCO dataset.
        """
        # Give the configuration a recognizable name
        NAME = "coco"

        # We use a GPU with 12GB memory, which can fit two images.
        # Adjust down if you use a smaller GPU.
        IMAGES_PER_GPU = 2

        # Uncomment to train on 8 GPUs (default is 1)
        # GPU_COUNT = 8

        # Number of classes (including background)
        # NUM_CLASSES = 37  # COCO has 80 classes
        NUM_CLASSES = 81

    class InferenceConfig(CocoConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
        # MAX_GT_INSTANCES = 100
        # TRAIN_ROIS_PER_IMAGE = 50
        # BACKBONE = "resnet50" #not working at all!
        # RPN_ANCHOR_STRIDE = 2
        # POST_NMS_ROIS_TRAINING = 1000
        # POST_NMS_ROIS_INFERENCE = 500
        # IMAGE_MIN_DIM = 400  # really much faster but bad results
        # IMAGE_MAX_DIM = 640
        # DETECTION_MAX_INSTANCES = 50 #a little faster but some instances not recognized

    config = InferenceConfig()
    config.display()

    left  = tf.placeholder(tf.float32, [2, args.input_height, args.input_width, 3])
    modeld = MonodepthModel(params, "test", left, None)
    modelrcnn = modellib.MaskRCNN(
        mode="inference", model_dir=MODEL_DIR, config=config
    )
    modelrcnn.load_weights(COCO_MODEL_PATH, by_name=True)
    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
        'bus', 'train', 'truck', 'boat', 'traffic light',
        'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
        'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
        'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
        'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
        'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
        'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
        'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
        'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
        'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
        'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
        'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
        'teddy bear', 'hair drier', 'toothbrush'
    ]

    now = datetime.now()
    directions_result = gmaps.directions("Dejvice, Prague 6",
                                         "Charles Square, Nové Město, Praha",
                                         mode="transit",
                                         departure_time=now)

    # api = overpy.Overpass()
    #
    # # fetch all ways and nodes
    # result = api.query("""way
    #                     ["name"="Běžecká"]
    #                     (50.07,14.0,50.8,14.4);
    #                     /*added by auto repair*/
    #                     (._;>;);
    #                     /*end of auto repair*/
    #                     out;
    #                                             """)
    location1 = []
    location = ''

    abc = polyline.decode(directions_result[0]['overview_polyline']['points'])
    for i in range(0, len(abc)):
        location1 = str(abc[i])
        location += location1.strip(')(') + '; '


    #result = api.query("node(50.077167,14.419660,50.077668,14.429223);out;")
    # print(len(result.nodes))
    # for node in result.nodes:
    #     #print("    Lat: %f, Lon: %f" % (node.lat, node.lon))
    #     lat = str(node.lat)
    #     lon = str(node.lon)
    #     location1 = lat + ',' + lon + '; '
    #     location += location1
    # print(location)
    # location1 = []
    # location = ''
    # for i in range(0, 5):
    #     lat = str((50.0753397 - i * 0.0001))
    #     lon = str(14.4189888)
    #     location1 = lat + ',' + lon + '; '
    #     location += location1

    apiargs = {
        'location': location ,#'50.0753397,14.4189888 ; 50.0795436,14.3907308 ;50.10291748018805, 14.39132777985096',
        'size': '640x640',
        'heading': '90',
        'fov': '90',
        'key': 'AIzaSyCciJlgQzOFXZXhvM1ORscu0Cj5dj-lTRo',
        'pitch': '0'
    }

    # Get a list of all possible queries from multiple parameters
    api_list = google_streetview.helpers.api_list(apiargs)

    # Create a results object for all possible queries
    resultsg = google_streetview.api.results(api_list)

    # Preview results
    # resultsg.preview()

    # Download images to directory 'downloads'
    resultsg.download_links('Downloads')

    # Save metadata
    # resultsg.save_metadata('metadata.json')

    onlyfiles = []

    while True:
        # ret , frame = capture.read()
        # input_image = frame

        images = os.path.join(ROOT_DIR, 'Downloads')
        for f in listdir(images):
            if f is not 'metadat.json':
                onlyfiles.append(f)
        onlyfiles = [f for f in listdir(images) if isfile(join(images, f))]
        frame = np.empty(len(onlyfiles), dtype=object)

        for n in range(0, len(onlyfiles) - 1):
            frame = cv2.imread(join(images, onlyfiles[n]))
            input_image = frame
            original_height, original_width, num_channels = input_image.shape
            input_image = scipy.misc.imresize(input_image, [args.input_height, args.input_width], interp='lanczos')
            input_image = input_image.astype(np.float32) / 255
            input_images = np.stack((input_image, np.fliplr(input_image)), 0)

            # SESSION
            config = tf.ConfigProto(allow_soft_placement=True)
            sess = tf.Session(config=config)

            # INIT
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            coordinator = tf.train.Coordinator()

            disp = sess.run(modeld.disp_left_est[0], feed_dict={left: input_images})
            disp_pp = post_process_disparity(disp.squeeze()).astype(np.float32)

            results = modelrcnn.detect([frame], verbose=0)
            r = results[0]

            disp_to_img = scipy.misc.imresize(disp_pp.squeeze(), [original_height, original_width])

            frame = display_instances(
                frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'],resultsg
            )

            cv2.imwrite(DET_PATH + str(n) + '.jpeg', frame)

        cv2.destroyAllWindows()
        break

    map.save(outfile='map.html')
Пример #24
0
    config.display()

    # Create model
    if args.command == "train":
        model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=args.logs)
    else:
        model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir=args.logs)

    # Select weights file to load
    if args.weights.lower() == "coco":
        weights_path = COCO_WEIGHTS_PATH
        # Download weights file
        if not os.path.exists(weights_path):
            utils.download_trained_weights(weights_path)
    elif args.weights.lower() == "last":
        # Find last trained weights
        weights_path = model.find_last()[1]
    elif args.weights.lower() == "imagenet":
        # Start from ImageNet trained weights
        weights_path = model.get_imagenet_weights()
    else:
        weights_path = args.weights

    # Load weights
    print("Loading weights ", weights_path)
    if args.weights.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
        model.load_weights(weights_path, by_name=True, exclude=[
def test_simple(params):
    """Test function."""
    import os
    import sys
    import coco
    import utils

    from mrcnn.config import Config

    ROOT_DIR = os.getcwd()
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    class CocoConfig(Config):
        """Configuration for training on MS COCO.
        Derives from the base Config class and overrides values specific
        to the COCO dataset.
        """
        # Give the configuration a recognizable name
        NAME = "coco"

        # We use a GPU with 12GB memory, which can fit two images.
        # Adjust down if you use a smaller GPU.
        IMAGES_PER_GPU = 2

        # Uncomment to train on 8 GPUs (default is 1)
        # GPU_COUNT = 8

        # Number of classes (including background)
        # NUM_CLASSES = 37  # COCO has 80 classes
        NUM_CLASSES = 81

    class InferenceConfig(CocoConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
        # MAX_GT_INSTANCES = 100
        # TRAIN_ROIS_PER_IMAGE = 50
        # BACKBONE = "resnet50" #not working at all!
        # RPN_ANCHOR_STRIDE = 2
        POST_NMS_ROIS_TRAINING = 1000
        POST_NMS_ROIS_INFERENCE = 500
        IMAGE_MIN_DIM = 400  # really much faster but bad results
        IMAGE_MAX_DIM = 512
        # DETECTION_MAX_INSTANCES = 50 #a little faster but some instances not recognized

    config = InferenceConfig()
    config.display()

    left = tf.placeholder(tf.float32,
                          [2, args.input_height, args.input_width, 3])
    modeld = MonodepthModel(params, "test", left, None)
    modelrcnn = modellib.MaskRCNN(mode="inference",
                                  model_dir=MODEL_DIR,
                                  config=config)
    modelrcnn.load_weights(COCO_MODEL_PATH, by_name=True)
    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
        'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
        'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
        'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
        'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]

    while True:
        ret, frame = capture.read()
        input_image = frame

        original_height, original_width, num_channels = input_image.shape
        input_image = scipy.misc.imresize(
            input_image, [args.input_height, args.input_width],
            interp='lanczos')
        input_image = input_image.astype(np.float32) / 255
        input_images = np.stack((input_image, np.fliplr(input_image)), 0)

        # SESSION
        config = tf.ConfigProto(allow_soft_placement=True)
        sess = tf.Session(config=config)

        # INIT
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coordinator = tf.train.Coordinator()

        disp = sess.run(modeld.disp_left_est[0],
                        feed_dict={left: input_images})
        disp_pp = post_process_disparity(disp.squeeze()).astype(np.float32)

        results = modelrcnn.detect([frame], verbose=0)
        r = results[0]

        disp_to_img = scipy.misc.imresize(disp_pp.squeeze(),
                                          [original_height, original_width])

        frame = display_instances(disp_to_img, r['rois'], r['masks'],
                                  r['class_ids'], class_names, r['scores'])

        cv2.imshow('frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Пример #26
0
if __name__ == '__main__':
    """
        test everything
    """
    import os
    import sys
    import coco
    import utils
    import model as modellib

    ROOT_DIR = os.getcwd()
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  #model stored dir
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(
            COCO_MODEL_PATH)  #if not exist download coco model

    class InferenceConfig(coco.CocoConfig):
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()
    config.display()

    model = modellib.MaskRCNN(  #model
        mode="inference", model_dir=MODEL_DIR, config=config)
    model.load_weights(
        COCO_MODEL_PATH,
        by_name=True)  #using alredy pretrainedwweights on coco dataset
    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
Пример #27
0
def person_blocker(args):

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    image = imageio.imread(args.image)

    # Create masks for all objects
    results = model.detect([image], verbose=0)
    r = results[0]

    if args.labeled:
        position_ids = [
            '[{}]'.format(x) for x in range(r['class_ids'].shape[0])
        ]
        visualize.display_instances(image, r['rois'],
                                    r['masks'], r['class_ids'],
                                    get_class_names(), position_ids)
        sys.exit()

    # Filter masks to only the selected objects
    objects = np.array(args.objects)

    # Object IDs:
    if np.all(np.chararray.isnumeric(objects)):
        object_indices = objects.astype(int)
    # Types of objects:
    else:
        selected_class_ids = np.flatnonzero(np.in1d(get_class_names(),
                                                    objects))
        object_indices = np.flatnonzero(
            np.in1d(r['class_ids'], selected_class_ids))

    mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2)

    # Replace object masks with noise
    mask_color = string_to_rgb_triplet(args.color)
    image_masked = image.copy()
    noisy_color = create_noisy_color(image, mask_color)
    image_masked[mask_selected > 0] = noisy_color[mask_selected > 0]

    imageio.imwrite('person_blocked.png', image_masked)

    # Create GIF. The noise will be random for each frame,
    # which creates a "static" effect

    images = [image_masked]
    num_images = 10  # should be a divisor of 30

    for _ in range(num_images - 1):
        new_image = image.copy()
        noisy_color = create_noisy_color(image, mask_color)
        new_image[mask_selected > 0] = noisy_color[mask_selected > 0]
        images.append(new_image)

    imageio.mimsave('person_blocked.gif', images, fps=30., subrectangles=True)
Пример #28
0
def person_blocker(args):

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR, config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(2.0)
    fps = FPS().start()


    while True:
        frame = vs.read()
        frame = imutils.resize(frame, width=1000)
        image = imageio.imread(frame)
        results = model.detect([image], verbose=0)
        r = results[0]

    # if args.labeled:
        # position_ids = ['[{}]'.format(x)
        #                 for x in range(r['class_ids'].shape[0])]
        # visualize.display_instances(img, r['rois'],
        #                             r['masks'], r['class_ids'],
        #                             get_class_names(), position_ids)
    #     sys.exit()

        # Filter masks to only the selected objects
        objects = np.array(args.objects)

        # Object IDs:
        if np.all(np.chararray.isnumeric(objects)):
            object_indices = objects.astype(int)
        # Types of objects:
        else:
            selected_class_ids = np.flatnonzero(np.in1d(get_class_names(),
                                                        objects))
            object_indices = np.flatnonzero(
                np.in1d(r['class_ids'], selected_class_ids))

        mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) if len(object_indices) > 0 else None

        # Replace object masks with noise
        # mask_color = string_to_rgb_triplet(args.color)
        image_masked = img.copy()
        # noisy_color = create_noisy_color(image, mask_color)
        noisy_color = create_noisy_color(img, (208, 130, 238))
        if mask_selected is not None:
            image_masked[mask_selected > 0] = noisy_color[mask_selected > 0]    

        image_masked = imutils.resize(image_masked, width=1500)
        cv2.imshow("Frame", image_masked)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

        update the FPS counter
        fps.update()

    fps.stop()
    print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

    do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()
    exit()
Пример #29
0
    config.display()

    # Create model
    if args.command == "train":
        model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=args.logs)
    else:
        model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir=args.logs)

    # Select weights file to load
    if args.weights.lower() == "coco":
        weights_path = COCO_WEIGHTS_PATH
        # Download weights file
        if not os.path.exists(weights_path):
            utils.download_trained_weights(weights_path)
    elif args.weights.lower() == "last":
        # Find last trained weights
        weights_path = model.find_last()[1]
    elif args.weights.lower() == "imagenet":
        # Start from ImageNet trained weights
        weights_path = model.get_imagenet_weights()
    else:
        weights_path = args.weights

    # Load weights
    print("Loading weights ", weights_path)
    if args.weights.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
        model.load_weights(weights_path, by_name=True, exclude=[
def continueTrainingCoCo_kfold(epochs):
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "model", "mask_rcnn_coco.h5")
    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    train_fold = [0, 1]
    for i in range(3):
        if i not in train_fold:
            print('skip fold', i)
            continue

        config = train_config()
        if FULLDATA:
            config.STEPS_PER_EPOCH = 10000
            config.LEARNING_RATE = config.LEARNING_RATE / 4
        config.NAME = '{}{}'.format(config.NAME, i)
        config.display()

        dataset = ShapesDataset(i, 0, -1)
        dataset.load_imgs(TRAIN_PATH)
        dataset.prepare()

        #print("Image Count: {}".format(len(dataset.image_ids)))
        #print("Class Count: {}".format(dataset.num_classes))

        dataset_valid = ShapesDataset(i, 1, 1)
        dataset_valid.load_imgs(VALID_PATH)
        dataset_valid.prepare()

        model = modellib.MaskRCNN(mode="training",
                                  config=config,
                                  model_dir=MODEL_DIR)

        search_dir = os.path.join(MODEL_DIR, config.NAME)
        if not os.path.exists(search_dir):
            os.mkdir(search_dir)
        print('search_dir', search_dir)
        model_path = model.find_last(search_dir)[1]
        if model_path:
            print("Loading weights from " + model_path)
            model.load_weights(model_path, by_name=True)
        elif config.TRAIN_FROM_COCO == 1:
            print('train from CoCo')
            model.load_weights(COCO_MODEL_PATH,
                               by_name=True,
                               exclude=[
                                   "mrcnn_class_logits", "mrcnn_bbox_fc",
                                   "mrcnn_bbox", "mrcnn_mask"
                               ])
        else:
            print('train from scrash')

        print('start training', i)
        model.train(dataset,
                    dataset_valid,
                    learning_rate=config.LEARNING_RATE * 2,
                    epochs=5,
                    layers="heads",
                    argument=config.ARGUMENT)

        model_path = model.find_last(search_dir)[1]
        print("Loading weights from " + model_path)
        model.load_weights(model_path, by_name=True)
        stage1_path = os.path.join(
            model.log_dir, '{}_{}.h5'.format(config.NAME.lower(), 'heads'))

        model_path = model.find_last(search_dir)[1]
        if model_path:
            print("Loading weights from " + model_path)
            model.load_weights(model_path, by_name=True)

        model.train(dataset,
                    dataset_valid,
                    learning_rate=config.LEARNING_RATE,
                    epochs=15,
                    layers="all",
                    argument=config.ARGUMENT)

        model_path = model.find_last(search_dir)[1]
        if model_path:
            print("Loading weights from " + model_path)
            model.load_weights(model_path, by_name=True)

        model.train(dataset,
                    dataset_valid,
                    learning_rate=config.LEARNING_RATE / 10,
                    epochs=50,
                    layers="all",
                    argument=config.ARGUMENT)

        from keras import backend as K
        K.clear_session()
Пример #31
0
import cv2
import numpy as np
import os
import sys
import coco
import utils
import model as modellib

ROOT_DIR = os.getcwd()
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)


class InferenceConfig(coco.CocoConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1


config = InferenceConfig()
config.display()

model = modellib.MaskRCNN(
    mode="inference", model_dir=MODEL_DIR, config=config
)
model.load_weights(COCO_MODEL_PATH, by_name=True)
class_names = [
    'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
    'bus', 'train', 'truck', 'boat', 'traffic light',
    'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
Пример #32
0
def person_blocker(args):

    # Required to load model, but otherwise unused
    ROOT_DIR = os.getcwd()
    COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    MODEL_DIR = os.path.join(ROOT_DIR, "logs")  # Required to load model

    if not os.path.exists(COCO_MODEL_PATH):
        utils.download_trained_weights(COCO_MODEL_PATH)

    # Load model and config
    config = InferenceConfig()
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    print("[INFO] starting video stream...")

    url = f"http://*****:*****@140.193.201.45:8080/shot.jpg"

    while True:

        # Create masks for all objects
        img_response = requests.get(url)
        img_array = np.array(bytearray(img_response.content), dtype=np.uint8)
        if img_array is not None:
            img = cv2.imdecode(img_array, -1)
        results = model.detect([img], verbose=0)
        r = results[0]

        # Filter masks to only the selected objects
        objects = np.array(args.objects)

        # Object IDs:
        if np.all(np.chararray.isnumeric(objects)):
            object_indices = objects.astype(int)
        # Types of objects:
        else:
            selected_class_ids = np.flatnonzero(
                np.in1d(get_class_names(), objects))
            object_indices = np.flatnonzero(
                np.in1d(r['class_ids'], selected_class_ids))

        mask_selected = np.sum(r['masks'][:, :, object_indices],
                               axis=2) if len(object_indices) > 0 else None

        # Replace object masks with noise
        # mask_color = string_to_rgb_triplet(args.color)
        image_masked = img.copy()
        # noisy_color = create_noisy_color(image, mask_color)
        noisy_color = create_noisy_color(img, (208, 130, 238))
        if mask_selected is not None:
            image_masked[mask_selected > 0] = noisy_color[mask_selected > 0]

        image_masked = imutils.resize(image_masked, width=1500)
        cv2.imshow("Frame", image_masked)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    exit()