Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/CharClassification/generate_images.py
3118 views
1
import os
2
import random
3
import numpy as np
4
import uuid
5
6
PATH_TO_LIGHT_BACKGROUNDS = 'light_backgrounds/'
7
PATH_TO_DARK_BACKGROUNDS = 'dark_backgrounds/'
8
PATH_TO_FONT_FILES = 'fonts/'
9
OUTPUT_DIR = 'output/'
10
NUM_IMAGES_PER_CLASS = 10
11
12
# Get all files from directory
13
def get_files_from_dir(dirname):
14
list_files = (os.listdir(dirname))
15
list_files = [dirname + x for x in list_files]
16
return list_files
17
18
19
# Random perspective distortion created by randomly moving the for corners of the image.
20
def get_distort_arg():
21
amount = 5
22
hundred_minus_amount = 100 - amount
23
return '\'0,0 ' + str(np.random.randint(0,amount)) + ',' + str(np.random.randint(0,amount)) + ' 100,0 ' + str(np.random.randint(hundred_minus_amount,100)) + ',' + str(np.random.randint(0,amount)) + ' 0,100 ' + str(np.random.randint(0,amount)) + ',' + str(np.random.randint(hundred_minus_amount,100)) + ' 100,100 ' + str(np.random.randint(hundred_minus_amount,100)) + ',' + str(np.random.randint(hundred_minus_amount,100)) + '\''
24
25
# Randomly extracts 32x32 regions of an image and saves it to outdir
26
def create_random_crops(image_filename, num_crops, out_dir):
27
dim = os.popen('convert ' + image_filename + ' -ping -format "%w %h" info:').read()
28
dim = dim.split()
29
im_width = int(dim[0])
30
im_height = int(dim[1])
31
32
for i in range(0, num_crops):
33
# Randomly select first co-ordinate of square for cropping image
34
x = random.randint(0,im_width - 32)
35
y = random.randint(0,im_height - 32)
36
outfile = uuid.uuid4().hex + '.jpg'
37
command = "magick convert "+ image_filename + " -crop 32x32"+"+"+str(x)+"+"+str(y)+" " + os.path.join(out_dir, outfile)
38
os.system(str(command))
39
40
# Generate crops for all files in file_list and store them in dirname
41
def generate_crops(file_list, dirname):
42
if not os.path.isdir(dirname):
43
os.mkdir(dirname)
44
for f in file_list:
45
create_random_crops(f, 10, dirname)
46
47
48
# List of characters
49
char_list = []
50
for i in range(65, 65+26):
51
char_list.append(chr(i))
52
53
# List of digits
54
for j in range(48,48+10):
55
char_list.append(chr(j))
56
57
# List of light font colors
58
color_light = ['white','lime','gray','yellow','silver','aqua']
59
60
# List of light dark colors
61
color_dark = ['black','green','maroon','blue','purple','red']
62
63
64
# List of light backgrounds
65
light_backgrounds = get_files_from_dir(PATH_TO_LIGHT_BACKGROUNDS)
66
67
# List of dark backgrounds
68
dark_backgrounds = get_files_from_dir(PATH_TO_DARK_BACKGROUNDS)
69
70
# List of font files
71
list_files_fontt = get_files_from_dir(PATH_TO_FONT_FILES)
72
73
74
75
light_backgrounds_crops_dir = 'light_backgrounds_crops/'
76
dark_backgrounds_crops_dir = 'dark_backgrounds_crops/'
77
78
generate_crops(light_backgrounds, light_backgrounds_crops_dir)
79
generate_crops(dark_backgrounds, dark_backgrounds_crops_dir)
80
81
# List of all files in the crops directory
82
light_backgrounds = get_files_from_dir(light_backgrounds_crops_dir)
83
dark_backgrounds = get_files_from_dir(dark_backgrounds_crops_dir)
84
85
# List of all backgrounds
86
all_backgrounds = [dark_backgrounds, light_backgrounds]
87
88
89
# Sample Command----- magick convert image.jpg -fill Black -font Courier-Oblique -weight 50 -pointsize 12 -gravity center -blur 0x8 -evaluate Gaussian-noise 1.2 -annotate 0+0 "Some text" output_image
90
91
for i in range(0,len(char_list)):
92
char = char_list[i]
93
char_output_dir = OUTPUT_DIR + str(char) + "/"
94
95
if not os.path.exists(char_output_dir):
96
os.makedirs(char_output_dir)
97
98
print("Generating data " + char_output_dir)
99
100
101
# Generate synthetic images
102
for j in range(0,NUM_IMAGES_PER_CLASS):
103
104
# Choose a light or dark background
105
path = random.choice(all_backgrounds)
106
107
# Choose a file
108
list_filernd = random.choice(path)
109
110
# Choose a font
111
list_rfo = random.choice(list_files_fontt)
112
113
# Get random distortion
114
distort_arg = get_distort_arg()
115
116
# Get random blur amount
117
blur = random.randint(0,3)
118
119
# Get random noise amount
120
noise = random.randint(0,5)
121
122
# Add random shifts from the center
123
x = str(random.randint(-3,3))
124
y = str(random.randint(-3,3))
125
126
# Choose light color for dark backgrounds and vice-versa
127
if path == all_backgrounds[0] :
128
color = random.choice(color_light)
129
else:
130
color = random.choice(color_dark)
131
132
command = "magick convert " + str(list_filernd) + " -fill "+str(color)+" -font "+ \
133
str(list_rfo) + " -weight 200 -pointsize 24 -distort Perspective "+str(distort_arg)+" "+"-gravity center" + " -blur 0x" + str(blur) \
134
+ " -evaluate Gaussian-noise " + str(noise) + " " + " -annotate +" + x + "+" + y + " " + str(char_list[i]) + " " + char_output_dir + "output_file"+str(i)+str(j)+".jpg"
135
136
# Uncomment line below to see what command is executed.
137
# print(command)
138
os.system(str(command))
139
140