def preprocess_ground_truth(self): print('Extracting features for data {}'.format(self.config.data_name)) data = [] self.ground_truth = pickle.load( open(self.config.path.ground_truth, 'rb'))[:self.config.data_size.max_data_size] if self.config.data_type == 'synthetic': grid = [0, 0, 1248, 384] elif self.config.data_type == 'naturalistic': grid = [570, 442, 1350, 682] '''Examples: the consecutive frames are 69,70,71,72,95,96,97,98 Before the loop, next_idx gives 99 The loop is in reverse order: 98,97,96,95,72,71,70,69 ''' # ---------------- Initialize data for calculating image difference ---------------- # Obtain the length of the prefix len_prefix = len(self.config.image_name_convention.prefix) len_postfix = len(self.config.image_name_convention.postfix) next_idx, next_file_name, next_velocity_im = self.get_next_image( self.ground_truth[-1]['image_name'], len_prefix, len_postfix, grid) # ---------------- Loop through data in reverse order ---------------- for file in reversed(self.ground_truth): # read and crop the image im = cv2.imread( os.path.join(self.config.path.images, file['image_name']))[grid[1]:grid[3], grid[0]:grid[2], :] # ---------------- obtain image array for distance ---------------- distance_im = cv2.resize(im, (1248, 384)) distance_im = distance_im[0:384, 432:816, :] # im_size: H*W*C # ---------obtain image array (diff between two consecutive images) for velocity ---------------- velocity_im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) velocity_im = cv2.resize(velocity_im, (220, 66), interpolation=cv2.INTER_AREA) # obtain next image's index, image name and image array # Example: for indice 98,97,96,95,72,71,70,69 # 72+1 != 95 = next_idx, the following command resets next_idx = 72+1 = 73 # otherwise, no change made to next_idx, next_file_name, next_velocity_im cur_idx = int(file['image_name'][len_prefix:-len_postfix]) if cur_idx + 1 != next_idx: next_idx, next_file_name, next_velocity_im = self.get_next_image( file['image_name'], len_prefix, len_postfix, grid) im_diff = opticalFlowDenseDim3(velocity_im, next_velocity_im) # ----------------------- Append all data ----------------------- data.append({ 'cur_frame': file['image_name'], 'next_frame': next_file_name, 'true_distance': file['true_distance'], 'true_velocity': file['true_velocity'], 'distance_im': distance_im, 'velocity_im_diff': im_diff }) next_idx, next_file_name, next_velocity_im = cur_idx, file[ 'image_name'], velocity_im return data
if time_now - time_prev > 0 and 0.0000001 < time_now - time_prev < 0.58: # 0.578111 is highest diff i have seen # in this case row_prev is x1 and row_now is x2 row1 = row_prev row2 = row_now elif time_next - time_now > 0 and 0.0000001 < time_next - time_now < 0.58: # in this case row_now is x1 and row_next is x2 row1 = row_now row2 = row_next x1, y1 = preprocess_image_valid_from_path(row1['image_path'].values[0], row1['speed'].values[0]) x2, y2 = preprocess_image_valid_from_path(row2['image_path'].values[0], row2['speed'].values[0]) img_diff = opticalFlowDenseDim3(x1, x2) img_diff_reshaped = img_diff.reshape(1, img_diff.shape[0], img_diff.shape[1], img_diff.shape[2]) prediction = model.predict(img_diff_reshaped) error = abs(prediction - y2) predict_path = os.path.join(TEST_PREDICT_PATH, str(idx) + '.jpg') # overwrite the prediction of y2 onto image x2 # save overwritten image x2 to new directory ./data/predict # Make a copy x2_copy = np.copy(x2) # to write new image via openCV offset = 30