Beispiel #1
0
def get_classes():
    logging.info(Config.get_var("class.names"))
    return jsonify({
        "classNames": Config.get_var("class.names"),
        "classColors": Config.get_var("class.colors"),
        "minHeight": Config.get_var("min_height"),
        "minWidth": Config.get_var("min_width")
    })
Beispiel #2
0
def main():
    global image_provider
    Config.load_conf()

    image_provider = ImageProvider()

    # Write txt file containing class info
    with open(
            os.path.join(Config.get_var("human_annotations_path"),
                         "class_names.txt"), "w") as f:
        f.write(','.join(Config.get_var("class.names")))

    app.run(host='0.0.0.0', threaded=True)
Beispiel #3
0
 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
     })
Beispiel #4
0
def set_image():
    message = flask.request.get_json()
    logging.info(message)

    for shape in message["detectedObjects"]:
        assert shape["class"] in Config.get_var("class.names")

    json_name = os.path.splitext(os.path.split(
        message['imageSrc'])[1])[0] + ".json"

    with open(
            os.path.join(Config.get_var("human_annotations_path"), json_name),
            'w') as outfile:
        json.dump({"rectangles": message["detectedObjects"]}, outfile)

    return "ok"
Beispiel #5
0
    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": []
            })
Beispiel #6
0
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
Beispiel #7
0
    def __init__(self):
        """class providing path of images to process
        """
        # user-provided attributes
        self.images_path = Config.get_var("images_path")
        self.human_annotations_path = Config.get_var("human_annotations_path")
        self.model_annotations_path = Config.get_var("model_annotations_path")

        # other attributes
        self.lock = threading.Lock()
        self.current_image = 0

        # Handle paths
        assert os.path.exists(self.images_path)
        os.makedirs(self.human_annotations_path, exist_ok=True)
        if self.model_annotations_path:
            os.makedirs(self.model_annotations_path, exist_ok=True)

        # Handle lists
        self.images_list = os.listdir(self.images_path)
        self.human_annotations_list = os.listdir(self.human_annotations_path)
        if self.model_annotations_path:
            self.model_annotations_list = os.listdir(
                self.model_annotations_path)
        else:
            self.model_annotations_list = []
        logging.info(f"there are {len(self.images_list)} images in total")

        # Only keep images that where not yet processed
        new_list = []
        human_annotation_ids = [
            os.path.splitext(file_name)[0]
            for file_name in self.human_annotations_list
        ]
        for image_name in self.images_list:
            image_id, _ = os.path.splitext(image_name)
            if image_id not in human_annotation_ids:
                new_list.append(image_name)
        self.images_list = new_list
        logging.info(f"there are {len(self.images_list)} images to annotate")
        self.images_list.sort()
        self.n_images = len(self.images_list)
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
Beispiel #9
0
 def test_load_conf_cmd(self, mock_args):
     Config.clear()
     Config.define_bool("VaR", False, "turn me true")
     Config.define_int("v1", 1, "var")
     Config.define_int("v2", 2, "var")
     Config.define_float_list("list", [5., 6, "7."], "A test for list")
     with Config.namespace("n1"):
         Config.define_int("v1", 1, "var")
         Config.define_int("v2", 2, "var")
         Config.define_bool("V3", True, "turn me false")
     Config.load_conf()
     self.assertEqual(Config.get_var("v1"), 2)
     self.assertEqual(Config.get_var("v2"), 3)
     self.assertEqual(Config.get_var("n1.v1"), 2)
     self.assertEqual(Config.get_var("n1.v2"), 3)
     self.assertEqual(Config.get_var("n1.v3"), False)
     self.assertEqual(Config.get_var("VAr"), True)
     self.assertEqual(Config.get_var("list"), [1.0, 2.0, 3.0])
Beispiel #10
0
 def test_load_conf_os_env(self, mock_args):
     Config.clear()
     Config.define_bool("VaR", False, "turn me true")
     Config.define_int("v1", 0, "var")
     Config.define_int("nm.v2", 0, "var")
     Config.load_conf()
     self.assertEqual(Config.get_var("v1"), 1)
     self.assertEqual(Config.get_var("nm.v2"), 2)
     self.assertEqual(Config.get_var("VAr"), True)
Beispiel #11
0
 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})
Beispiel #12
0
import os
import logging
import json
import coloredlogs
import cv2
import tensorflow as tf
import numpy as np
from tqdm import tqdm  # progress bar
from distribute_config import Config
# Run a frozen model on a set of images and output the detections as .json files, one per image.
# For now it only keeps "detection_classes" == 1, i.e. "class"="person"

coloredlogs.install(level="DEBUG")

Config.define_str(
    "model_path", "",
    "Path of the model to load and execute, for instance models/frozen_inference_graph.pb"
)
Config.define_str("input_dir", "",
                  "Path where the images to annotate are stored")
Config.define_str(
    "output_dir", "",
    "Path to store pre-annotations (model annotations to help human annotators)"
)

with Config.namespace("object_detection"):
    Config.define_float("threshold", 0.2,
                        "Discard boxes with score below this value")
    Config.define_float(
        "max_width", 0.7,
        "Discard boxes with width upper this value because in some cases, very large detections are mostly false positives"
    )
Beispiel #13
0
import threading
import json
import logging
import coloredlogs
from PIL import Image

from flask import Flask, url_for, redirect, Response, jsonify
from flask_cors import CORS

import flask

from distribute_config import Config

coloredlogs.install(level='DEBUG')

Config.define_str("images_path", "static/images",
                  "Path where are stored the images to annotate")
Config.define_str("human_annotations_path", "static/human_annotations",
                  "Path where are stored human annotation")
Config.define_str("model_annotations_path", "static/model_annotations",
                  "Path where are stored model annotation for helping human")
with Config.namespace("class"):
    Config.define_str_list("names", [], "name of the classes to annotate")
    Config.define_str_list("colors", [], "colors for each classes")
Config.define_int("min_height", 0,
                  "Rectangle with lower height will be displayed red")
Config.define_int("min_width", 0,
                  "Rectangle with lower width will be displayed red")

image_provider = None

Beispiel #14
0
import json
import time
from distribute_config import Config
from reachy import Reachy

Config.define_str("file_path", "",
                  "path of the file to write the different positions")
Config.define_bool("record", True,
                   "if true then record and run. Else only run ")


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
Beispiel #15
0
 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
     })
Beispiel #16
0
 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"
     })
Beispiel #17
0
import logging
import json
import coloredlogs
import cv2
import tensorflow as tf
import numpy as np
from tqdm import tqdm  # progress bar
from distribute_config import Config
# Run a frozen model on a set of images and output the detections as .json files, one per image.
# For now it only keeps "detection_classes" == 1, i.e. "class"="person"

coloredlogs.install(level="DEBUG")

Config.define_str(
    "model_path", "/opt/model/frozen_inference_graph.pb",
    "Path of the model to load and execute, for instance"
    "/opt/model/frozen_inference_graph.pb. If you're using docker-compose you shouldn't change this."
)
Config.define_str("input_dir", "",
                  "Path where the images to annotate are stored")
Config.define_str(
    "output_dir", "",
    "Path to store pre-annotations (model annotations to help human annotators)"
)
with Config.namespace("class"):
    Config.define_str_list("names", [], "name of the classes to annotate")
with Config.namespace("object_detection"):
    Config.define_float("threshold", 0.2,
                        "Discard boxes with score below this value")
    Config.define_float(
        "max_width", 1.0,
Beispiel #18
0
 def test_define_enum(self):
     Config.clear()
     Config.define_enum("test", "a", ["a", "b"], "a or b")
     self.assertDictEqual(Config.get_dict(), {"test": "a"})
Beispiel #19
0
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)
Beispiel #20
0
 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
                 }
             }
         })
import cv2
import logging
import os
import coloredlogs
from distribute_config import Config

coloredlogs.install(level="DEBUG")

Config.define_str("file", "", "input file: video to read and split")
Config.define_float("extract_every", 100,
                    "Time in ms between two extracted images")
Config.define_str("prefix", "", "Prefix to the name of the images")
Config.define_str("outputdir", ".", "Where to save the pictures")


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():