def analyze_centroid_displacement_history(files, num_frames_per_iteration=1800): """ Given an array of file names, get centroid displacement history iteratively over 30 mins of frames. Args: files ([type]): [description] num_frames (int, optional): [description]. Defaults to 1800 (30 mins). """ total_frames = len(files) analysis_results = {} counter = 0 num_interval = 1 while counter < total_frames: start_index = counter if counter + num_frames_per_iteration > total_frames: end_index = total_frames else: end_index = counter + num_frames_per_iteration print("running analysis for {} - {}".format( start_index, end_index)) startTime = basename(files[start_index]) endTime = basename(files[end_index]) displacement_dict = { num_interval: displacement_history(files[start_index:end_index], startTime, endTime) } analysis_results = {**analysis_results, **displacement_dict} counter += num_frames_per_iteration num_interval += 1 return analysis_results
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)
def analyze_centroid_area_history(files, num_frames_per_iteration=1800, key_format="from_to"): """ Given an array of file names, get centroid area history iteratively over 30 mins of frames. Args: files ([type]): [description] num_frames (int, optional): [description]. Defaults to 1800 (30 mins). """ total_frames = len(files) # each frame is 1 second analysis_results = {} counter = 0 while counter < total_frames: start_index = counter if counter + num_frames_per_iteration > total_frames: end_index = total_frames else: end_index = counter + num_frames_per_iteration area_movement_counter = get_centroid_area_history( files[start_index:end_index], debug=False, key_format="from_to") dictkey = basename(files[start_index]) analysis_results[dictkey] = { "keyformat": "from_to", "duration": end_index - start_index, "analysis": area_movement_counter } counter += num_frames_per_iteration return analysis_results
def test_analyze_centroid_displacement_history(): data_path = "data/teck_one_day_activity" files = get_all_files(data_path) analysis_results = analyze_centroid_displacement_history(files) print(analysis_results) write_to_json(analysis_results, "displacement_{}.json".format(basename(data_path)))
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)
def test_analyze_centroid_area_history_short_time(): data_path = "data/teck_calib_2" files = get_all_files(data_path) analysis_results = analyze_centroid_area_history(files) print(analysis_results) write_to_json( analysis_results, "sample_presence_detection_analysis/{}.json".format( basename(data_path)))
def test_analyze_centroid_displacement_history(): start = time.time() data_path = "data/dataset_for_xavier/2020.07.16" files = get_all_files(data_path) print("Number of files: {}".format(len(files))) analysis_results = analyze_centroid_displacement_history(files) json_name = basename(data_path) print("Analysis done, writing to {}.json...".format(json_name)) write_to_json(analysis_results, "displacement_history/{}.json".format(json_name)) end = time.time() print("Time taken to collect displacement dictionary for {} files : {}". format(len(files), end - start))
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")