Beispiel #1
0
def produce_stuttering(image, label):
    vdivisors = divisors(image.shape[0])
    hdivisors = divisors(image.shape[1])
    iterations = np.random.choice(np.arange(1, 5), p=[0.65, 0.2, 0.1, 0.05])

    res = TreeRet(image.copy(), [], [])

    for i in list(range(iterations)):
        sizex = random.choice(hdivisors)
        sizey = random.choice(vdivisors)
        res = stutter(res, label, sizex, sizey)

    r_shapes = labelMe_class.Shapes(
        label, [[0, 0], [image.shape[1] - 1, image.shape[0] - 1]], None,
        "rectangle", {})
    res.r_json.append(r_shapes.to_string_form())

    return res


# img=np.array(cv2.imread(in_name,1))
# vdivisors=divisors(img.shape[0])
# hdivisors=divisors(img.shape[1])
# iterations=np.random.choice(np.arange(1,5),p=[0.65,0.2,0.1,0.05])
# for i in list(range(iterations)):
# 	sizex=random.choice(hdivisors)
# 	sizey=random.choice(vdivisors)
# 	img=stutter(img,sizex,sizey)
# cv2.imwrite(out_name,img)
def random_patch(img):
    height, width, channel = img.shape
    for i in range(height):
        for j in range(width):
            for k in range(3):
                img[i, j, k] = np.random.randint(0, 256)

    res = TreeRet(img, None, None)
    return res
Beispiel #3
0
def create_radiation(img, label):
	height, width, channel = img.shape

	mask_triangles, vertex_triangles, common_vertex = get_triangles(width, height)
	warp_img = radiation(img, vertex_triangles, common_vertex)
	warp_mask = radiation(mask_triangles, vertex_triangles, common_vertex)
	mask_contours, rect_contours = find_contours(warp_mask)

	f_json_list = []

	for rct in rect_contours:
		f_shapes = labelMe_class.Shapes(label, rct, None, "rectangle", {})
		f_json_list.append(f_shapes.to_string_form())

	res = TreeRet(warp_img, f_json_list, None)
	return res
def create_desktop_glitch_two(img, label):
    height, width, channel = img.shape
    sub_height = np.random.randint(int(height / 40), int(height / 30))
    sub_width = sub_height

    f_json_list = []

    count = 0
    for i in range(0, height, sub_height):
        generate_pattern = False
        for j in range(0, width, sub_width * 4):
            if count % 4 == 0:
                if np.random.uniform() < 0.05:
                    generate_pattern = True
                else:
                    generate_pattern = False

            if generate_pattern:
                img[i:i + sub_height,
                    j:j + sub_width, :] = form_combined_pattern(
                        img[i:i + sub_height, j:j + sub_width, :])
                if np.random.uniform() < 0.1:
                    generate_pattern = False

                f_shapes = labelMe_class.Shapes(
                    label, [[j, i], [j + sub_width, i + sub_height]], None,
                    "rectangle", {})
                f_json_list.append(f_shapes.to_string_form())
            count += 1

    #r_shapes = labelMe_class.Shapes(label, [[y0, x0], [y1, x1]], None, "rectangle", {})
    #r_json_list = [r_shapes.to_string_form()]

    #r_shapes2 = labelMe_class.Shapes("2", [[sub_orig_y, sub_orig_x], [sub_orig_y + subwidth, sub_orig_x + subheight]], None, "rectangle", {})
    #r_json_list.append(r_shapes2.to_string_form())
    #res = TreeRet(orig_img, f_json_list, r_json_list)

    res = TreeRet(img, f_json_list, None)
    return res
Beispiel #5
0
def create_desktop_glitch_one(orig_img, label):
    height, width, _ = orig_img.shape
    x0 = npr.randint(0, int(height / 6))
    y0 = npr.randint(0, int(width / 6))
    x1 = npr.randint(int(5 * height / 6), height)
    y1 = npr.randint(int(5 * width / 6), width)

    copy = np.copy(orig_img[x0:x1, y0:y1, :])

    square_width = np.random.randint(50, 80)
    img = np.empty_like(copy)
    height, width, channel = img.shape
    img = add_glitch(img, square_width)

    subwidth = int(width / 3)
    subheight = int(height / 3)

    sub_orig_x = np.random.randint(int(subheight / 2), int(subheight * 3 / 2))
    sub_orig_y = np.random.randint(0, subwidth)

    added_patch = add_glitch(
        img[sub_orig_x:sub_orig_x + subheight,
            sub_orig_y:sub_orig_y + subwidth, :], square_width)
    img[sub_orig_x:sub_orig_x + subheight,
        sub_orig_y:sub_orig_y + subwidth, :] = np.clip(added_patch, 0, 255)
    img = merge(copy, img, square_width)

    orig_img[x0:x1, y0:y1, :] = img

    r_shapes = labelMe_class.Shapes(label, [[y0, x0], [y1, x1]], None,
                                    "rectangle", {})
    r_json_list = [r_shapes.to_string_form()]

    #res = TreeRet(orig_img, f_json_list, r_json_list)

    res = TreeRet(orig_img, None, r_json_list)
    return res
def add_shaders(im, label, lo = 1, hi = 3):
	angles = np.array([0,90,180,270])

	h,w,_ = im.shape
	output = im.copy()
	overlay1 = im.copy()
	overlay2 = im.copy()

	#####big shaders in forms of n-gons
	num_shapes = np.random.randint(lo,hi+1)

	f_json_list = []
	max_x = 0
	max_y = 0
	min_x = w
	min_y = h

	for i in range(num_shapes):
		x_0, y_0 = np.random.randint(w), np.random.randint(h)
		x_1, y_1 = np.random.randint(-300,w+300), np.random.randint(-300,h+300)
		x_2, y_2 = np.random.randint(-300,w+300), np.random.randint(-300,h+300)

		pts = np.array(((x_0, y_0), (x_1, y_1), (x_2, y_2)), dtype=int)

		temp_max_x = max(x_0,x_1,x_2)
		temp_max_y = max(y_0,y_1,y_2)
		temp_min_x = min(x_0,x_1,x_2)
		temp_min_y = min(y_0,y_1,y_2)

		extra_n = np.random.randint(4)

		for i in range(extra_n): #extra number of points to make an n_gon
			temp_ran_x = np.random.randint(-300,h+300)
			temp_ran_y = np.random.randint(-300,w+300)
			pts = np.append(pts, [[temp_ran_x, temp_ran_y]], axis = 0)


		contur = contntur_limitation(pts, 0,0, w-1,h-1)

		(t_x1, t_y1) = contur[0]
		t_x1 = int(math.ceil(t_x1))
		t_y1 = int(math.ceil(t_y1))
		temp_min_x = t_x1
		temp_min_y = t_y1

		temp_max_x = t_x1
		temp_max_y = t_y1

		for (t_x,t_y) in contur:
			t_x = int(math.ceil(t_x))
			t_y = int(math.ceil(t_y))

			temp_min_x = min(t_x,temp_min_x)
			temp_min_y = min(t_y,temp_min_y)

			temp_max_x = max(t_x,temp_max_x)
			temp_max_y = max(t_y,temp_max_y)

		f_shapes = labelMe_class.Shapes(label, [[temp_min_x, temp_min_y], [temp_max_x, temp_max_y]], None, "rectangle", {})
		f_json_list.append(f_shapes.to_string_form())

		min_x = min(min_x, temp_min_x)
		max_x = max(max_x, temp_max_x)

		min_y = min(min_y, temp_min_y)
		max_y = max(max_y, temp_max_y)

		alpha = 1

		colors = np.empty([2, 3])
		start_x = min(max(0, x_0), h-1)
		start_y = min(max(0, y_0), w-1)

		colors[0, :] = im[start_x, start_y,:] + npr.randint(-30, 30, size = [3])
		mid_x = (x_1+x_2)//2
		mid_y = (y_1+y_2)//2

		mid_x = min(max(0, mid_x), h-1)
		mid_y = min(max(0, mid_y), w-1)

		colors[1, :] = im[mid_x,mid_y,:] + npr.randint(-30, 30, size = [3])

		colors = np.clip(colors, a_min = 0, a_max = 255)


		#f_shapes = labelMe_class.Shapes("2", [[start_x, start_y], [mid_x, mid_y]], None, "rectangle", {})
		#f_json_list.append(f_shapes.to_string_form())

		# colors[0,:] = npr.randint(0, 256, size = 3)
		# colors[1,:] = colors[0,:] + npr.randint(0, 100, size = 3)
		# colors[1,:] = np.clip(colors[1,:], 0, 255)


		cv2.fillConvexPoly(overlay1, pts, color= tuple([int(x) for x in colors[0]]) )
		cv2.fillConvexPoly(overlay2, pts, color= tuple([int(x) for x in colors[1]]) )


	############
	a1, a2 = random.choice(angles), random.choice(angles)

	t_img = gradient(output, color_blend(im, overlay1, overlay2, a1), a2)

	r_shapes = labelMe_class.Shapes(label, [[min_x, min_y], [max_x, max_y]], None, "rectangle", {})
	r_json_list = [r_shapes.to_string_form()]

	res = TreeRet(t_img, f_json_list, r_json_list)
	return res
def add_shapes(im, label, lo = 2, hi = 5):
	img = im.copy()
	h, w, _ = img.shape

	# Find the darkest region of the image
	grid = (-1,-1)
	mean_shade = np.mean(img)

	x_step, y_step = int(w/6), int(h/4)
	for y in range(0, h, y_step):
		for x in range(0, w, x_step):
			new_shade = np.mean(img[y:y+y_step, x:x+x_step])
			if  new_shade <= mean_shade:
				mean_shade = new_shade
				grid = (x,y)

	f_json_list = []
	max_x = 0
	max_y = 0
	min_x = w
	min_y = h

	# Add shapes
	minLoc = (np.random.randint(grid[0], min(grid[0]+x_step, w)), np.random.randint(grid[1], min(grid[1]+x_step, h)))
	#minLoc = (np.random.randint(0.1 * w, 0.9 * w), np.random.randint(0.1 * h, 0.9 * h))
	num_shapes = np.random.randint(lo,hi+1)

	for i in range(num_shapes):
		stretch = np.random.randint(40, 100)
		diff1, diff2 = np.random.randint(-5,5), np.random.randint(-5,5)
		x1 = minLoc[0] + diff1 * stretch
		y1 = minLoc[1] + diff2 * stretch
		x2 = x1 + np.random.randint(1,12)/5 * diff1 * stretch
		y2 = y1 + np.random.randint(1,12)/5 * diff2 * stretch

		pts = np.array((minLoc, (x1, y1), (x2, y2)), dtype=int)
		contur = contntur_limitation([minLoc, [x1,y1], [x2,y2]]	, 0,0, w-1,h-1)

		(t_x1, t_y1) = contur[0]
		t_x1 = int(math.ceil(t_x1))
		t_y1 = int(math.ceil(t_y1))
		temp_min_x = t_x1
		temp_min_y = t_y1

		temp_max_x = t_x1
		temp_max_y = t_y1

		for (t_x,t_y) in contur:
			t_x = int(math.ceil(t_x))
			t_y = int(math.ceil(t_y))

			temp_min_x = min(t_x,temp_min_x)
			temp_min_y = min(t_y,temp_min_y)

			temp_max_x = max(t_x,temp_max_x)
			temp_max_y = max(t_y,temp_max_y)

		f_shapes = labelMe_class.Shapes(label, [[temp_min_x, temp_min_y], [temp_max_x, temp_max_y]], None, "rectangle", {})
		f_json_list.append(f_shapes.to_string_form())

		min_x = min(min_x, temp_min_x)
		max_x = max(max_x, temp_max_x)

		min_y = min(min_y, temp_min_y)
		max_y = max(max_y, temp_max_y)

		c1, c2, c3 = np.random.randint(0,50),np.random.randint(0,50),np.random.randint(0,50)
		cv2.fillConvexPoly(img, pts, color= (c1,c2,c3))

	r_shapes = labelMe_class.Shapes(label, [[min_x, min_y], [max_x, max_y]], None, "rectangle", {})
	r_json_list = [r_shapes.to_string_form()]

	res = TreeRet(img, f_json_list, r_json_list)
	return res
def line_pixelation(img, label, label2="2"):

    f_json_list = []
    max_x = 0
    max_y = 0
    min_x = img.shape[1]
    min_y = img.shape[0]

    max_x_p = 0
    max_y_p = 0
    min_x_p = img.shape[1]
    min_y_p = img.shape[0]

    while (True):
        vertical = 0
        horizontal = 1

        # PARAMETERS
        skipstripe = random.randrange(0, 2)
        orientation = random.randrange(0, 2)
        brightness = random.randrange(0, 2)
        monobias = random.randrange(0, 3)
        biasval = random.randrange(0, 11)
        glow = random.randrange(0, 8)

        image = np.copy(img)
        height = max(abs(int(np.random.normal(int(image.shape[0] / 200), 2))),
                     1)
        width = max(abs(int(np.random.normal(height, 2))), 1)
        if (orientation == vertical):
            if (width < image.shape[1]):
                indent = random.randrange(0, image.shape[1] - width)
            else:
                print("Error: 'width' is too large for input dimensions. ")
                continue
        if (orientation == horizontal):
            if (height < image.shape[0]):
                indent = random.randrange(0, image.shape[0] - height)
            else:
                print("Error: 'height' is too large for input dimensions.")
                continue
        stripes = random.randrange(
            1, max(1 + abs(int(np.random.normal(20, 20))), 2))
        ss = np.ones(stripes)
        if (skipstripe == 1):
            ss = [1]
            for i in list(range(stripes - 2)):
                ss.append(random.randrange(0, 2))
            ss.append(1)
        if (monobias == 1):
            monocolor = [0, 0, 0]
        if (monobias == 2):
            monocolor = [255, 255, 255]

        if (orientation == vertical):
            for n in list(range(stripes)):
                if (ss[n] == 1):

                    temp_min_x = img.shape[1] - 1
                    temp_min_y = img.shape[0] - 1
                    temp_max_x = 0
                    temp_max_y = 0

                    temp_min_x_p = img.shape[1] - 1
                    temp_min_y_p = img.shape[0] - 1
                    temp_max_x_p = 0
                    temp_max_y_p = 0

                    for i in list(range(0, image.shape[0], height)):

                        color = np.array([
                            random.randrange(0, 256),
                            random.randrange(0, 256),
                            random.randrange(0, 256)
                        ])
                        mono = 0
                        if (monobias > 0):
                            mono = random.randrange(1, 11)
                        if (glow == 6 and n == 0
                                and random.randrange(1, 10) < random.randrange(
                                    1, 3)):
                            radius = random.randrange(5, 4 + 4 * height)
                            y = i * height + int(height / 2)
                            if (y - radius < image.shape[0] - 10):
                                image = vlglow(image, [y, indent], color,
                                               radius)

                                temp_min_x_p = min(max(indent - radius, 0),
                                                   img.shape[1] - 1)
                                temp_min_y_p = min(max(y - radius, 0),
                                                   img.shape[0] - 1)

                                temp_max_x_p = min(indent, img.shape[1] - 1)
                                temp_max_y_p = min(y + radius,
                                                   img.shape[0] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        if (glow == 7 and n == (len(ss) - 1)
                                and random.randrange(1, 10) < random.randrange(
                                    1, 4)):
                            radius = random.randrange(5, 70)
                            y = i * height + int(height / 2)
                            if (y - radius < image.shape[0] - 10):
                                image = vrglow(image, [y, indent + n * width],
                                               color, radius)

                                temp_min_x_p = min(max(indent + n * width, 0),
                                                   img.shape[1] - 1)
                                temp_min_y_p = min(max(y - radius, 0),
                                                   img.shape[0] - 1)

                                temp_max_x_p = min(
                                    indent + n * width + 1 + radius,
                                    img.shape[1] - 1)
                                temp_max_y_p = min(y + radius,
                                                   img.shape[0] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        for j in list(range(height)):
                            for k in list(range(width)):
                                localcolor = np.array(color)
                                if (((i + j) < image.shape[0]) and
                                    (indent + k + n * width < image.shape[1])):
                                    if (brightness == 1 and mono <= biasval):
                                        seed = int(np.random.normal(0, 10))
                                        localcolor[0] = max(
                                            min(color[0] + seed, 255), 0)
                                        localcolor[1] = max(
                                            min(color[1] + seed, 255), 0)
                                        localcolor[2] = max(
                                            min(color[2] + seed, 255), 0)
                                    elif (mono > biasval):
                                        localcolor = monocolor
                                    image[i + j, indent +
                                          (k + n * width)] = localcolor

                                    temp_min_x = min(indent + (k + n * width),
                                                     temp_min_x)
                                    temp_min_y = min(i + j, temp_min_y)

                                    temp_max_x = min(
                                        max(indent + (k + n * width) + 1,
                                            temp_max_x), img.shape[1] - 1)
                                    temp_max_y = min(
                                        max(i + j, temp_max_y) + 1,
                                        img.shape[0] - 1)

                    if (temp_min_x != 0 or temp_min_y != 0
                            or temp_max_x != img.shape[1] - 1
                            or temp_max_y != img.shape[0] - 1):

                        f_shapes = labelMe_class.Shapes(
                            label, [[temp_min_x, temp_min_y],
                                    [temp_max_x, temp_max_y]], None,
                            "rectangle", {})
                        f_json_list.append(f_shapes.to_string_form())

                        min_x = min(min_x, temp_min_x)
                        max_x = max(max_x, temp_max_x)

                        min_y = min(min_y, temp_min_y)
                        max_y = max(max_y, temp_max_y)

        if (orientation == horizontal):
            for n in list(range(stripes)):
                if (ss[n] == 1):
                    temp_min_x = img.shape[1] - 1
                    temp_min_y = img.shape[0] - 1
                    temp_max_x = 0
                    temp_max_y = 0

                    temp_min_x_p = img.shape[1] - 1
                    temp_min_y_p = img.shape[0] - 1
                    temp_max_x_p = 0
                    temp_max_y_p = 0

                    for i in list(range(0, image.shape[1], width)):
                        color = np.array([
                            random.randrange(0, 256),
                            random.randrange(0, 256),
                            random.randrange(0, 256)
                        ])
                        mono = 0
                        if (monobias > 0):
                            mono = random.randrange(1, 11)
                        if (glow == 6 and n == 0
                                and random.randrange(1, 10) < random.randrange(
                                    1, 3)):
                            x = i * width + int(width / 2)
                            radius = random.randrange(5, 4 + 4 * width)
                            if (x - radius < img.shape[1] - 10):
                                image = htglow(image, [indent, x], color,
                                               radius)

                                temp_min_y_p = min(max(indent - radius, 0),
                                                   img.shape[0] - 1)
                                temp_min_x_p = min(max(x - radius, 0),
                                                   img.shape[1] - 1)

                                temp_max_y_p = min(indent, img.shape[0] - 1)
                                temp_max_x_p = min(x + radius,
                                                   img.shape[1] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        if (glow == 7 and n == (len(ss) - 1)
                                and random.randrange(1, 10) < random.randrange(
                                    1, 4)):
                            radius = random.randrange(5, 70)
                            x = i * width + int(width / 2)
                            if (x - radius < img.shape[1] - 10):
                                image = hbglow(image, [indent + height * n, x],
                                               color, radius)

                                temp_min_y_p = min(max(indent + height * n, 0),
                                                   img.shape[0] - 1)
                                temp_min_x_p = min(max(x - radius, 0),
                                                   img.shape[1] - 1)

                                temp_max_y_p = min(
                                    indent + height * n + radius,
                                    img.shape[0] - 1)
                                temp_max_x_p = min(x + radius,
                                                   img.shape[1] - 1)

                                min_x_p = min(min_x_p, temp_min_x_p)
                                max_x_p = max(max_x_p, temp_max_x_p)

                                min_y_p = min(min_y_p, temp_min_y_p)
                                max_y_p = max(max_y_p, temp_max_y_p)

                                f_shapes = labelMe_class.Shapes(
                                    label2, [[temp_min_x_p, temp_min_y_p],
                                             [temp_max_x_p, temp_max_y_p]],
                                    None, "rectangle", {})
                                f_json_list.append(f_shapes.to_string_form())

                        for j in list(range(width)):
                            for k in list(range(height)):
                                localcolor = np.array(color)
                                if ((
                                    (k + n * height + indent) < image.shape[0])
                                        and (i + j < image.shape[1])):
                                    if (brightness == 1 and mono <= biasval):
                                        seed = int(np.random.normal(0, 10))
                                        localcolor[0] = max(
                                            min(color[0] + seed, 255), 0)
                                        localcolor[1] = max(
                                            min(color[1] + seed, 255), 0)
                                        localcolor[2] = max(
                                            min(color[2] + seed, 255), 0)
                                    elif (mono > biasval):
                                        localcolor = monocolor
                                    image[indent + k + (n * height),
                                          i + j] = localcolor

                                    temp_min_y = min(indent + k + (n * height),
                                                     temp_min_y)
                                    temp_min_x = min(i + j, temp_min_x)

                                    temp_max_y = min(
                                        max(indent + k + (n * height) + 1,
                                            temp_max_y), img.shape[0] - 1)
                                    temp_max_x = min(max(i + j, temp_max_x),
                                                     img.shape[1] - 1)

                    if (temp_min_x != 0 or temp_min_y != 0
                            or temp_max_x != img.shape[1] - 1
                            or temp_max_y != img.shape[0] - 1):

                        f_shapes = labelMe_class.Shapes(
                            label, [[temp_min_x, temp_min_y],
                                    [temp_max_x, temp_max_y]], None,
                            "rectangle", {})
                        f_json_list.append(f_shapes.to_string_form())

                        min_x = min(min_x, temp_min_x)
                        max_x = max(max_x, temp_max_x)

                        min_y = min(min_y, temp_min_y)
                        max_y = max(max_y, temp_max_y)

        if (not np.array_equal(img, image)):
            r_shapes = labelMe_class.Shapes(label,
                                            [[min_x, min_y], [max_x, max_y]],
                                            None, "rectangle", {})
            r_json_list = [r_shapes.to_string_form()]
            if (max_x_p != 0 or max_y_p != 0 or min_x_p != img.shape[1] - 1
                    or min_y_p != img.shape[0] - 1):
                r_shapes = labelMe_class.Shapes(
                    label2, [[min_x_p, min_y_p], [max_x_p, max_y_p]], None,
                    "rectangle", {})
                r_json_list.append(r_shapes.to_string_form())
            res = TreeRet(image, f_json_list, r_json_list)
            return res