Example #1
0
 def __init__(self, input_path, output_path, output_size, bg):
     src_img, img = initialize(src_path=input_path,
                               input_path=None,
                               size=output_size)
     self.src_img = Image(src_img)
     self.img = Image(img)
     self.viewer = None
     self.output_path = output_path
     color_to_fill = self.src_img.get_average_color(
     ) if bg == 'None' else hex_to_rgb(bg)
     self.img.fill_with(color_to_fill)
Example #2
0
def generate_set(image_n, image_size, name):
    def initialize():
        X = np.zeros((image_n, image_size, image_size, 3))
        Y = np.zeros((image_n, 9))
        return X, Y

    def rand():
        return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
               np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
               np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.)

    X, Y = initialize()

    image = Image(initializer.new_image(image_size, image_size))
    white_image_np = np.ones((image_size, image_size, 3))

    action = Environment.Action.LINE

    for i in tqdm.tqdm(range(image_n), "Generating %s set" % name):
        r, g, b, a, x1, y1, x2, y2, size = rand()
        image.perform_action(action, (r, g, b, a, x1, y1, x2, y2, size))
        array = image.array
        X[i] = (array / 255.) - white_image_np
        Y[i] = [r, g, b, a, x1, y1, x2, y2, size]
        image.clear()

    path = "./{}_{}.npy"
    np.save(path.format(name, "X"), X)
    np.save(path.format(name, "Y"), Y)
Example #3
0
 def undo(self):
     self.successful_actions -= 1
     self.img.delete()
     self.img = Image(self.prev_img.img, self.prev_img.array)
     self.reward = self.prev_reward
     self.distance = self.prev_distance
     self.undo_before_step = True
Example #4
0
def generate_set(image_n, image_size, name, shape, without_rotation):
    def initialize():
        X = np.zeros((image_n, image_size, image_size, 3))
        Y = np.zeros((image_n, 9))
        return X, Y

    def rand():
        return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
               np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
               np.random.uniform(0. + (1. / image_size), 1.), \
               np.random.uniform(0. + (1. / image_size), 1.), np.random.uniform(0., 1.)

    X, Y = initialize()

    image = Image(initializer.new_image(image_size, image_size))
    white_image_np = np.ones((image_size, image_size, 3))

    action = Environment.Action.ELLIPSE if shape == 'ellipse' else Environment.Action.RECTANGLE

    for i in tqdm.tqdm(range(image_n), "Generating %s set" % name):
        r, g, b, a, x, y, w, h, rotation = rand()
        w = min(w, 1 - x)
        h = min(h, 1 - y)
        rotation = 0.5 if without_rotation else rotation
        image.perform_action(action, (r, g, b, a, x, y, w, h, rotation))
        array = image.array
        X[i] = (array / 255.) - white_image_np
        Y[i] = [r, g, b, a, x, y, w, h, rotation]
        image.clear()

    path = "./{}_{}.npy"
    np.save(path.format(name, "X"), X)
    np.save(path.format(name, "Y"), Y)
Example #5
0
 def __init__(self, src_path, action_type, limit, output_size, diffs):
     self.src_path = src_path
     src_image, image = initialize_with_scaled_src(
         self.src_path + "/src.jpg", output_size)
     self.src_image = Image(src_image)
     self.image = Image(image)
     self.diffs = diffs
     if self.diffs:
         self.action_type_limited = True if action_type >= 0 else False
         if self.action_type_limited:
             self.action_type = action_type
         self.action_number_limited = True if limit >= 0 else False
         if self.action_number_limited:
             self.limit = limit
     self.out_path = None
     self.img_src_out_path = None
     self.abs_out_path = None
Example #6
0
 def __init__(self, src_path, acceptable_distance, input_path, actions,
              size):
     self.src_path = src_path
     src_img, img = initializer.initialize(src_path, input_path, size)
     self.src_img = Image(src_img)
     self.img = Image(img)
     self.prev_img = None
     self.acceptable_dist = acceptable_distance
     self.state = None
     self.reward = 0
     self.prev_reward = 0
     self.distance = sum(abs(self.src_img.array - self.img.array))
     self.prev_distance = 0
     self.done = False
     self.action_space = ToolSpace()
     self.viewer = None
     self.version_info = self.construct_version_info()
     self.out_path = None
     self.undo_before_step = False
     self.action = None
     self.args = None
     self.successful_actions = 0
     self.actions_before_success = 0
     self.actions = actions
def generate_set(image_n, image_size, name):
    def scale(x):
        return x / float(image_size)

    def initialize():
        X = np.zeros((image_n, image_size, image_size))
        Y = np.zeros((image_n, 9))
        return X, Y

    X, Y = initialize()

    image = Image(initializer.new_image(image_size, image_size))
    white_image_np = np.ones((image_size, image_size))

    for i in tqdm.tqdm(range(image_n), "Generating %s set" % name):
        square_size = np.random.randint(3, image_size)
        x_coor = np.random.randint(0, image_size - square_size)
        y_coor = np.random.randint(0, image_size - square_size)
        image.perform_action(
            RECTANGLE, (RED, GREEN, BLUE, ALPHA, scale(x_coor), scale(y_coor),
                        scale(square_size), scale(square_size), ROTATION))
        array = np.sum(image.array, axis=2) / 3
        X[i] = (array / 255.) - white_image_np
        # draw white rectangle to "reset" image
        image.perform_action_without_array_update(
            RECTANGLE, (1., 1., 1., ALPHA, scale(x_coor), scale(y_coor),
                        scale(square_size), scale(square_size), ROTATION))
        Y[i][0] = RED
        Y[i][1] = GREEN
        Y[i][2] = BLUE
        Y[i][3] = ALPHA
        Y[i][4] = scale(x_coor)
        Y[i][5] = scale(y_coor)
        Y[i][6] = scale(square_size)
        Y[i][7] = scale(square_size)
        Y[i][8] = ROTATION

    path = "./{}_{}.npy"
    np.save(path.format(name, "X"), X)
    np.save(path.format(name, "Y"), Y)
Example #8
0
def plugin_main(name, size, r, g, b, a, x1, y1, x2, y2, x3, y3):
    image = Image(initializer.new_image(size, size))
    action = Environment.Action.TRIANGLE
    image.perform_action(action, (r, g, b, a, x1, y1, x2, y2, x3, y3))
    image.save("%s/%s" % (PATH, name))
Example #9
0
def plugin_main(name, size, r, g, b, a, x, y, w, h, rotation, shape):
    image = Image(initializer.new_image(size, size))
    action = Environment.Action.ELLIPSE if shape == 'ellipse' else Environment.Action.RECTANGLE
    image.perform_action(action, (r, g, b, a, x, y, w, h, rotation))
    image.save("%s/%s" % (PATH, name))
Example #10
0
def generate_set(num_images, image_size, name, max_examples_per_part,
                 without_rotation):
    def initialize(num_examples):
        X = np.zeros((num_examples, image_size, image_size, 3))
        Y = np.zeros((num_examples, 4))
        return X, Y

    def save(X, Y, p):
        path = "./{}_{}_{}.npy"
        np.save(path.format(name, "X", p), X)
        np.save(path.format(name, "Y", p), Y)

    def generate_args(a):
        if a == 0 or a == 1:
            return generate_args_for_selection_shape()
        elif a == 2:
            return generate_args_for_line()
        else:
            return generate_args_for_triangle()

    def generate_args_for_selection_shape():
        def rand():
            return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0. + (1. / image_size), 1.), \
                   np.random.uniform(0. + (1. / image_size), 1.), np.random.uniform(0., 1.)

        r, g, b, a, x, y, w, h, rotation = rand()
        w = min(w, 1 - x)
        h = min(h, 1 - y)
        rotation = 0.5 if without_rotation else rotation
        return r, g, b, a, x, y, w, h, rotation

    def generate_args_for_line():
        def rand():
            return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.)

        return rand()

    def generate_args_for_triangle():
        def rand():
            return np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0., 1.), np.random.uniform(0., 1.), np.random.uniform(0., 1.), \
                   np.random.uniform(0., 1.)

        return rand()

    remaining_examples = num_images
    part = 1

    image = Image(initializer.new_image(image_size, image_size))
    white_image_np = np.ones((image_size, image_size, 3))

    while remaining_examples > 0:
        examples_in_current_part = min(remaining_examples,
                                       max_examples_per_part)
        X, Y = initialize(examples_in_current_part)
        for i in tqdm.tqdm(range(examples_in_current_part),
                           "Generating {} set, part {}".format(name, part)):
            action = np.random.randint(0, 4)
            args = generate_args(action)
            image.perform_action(action, args)
            array = image.array
            X[i] = (array / 255.) - white_image_np
            Y[i][action] = 1
            image.clear()

        save(X, Y, part)
        remaining_examples -= examples_in_current_part
        part += 1