Пример #1
0
def bgsub(vidfile_basename, threshold, quiet=False, drawBoxes=True):
    operator = BackgroundSubtractor(2000, threshold, True)
    # Learn the bg
    operator.model_bg2(VIDEO_DIR + vidfile_basename)

    tp_t = fp_t = fn_t = p_t = n_t = 0

    video = cv2.VideoCapture(VIDEO_DIR + vidfile_basename)
    ret, frame = video.read()
    frame_num = 0
    while ret:
        mask = operator.apply(frame)
        mask = tools.morph_openclose(mask)
        mask_binary = (mask == 255).astype(np.uint8)

        gt_filename = "{0}/{1}/{2}.jpg.seg.bmp".format(GT_IMG_DIR, vidfile_basename, frame_num)
        if os.path.exists(gt_filename):
            if not quiet:
                cv2.imshow("Ground truth", cv2.imread(gt_filename) * 255)
            tp, fp, fn = compare_response_to_truth(mask_binary, gt_filename)
            # print("True Pos: {0}\nFalse Pos: {1}".format(tp, fp))
            pos_detected, neg_detected = class_counter.count_posneg(mask_binary)
            tp_t += tp
            fp_t += fp
            fn_t += fn
            p_t += pos_detected
            n_t += neg_detected
            # print("Foreground pixels: {0}\nBackground pixels: {1}".format(pos_detected, neg_detected))

        if not quiet:
            mask = ((mask == 255) * 255).astype(np.uint8)
            cv2.imshow("Mask", mask)
            if drawBoxes:
                blob_detect(mask, frame)
            else:
                cv2.imshow("Frame", frame)
                
        ret, frame = video.read()
        frame_num += 1
        if handle_keys() is 1: break

    with np.errstate(invalid='ignore'):
        precision = np.float64(tp_t) / (tp_t + fp_t)
        recall = np.float64(tp_t) / (tp_t + fn_t)
    if np.isinf(precision) or np.isnan(precision):
        precision = 1
    if np.isinf(recall) or np.isnan(recall):
        recall = 1
    return precision, recall
Пример #2
0
def cascade_detect(vidfile_basename, min_neighbors, quiet=False):
    cascade = cv2.CascadeClassifier("../classifier/v2verticaldown/cascade.xml")
    cascade2 = cv2.CascadeClassifier("../classifier/v2leftside/cascade.xml")
    print(quiet)
    tp_t = fp_t = fn_t = 0

    video = cv2.VideoCapture(VIDEO_DIR + vidfile_basename)
    ret, frame = video.read()
    frame_h, frame_w, _ = frame.shape
    frame_num = 0
    while ret:
        gt_filename = "{0}/{1}/{2}.jpg.seg.bmp".format(GT_IMG_DIR, vidfile_basename, 
            frame_num)

        bees = cascade.detectMultiScale(frame, minNeighbors=min_neighbors, scaleFactor=1.025)
        mask_binary = np.zeros((frame_h, frame_w))
        for x, y, w, h in bees:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
            mask_binary[y:y+h, x:x+w] = 1
        if not quiet:
            cv2.imshow("Detections", mask_binary * 255)
            cv2.imshow("Frame- {0}".format(min_neighbors), frame)

        if os.path.exists(gt_filename):
            tp, fp, fn = compare_response_to_truth(mask_binary, gt_filename, cascade=True)
            tp_t += tp
            fp_t += fp
            fn_t += fn
            # precision, recall = area_precision_recall(mask_binary, gt_filename)
            if not quiet:
                cv2.imshow("Ground truth", cv2.imread(gt_filename) * 255)

        ret, frame = video.read()
        frame_num += 1
        if handle_keys() is 1: break

    with np.errstate(invalid='ignore'):
        precision = np.float64(tp_t) / (tp_t + fp_t)
        recall = np.float64(tp_t) / (tp_t + fn_t)
    if np.isinf(precision) or np.isnan(precision):
        precision = 1
    if np.isinf(recall) or np.isnan(recall):
        recall = 1

    return precision, recall