diff --git a/.idea/misc.xml b/.idea/misc.xml index f946d59..be60604 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/receipt-recognition.iml b/.idea/receipt-recognition.iml index e890d94..361554c 100644 --- a/.idea/receipt-recognition.iml +++ b/.idea/receipt-recognition.iml @@ -2,12 +2,10 @@ + - + - - \ No newline at end of file diff --git a/resources/fonts/Merchant Copy.ttf b/resources/fonts/Merchant Copy.ttf new file mode 100644 index 0000000..a095272 Binary files /dev/null and b/resources/fonts/Merchant Copy.ttf differ diff --git a/src/ImageGenerator.py b/src/ImageGenerator.py index c5e4ec3..fbd8191 100644 --- a/src/ImageGenerator.py +++ b/src/ImageGenerator.py @@ -1,21 +1,3 @@ -#from IPython.core.completer import back_latex_name_matches -#from PIL import Image, ImageDraw, ImageFont -# get an image -#base = Image.open('Pillow/Tests/images/hopper.png').convert('RGBA') - -# make a blank image for the text, initialized to transparent text color -#img = Image.new('RGB', (30, 30), (255,255,255)) -# -# # get a font -# fnt = ImageFont.truetype('resources\HelveticaNeueLight.ttf', 40) -# # get a drawing context -# d = ImageDraw.Draw(img) -# -# -# # draw text -# d.text((0,0), "T", font=fnt, fill=(0,0,0)) -# text_width, text_height = d.textsize('T') -# print("width {:d} - height {:d}".format(text_width, text_height)) # ------------------------------ Generate Characters ----------------------------# # Import python imaging libs @@ -27,13 +9,72 @@ from PIL import ImageFont, ImageFilter import numpy as np # Import operating system lib import os, errno - +from ImageHelper import noisy # Import random generator from random import randint import shutil import csv import pickle +#---------------------------------- Input and Output ---------------------------# +# Directory containing fonts +font_dir = '../resources/fonts' + +# Output +out_dir = '../results/' + +k = 0 + +# ------------------------------------ Characters -------------------------------# +# Numbers +NUMBERS = ['0', '1', '2'] + +# Small letters +SMALL_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] +SMALL_LETTERS_SAMPLE = ['k', 'b', 'c', 'e'] +CAPITAL_LETTERS = [x.capitalize() for x in SMALL_LETTERS] +CAPITAL_LETTERS_SAMPLE = [x.capitalize() for x in SMALL_LETTERS_SAMPLE] + +# Capital letters +#capital_letters = ["A", 'B', 'C'] + +# Select characters +CHARACTERS = SMALL_LETTERS +CHARACTERS_SAMPLE = SMALL_LETTERS_SAMPLE +#------------------------------------- Colors ----------------------------------# + +# Background color +COLORS_WHITE = [(255, 255, 255)] +COLORS_WHITE_SAMPLE = COLORS_WHITE +COLORS_BLACK = (0, 10, 20, 30) +COLORS_GRAY = [(135, 145, 155)] + +#background_colors = white_colors +COLORS_BACKGRND_CHOSEN = COLORS_WHITE_SAMPLE + +# -------------------------------------- Sizes ----------------------------------# + +# Character sizes +FONTSIZE_SMALL = (10, 14, 16) +FONTSIZE_MEDIUM = (20, 24, 28) +FONTSIZE_LARGE = (30, 36, 40) +FONTSIZE_LARGE_SAMPLE = (24, 30) + +FONTSIZE_CHOSEN = FONTSIZE_LARGE_SAMPLE + +# Image size +IMAGE_SIZE = 32 + +OFFSET_X = [-0.3, -0.1, 0, 0.1, 0.3] +OFFSET_Y = [-0.3, -0.1, 0, 0.1, 0.3] +OFFSET_X_SAMPLE = [-0.1, 0, 0.1] +OFFSET_Y_SAMPLE = [-0.3, -0.1, 0, 0.1, 0.3] + + + +# -------------------------------------- rotation and offset ----------------------------------# +ROTATION = [-0.1, -0.5, 0, 0.5, 0.1] +ROTATION_SAMPLE = [0] # ------------------------------------ Cleanup ----------------------------------# @@ -53,10 +94,10 @@ def cleanup(): def generateCharacters(mode ='image', sample = True): if(mode == 'image'): - myoffsetsX = offsetsX_sample if sample else offsetsX - myoffsetsY = offsetsY_sample if sample else offsetsY - mydegrees = degrees_sample if sample else degrees - mycharacters = characters_sample if sample else characters + myoffsetsX = OFFSET_X_SAMPLE if sample else OFFSET_X + myoffsetsY = OFFSET_Y_SAMPLE if sample else OFFSET_Y + mydegrees = ROTATION_SAMPLE if sample else ROTATION + mycharacters = CHARACTERS_SAMPLE if sample else CHARACTERS cleanup() # Counter k = 1 @@ -69,10 +110,10 @@ def generateCharacters(mode ='image', sample = True): # For each character do for char in mycharacters: # For each font size do - for font_size in font_sizes: + for font_size in FONTSIZE_CHOSEN: if font_size > 0: # For each background color do - for background_color in background_colors: + for background_color in COLORS_BACKGRND_CHOSEN: for offsetx in myoffsetsX: for offsety in myoffsetsY: for degree in mydegrees: @@ -81,7 +122,7 @@ def generateCharacters(mode ='image', sample = True): "font size: {}\t" "degree: {}\t \t" "offsetX: {}\t" - "offsetY: {}".format(image_size, char, font_size, degree,offsetx, offsety)) + "offsetY: {}".format(IMAGE_SIZE, char, font_size, degree, offsetx, offsety)) try: dir = out_dir+char+'/' @@ -98,25 +139,25 @@ def generateCharacters(mode ='image', sample = True): GenerateCSVData(sample) def GenerateCSVData(sample = True): - myoffsetsX = offsetsX_sample if sample else offsetsX - myoffsetsY = offsetsY_sample if sample else offsetsY - mydegrees = degrees_sample if sample else degrees - mycharacters = characters_sample if sample else characters + myoffsetsX = OFFSET_X_SAMPLE if sample else OFFSET_X + myoffsetsY = OFFSET_Y_SAMPLE if sample else OFFSET_Y + mydegrees = ROTATION_SAMPLE if sample else ROTATION + mycharacters = CHARACTERS_SAMPLE if sample else CHARACTERS print("image size: {} \t " "characters: {}\t" "backgrounds: {}\t" "font sizes: {}\t" "degree: {}\t" "offsetX: {}\t" - "offsetY: {}".format(image_size, mycharacters, background_colors, font_sizes, mydegrees,myoffsetsX, myoffsetsY)) + "offsetY: {}".format(IMAGE_SIZE, mycharacters, COLORS_BACKGRND_CHOSEN, FONTSIZE_CHOSEN, mydegrees, myoffsetsX, myoffsetsY)) k = 0 i = 0 path, dirs, files = next(os.walk(font_dir)) file_count = len(files) - height = file_count*len(mycharacters)*len(font_sizes)*len(background_colors)*len(myoffsetsX)*len(myoffsetsY)*len(mydegrees)*3 + height = file_count * len(mycharacters) * len(FONTSIZE_CHOSEN) * len(COLORS_BACKGRND_CHOSEN) * len(myoffsetsX) * len(myoffsetsY) * len(mydegrees) * 3 print(height) - width = image_size*image_size+1 + width = IMAGE_SIZE * IMAGE_SIZE + 1 output = np.ones((height,width), dtype=object) mapDic = {} for dirname, dirnames, filenames in os.walk(font_dir): @@ -129,10 +170,10 @@ def GenerateCSVData(sample = True): for char in mycharacters: mapDic[char] = i # For each font size do - for font_size in font_sizes: + for font_size in FONTSIZE_CHOSEN: if font_size > 0: # For each background color do - for background_color in background_colors: + for background_color in COLORS_BACKGRND_CHOSEN: for offsetx in myoffsetsX: for offsety in myoffsetsY: for degree in mydegrees: @@ -195,7 +236,7 @@ def getMapDic(): def createImageTemplate(background_color, char, font_size, k, font_filename, degree = 0, offsetX = 0, offsetY = 0, noise = 0): # Create character image : # Grayscale, image size, background color - char_image = Image.new('RGBA', (image_size, image_size), \ + char_image = Image.new('RGBA', (IMAGE_SIZE, IMAGE_SIZE), \ background_color) # Draw character image draw = ImageDraw.Draw(char_image) @@ -205,9 +246,9 @@ def createImageTemplate(background_color, char, font_size, k, font_filename, deg # Get character width and height (font_width, font_height) = font.getsize(char) # Calculate x position - x = (image_size - font_width) / 2 + offsetX*image_size + x = (IMAGE_SIZE - font_width) / 2 + offsetX * IMAGE_SIZE # Calculate y position - y = (image_size - font_height) / 2 + offsetY*image_size + y = (IMAGE_SIZE - font_height) / 2 + offsetY * IMAGE_SIZE # Draw text : Position, String, # Options = Fill color, Font draw.text((x, y), char,(0,0,0), font=font) @@ -216,11 +257,6 @@ def createImageTemplate(background_color, char, font_size, k, font_filename, deg # Final file name file_name = "{}_f_{}_fs_{}_bc_{}_n_{}_r_{}_ox_{}_oy_{}_{}.png".\ format(char,fontname, font_size,background_color,noise,degree,offsetX,offsetY,k) - #str(font_size) + '_bc_' + \ - # str(background_color) + '_r_' + \ - # str(background_color) + '_r_' + \ - # str(background_color) + '_r_' + \ - # str(k) + '.png' # Save image #char_image.save('results/' + file_name) @@ -229,7 +265,7 @@ def createImageTemplate(background_color, char, font_size, k, font_filename, deg def createImage(directory, background_color, char, font_size, k, font_filename, degree = 0, offsetX = 0, offsetY = 0, noise = 0, save = True): blankImage, file_name = createBlankImage() - blankImage.save('blankIamge.png') + #blankImage.save('blankImage.png') image2, file_name2 = createImageTemplate(background_color, char, font_size, k, font_filename, degree, offsetX, offsetY, noise) #image2.save('./results/'+str(k)+'.png') px, py = 0, 0 @@ -263,79 +299,19 @@ def addNoise(image, noise = 0): def createBlankImage(): # Create character image : # Grayscale, image size, background color - char_image = Image.new('RGBA', (image_size, image_size), \ + char_image = Image.new('RGBA', (IMAGE_SIZE, IMAGE_SIZE), \ (255,255,255)) # w = char_image.rotate(45.5, expand=1) #char_image.rotate(45).show() # Final file name - file_name = 'blankoImage {}x{}.png'.format(image_size, image_size) + file_name = 'blankoImage {}x{}.png'.format(IMAGE_SIZE, IMAGE_SIZE) #print(file_name) # Save image #char_image.save('results/' + file_name) return (char_image, file_name) -#---------------------------------- Input and Output ---------------------------# - -# Directory containing fonts -font_dir = './resources/fonts' - -# Output -out_dir = './results/' - -k = 0 - -# ------------------------------------ Characters -------------------------------# -# Numbers -numbers = ['0', '1', '2'] - -# Small letters -small_letters = ['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] -small_letters_limited = ['k', 'b', 'c','e'] -capital_letters = [x.capitalize() for x in small_letters_limited] -#print(big_letters) -# Capital letters -#capital_letters = ["A", 'B', 'C'] - -# Select characters -characters = small_letters -characters_sample = small_letters_limited -#------------------------------------- Colors ----------------------------------# - -# Background color -white_colors = [(255,255,255),(220,220,220)] -white_colors_sample = [(255,255,255)] -black_colors = (0, 10, 20, 30) -gray_colors = [(135, 145, 155)] - -#background_colors = white_colors -background_colors = white_colors_sample - -# -------------------------------------- Sizes ----------------------------------# - -# Character sizes -small_sizes = (10, 14, 16) -medium_sizes = (20, 24, 28) -medium_sizes = (20, 24, 28) -large_sizes = (30,36,40) -large_sizes_sample = (24,30) - -font_sizes = large_sizes_sample - -# Image size -image_size = 32 - -offsetsX = [-0.3,-0.1,0,0.1,0.3] -offsetsY = [-0.3,-0.1,0,0.1,0.3] -degrees = [-0.1,-0.5,0,0.5,0.1] -offsetsX_sample = [-0.1,0,0.1] -offsetsY_sample = [-0.3,-0.1,0,0.1,0.3] -degrees_sample = [-0.1,-0.5,0,0.5,0.1] - - -# -------------------------------------- rotation and offset ----------------------------------# - diff --git a/src/MyImageHelper.py b/src/ImageHelper.py similarity index 100% rename from src/MyImageHelper.py rename to src/ImageHelper.py