Example #1
0
def train(train_indexes, BG_img, params):
    folder = params["folder"]
    marginX = params["marginX"]
    marginY = params["marginY"]
    neg_weight = params["neg_weight"]
    method = params["method"]
    feature = params["feature"]

    train_feature_count = 0
    train_features, train_labels = [], []

    print "Extracting positive training features..."

    # Read positive train features
    for i in tqdm(range(len(train_indexes))):
        img = img_read(folder, train_indexes[i])
        #motion_img = read_motion_image(folder, train_indexes[i], BG_img)
        height, width = img.shape
        bboxes = add_bbox_margin(read_bboxes(folder, train_indexes[i]),
                                 marginX, marginY, height, width)

        for j in bboxes:
            img_cut = img[j[0]:j[1], j[2]:j[3]]
            #motion_img_cut = motion_img[j[0]:j[1], j[2]:j[3]]
            train_feature_count += 1
            #train_features.append(extract(img_cut, motion_img_cut, method, feature))
            train_features.append(extract(img_cut, None, method, feature))
            train_labels.append(1)

    print "Positive training features are extracted."
    print "Extracting negative training features..."

    pos_train_feature_count = train_feature_count

    # Read negative train features
    for j in tqdm(range(pos_train_feature_count * neg_weight)):
        i = sample(train_indexes, 1)[0]

        img = img_read(folder, i)

        height, width = img.shape
        bboxes = add_bbox_margin(read_bboxes(folder, i), marginX, marginY,
                                 height, width)

        neg_bb = rand_bbox(bboxes, height, width)

        if overlaps(neg_bb, bboxes) != -1:
            continue

        #motion_img = read_motion_image(folder, i, BG_img)

        img_cut = img[neg_bb[0]:neg_bb[1], neg_bb[2]:neg_bb[3]]
        #motion_img_cut = motion_img[neg_bb[0]:neg_bb[1], neg_bb[2]:neg_bb[3]]
        train_feature_count += 1
        #train_features.append(extract(img_cut, motion_img_cut, method, feature))
        train_features.append(extract(img_cut, None, method, feature))
        train_labels.append(-1)
    print "Negative training features are extracted."

    return train_features, train_labels, train_feature_count
Example #2
0
def test(test_indexes, BG_img, params):
    folder = params["folder"]
    marginX = params["marginX"]
    marginY = params["marginY"]
    neg_weight = params["neg_weight"]
    method = params["method"]
    feature = params["feature"]

    test_features, test_labels = [], []
    test_feature_count = 0

    print "Extracting positive test features..."

    # Read positive test examples
    for i in tqdm(range(len(test_indexes))):
        img = img_read(folder, test_indexes[i])
        #motion_img = read_motion_image(folder, test_indexes[i], BG_img)

        height, width = img.shape
        bboxes = add_bbox_margin(read_bboxes(folder, test_indexes[i]), marginX, marginY, height, width)

        for j in bboxes:
            img_cut = img[j[0]:j[1], j[2]:j[3]]
            #motion_img_cut = motion_img[j[0]:j[1], j[2]:j[3]]
            test_feature_count += 1
            #test_features.append(extract(img_cut, motion_img_cut, method, feature))
            test_features.append(extract(img_cut, None, method, feature))
            test_labels.append(1)

    pos_test_feature_count = test_feature_count

    print "Positive test features are extracted."
    print "Extracting negative test features..."

    # Read negative test examples
    for j in tqdm(range(pos_test_feature_count*neg_weight)):
        i = sample(test_indexes, 1)[0]

        img = img_read(folder, i)

        height, width = img.shape
        bboxes = add_bbox_margin(read_bboxes(folder, i), marginX, marginY, height, width)

        neg_bb = rand_bbox(bboxes, height, width);

        if overlaps(neg_bb, bboxes) != -1:
            continue

        #motion_img = read_motion_image(folder, i, BG_img)

        img_cut = img[neg_bb[0]:neg_bb[1], neg_bb[2]:neg_bb[3]]
        #motion_img_cut = motion_img[neg_bb[0]:neg_bb[1], neg_bb[2]:neg_bb[3]]
        test_feature_count += 1
        #test_features.append(extract(img_cut, motion_img_cut, method, feature))
        test_features.append(extract(img_cut, None, method, feature))
        test_labels.append(-1)
    
    print "Negative test features are extracted."

    return test_features, test_labels, test_feature_count
Example #3
0
def bootstrap(bootstrap_indexes, BG_img, params, trf, trl, trfc, svm):
    folder = params["folder"]
    marginX = params["marginX"]
    marginY = params["marginY"]
    neg_weight = params["neg_weight"]
    method = params["method"]
    feature = params["feature"]

    train_features = trf
    train_labels = trl
    train_feature_count = trfc

    print "Starting bootstrapping..."

    # Bootstrapping
    for i in tqdm(range(len(bootstrap_indexes))):
        img = img_read(folder, bootstrap_indexes[i])
        #motion_img = read_motion_image(folder, bootstrap_indexes[i], BG_img)
        bboxes = read_bboxes(folder, bootstrap_indexes[i])

        #detections = detect_vehicles(img, motion_img, svm, params)
        detections = detect_vehicles(img, None, svm, params)

        hard_negatives = []
        for j in detections:
            if overlaps(j, bboxes) == -1:
                hard_negatives.append(j)

        height, width = img.shape
        hard_negatives = add_bbox_margin(hard_negatives, marginX, marginY,
                                         height, width)

        for j in hard_negatives:
            img_cut = img[j[0]:j[1], j[2]:j[3]]
            #motion_img_cut = motion_img[j[0]:j[1], j[2]:j[3]]
            train_feature_count += 1
            #train_features.append(extract(img_cut, motion_img_cut, method, feature))
            train_features.append(extract(img_cut, None, method, feature))
            train_labels.append(-1)

    print "Bootstrap finished."

    return train_features, train_labels
Example #4
0
def bootstrap(bootstrap_indexes, BG_img, params, trf, trl, trfc, svm):
    folder = params["folder"]
    marginX = params["marginX"]
    marginY = params["marginY"]
    neg_weight = params["neg_weight"]
    method = params["method"]
    feature = params["feature"]

    train_features = trf
    train_labels = trl
    train_feature_count = trfc

    print "Starting bootstrapping..."

    # Bootstrapping
    for i in tqdm(range(len(bootstrap_indexes))):
        img = img_read(folder, bootstrap_indexes[i])
        #motion_img = read_motion_image(folder, bootstrap_indexes[i], BG_img)
        bboxes = read_bboxes(folder, bootstrap_indexes[i])

        #detections = detect_vehicles(img, motion_img, svm, params)
        detections = detect_vehicles(img, None, svm, params)

        hard_negatives = []
        for j in detections:
            if overlaps(j, bboxes) == -1:
                hard_negatives.append(j)

        height, width = img.shape
        hard_negatives = add_bbox_margin(hard_negatives, marginX, marginY, height, width)

        for j in hard_negatives:
            img_cut = img[j[0]:j[1], j[2]:j[3]]
            #motion_img_cut = motion_img[j[0]:j[1], j[2]:j[3]]
            train_feature_count += 1
            #train_features.append(extract(img_cut, motion_img_cut, method, feature))
            train_features.append(extract(img_cut, None, method, feature))
            train_labels.append(-1)
    
    print "Bootstrap finished."

    return train_features, train_labels