def test_plot_random(): array_shape = (24, 32) min_value = 30 max_value = 40 plot = init_heatmap("Heatmap", array_shape, min_value, max_value) while True: frame = np.around(np.random.random(array_shape) * 10 + 30, decimals=2) update_heatmap(frame, plot)
def optical_flow_lk(files, track_length=10, detect_interval=5): print("Performing Lucas-Kanade Optical Flow") plot = get_init_heatmap_plot() # params for ShiTomasi corner detection feature_params = dict(maxCorners=4, qualityLevel=0.2, minDistance=6, blockSize=4) # Parameters for lucas kanade optical flow lk_params = dict(winSize=(3, 3), maxLevel=3, criteria=(cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03)) # Take first frame and find corners in it first_frame_gray = get_frame_GREY(files[0]) # TODO: instead of using good features to track, possibly just use contour points directly prevPts = cv.goodFeaturesToTrack(first_frame_gray, mask=None, **feature_params) color = np.random.randint(0, 255, (100, 3)) counter = 1 prevImg = first_frame_gray while counter < len(files): frame = get_frame_GREY(files[counter]) nextImg = frame.copy() update_heatmap(get_frame(files[counter]), plot) nextPts, status, err = cv.calcOpticalFlowPyrLK(prevImg, nextImg, prevPts, None, **lk_params) displacement = nextPts - prevPts if (abs(displacement) > 3).any(): print(displacement) plt.xlabel("Displacement: {}".format(displacement)) else: plt.xlabel("Displacement in x/y lower than 3 ") if nextPts is None: print("Target not moving") prevPts = cv.goodFeaturesToTrack(frame, mask=None, **feature_params) nextPts, status, err = cv.calcOpticalFlowPyrLK( prevImg, nextImg, prevPts, None, **lk_params) # Select good points # each element of the vector is set to 1 if the flow for the corresponding features has been found, otherwise, it is set to 0. good_new = nextPts[status == 1] good_old = prevPts[status == 1] # Now update the previous frame and previous points prevImg = nextImg.copy() prevPts = good_new.reshape(-1, 1, 2) counter += 1
def test_plot(files): array_shape = (24, 32) min_value = 30 max_value = 40 plot = init_heatmap("Heatmap", array_shape, min_value, max_value, debug=True) for f in files: frame = get_frame(f) update_heatmap(frame, plot)
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
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
def test_get_centroid_area_history(files): area_counter, area_movement_counter, centroid_area_numbers, annotated_images = get_centroid_area_history(files) assert len(centroid_area_numbers) == len(annotated_images) print(area_counter) print(area_movement_counter) contours_plot = init_heatmap("Detected Contours", (24,32), show=True) track_plot = init_heatmap("Tracked Centroid", (2, 4), min_value=0, max_value=1, show=True) area_number_frames = [] for i in range(len(centroid_area_numbers)): area_number = centroid_area_numbers[i] frame = np.zeros(8) frame[area_number] = 1 frame = frame.reshape((2,4)) update_heatmap(frame, track_plot) update_heatmap(annotated_images[i], contours_plot)
def naive_detection_from_files(data_path, startIndex=None, endIndex=None): heatmap_plot = get_init_heatmap_plot() likelihood_plot = get_init_likelihood_plot() files = get_all_files(data_path) if startIndex == None: startIndex = 0 if endIndex == None: endIndex = len(files) print(startIndex, endIndex) for i in range(startIndex, endIndex): frame = get_frame(files[i]) areas_person_is_in = naive_detection_by_frame(frame) likelihood_array = [[ areas_person_is_in[i]["likelihood"] for i in range(4) ], [areas_person_is_in[i]["likelihood"] for i in range(4, 8)]] # if debugging with plot view update_heatmap(frame, heatmap_plot) update_heatmap(likelihood_array, likelihood_plot)