Ejemplo n.º 1
0
def main():
    common.create_directories()

    print("         Q = ignore image")
    print("         1 = label as round")
    print("         2 = label as wide")
    print("         3 = label as narrow")
    print("ARROW KEYS = label directions\n")

    global type_label
    global direction_label
    global plt_text

    unlabeled_imgs = common.get_files(common.SCREENSHOTS_DIR)

    num_labeled = 0
    for path, filename in unlabeled_imgs:
        print("Processing {}...".format(filename))

        img = plt.imread(path)

        ax = plt.gca()
        fig = plt.gcf()
        plot = ax.imshow(img)

        plt.axis('off')
        plt.tight_layout()
        plt_text = plt.text(0, 0, "")

        fig.canvas.mpl_connect('key_press_event', on_press)

        mng = plt.get_current_fig_manager()
        mng.window.state('zoomed')

        plt.show()

        if type_label and direction_label:
            dst_filename = "{}_{}_{}.png".format(
                type_dictionary[type_label], direction_label, time.strftime("%Y%m%d-%H%M%S"))

            os.rename(path, common.LABELED_DIR + dst_filename)

            direction_label = ''
            type_label = None

            num_labeled += 1

    if len(unlabeled_imgs) > 0:
        print("\nLabeled {} out of {} images ({}%).".format(
            num_labeled, len(unlabeled_imgs), 100 * num_labeled // len(unlabeled_imgs)))
        print("Finished!")
    else:
        print("\nThere are no images to label.")
Ejemplo n.º 2
0
def main(training_set_ratio):
    common.create_directories()

    arrows = pd.DataFrame(np.zeros((3, 4), dtype=np.int32),
                          index=('round', 'wide', 'narrow'),
                          columns=('down', 'left', 'right', 'up'))

    images = [(p, f) for p, f in common.get_files(common.SAMPLES_DIR)
              if f[-5] != 'F']

    if images:
        for _, filename in images:
            arrow_direction, arrow_type = common.arrow_labels(filename)

            arrows[arrow_direction][arrow_type] += 1

        num_samples = int(arrows.min().min() * training_set_ratio)

        print("Samples per type: {}".format(num_samples * 4))

        for t, _ in arrows.iterrows():
            print("\nProcessing {} arrows...".format(t))

            for direction in arrows:
                candidates = [(p, f) for p, f in images
                              if common.arrow_labels(f) == (direction, t)]

                print("{}: {}".format(direction, len(candidates)))

                training = random.sample(candidates, num_samples)
                for path, filename in training:
                    dst_dir = common.TRAINING_DIR + direction + '/'
                    os.rename(path, dst_dir + filename)
                    os.rename(flipped(path), dst_dir + flipped(filename))

                candidates = [c for c in candidates if c not in training]

                validation = random.sample(
                    candidates, int(len(candidates) * VALIDATION_SET_RATIO))
                for path, filename in validation:
                    dst_dir = common.VALIDATION_DIR + direction + '/'
                    os.rename(path, dst_dir + filename)
                    os.rename(flipped(path), dst_dir + flipped(filename))

                testing = [c for c in candidates if c not in validation]
                for path, filename in testing:
                    dst_dir = common.TESTING_DIR + direction + '/'
                    os.rename(path, dst_dir + filename)
                    os.rename(flipped(path), dst_dir + flipped(filename))

    show_summary()

    print("\nFinished!")
Ejemplo n.º 3
0
def main():
    common.create_directories()

    print("Reverting images from the training directory...")
    revert_files(common.TRAINING_DIR)

    print("Reverting images from the validation directory...")
    revert_files(common.VALIDATION_DIR)

    print("Reverting images from the testing directory...")
    revert_files(common.TESTING_DIR)

    print("Finished!")
def main():

    json_message = json.loads(get_message_from_stdin())

    try:
        # just testing connectivity
        if json_message["native_messaging_host_accessible"]:
            return

    except Exception as e:
        pass

    output_archive_content = base64.b64decode(json_message["output_archive"])
    output_archive_extension = json_message["output_archive_extension"]

    board = json_message["board"]
    application_name = json_message["application_name"]

    try:
        temporary_directory = os.path.join("tmp", application_name)
        common.create_directories(temporary_directory)

        archive_file_path = os.path.join(
            temporary_directory,
            application_name + "." + output_archive_extension)

        with open(archive_file_path, "wb") as archive:
            archive.write(output_archive_content)

        dest_path = os.path.join(temporary_directory, application_name)
        common.create_directories(dest_path)

        tar = tarfile.open(archive_file_path, "r:gz")
        for tarinfo in tar:
            tar.extract(tarinfo, dest_path)

        tar.close()

        path_to_makefile = os.path.join(dest_path, "generated_by_riotam",
                                        application_name)

        #open standard terminal and execute shell script "flash"
        Popen([
            "x-terminal-emulator", "-e",
            "./flash {0} {1}".format(board, path_to_makefile)
        ])

        #rmtree(temporary_directory)

    except Exception as e:
        logging.error(str(e), exc_info=True)
Ejemplo n.º 5
0
    def cache(self, path, board, name):

        create_directories(self._cache_dir)

        # only store if not in cache already
        if self.get_entry(board, name) is None:

            dest_in_cache = os.path.join(self._cache_dir, board, name)

            copytree(path, dest_in_cache)

            # show that cached application/module is now ready to use
            ready_file_path = os.path.join(dest_in_cache, ".ready_to_use")
            open(ready_file_path, "a").close()
Ejemplo n.º 6
0
    def cache(self, src_path, board, app_dir_name, file_name):

        create_directories(self._cache_dir)

        # only store if not in cache already
        if self.get_entry(board, app_dir_name, file_name) is None:

            dest_in_cache = os.path.join(self._cache_dir, board, app_dir_name)

            create_directories(dest_in_cache)
            logging.debug("CACHING: %s" % file_name)
            copyfile(src_path, os.path.join(dest_in_cache, file_name))

            # show that cached application/module is now ready to use
            ready_file_path = os.path.join(dest_in_cache, ".ready_to_use")
            open(ready_file_path, "a").close()
Ejemplo n.º 7
0
def main(batch_size, model_name):
    common.create_directories()

    show_settings(batch_size)

    model = make_model()

    training, validation = make_generators(batch_size)

    fit(model, training, validation, batch_size)

    save(model, model_name)

    print("\nFinished!")
    print("Run " + cf.skyBlue("classify.py") + 
          " to test the model and get information about its performance.")
    print("More information available with " + cf.orange("Tensorboard") + ".")
def main(argv):

    parser = init_argparse()

    try:
        args = parser.parse_args(argv)

    except Exception as e:
        print(str(e))
        return

    home_dir = expanduser("~")

    try:
        target_dir = common.get_target_dir(home_dir, args.browser)

    except common.BrowserNotSupportedException as e:
        print(str(e))
        return

    # create directory to store native messaging host
    common.create_directories(target_dir)

    # copy native messaging host manifest
    json_manifest_name = "%s.json" % common.HOST_NAME
    copyfile(os.path.join(CUR_DIR, json_manifest_name),
             os.path.join(target_dir, json_manifest_name))

    # replace HOST_PATH placeholder in the manifest
    host_path = "%s/riot_app_market.py" % CUR_DIR
    replace_host_path(os.path.join(target_dir, json_manifest_name), host_path)

    # set permissions for the manifest so that all users can read it
    json_manifest = "{0}/{1}".format(target_dir, json_manifest_name)
    st = os.stat(json_manifest)
    os.chmod(json_manifest, st.st_mode | stat.S_IROTH)

    print("Native messaging host {0} has been installed for {1}".format(
        common.HOST_NAME, args.browser))
Ejemplo n.º 9
0
def main(src_subdir, verbose, model_name):
    common.create_directories()

    src_dir = common.DATA_DIR + src_subdir + "/"

    model = tensorflow.keras.models.load_model(common.MODEL_DIR + model_name)

    # real (index) x predicted (column)
    confusion_matrix = pd.DataFrame(np.zeros((4, 4), dtype=np.int32),
                                    index=('down', 'left', 'right', 'up'),
                                    columns=('down', 'left', 'right', 'up'))

    classification_matrix = pd.DataFrame(np.zeros((4, 3)),
                                         index=('down', 'left', 'right', 'up'),
                                         columns=('precision', 'recall', 'f1'))

    type_matrix = pd.DataFrame(np.zeros((4, 2), dtype=np.int32),
                               index=('round', 'wide', 'narrow', 'total'),
                               columns=('correct', 'incorrect'))

    images = common.get_files(src_dir)

    print("Processing {} file(s) in {}/...\n".format(len(images), src_subdir))

    for path, filename in images:
        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

        data = np.reshape(img, (1, ) + common.INPUT_SHAPE)
        prediction = model.predict(data)

        class_index = np.argmax(prediction)
        predicted_class = common.CLASSES[class_index]

        real_class, arrow_type = common.arrow_labels(filename)

        if verbose and real_class != predicted_class:
            print(path)
            print("Expected {} but got {}: {}\n".format(
                cf.lightGreen(real_class), cf.lightCoral(predicted_class),
                str(prediction[0])))

        confusion_matrix[predicted_class][real_class] += 1

        if real_class == predicted_class:
            type_matrix['correct'][arrow_type] += 1
            type_matrix['correct']['total'] += 1
        else:
            type_matrix['incorrect'][arrow_type] += 1
            type_matrix['incorrect']['total'] += 1

    print("\n" + cf.sandyBrown("Confusion matrix"))
    print(confusion_matrix)

    classification_matrix['precision'] = confusion_matrix.apply(precision)
    classification_matrix['recall'] = confusion_matrix.apply(recall, axis=1)

    classification_matrix['f1'] = classification_matrix.apply(f1, axis=1)

    print("\n" + cf.skyBlue("Classification summary"))
    print(classification_matrix)

    type_matrix['accuracy'] = type_matrix.apply(type_accuracy, axis=1)

    print("\n" + cf.plum("Accuracy by type"))
    print(type_matrix)

    print("\nFinished!")
Ejemplo n.º 10
0
def main(inspection, mode, automatic):
    common.create_directories()

    print("     SPACE = approve")
    print("OTHER KEYS = skip")
    print("         Q = quit\n")

    labeled_imgs = common.get_files(common.LABELED_DIR)

    approved = 0
    for path, filename in labeled_imgs:
        print("Processing " + cf.skyBlue(path))

        arrows = []

        display = cv2.imread(path)
        height, width, _ = display.shape

        # manually tuned values
        search_x, search_y = width // 5 + 35, height // 4
        search_width, search_height = SEARCH_REGION_WIDTH, height // 2 - search_y

        for _ in range(4):
            x0 = search_x
            x1 = x0 + search_width

            y0 = search_y
            y1 = y0 + search_height

            img = display[y0:y1, x0:x1]
            (cx, cy), arrow_box = process_arrow(img, mode)

            search_x += int(cx + ARROW_BOX_DIST - SEARCH_REGION_WIDTH / 2)
            search_y += int(cy - SEARCH_REGION_HEIGHT / 2)

            search_width = SEARCH_REGION_WIDTH
            search_height = SEARCH_REGION_HEIGHT

            arrows.append(arrow_box)

        if not automatic:
            arrow_type, directions, _ = re.split('_', filename)
            reference = get_reference_arrows(directions, arrows[0].shape)

            cv2.imshow(arrow_type, np.vstack([np.hstack(arrows), reference]))

            key = cv2.waitKey()
            cv2.destroyAllWindows()
        else:
            key = APPROVE_KEY

        if key == APPROVE_KEY:
            if not inspection:
                save_arrow_imgs(arrows, filename)
            approved += 1
        elif key == EXIT_KEY:
            break
        else:
            print("Skipped!")

    if len(labeled_imgs) > 0:
        print("\nApproved {} out of {} images ({}%).\n".format(
            approved, len(labeled_imgs), 100 * approved // len(labeled_imgs)))
    else:
        print("There are no images to preprocess.\n")

    show_summary()

    print("Finished!")