Esempio n. 1
0
def process(image, model, debug=None):
    """
    This function processes an image given a model, and returns a list of Box.
    """

    debug_inner = debug in ["inner", "all"]
    contours = get_box_contours(image, debug=debug_inner)
    sprites = get_sprites(image, contours, debug=debug_inner)
    inputs = preprocess_sprites(sprites, debug=debug_inner)
    labels = [model.infer(i) for i in inputs]

    boxes = [
        Box(contour=c, sprite=s, label=l)
        for c, s, l in zip(contours, sprites, labels)
    ]

    if debug in ["all", "synthesis"]:
        for box in boxes:
            fig, ax = plt.subplots(nrows=2)
            ax[0].imshow(image)
            ax[0].plot(box.contour[:, 0], box.contour[:, 1], "og")
            ax[0].plot(
                box.contour.mean(axis=0)[0],
                box.contour.mean(axis=0)[1], "og")
            ax[1].imshow(box.sprite)
            ax[1].set_title("Label recognized: {}".format(box.label))
            plt.show()

    return boxes
Esempio n. 2
0
def process(image, model, debug=None):
    """
    This function processes an image given a model, and returns a list of Box.
    """

    debug_inner = debug in ["inner", "all"]
    contours = get_box_contours(image, debug=debug_inner)
    sprites = get_sprites(image, contours, debug=debug_inner)
    inputs = preprocess_sprites(sprites, debug=debug_inner)
    labels = [
        model.predict(i.reshape(1, 28, 28, 1)).squeeze().argmax() + 1
        for i in inputs
    ]

    boxes = [
        Box(contour=c, sprite=s, input=i, label=l)
        for c, s, i, l in zip(contours, sprites, inputs, labels)
    ]

    if debug in ["all", "synthesis"]:
        for box in boxes:
            fig, ax = plt.subplots(nrows=2)
            ax[0].imshow(image, cmap='gray')
            ax[0].plot(box.contour[:, 0], box.contour[:, 1], "og")
            ax[0].plot(
                box.contour.mean(axis=0)[0],
                box.contour.mean(axis=0)[1], "og")
            ax[0].set_axis_off()
            ax[1].imshow(box.sprite, cmap='gray')
            ax[1].imshow(box.input, cmap='gray')
            ax[1].set_title("Label recognized: {}".format(box.label))
            ax[1].set_axis_off()
            plt.tight_layout()

            plt.show()

    return boxes