예제 #1
0
def test_frame_filter_for_movement_detection(with_godec=False, savegif=False):
    noise_remover = FrameKalmanFilter()
    save_path = kalman_pics_path + basename(data_path)
    create_folder_if_absent(save_path)
    data_i = get_frame_GREY(data[0])
    subplt_titles = ["Original Image", "Movement", "KF Original"]
    if with_godec:
        subplt_titles.extend(["With Godec", "Movement", "KF Godec"])
        ims = init_comparison_plot(data_i, subplt_titles, 2, 3)
        M, LS, L, S, width, height = bs_godec(data)
        noise_remover2 = FrameKalmanFilter()
    elif not with_godec:
        ims = init_comparison_plot(data_i, subplt_titles, 1, 3)
        
    for i in tqdm(range(len(data))):
        data_i = get_frame_GREY(data[i])
        processed = noise_remover.process_frame(data_i)
        movement = normalize_frame(processed - data_i)
        imgs = [data_i, movement, processed]
        if with_godec:
            L_frame = normalize_frame(L[:, i].reshape(width, height).T)
            S_frame = normalize_frame(S[:, i].reshape(width, height).T)
            data_i2 = cleaned_godec_img(L_frame, S_frame)
            processed2 = noise_remover2.process_frame(data_i2)
            movement2 = normalize_frame(processed2 - data_i2)
            imgs.extend([data_i2, movement2, processed2])
            
        update_comparison_plot(ims, imgs)
        plt.savefig(save_path + "/{}.png".format(i)) #make png
    if savegif:
        gif_pics = [save_path+ "/{}.png".format(i) for i in range(len(data))]
        gif_path = kalman_gifs_path+basename(data_path)+"_movement.gif"
        write_gif(gif_pics, gif_path, 0, len(gif_pics), fps=20)
        optimize_size(gif_path)
예제 #2
0
def test_foreground_probability(savegif=False):

    subplt_titles = ["Original", "Foreground %"]
    ims = init_comparison_plot(get_frame_GREY(data[0]),
                               subplt_titles,
                               1,
                               2,
                               title="Probabilistic Foreground Detection")

    for i in range(len(data)):
        frame = get_frame(data[i])
        L_frame = L[:, i].reshape(width, height).T
        S_frame = S[:, i].reshape(width, height).T
        L_probability = foreground_probability(L_frame, frame)

        # this condition is now abstracted as the function cleaned_godec_img() in background_subtractio.py
        S_probability = foreground_probability(S_frame, frame)
        if np.amax(L_probability) > np.amax(S_probability):
            probability = L_probability
            img = L_frame
        else:
            probability = S_probability
            img = S_frame

        images = [normalize_frame(img), normalize_frame(probability)]
        update_comparison_plot(ims, images)
        create_folder_if_absent("testpics")
        plt.savefig("testpics/{}.png".format(i))

    if savegif:
        files = get_all_files("testpics")
        write_gif_from_pics(files,
                            "probabilistic_foreground_detection.gif",
                            fps=20)
def optical_flow_dense(files):
    # Perform Godec first on all frames
    M, LS, L, S, width, height = bs_godec(files)
    first_frame = get_frame(files[0])

    # frames to be compared is after godec and postprocessing
    godec_frame, probability = get_godec_frame(M, L, S, width, height, 0)
    img, centroids = postprocess_img(godec_frame, all_images=False)
    prev_gray = img
    ims = init_comparison_plot(first_frame,
                               ["Original", "Thresholded", "FlowS"], 1, 3)
    test = cv.cvtColor(first_frame.astype("uint8"), cv.COLOR_GRAY2BGR)
    hsv_mask = np.zeros_like(test)
    hsv_mask[..., 1] = 255
    window_name = "Dense Optical Flow"

    counter = 1

    while counter < len(files):
        print(counter)
        godec_frame, probability = get_godec_frame(M, L, S, width, height,
                                                   counter)
        img, centroids = postprocess_img(godec_frame, all_images=False)
        next_gray = img
        flow = cv.calcOpticalFlowFarneback(prev_gray,
                                           next_gray,
                                           None,
                                           pyr_scale=0.5,
                                           levels=5,
                                           winsize=11,
                                           iterations=5,
                                           poly_n=5,
                                           poly_sigma=1.1,
                                           flags=0)
        magnitude, angle = cv.cartToPolar(flow[..., 0], flow[..., 1])
        hsv_mask[
            ...,
            0] = angle * 180 / np.pi / 2  # Set image hue according to the optical flow direction
        hsv_mask[..., 2] = cv.normalize(
            magnitude, None, 0, 255, cv.NORM_MINMAX
        )  # Set image value according to the optical flow magnitude (normalized)

        # plotting of grayscale flowmap and data heatmap
        update_comparison_plot(
            ims, [get_frame(files[counter]), next_gray, hsv_mask])
        plt.title("Max Magnitude :" + str(np.amax(magnitude)) +
                  "\nMax Angle:" + str(np.amax(angle)))
        create_folder_if_absent("optical_flow_pics")
        plt.savefig("optical_flow_pics/{}.png".format(counter))
        prev_gray = next_gray
        k = cv.waitKey(30) & 0xff
        counter += 1
def test_plot_without_labels(files):
    array_shape = (24, 32)
    min_value = 25
    max_value = 40
    plot = init_heatmap("Heatmap",
                        array_shape,
                        min_value,
                        max_value,
                        debug=False)
    for i in range(len(files)):
        frame = get_frame(files[i])
        update_heatmap(frame, plot)
        create_folder_if_absent("testpics")
        plt.savefig("testpics/{}.png".format(i))
def save_serial_output(forever, num_samples=3000, mode=DEBUG_MODE):
    """
    Save I2C output 
    """

    counter = 0

    to_read = forever or counter < num_samples
    plot = None
    if mode == DEBUG_MODE:
        min_temp = 28
        max_temp = 40
        plot = init_heatmap("MLX90640 Heatmap", ARRAY_SHAPE, min_temp,
                            max_temp)
    elif mode == WRITE_MODE:
        create_folder_if_absent(DATA_PATH)

    while to_read:
        try:
            frame = np.zeros(
                (24 * 32))  #  initialise array for storing temp values
            mlx.getFrame(
                frame
            )  #  get the mlx values and put them into the array we just created
            array = np.array(frame)
            print(array)
            if array.shape[0] == ARRAY_SHAPE[0] * ARRAY_SHAPE[1]:
                df = np.reshape(array.astype(float), ARRAY_SHAPE)
                df = interpolate_values(df)
                max_temp = np.amax(df)
                min_temp = np.amin(df)

                if mode == DEBUG_MODE:
                    print("Updating Heatmap...", "[{}]".format(counter))
                    update_heatmap(df, plot)

                elif mode == WRITE_MODE:
                    print("Saving npy object...", "[{}]".format(counter))
                    save_npy(df, DATA_PATH, directory_sort=DATA_DIR_SORT)

                elif mode == PUBLISH_MODE:
                    pass

            counter += 1

        except KeyboardInterrupt:
            raise
        except Exception as e:
            print(e)
            break
예제 #6
0
def save_serial_output(forever, num_samples=3000, mode=DEBUG_MODE):
    """
    Save serial output from arduino 
    """
    ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
    ser.reset_output_buffer()
    counter = 0

    to_read = forever or counter < num_samples
    plot = None
    if mode == DEBUG_MODE:
        min_temp = 28
        max_temp = 40
        plot = init_heatmap("MLX90640 Heatmap", ARRAY_SHAPE, min_temp, max_temp)
    elif mode == WRITE_MODE:
        create_folder_if_absent(DATA_PATH)
        
    while to_read:
        try:
            ser_bytes = ser.readline()
            decoded_string = ser_bytes.decode("utf-8", errors='ignore').strip("\r\n")
            values = decoded_string.split(",")[:-1]
            array = np.array(values)    
            if array.shape[0] == ARRAY_SHAPE[0] * ARRAY_SHAPE[1]:
                df = np.reshape(array.astype(float), ARRAY_SHAPE)
                df = interpolate_values(df)
                max_temp = np.amax(df)
                min_temp = np.amin(df)

                if mode == DEBUG_MODE:
                    print("Updating Heatmap...", "[{}]".format(counter))
                    update_heatmap(df, plot)

                elif mode == WRITE_MODE:
                    print("Saving npy object...", "[{}]".format(counter))
                    save_npy(df, DATA_PATH, directory_sort=DATA_DIR_SORT)

                elif mode == PUBLISH_MODE:
                    pass
                
            counter += 1

        except KeyboardInterrupt:
            raise
        except Exception as e:
            print(e)
            break
예제 #7
0
def test_frame_filter(savegif=False):
    noise_remover = FrameKalmanFilter()
    save_path = kalman_pics_path + basename(data_path)
    create_folder_if_absent(save_path)
    data_i = get_frame(data[0])
    subplt_titles = ["Original Image", "noise removed"]
    ims, axs, fig = init_noise_reduction_plot(data_i, subplt_titles)
    for i in tqdm(range(len(data))):
        data_i = get_frame(data[i])
        processed = noise_remover.process_frame(data_i)
        update_noise_reduction_plot(ims, [data_i, processed])
        plt.savefig(save_path + "/{}.png".format(i)) #make png
    if savegif:
        gif_pics = [save_path+ "/{}.png".format(i) for i in range(len(data))]
        gif_path = kalman_gifs_path+basename(data_path)+".gif"
        write_gif(gif_pics, gif_path, 0, len(gif_pics), fps=30)
        optimize_size(gif_path)
예제 #8
0
def plot_godec(M,
               LS,
               L,
               S,
               folder_path,
               width=32,
               height=24,
               length=None,
               preview=False):
    length = M.shape[1] if length is None else length
    ims = init_godec_plot(M, LS, L, S, width, height, preview)
    matrixes = (M, LS, L, S)
    create_folder_if_absent(folder_path)
    for i in tqdm(range(length - 1)):
        frames = get_reshaped_frames(matrixes, i, width, height)
        set_data(ims, frames)
        if preview:
            plt.pause(.01)  # required for very small datasets for previewing
        plt.draw()
        pic_name = '{}/{}.png'.format(folder_path, i)
        plt.savefig(pic_name)
예제 #9
0
def write_gif(files, name, start=0, end=0, fps=1):
    """
  Primary function to write gifs.
  - If file type given is .npy (temperature npy arrays),
    then it will plot the heatmap and save these pics before making them into a gif.  
  - If file type given is .png (godec plots),
    then it will just make them into a gif directly.

  Arguments:
      files {string[]} -- result from get_all_files()
      name {string} -- name of the gif to be saved

  Keyword Arguments:
      start {int} -- index of files (default: {0})
      end {int} -- index of files (default: {0})
      fps {int} -- frames per second (default: {1})
  """
    filename, file_extension = os.path.splitext(files[0])
    create_folder_if_absent(folder_path(name))
    if file_extension == ".png":
        write_gif_from_pics(files, name, start=0, end=0, fps=fps)
    elif file_extension == ".npy":
        write_gif_from_npy(files, name, start=0, end=0, fps=fps)
예제 #10
0
def test_godec_over_multiple_iterations(frames_per_iterations=30):
    ims = init_comparison_plot(get_frame_GREY(files[0]),
                               subplt_titles=[
                                   "Original", "L_frame", "S_Frame",
                                   "Cleaned_Frame", "L_fram_%", "S_frame_%"
                               ],
                               num_rows=2,
                               num_columns=3)
    for j in range(0, len(files), frames_per_iterations):
        if j + frames_per_iterations < len(files):
            end_index = j + frames_per_iterations
        else:
            end_index = len(files)
        M, LS, L, S, width, height = bs_godec(files[j:end_index],
                                              normalize=False)

        for i in range(i, end_index):
            img = get_frame_GREY(files[i])
            L_frame = normalize_frame(L[:, i].reshape(width, height).T)
            S_frame = normalize_frame(S[:, i].reshape(width, height).T)
            L_probability = foreground_probability(
                L[:, i].reshape(width, height).T, get_frame(files[i]))
            S_probability = foreground_probability(
                S[:, i].reshape(width, height).T, get_frame(files[i]))
            print("L_probability")
            print(L_probability)
            print("S_probability")
            print(S_probability)
            cleaned_frame, prob = cleaned_godec_img(L_frame, S_frame,
                                                    get_frame(files[i]))
            update_comparison_plot(ims, [
                img, L_frame, S_frame, cleaned_frame,
                normalize_frame(L_probability),
                normalize_frame(S_probability)
            ])
            create_folder_if_absent("testpics")
            plt.savefig("testpics/" + "{}.png".format(i))
def test_create_folder_if_absent(folder):
    create_folder_if_absent(folder)
예제 #12
0
def test_bs_godec(files,
                  save_data=False,
                  save_gif=False,
                  gif_name=None,
                  fps=60):
    """Test bs_godec Implementation

    Keyword Arguments:
        save_data {bool} -- (default: {False})
        save_gif {bool} -- (default: {False})
        gif_name {string} -- (default: {None})
        fps {int} -- (default: {60})
    ---
    This test shows you different ways of running the bs_godec method and obtain the various type of data you need.
    """
    print("Testing Godec...")
    t = Timer("accumulate")
    t.start()
    M, LS, L, S, width, height = bs_godec(files)
    print("M: ")
    print(M)
    print("L: ")
    print(L)
    t.stop("Time taken to run background subtraction with godec: ")

    if save_data:
        t.start()

        npy_path = "godec_data/" + basename(data_path)
        create_folder_if_absent(godec_data_path)
        save_npy(M, npy_path, "M")
        save_npy(L, npy_path, "L")
        save_npy(S, npy_path, "S")
        save_npy(LS, npy_path, "LS")

        t.stop("Time taken to save godec result npy arrays: ")

    plots_path = godec_pics_path + basename(data_path)
    if not save_gif:
        print("Plotting....")
        t.start()

        plot_godec(M,
                   LS,
                   L,
                   S,
                   plots_path,
                   width=width,
                   height=height,
                   preview=True)

        t.stop("Time taken to save godec plots: ")
    elif save_gif and gif_name:
        print("Saving gif as {}....".format(gif_name))
        t.start()

        plot_godec(M, LS, L, S, plots_path, width=width, height=height)
        pics = get_all_files(plots_path)

        t.stop("Time taken to save godec plots: ")
        write_gif(pics,
                  godec_gifs_path + gif_name,
                  start=0,
                  end=len(pics),
                  fps=fps)

    print("The entire process has taken: ", t.timers['accumulate'], " seconds")