-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake.py
More file actions
64 lines (60 loc) · 3.09 KB
/
fake.py
File metadata and controls
64 lines (60 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Draw fake pictures to test detection"""
import random
import cv2
import numpy
import glyphs
def fake(img_dim, screen_dim, text, edge_tol=None, black=(3, 3, 3), color=(128, 128, 128), bg_color=(250, 250, 250), with_smoothing=True, with_noise=True):
result = numpy.zeros((img_dim[0], img_dim[1], 3), dtype="uint8")
scale = min(int(img_dim[0] / screen_dim[0]), int(img_dim[1] / screen_dim[1]))
vert_border = (img_dim[0] - screen_dim[0] * scale) / 2
horz_border = (img_dim[1] - screen_dim[1] * scale) / 2
# define four corners
ul = (horz_border + (0 if edge_tol is None else random.randint(-edge_tol, edge_tol)),
vert_border + (0 if edge_tol is None else random.randint(-edge_tol, edge_tol)))
ur = (img_dim[1] - horz_border +
(0 if edge_tol is None else random.randint(-edge_tol, edge_tol)),
vert_border + (0 if edge_tol is None else random.randint(-edge_tol, edge_tol)))
bl = (horz_border + (0 if edge_tol is None else random.randint(-edge_tol, edge_tol)),
img_dim[0] - vert_border +
(0 if edge_tol is None else random.randint(-edge_tol, edge_tol)))
br = (img_dim[1] - horz_border +
(0 if edge_tol is None else random.randint(-edge_tol, edge_tol)),
img_dim[0] - vert_border +
(0 if edge_tol is None else random.randint(-edge_tol, edge_tol)))
# draw black border
cv2.fillPoly(result,
[numpy.array(((0, 0), ul, ur,
br, bl, ul,
(0, 0), (img_dim[1] - 1, 0),
(img_dim[1] - 1, img_dim[0] - 1),
(0, img_dim[0] - 1),
(0, 0))
)],
black)
# fill screen with white
cv2.fillPoly(result,
[numpy.array((ul, ur, br, bl, ul,))],
bg_color)
# write string
result = glyphs.render_string_on_img(text, result,
horz_border + ((2 * scale) if edge_tol is None else edge_tol),
vert_border + ((2 * scale) if edge_tol is None else edge_tol),
scale=scale,
color=color,
bg_color=bg_color,
line_spacing=scale,
glyph_spacing=scale)
if with_smoothing:
# smooth
result = cv2.blur(result, ((5 * scale + 1) / 4, (5 * scale + 1) / 4))
if with_noise:
# add noise: where ever the random values are larger than a treshold,
# replace the pixel by its inverse
noise = numpy.empty(result.shape[:-1], dtype=numpy.float)
cv2.randu(noise, 0.0, 16.0 * float(scale))
noise = noise <= 1.0
# make noise 3D (to have same value across RGB channels
noise = numpy.swapaxes(numpy.array((noise, noise, noise)).T, 0, 1)
result = numpy.multiply(result, (numpy.uint8(1) - noise)) + numpy.multiply(
numpy.uint8(255) - result, noise)
return result