def prepare_batch_frames(video_path):
    fg_bg = cv2.createBackgroundSubtractorMOG2()
    video = imageio.get_reader(video_path, 'ffmpeg')
    frame_batch = np.zeros((240, 240))
    frame_batch = frame_batch.reshape((1, 240, 240))

    for i in range(len(video)):
        frame = video.get_data(i)
        edged_image = cv_utils.apply_canny(frame, 50, 150)
        rect_pts = detect_person(frame)
        fg_mask = fg_bg.apply(frame)
        fg_mask = fg_mask[int(rect_pts[0]):int(rect_pts[2] - 120),
                          int(rect_pts[1]):int(rect_pts[3] - 50)]
        edged_image = edged_image[int(rect_pts[0]):int(rect_pts[2] - 120),
                                  int(rect_pts[1]):int(rect_pts[3] - 50)]
        fg_mask[fg_mask > 0] = 255.0
        print(fg_mask.shape)
        fg_mask = cv2.addWeighted(fg_mask, 1, edged_image, 1, 0)
        # fg_mask = cv2.bitwise_and(fg_mask, edged_image)
        reshaped_img = cv_utils.resize(fg_mask, (240, 240))
        reshaped_img = reshaped_img / 255.0
        cv2.imshow("bg_subtraction", reshaped_img)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

        reshaped_img = reshaped_img.reshape((1, 240, 240))
        frame_batch = np.vstack((frame_batch, reshaped_img))

    frame_batch = frame_batch.reshape(frame_batch.shape[0], 240, 240, 1)
    frame_batch = frame_batch[2:, :, :, :]

    return frame_batch
def process_image(image):
    edged_image = cv_utils.apply_canny(image, 50, 150)
    rect_pts = detect_person(image)
    fg_mask = fg_bg.apply(image)
    fg_mask = fg_mask[int(rect_pts[0]):int(rect_pts[2] - 120),
                      int(rect_pts[1]):int(rect_pts[3] - 50)]
    edged_image = edged_image[int(rect_pts[0]):int(rect_pts[2] - 120),
                              int(rect_pts[1]):int(rect_pts[3] - 50)]
    fg_mask[fg_mask > 0] = 255.0
    # print(fg_mask.shape)
    fg_mask = cv2.addWeighted(fg_mask, 1, edged_image, 1, 0)
    reshaped_img = cv_utils.resize(fg_mask, (500, 500))
    reshaped_img = np.dstack((reshaped_img, np.zeros_like(reshaped_img),
                              np.zeros_like(reshaped_img)))
    # cv2.imshow("bg_subtraction", reshaped_img)
    return reshaped_img
def prepare_batch_frames_from_bg_data(video_path,
                                      frame_limit=109,
                                      resize=(240, 240)):
    """

    This function prepares batches by reading the video and extracting
    frames which is used as one mini-batch in training

    :param video_path: string
                       path to video which is to be read

    :param frame_limit: int
                        limiting the number frames which is to be returned
                        if the number of frames in the video is > frame_limit
                        then random sampling will be carried out to extract frames exactly of frame_limit
    :param resize: tuple of shape 2 elements
                   resizing the frames
    :return: frame_batch : numpy array of shape (batch_size, height, width, 1)
    """
    sampling = False
    video = imageio.get_reader(video_path, 'ffmpeg')
    frame_batch = np.zeros(resize)
    frame_batch = frame_batch.reshape((1, resize[0], resize[1]))
    if frame_limit < len(video):
        sampling = True
        sampling_list = random.sample(range(0, len(video) - 1), frame_limit)

    for i in range(len(video)):
        if sampling and i not in sampling_list:
            continue
        frame = video.get_data(i)
        red_channel = frame[:, :, 0]
        red_channel = cv_utils.resize(red_channel, resize)
        red_channel[red_channel > 0] == 255.0
        red_channel = red_channel / 255.0
        cv2.imshow("bg_subtraction", red_channel)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

        red_channel = red_channel.reshape((1, resize[0], resize[1]))
        frame_batch = np.vstack((frame_batch, red_channel))

    frame_batch = frame_batch.reshape(frame_batch.shape[0], resize[0],
                                      resize[1], 1)
    frame_batch = frame_batch[2:, :, :, :]

    return frame_batch
Example #4
0
def process_image(image):

    edged_image = cv_utils.apply_canny(image, 50, 150)
    rect_pts = detect_person(image)
    print("**********************************")
    print(rect_pts)
    print("**********************************")
    fg_bg = cv2.createBackgroundSubtractorMOG2()
    IMAGE_SIZE = (12, 8)

    pic_rand = random.randint(1, 100)

    # plt.figure(figsize=IMAGE_SIZE)
    # plt.imshow(image)
    # plt.savefig("mask/before_mask_bg_temp{0}.png".format(pic_rand))

    fg_mask = fg_bg.apply(image)

    # plt.figure(figsize=IMAGE_SIZE)
    # plt.imshow(fg_mask)
    # plt.savefig("mask/after_mask_bg_temp{0}.png".format(pic_rand))

    fg_mask = fg_mask[int(rect_pts[0]):int(rect_pts[2] - 120),
                      int(rect_pts[1]):int(rect_pts[3] - 50)]
    edged_image = edged_image[int(rect_pts[0]):int(rect_pts[2] - 120),
                              int(rect_pts[1]):int(rect_pts[3] - 50)]
    fg_mask[fg_mask > 0] = 255.0
    # print(fg_mask.shape)
    fg_mask = cv2.addWeighted(fg_mask, 0, edged_image, 1, 0)
    reshaped_img = cv_utils.resize(fg_mask, (500, 500))
    reshaped_img = np.dstack((reshaped_img, np.zeros_like(reshaped_img),
                              np.zeros_like(reshaped_img)))
    # cv2_imshow(reshaped_img)
    IMAGE_SIZE = (12, 8)
    # plt.figure(figsize=IMAGE_SIZE)
    # plt.imshow(reshaped_img)
    # plt.savefig("bg_temp.png")
    return reshaped_img