def predict(min_proba): # Load classifier along with the label encoder. with open(os.path.join(common.PRJ_DIR, common.SVM_MODEL), 'rb') as fp: model = pickle.load(fp) with open(os.path.join(common.PRJ_DIR, common.LABELS), 'rb') as fp: le = pickle.load(fp) # Calculate size of radar image data array used for training. train_size_z = int((common.R_MAX - common.R_MIN) / common.R_RES) + 1 train_size_y = int((common.PHI_MAX - common.PHI_MIN) / common.PHI_RES) + 1 train_size_x = int( (common.THETA_MAX - common.THETA_MIN) / common.THETA_RES) + 1 logger.debug(f'train_size: {train_size_x}, {train_size_y}, {train_size_z}') try: while True: # Scan according to profile and record targets. radar.Trigger() # Retrieve any targets from the last recording. targets = radar.GetSensorTargets() if not targets: continue # Retrieve the last completed triggered recording raw_image, size_x, size_y, size_z, _ = radar.GetRawImage() raw_image_np = np.array(raw_image, dtype=np.float32) for t, target in enumerate(targets): logger.info('**********') logger.info( 'Target #{}:\nx: {}\ny: {}\nz: {}\namplitude: {}\n'.format( t + 1, target.xPosCm, target.yPosCm, target.zPosCm, target.amplitude)) i, j, k = common.calculate_matrix_indices( target.xPosCm, target.yPosCm, target.zPosCm, size_x, size_y, size_z) # projection_yz is the 2D projection of target in y-z plane. projection_yz = raw_image_np[i, :, :] # projection_xz is the 2D projection of target in x-z plane. projection_xz = raw_image_np[:, j, :] # projection_xy is 2D projection of target signal in x-y plane. projection_xy = raw_image_np[:, :, k] proj_zoom = calc_proj_zoom(train_size_x, train_size_y, train_size_z, size_x, size_y, size_z) observation = common.process_samples( [(projection_xz, projection_yz, projection_xy)], proj_mask=PROJ_MASK, proj_zoom=proj_zoom, scale=True) # Make a prediction. name, prob = classifier(observation, model, le, min_proba) logger.info(f'Detected {name} with probability {prob}') logger.info('**********') except KeyboardInterrupt: pass finally: # Stop and Disconnect. radar.Stop() radar.Disconnect() radar.Clean() logger.info('Successful radar shutdown.') return
def get_samples(): active = True sample_num = 1 while active: # Scan according to profile and record targets (if any). radar.Trigger() # Get object detection results from server (if any). detected_objects = get_detected_objects(stub, desired_labels) if not detected_objects: continue # Retrieve any targets from the last recording. #targets = radar.GetTrackerTargets() targets = radar.GetSensorTargets() if not targets: continue # raw_image ordering: (theta, phi, r) raw_image, size_x, size_y, size_z, _ = radar.GetRawImage() raw_image_np = np.array(raw_image, dtype=np.float32) logger.debug(f'Raw image np shape: {raw_image_np.shape}') #targets = get_derived_targets(raw_image_np, size_x, size_y, size_z) logger.info(f'Sample number {sample_num} of {num_samples}'.center(60, '-')) # Find the detected object closest to each radar target. for t, target in enumerate(targets): logger.info(f'Target #{t + 1}:') logger.debug('\nx: {}\ny: {}\nz: {}\namplitude: {}\n'.format( target.xPosCm, target.yPosCm, target.zPosCm, target.amplitude)) i, j, k = common.calculate_matrix_indices( target.xPosCm, target.yPosCm, target.zPosCm, size_x, size_y, size_z) logger.debug(f'i: {i}, j: {j}, k: {k}') # Init distance between radar and camera target as a % of radar target depth. # This is used as a threshold to declare correspondence. current_distance = DETECTION_THRESHOLD_PERCENT * target.zPosCm #current_distance = MAX_TARGET_OBJECT_DISTANCE logger.debug(f'Initial threshold: {current_distance:.1f} (cm)') target_object_close = False for obj in detected_objects: if obj.score < MIN_DETECTED_OBJECT_SCORE: logger.debug(f'Object ({obj.label}) score ({obj.score:.1f}) too low...skipping.') continue # Convert position of detected object's centroid to radar coordinate system. centroid_camera = (width*obj.centroid.x, height*obj.centroid.y) logger.debug(f'Centroid camera: {centroid_camera}') centroid_radar = convert_coordinates(centroid_camera, target.zPosCm, fx, fy, cx, cy) logger.debug(f'Centroid radar: {centroid_radar}') # Calculate distance between detected object and radar target. distance = compute_distance((target.xPosCm, target.yPosCm), centroid_radar) logger.debug(f'Distance: {distance}') # Find the detected object closest to the radar target. if distance < current_distance: target_object_close = True current_distance = distance current_score = obj.score target_name = obj.label target_position = target.xPosCm, target.yPosCm, target.zPosCm centroid_position = centroid_radar # Calculate 3D to 2D projections of target return signal. # Signal in raw_image_np with shape (size_x, size_y, size_z). # axis 1 and 2 of the matrix (j, k) contain the projections # represented in angle phi and distance r, respectively. # These are 2D projections the y-z plane. # axis 0 and 2 of the matrix (i, k) contain the projections # represented in angle theta and distance r, respectively. # These are 2D projections the x-z plane. # # projection_yz is the 2D projection of target in y-z plane. projection_yz = raw_image_np[i,:,:] logger.debug(f'Projection_yz shape: {projection_yz.shape}') # projection_xz is the 2D projection of target in x-z plane. projection_xz = raw_image_np[:,j,:] logger.debug(f'Projection_xz shape: {projection_xz.shape}') # projection_xy is 2D projection of target signal in x-y plane. projection_xy = raw_image_np[:,:,k] logger.debug(f'Projection_xy shape: {projection_xy.shape}') else: msg = ( f'Found "{obj.label}" with score {obj.score:.1f} at {distance:.1f} (cm)' f' too far from target at z {target.zPosCm:.1f} (cm)...skipping.' ) logger.info(msg) if target_object_close: msg = ( f'Found "{target_name}" with score {current_score:.1f} at {distance:.1f} (cm)' f' from target at z {target.zPosCm:.1f} (cm)...storing.' ) logger.info(msg) yield ((projection_xz, projection_yz, projection_xy), target_name, target_position, centroid_position) logger.info('-'*60+'\n') if sample_num < num_samples: sample_num += 1 else: active = False if realtime_plot: print('\n**** Close plot window to continue. ****\n')
def main(): radar.Init() # Configure Walabot database install location. radar.SetSettingsFolder() # Establish communication with walabot. try: radar.ConnectAny() except radar.WalabotError as err: print(f'Failed to connect to Walabot.\nerror code: {str(err.code)}') exit(1) # Set radar scan profile. radar.SetProfile(common.RADAR_PROFILE) # Set scan arena in polar coords radar.SetArenaR(common.R_MIN, common.R_MAX, common.R_RES) radar.SetArenaPhi(common.PHI_MIN, common.PHI_MAX, common.PHI_RES) radar.SetArenaTheta(common.THETA_MIN, common.THETA_MAX, common.THETA_RES) # Threshold radar.SetThreshold(RADAR_THRESHOLD) # radar filtering filter_type = radar.FILTER_TYPE_MTI if MTI else radar.FILTER_TYPE_NONE radar.SetDynamicImageFilter(filter_type) # Start the system in preparation for scanning. radar.Start() # Calibrate scanning to ignore or reduce the signals if not in MTI mode. if not MTI: common.calibrate() try: while True: # Scan according to profile and record targets. radar.Trigger() # Retrieve any targets from the last recording. targets = radar.GetSensorTargets() if not targets: continue # Retrieve the last completed triggered recording raw_image, size_x, size_y, size_z, _ = radar.GetRawImage() raw_image_np = np.array(raw_image, dtype=np.float32) for t, target in enumerate(targets): print( 'Target #{}:\nx: {}\ny: {}\nz: {}\namplitude: {}\n'.format( t + 1, target.xPosCm, target.yPosCm, target.zPosCm, target.amplitude)) i, j, k = common.calculate_matrix_indices( target.xPosCm, target.yPosCm, target.zPosCm, size_x, size_y, size_z) # projection_yz is the 2D projection of target in y-z plane. projection_yz = raw_image_np[i, :, :] # projection_xz is the 2D projection of target in x-z plane. projection_xz = raw_image_np[:, j, :] # projection_xy is 2D projection of target signal in x-y plane. projection_xy = raw_image_np[:, :, k] observation = common.process_samples( [(projection_xy, projection_yz, projection_xz)], proj_mask=PROJ_MASK) # Make a prediction. name, prob = classifier(observation) if name == 'person': color_name = colored(name, 'green') elif name == 'dog': color_name = colored(name, 'yellow') elif name == 'cat': color_name = colored(name, 'blue') else: color_name = colored(name, 'red') print(f'Detected {color_name} with probability {prob}\n') except KeyboardInterrupt: pass finally: # Stop and Disconnect. radar.Stop() radar.Disconnect() radar.Clean() print('Successful termination.')