Ejemplo n.º 1
0
def detect_image_folder(image_folder, output_path, model_config_path,
                        model_checkpoint_path):
    """
    Runs object detection on a folder of images. Saves the results to a csv
    """

    img_reader = ImageReader(image_folder)

    model = Detector(model_config_path, model_checkpoint_path)
    results = model.detect_images(img_reader.load_images(),
                                  img_reader.filenames)

    results.to_csv(output_path, index=None)
Ejemplo n.º 2
0
def full_run_single(video_id,
                    video_dir,
                    static_dir,
                    frame_by_frame_results_dir,
                    static_results_dir,
                    crop_boxes_dir,
                    ignore_mask_dir,
                    detector_config_path,
                    detector_model_path,
                    reid_model_path,
                    reid_model_backbone,
                    crop_results_dir,
                    anomaly_results_dir,
                    bg_interval=4,
                    bg_alpha=0.05,
                    bg_start_frame=1,
                    bg_threshold=5,
                    raw_detect_interval=30,
                    crop_min_obj_size=8,
                    crop_row_capacity=3,
                    crop_box_aspect_ratio=2,
                    ignore_count_thresh=0.08,
                    ignore_area_thresh=2000,
                    ignore_score_thresh=0.1,
                    ignore_gau_sigma=3,
                    abnormal_duration_thresh=60,
                    detect_duration_thresh=6,
                    undetect_duration_thresh=8,
                    bbox_score_thresh=0.3,
                    light_thresh=0.8,
                    anomaly_thresh=0.8,
                    similarity_thresh=0.95,
                    suspicious_duration_thresh=18,
                    detector_verbose_interval=20,
                    verbose=True):
    """
    Runs the full anomaly detection pipeline on a video

    video_id: video id/name
    video_dir: folder the video is in
    static_dir: folder to put the background images in
    frame_by_frame_results_dir: folder to put the raw video detection results in
    static_results_dir: folder to put the background image detection results in
    crop_boxes_dir: folder to put the crop boxes in
    ignore_mask_dir: folder to put the ignore region mask in

    detector_config_path: path to detector configuration file
    detector_model_path: path to detector model checkpoint
    reid_model_path: path to re-ID model checkpoint
    reid_model_backbone: re-ID model backbone. eg. "resnet50"

    bg_interval, bg_alpha, bg_start_frame, bg_threshold: see calc_bg_full_video function
    raw_detect_interval: number of frames between detection on raw video
    crop_min_obj_size, crop_row_capacity, crop_box_aspect_ratio: see create_crop_boxes function
    ignore_count_thresh, ignore_area_thresh, ignore_score_thresh, ignore_gau_sigma: see create_ignore_mask function
    abnormal_duration_thresh, detect_duration_thresh, undetect_duration_thresh, bbox_score_thresh,
        light_thresh, anomaly_thresh, similarity_thresh, suspicious_duration_thresh:
            See get_anomalies function

    detector_verbose_interval: detector progress printing interval
    verbose: verbose printing


    """

    # Set up file paths
    video_path = os.path.join(video_dir, f"{video_id}.mp4")
    static_images_folder = os.path.join(static_dir, f"{video_id}")
    fbf_results_path = os.path.join(frame_by_frame_results_dir,
                                    f"{video_id}.csv")
    static_results_path = os.path.join(static_results_dir, f"{video_id}.csv")
    crop_boxes_path = os.path.join(crop_boxes_dir, f"{video_id}.csv")
    crop_results_path = os.path.join(crop_results_dir, f"{video_id}.csv")
    ignore_mask_path = os.path.join(ignore_mask_dir, f"{video_id}.npy")
    anomaly_results_path = os.path.join(anomaly_results_dir, f"{video_id}.csv")

    # Create folders
    os.makedirs(static_images_folder, exist_ok=True)
    os.makedirs(frame_by_frame_results_dir, exist_ok=True)
    os.makedirs(static_results_dir, exist_ok=True)
    os.makedirs(crop_boxes_dir, exist_ok=True)
    os.makedirs(crop_results_dir, exist_ok=True)
    os.makedirs(ignore_mask_dir, exist_ok=True)
    os.makedirs(anomaly_results_dir, exist_ok=True)

    # Read Video
    raw_video = VideoReader(video_path)

    # bg modeling
    print("Creating background...")
    calc_bg_full_video(video_path, static_images_folder, bg_interval, bg_alpha,
                       bg_start_frame, bg_threshold, verbose)

    # Detection
    detector = Detector(detector_config_path,
                        detector_model_path,
                        detector_verbose_interval,
                        class_restrictions=None)
    # class_names = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
    #                'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
    #                'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
    #                'tvmonitor')
    # detector.model.CLASSES = class_names
    # detector.class_labels = class_names
    ## Raw Video
    print("Detecting raw video...")
    raw_images, raw_frame_nums = raw_video.load_video(raw_detect_interval)
    fbf_results = detector.detect_images(raw_images, raw_frame_nums)
    fbf_results.to_csv(fbf_results_path, index=False)

    ## Static Images
    static_reader = ImageReader(static_images_folder)
    static_frame_names = list(
        map(lambda f: int(f[:-4]),
            static_reader.filenames))  # "123.jpg" -> 123

    print("Detecting background...")
    static_results = detector.detect_images(static_reader.load_images(),
                                            static_frame_names)
    static_results.to_csv(static_results_path, index=False)

    # Perspective Cropping
    print("Creating crop boxes...")
    create_crop_boxes(
        fbf_results_path, crop_boxes_path, raw_video.img_shape,
        crop_min_obj_size, crop_row_capacity,
        crop_box_aspect_ratio)  # either static/fbf results should work

    # Should be able to use this in place of normal static images. Doesnt look feasable atm, way too long detection time
    crop_boxes = pd.read_csv(crop_boxes_path).values
    print("Detecting cropped background...")
    crop_detect_results = detector.detect_images(static_reader.load_images(),
                                                 static_frame_names,
                                                 crop_boxes=crop_boxes)
    crop_detect_results.to_csv(crop_results_path)

    #     # Ignore Region
    print("Creating ingore mask...")
    create_ignore_mask(fbf_results_path, ignore_mask_path, raw_video.img_shape,
                       ignore_count_thresh, ignore_area_thresh,
                       ignore_score_thresh, ignore_gau_sigma)

    # Detect anomalies
    print("Detecting anomalies...")
    anomalies = get_anomalies_preprocessed(
        video_path, reid_model_path, fbf_results_path, static_results_path,
        ignore_mask_path, reid_model_backbone, bg_start_frame, bg_interval,
        abnormal_duration_thresh, detect_duration_thresh,
        undetect_duration_thresh, bbox_score_thresh, light_thresh,
        anomaly_thresh, similarity_thresh, suspicious_duration_thresh, verbose)

    if anomalies is not None:
        anomaly_event_times = get_overlapping_time(anomalies)

        # Save results
        print("Saving Results...")
        anomalies.to_csv(anomaly_results_path, index=False)

        return anomalies, anomaly_event_times

    else:
        return [], []