def static_clutter_algo(data): # data: numpy array file. Outputs the uncluttered data and backgrounds in the form of an array of numpy arrays # each representing one frame M, LS, L, S, width, height = bs_godec(data[0:2]) first_background = S[:, 0].reshape(width, height).T first_uncluttered = L[:, 1].reshape(width, height).T background = first_background # xxXX__set initialisation parameters here__XXxx m = len(data) result = [] backgrounds = [] for i in range(len(data)): if i == 2: background = generate_background_est(m, background, first_uncluttered, 0.2) elif i > 2: background = generate_background_est(m, backgrounds[i - 2], result[i - 1], 0.2) backgrounds.append(background) r = get_frame_GREY(data[i]) - background result.append(r) return result, backgrounds
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 displacement_history(files, start_time, end_time): """ Primary function for getting history of the following format: { start: "20200714_081300", end: "20200714_084300", numFrames: 1800, frames: [x1, ..., x1800] } where xi is the displacement from xi-1 to xi frame Instead of geting centroid area number for each centroid, calculate displacement directly. Args: files ([np.array]): [description] debug (bool, optional): [description]. Defaults to True. Returns: [type]: [description] """ annotated_images = [] centroid_history = [] M, LS, L, S, width, height = bs_godec(files) for i in range(len(files)): img = normalize_frame(files[i]) L_frame = normalize_frame(L[:, i].reshape(width, height).T) S_frame = normalize_frame(S[:, i].reshape(width, height).T) img = cleaned_godec_img(L_frame, S_frame, files[i]) images, centroids = postprocess_img(img) annotated_img = images[-1] annotated_images.append(annotated_img) append_centroid_history(centroids, i, centroid_history) interpolated_centroid_history = Interpolator(centroid_history).history displacements = [] numFrames = len(interpolated_centroid_history) for i in range(numFrames - 1): prev_centroid = interpolated_centroid_history[i + 1] curr_centroid = interpolated_centroid_history[i] if not (prev_centroid == None or curr_centroid == None): curr_displacement = np.sqrt( (prev_centroid[0] - curr_centroid[0])**2 + (prev_centroid[1] - curr_centroid[1])**2) displacements.append(curr_displacement) timeElapsed = datetime.strptime(end_time, "%Y.%m.%d_%H%M%S") - datetime.strptime( start_time, "%Y.%m.%d_%H%M%S") print(timeElapsed.total_seconds()) return { "start": start_time, "end": end_time, "timeElapsedInSeconds": timeElapsed.total_seconds(), "numFrames": numFrames, "frames": displacements, }
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_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))
import cv2 as cv import matplotlib.pyplot as plt import numpy as np from background_subtraction import bs_godec from file_utils import (basename, create_folder_if_absent, get_all_files, get_frame, get_frame_GREY, normalize_frame) from foreground_probability import (foreground_probability, probability_from_residue, residual_squares) from visualizer import (init_comparison_plot, update_comparison_plot, write_gif_from_pics) data_path = "data/teck_calib" data = get_all_files(data_path) M, LS, L, S, width, height = bs_godec( data, normalize=False) # get that godec data mmmm 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
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")
def get_centroid_area_history(files, debug=True, key_format="simple"): """ Primary function to be called for obtaining history in the following format: { 0: { 0: ..., 1: ..., }, 1: { ... } } Arguments: files {[str]} -- up to 30 mins of files, since we decided that recalibration of godec should be done every 30 mins """ annotated_images = [] centroid_history = [] M, LS, L, S, width, height = bs_godec(files) for i in range(len(files)): 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) img = cleaned_godec_img(L_frame, S_frame, get_frame(files[i])) images, centroids = postprocess_img(img) annotated_img = images[-1] annotated_images.append(annotated_img) append_centroid_history(centroids, i, centroid_history) interpolated_centroid_history = Interpolator(centroid_history).history centroid_area_array = [ get_centroid_area_number(cnt) for cnt in interpolated_centroid_history ] area_counter = Counter(centroid_area_array) for i in range(8): if i not in area_counter: area_counter[i] = 0 if key_format == "simple": area_movement_counter = Counter() for i in range(len(centroid_area_array) - 1): from_area = str(centroid_area_array[i]) to_area = str(centroid_area_array[i + 1]) label = from_area + "→" + to_area area_movement_counter[label] += 1 elif key_format == "from_to": area_movement_counter = {"None": Counter()} for i in range(8): area_movement_counter[str(i)] = Counter() for i in range(len(centroid_area_array) - 1): from_area = str(centroid_area_array[i]) to_area = str(centroid_area_array[i + 1]) area_movement_counter[from_area][to_area] += 1 if debug: return area_counter, area_movement_counter, centroid_area_array, annotated_images return area_movement_counter