code improvements

This commit is contained in:
Satya Mallick committed 2018-06-27 05:58:35 -07:00
1 parent e6efe78642
commit c5192aa45f
6 files changed
+142 -101

No files matched your search

-101
View File
@@ -1,101 +0,0 @@
import os
import random
import numpy as np
list1=[]
for i in range(65, 65+26): # List of Characters
list1.append(chr(i))
list2=[]
for j in range(48,48+10): # List of digits
list2.append(chr(j))
list3=list1+list2
def get_distort_arg(): # Function to create Distortion
amount = 5
hundred_minus_amount = 100 - amount
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)) + '\''
distort_arg = get_distort_arg()
blur_list = ['0x1','0x1','0x2','0x3','0x2'] # List of blur values
blur_e = random.choice(blur_list)
GN = random.randint(0,1) # Select figure randomly to generate noise
gravity = ['south','north','east'] # Assign gravity to place text
color_light = ['white','lime','gray','yellow','silver','aqua'] # List of light font colors
color_dark = ['black','green','maroon','blue','purple','red'] # List of light dark colors
path1 = 'path to light backgrounds '
path2 = 'path to dark backgrounds'
# Command Required [convert image.jpg -resize 600x400\> image.jpg]
list_files_light=(os.listdir('path1')) # To make list of light backgrounds
list_files_light = ['path1' + x for x in list_files_light]
list_files_fontt = (os.listdir('path of font file'))
list_files_fontt = ['path of font file'+ y for y in list_files_fontt] # To make list of fonts
list_files_dark = (os.listdir('path2')) # To make list of dark backgrounds
list_files_dark = ['path2' + x for x in list_files_dark]
final_list = [list_files_dark, list_files_light] # Make list of paths of light and dark backgrounds
for k in range(0,len(list_files_light)):
p1 = random.randint(0,300) # Randomly select first co-ordinate of square for cropping image
p2 = random.randint(0,300)
command = "magick convert "+ str(list_files_light[k])+ " -crop 32x32"+"+"+str(p1)+"+"+str(p2)+" " + str(list_files_light[k])
print(command)
os.system(str(command))
for m in range(0,len(list_files_dark)):
p1 = random.randint(0,200) # Randomly select first co-ordinate of square for cropping image
p2 = random.randint(0,200)
command = "magick convert "+ str(list_files_dark[m])+ " -crop 32x32"+"+"+str(p1)+"+"+str(p2)+" " + str(list_files_dark[m])
#print(command)
os.system(str(command))
# 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
for i in range(0,len(list3)):
directory = "path to save images of each label"
char = list3[i]
directory = directory + str(char) + "/"
if not os.path.exists(directory):
os.makedirs(directory)
print("Directory made")
for j in range(0,1000): # To generate 1000 images for each character.
gv = random.choice(gravity)
path = random.choice(final_list)
list_filernd = random.choice(path)
list_rfo = random.choice(list_files_fontt)
if( path == final_list[0]):
color = random.choice(color_light)
command = "magick convert " + str(list_filernd) + " -fill "+str(color)+" -font "+ \
str(list_rfo) + " -weight 200 -pointsize 24 -distort Perspective "+str(distort_arg)+" "+"-gravity "+str(gv) + " -blur " + str(blur_e) \
+ " -evaluate Gaussian-noise " + str(GN) + " " + " -annotate +0+0 "+ str(list3[i]) + " " + directory + "output_file"+str(i)+str(j)+".jpg"
print(command)
os.system(str(command))
elif(path == final_list[1]):
color = random.choice(color_dark)
command = "magick convert " + str(list_filernd) + " -fill "+str(color)+" -font "+ \
str(list_rfo) + " -weight 200 -pointsize 24 -distort Perspective "+str(distort_arg)+" "+"-gravity "+str(gv) + " -blur " + str(blur_e) \
+ " -evaluate Gaussian-noise " + str(GN) + " " + " -annotate +0+0 "+ str(list3[i]) + " " + directory + "output_file"+str(i)+str(j)+".jpg"
os.system(str(command))
Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+142
View File
@@ -0,0 +1,142 @@
import os
import random
import numpy as np
import uuid
PATH_TO_LIGHT_BACKGROUNDS = 'light_backgrounds/'
PATH_TO_DARK_BACKGROUNDS = 'dark_backgrounds/'
PATH_TO_FONT_FILES = 'fonts/'
OUTPUT_DIR = 'output/'
NUM_IMAGES_PER_CLASS = 10
def get_files_from_dir(dirname):
list_files = (os.listdir(dirname))
list_files = [dirname + x for x in list_files]
return list_files
# Random perspective distortion created by randomly moving the for corners of the image.
def get_distort_arg():
amount = 5
hundred_minus_amount = 100 - amount
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)) + '\''
# Randomly extracts 32x32 regions of an image and saves it to outdir
def create_random_crops(image_filename, num_crops, out_dir):
dim = os.popen('convert ' + image_filename + ' -ping -format "%w %h" info:').read()
dim = dim.split()
im_width = int(dim[0])
im_height = int(dim[1])
for i in range(0, num_crops):
# Randomly select first co-ordinate of square for cropping image
x = random.randint(0,im_width - 32)
y = random.randint(0,im_height - 32)
outfile = uuid.uuid4().hex + '.jpg'
command = "magick convert "+ image_filename + " -crop 32x32"+"+"+str(x)+"+"+str(y)+" " + os.path.join(out_dir, outfile)
os.system(str(command))
# Generate crops for all files in file_list and store them in dirname
def generate_crops(file_list, dirname):
if not os.path.isdir(dirname):
os.mkdir(dirname)
for f in file_list:
create_random_crops(f, 10, dirname)
# List of characters
char_list = []
for i in range(65, 65+26):
char_list.append(chr(i))
# List of digits
for j in range(48,48+10):
char_list.append(chr(j))
# List of light font colors
color_light = ['white','lime','gray','yellow','silver','aqua']
# List of light dark colors
color_dark = ['black','green','maroon','blue','purple','red']
# List of light backgrounds
light_backgrounds = get_files_from_dir(PATH_TO_LIGHT_BACKGROUNDS)
# List of dark backgrounds
dark_backgrounds = get_files_from_dir(PATH_TO_DARK_BACKGROUNDS)
# List of font files
list_files_fontt = get_files_from_dir(PATH_TO_FONT_FILES)
light_backgrounds_crops_dir = 'light_backgrounds_crops/'
dark_backgrounds_crops_dir = 'dark_backgrounds_crops/'
generate_crops(light_backgrounds, light_backgrounds_crops_dir)
generate_crops(dark_backgrounds, dark_backgrounds_crops_dir)
# List of all files in the crops directory
light_backgrounds = get_files_from_dir(light_backgrounds_crops_dir)
dark_backgrounds = get_files_from_dir(dark_backgrounds_crops_dir)
# List of all backgrounds
all_backgrounds = [dark_backgrounds, light_backgrounds]
# 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
for i in range(0,len(char_list)):
char = char_list[i]
char_output_dir = OUTPUT_DIR + str(char) + "/"
if not os.path.exists(char_output_dir):
os.makedirs(char_output_dir)
print("Generating data " + char_output_dir)
# Generate synthetic images
for j in range(0,NUM_IMAGES_PER_CLASS):
# Choose a light or dark background
path = random.choice(all_backgrounds)
# Choose a file
list_filernd = random.choice(path)
# Choose a font
list_rfo = random.choice(list_files_fontt)
# Get random distortion
distort_arg = get_distort_arg()
# Get random blur amount
blur = random.randint(0,3)
#
noise = random.randint(0,5)
# Add random shifts from the center
x = str(random.randint(-3,3))
y = str(random.randint(-3,3))
# Choose light color for dark backgrounds and vice-versa
if path == all_backgrounds[0] :
color = random.choice(color_light)
else:
color = random.choice(color_dark)
command = "magick convert " + str(list_filernd) + " -fill "+str(color)+" -font "+ \
str(list_rfo) + " -weight 200 -pointsize 24 -distort Perspective "+str(distort_arg)+" "+"-gravity center" + " -blur 0x" + str(blur) \
+ " -evaluate Gaussian-noise " + str(noise) + " " + " -annotate +" + x + "+" + y + " " + str(char_list[i]) + " " + char_output_dir + "output_file"+str(i)+str(j)+".jpg"
# Uncomment line below to see what command is executed.
# print(command)
os.system(str(command))
Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

File renamed without changes.