Beispiel #1
0
def compute_map_gt_det(det_file, gt_file):
    det_list = read_detections(det_file)
    gt_list = read_detections(gt_file)

    frames = []

    for i in range(0, len(det_list)):
        frame = Frame(i)
        frame.detections = det_list[i]
        frame.ground_truth = gt_list[i]
        frames.append(frame)

    mAP = mean_average_precision(frames, ignore_classes=True)

    return mAP
def create_siamese_dataset(sequence, camera):
    AICity_dataset_path = '../datasets/AICity_data/train/' + sequence + '/' + camera + '/'
    siamese_dataset_path = '../datasets/full_siamese_data/'

    annotation_file = 'gt/gt.txt'
    gt_detections = read_detections(AICity_dataset_path + annotation_file)

    frames = len(gt_detections)

    for frame in range(frames):
        try:
            frame_name = "frame{:04d}.jpg".format(frame)
            img = cv2.imread(AICity_dataset_path + 'frames/' + frame_name)
            print(AICity_dataset_path + 'frames/' + frame_name)

            for detection in gt_detections[frame]:
                xtl, ytl = detection.top_left
                w = detection.width
                h = detection.height

                class_path = sequence + '-' + str(detection.id) + '/'
                if not os.path.exists(siamese_dataset_path + class_path):
                    os.mkdir(siamese_dataset_path + class_path)
                cropped_img = img[ytl:ytl + h, xtl:xtl + w]
                cv2.imwrite(siamese_dataset_path + class_path + frame_name,
                            cropped_img)
        except:
            import code
            code.interact(local=dict(globals(), **locals()))
def week2_adaptive_hsv(video: Video, debug=False) -> Iterator[Frame]:
    model_mean, model_std = get_background_model(video,
                                                 int(2141 * 0.25),
                                                 total_frames=int(2141 * 0.25),
                                                 pixel_value=PixelValue.HSV)

    ground_truth = read_detections(
        '../datasets/AICity_data/train/S03/c010/gt/gt.txt')

    frame_id = int(2141 * 0.25)
    roi = cv2.cvtColor(
        cv2.imread('../datasets/AICity_data/train/S03/c010/roi.jpg'),
        cv2.COLOR_BGR2GRAY)
    for im, mask in gaussian_model_adaptive(video,
                                            int(2141 * 0.25),
                                            model_mean,
                                            model_std,
                                            total_frames=int(2141 * 0.10),
                                            pixel_value=PixelValue.HSV,
                                            alpha=1.75,
                                            rho=0.01):
        mask = mask & roi
        if debug:
            cv2.imshow('f', mask)
            cv2.waitKey()
        mask = opening(mask, 7)
        if debug:
            cv2.imshow('f', mask)
            cv2.waitKey()
        mask = closing(mask, 35)
        if debug:
            cv2.imshow('f', mask)
            cv2.waitKey()
        mask, detections = find_boxes(mask)

        frame = Frame(frame_id)
        frame.detections = detections
        frame.ground_truth = ground_truth[frame_id]

        if debug:
            mask2 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
            for detection in detections:
                cv2.rectangle(
                    mask2,
                    (int(detection.top_left[1]), int(detection.top_left[0])),
                    (int(detection.get_bottom_right()[1]),
                     int(detection.get_bottom_right()[0])), (0, 255, 0), 5)
            for gt in ground_truth[frame_id]:
                cv2.rectangle(mask2,
                              (int(gt.top_left[1]), int(gt.top_left[0])),
                              (int(gt.get_bottom_right()[1]),
                               int(gt.get_bottom_right()[0])), (255, 0, 0), 5)
            cv2.imshow('f', mask2)
            cv2.waitKey()

        yield im, mask, frame

        frame_id += 1
Beispiel #4
0
    def get_frames(self) -> Iterable[Frame]:
        video = cv2.VideoCapture(os.path.join(self.video_path, "vdo.avi"))
        full_detections = read_detections(
            os.path.join(self.video_path, "det/det_yolo3.txt"))
        if os.path.exists(os.path.join(self.video_path, "gt/gt.txt")):
            full_ground_truth = read_detections(
                os.path.join(self.video_path, "gt/gt.txt"))
        else:
            full_ground_truth = []

        count = 0
        while video.isOpened():
            ret, frame = video.read()
            if ret:
                det = full_detections[count] if count < len(
                    full_detections) else []
                gt = full_ground_truth[count] if count < len(
                    full_ground_truth) else []
                yield Frame(count, det, gt, frame)
                count += 1
            else:
                video.release()
def main():
    start_frame = 1440
    end_frame = 1789

    gt = read_annotations('../annotations', start_frame, end_frame)

    alg = 'mask_rcnn'

    detections = read_detections('../datasets/AICity_data/train/S03/c010/det/det_{0}.txt'.format(alg))

    kalman = KalmanTracking()
    for i in range(start_frame, end_frame):
        f = Frame(i)
        f.detections = detections[i]
        f.ground_truth = gt[i - start_frame]
        kalman(f)
        print(seq(f.detections).map(lambda d: d.id).to_list())
Beispiel #6
0
def week2_soa(video: Video, debug=False) -> Iterator[Frame]:
    th = 150
    frame_id = 0
    fgbg = cv.createBackgroundSubtractorMOG2()

    ground_truth = read_detections(
        '../datasets/AICity_data/train/S03/c010/gt/gt.txt')
    roi = cv.cvtColor(
        cv.imread('../datasets/AICity_data/train/S03/c010/roi.jpg'),
        cv.COLOR_BGR2GRAY)

    for im in tqdm(video.get_frames(),
                   total=2141,
                   file=sys.stdout,
                   desc='Training model...'):
        mask = fgbg.apply(im)
        mask[mask < th] = 0

        mask.astype(np.uint8) * 255

        mask = mask & roi

        mask = opening(mask, 5)
        # cv.imshow('f', mask)
        # cv.waitKey()

        mask = closing(mask, 25)
        # cv.imshow('f', mask)
        # cv.waitKey()

        mask, detections = find_boxes(mask)

        frame = Frame(frame_id)
        frame.detections = detections
        frame.ground_truth = ground_truth[frame_id]

        frame_id += 1

        yield im, mask, frame
Beispiel #7
0
def main():
    video = Video("../datasets/AICity_data/train/S03/c010/vdo.avi")

    gt = read_annotations('../annotations', start_frame, end_frame)
    """
        DETECTIONS
    """
    det_algs = ['yolo3', 'mask_rcnn', 'ssd512']
    for alg in det_algs:
        detections = read_detections(
            '../datasets/AICity_data/train/S03/c010/det/det_{0}.txt'.format(
                alg))
        detections = detections[start_frame:end_frame + 1]

        frames = []

        # roi = cv2.imread('../datasets/AICity_data/train/S03/c010/roi.jpg')

        for im, f in seq(video.get_frames(
                start_frame_number=start_frame)).take(end_frame - start_frame +
                                                      1):
            f.ground_truth = gt[f.id]
            f.detections = detections[f.id]
            frames.append(f)

            if make_video:
                make_video_frame(im, f, frames)

        iou_over_time(frames)
        mAP = mean_average_precision(frames)
        print(alg, " mAP:", mAP)
    """
        DETECTIONS FROM ALTERED GROUND TRUTH 
    """
    frames = []

    for im, f in seq(video.get_frames()).take(end_frame - start_frame + 1):
        f.ground_truth = gt[f.id]
        f.detections = alter_detections(f.ground_truth)
        frames.append(f)

        if make_video:
            make_video_frame(im, f, frames)

    iou_over_time(frames)
    mAP = mean_average_precision(frames)
    print('Random alteration', " mAP:", mAP)
    """
        OPTICAL FLOW 
    """
    of_det_1 = read_optical_flow(
        '../datasets/optical_flow/detection/LKflow_000045_10.png')
    of_det_2 = read_optical_flow(
        '../datasets/optical_flow/detection/LKflow_000157_10.png')

    of_gt_1 = read_optical_flow('../datasets/optical_flow/gt/000045_10.png')
    of_gt_2 = read_optical_flow('../datasets/optical_flow/gt/000157_10.png')

    img_1 = cv2.imread('../datasets/optical_flow/img/000045_10.png')
    img_2 = cv2.imread('../datasets/optical_flow/img/000157_10.png')

    msen_of = msen(of_det_2, of_gt_2)
    pepn_of = pepn(of_det_2, of_gt_2)

    print(msen_of, pepn_of)
    show_optical_flow(of_gt_1)
    show_optical_flow_arrows(img_1, of_gt_1)

    msen_45 = msen(of_det_1, of_gt_1, plot=True)
    pepn_45 = pepn(of_det_1, of_gt_1)
    print("Sequence 045: MSEN", msen_45, "PEPN", pepn_45)

    msen_157 = msen(of_det_2, of_gt_2, plot=True)
    pepn_157 = pepn(of_det_2, of_gt_2)
    print("Sequence 157: MSEN", msen_157, "PEPN", pepn_157)

    show_optical_flow(of_gt_1)