def test_define_bool(self): Config.clear() Config.define_bool("test", 0, "int") Config.define_bool("test2", "false", "str") Config.define_bool("test3", "truE", "str") self.assertDictEqual(Config.get_dict(), { 'test': False, 'test2': False, 'test3': True })
def test_define_str(self): Config.clear() Config.define_str("test", 5, "A test for int") Config.define_str("test_f", 5., "A test for int with a float var") Config.define_str("test_f2", 5.6, "A test for int with a float var") Config.define_str("test_str", "5.6", "A test for int with a float var") self.assertDictEqual(Config.get_dict(), { 'test': "5", 'test_f': "5.0", 'test_f2': "5.6", 'test_str': "5.6" })
def test_define_int(self): Config.clear() # int and flat float can be assign as int Config.define_int("a", 5, "A test for int") Config.define_int("b", 5., "A test for int with a float var") # But not float with self.assertRaises(ValueError): Config.define_int("c", 5.5, "A test for int with a float var") # Can't assign twice the same variable with self.assertRaises(KeyError): Config.define_int("a", 5, "A test for int") self.assertDictEqual(Config.get_dict(), {'a': 5, 'b': 5})
def test_define_list(self): Config.clear() Config.define_float_list("test_float", [5., 6, "7."], "A test for float") Config.define_int_list("test_int_float_valid", [5., 5], "A test for int with a float var") Config.define_str_list("str", [], "A test for int with a float var") self.assertDictEqual( Config.get_dict(), { 'test_float': [5.0, 6.0, 7.0], 'test_int_float_valid': [5, 5], "str": [] })
def test_define_float(self): Config.clear() Config.define_float("a", 5, "A test for float") Config.define_float("b", 5., "A test for float with a float var") Config.define_float("c", 5.6, "A test for float with a float var") Config.define_float("d", "5.6", "A test for float with a str var") with self.assertRaises(ValueError): Config.define_float("e", "bob", "A test for float with a unparsable str") self.assertDictEqual(Config.get_dict(), { 'a': 5.0, 'b': 5.0, 'c': 5.6, 'd': 5.6 })
def main(): Config.load_conf() config = Config.get_dict() reachy = Reachy() for motor in reachy.motors: motor.compliant = True if config["record"]: # First part of the script : the user can move the arm and hit return to save the arm position all_positions = [] action = input("> ") while action != "save": # Save the position of the motors position = {} for motor in reachy.motors: print( f"The motor \"{motor.name}\" is currently in position: {motor.present_position}" ) position[motor.name] = motor.present_position all_positions.append(position) action = input("> ") # Save the list of position in a file with open(config["file_path"], "w") as f: json.dump(all_positions, f) else: # If this first part was not executed, then we need to read the json file to load the key points with open(config["file_path"], "r") as f: all_positions = json.load(f) # Move the arm for motor in reachy.motors: motor.compliant = False for position in all_positions: # Move to position for motor in reachy.motors: motor.goal_position = position[motor.name] # Wait 2 s time.sleep(2) for motor in reachy.motors: motor.compliant = True
def test_namespace(self): Config.clear() Config.define_int("namespace1.test_int", 5, "A test for int") with Config.namespace("namespace2"): Config.define_int("test_int", 5, "A test for int") Config.define_int("subnamespace.test_int", 5, "A test for int") with Config.namespace("subnamespace2"): Config.define_int("plop", 4, "test of subnamespace") self.assertDictEqual( Config.get_dict(), { 'namespace1': { 'test_int': 5 }, 'namespace2': { 'test_int': 5, 'subnamespace': { 'test_int': 5 }, 'subnamespace2': { 'plop': 4 } } })
def main(): Config.load_conf("config_video_burst.yml") config = Config.get_dict() # check if the script can run assert os.path.isfile(config["file"]), f"Option 'file' need to be provided" os.makedirs(config["outputdir"], exist_ok=True) if (config["prefix"] is ""): config["prefix"] = get_prefix(config["file"]) logging.info(f'prefix: {config["prefix"]}') frame_id = 0 last_save = -10000 video = cv2.VideoCapture(config["file"]) if not video.isOpened(): raise Exception(f"Cannot open video {config['file']}") interval_between_pic = int( video.get(cv2.CAP_PROP_FPS) * config["extract_every"] / 1000) frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) logging.info(f'frame_count: {frame_count}') frame_count_length = len(str(frame_count)) while True: got_frame, img = video.read() if not got_frame: logging.info('end of video') break if (frame_id - last_save > interval_between_pic): picture_path = os.path.join( config["outputdir"], f'{config["prefix"]}_{frame_id:0{frame_count_length}}.jpg') cv2.imwrite(picture_path, img) last_save = frame_id logging.info('Saving picture ' + picture_path) frame_id += 1
def test_define_enum(self): Config.clear() Config.define_enum("test", "a", ["a", "b"], "a or b") self.assertDictEqual(Config.get_dict(), {"test": "a"})
def main(): Config.load_conf() config = Config.get_dict() assert config["model_path"] != "", "model_path can't be empty" assert config["input_dir"] != "", "input_dir can't be empty" assert config["output_dir"] != "", "output_dir can't be empty" os.makedirs(config["output_dir"], exist_ok=True) images_list = os.listdir(config["input_dir"]) annotations_list = os.listdir(config["output_dir"]) # Only keep images that aren't processed yet new_list = [] annotation_ids = [ os.path.splitext(file_name)[0] for file_name in annotations_list ] for image_name in images_list: image_id, _ = os.path.splitext(image_name) if image_id not in annotation_ids: new_list.append(image_name) images_list = new_list images_list.sort() logging.info("there are {} images to annotate".format(len(images_list))) # load tensorflow model (must be a frozen model) od_graph_def = tf.GraphDef() with tf.gfile.GFile(config["model_path"], 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') with tf.Session() as session: # Get all tensors ops = tf.get_default_graph().get_operations() all_tensor_names = {output.name for op in ops for output in op.outputs} tensor_dict = {} for key in [ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes' ]: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = tf.get_default_graph().get_tensor_by_name( tensor_name) image_tensor = tf.get_default_graph().get_tensor_by_name( 'image_tensor:0') # Run inference first_iter = True for image_id in tqdm(range(len(images_list))): image = cv2.cvtColor( cv2.imread( os.path.join(config["input_dir"], images_list[image_id])), cv2.COLOR_BGR2RGB) if first_iter: logging.info(f"image.shape: {image.shape}") first_iter = False height, width = image.shape[:2] image_expanded = np.expand_dims(image, axis=0) output_dict = session.run(tensor_dict, feed_dict={image_tensor: image_expanded}) good_rectangles = [] for i, detection_score in enumerate( output_dict["detection_scores"][0]): if detection_score >= config["object_detection"]["threshold"]: box = output_dict["detection_boxes"][0][ i] # ymin, xmin, ymax, xmax if box[3] - box[1] < config["object_detection"][ "max_width"]: good_rectangles.append({ "xMin": int(box[1] * width), "yMin": int(box[0] * height), "xMax": int(box[3] * width), "yMax": int(box[2] * height), "detection_score": detection_score.item(), "class": config["class"]["names"] [int(output_dict["detection_classes"][0][i]) - 1] }) else: break json_name = os.path.splitext(images_list[image_id])[0] + ".json" with open(os.path.join(config["output_dir"], json_name), 'w') as outfile: json.dump({"rectangles": good_rectangles}, outfile)