Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Asd
Browse files Browse the repository at this point in the history
  • Loading branch information
fwy423 committed Apr 4, 2017
0 parents commit 4df1b13
Show file tree
Hide file tree
Showing 3 changed files with 549 additions and 0 deletions.
152 changes: 152 additions & 0 deletions generate_clip_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
import codecs
import os

font_type = "/Users/Fuwy/Documents/Columbia/coursework/EECS_6895_ABD/Arial.ttf"
dataset_path = "/Users/Fuwy/Documents/Columbia/coursework/EECS_6895_ABD/Datasets/E-book/"
result_path = "result/"


def read_character(file):
try:
string = file.read(1)
except UnicodeDecodeError:
string = '?'
return string


def read_file(file, length):
"""
:param file: the input file stream
:param length: the length of words
:return: the string we get from the file
"""

result_string = ''
str = read_character(file)
# remove the blanks and the turning for the first time reading
while str != '' and (str == '\n' or str == ' '):
str = read_character(file)
result_string += str

# read the words
for i in range(length):
str = read_character(file)
if str == '' or str == '\n':
return result_string
result_string += str

# finish reading the last word
while str != '' and str != '\n' and str != ' ':
str = read_character(file)
result_string += str

return result_string


def generate_batch_image(file,
store_path,
number,
size=(512, 16),
font_type=font_type,
font_size=12,
length=80,
mask_portion=3,
mask_from_top=True
):
# print information
print("Generation image from file:", file.name)
print("Picture result is stored in:", store_path)
print("Generation picture number:", number)
print("Size of Image:", size)
if mask_from_top:
print("With mask from top for: 1/", mask_portion)
else:
print("With mask on the bottom for: 1/", mask_portion)

if not os.path.exists(store_path):
os.makedirs(store_path)

# store the data info
info_file = open(store_path + "#info.txt", "w")
info_file.write("size:" + str(size) + "\nnumber:" + str(number))
info_file.close()

for i in range(number):
# generate a single image file and the result
image, image_true, string = generate_single_img(
file=file,
size=size,
font_type=font_type,
font_size=font_size,
length=length,
mask_portion=mask_portion,
mask_from_top=mask_from_top
)

if string == '':
break

# save the image
if i % 100 == 0:
print("index:%d" % i)

image.save((store_path + "line_%d.png") % i, "PNG")
image_true.save((store_path + "line_truth_%d.png") % i, "PNG")

# save the ground truth
txt_file = open(store_path + "ground_truth_%d.txt" % i, "w")
txt_file.write(string)
txt_file.close()


def generate_single_img(file,
size=(800, 20),
font_type=font_type,
font_size=14,
length=100,
mask_portion=3,
mask_from_top=True
):
# read the string that needs to be print
strs = read_file(file=file, length=length)
font = ImageFont.truetype(font_type, font_size)
font_width, font_height = font.getsize(strs)
# size = (font_width, font_height)

# construct drawing
img_true = Image.new(mode="1", size=size, color=255)
draw_true = ImageDraw.Draw(img_true)

bg_color = random.randrange(127) + 128
img = Image.new(mode="L", size=size, color=bg_color)
draw = ImageDraw.Draw(img)

# draw text on the canvas
draw.text(xy=(0, 0), text=strs, font=font)
draw_true.text(xy=(0, 0), text=strs, font=font)

# draw mask for the text
if mask_portion <= 0:
mask_portion = font_height

if mask_from_top:
draw.rectangle((0, 0, font_width, font_height / mask_portion), fill=bg_color)
else:
draw.rectangle((0, font_height * (1 - 1 / mask_portion), font_width, font_height), fill=bg_color)

return img, img_true, strs


if __name__ == '__main__':
# generate_img()
with open(dataset_path + "Complete+Prose+Works+of+Walt+Whitman.txt", "r") as file:
# with codecs.open(dataset_path + "test.txt", "r", encoding="utf-8") as file:
generate_batch_image(file, result_path, number=1000)

file.close()
142 changes: 142 additions & 0 deletions image_load_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import numpy as np
import scipy.io as sio
import os
from PIL import Image

image_path = "result/"


def check_path(path):
if not os.path.exists(path):
os.mkdir(path)


def crop_image(image_input):
# crop the input image with size 16*512, turn it to 63 image with 16*16 (store as 64*256)
image_length = image_input.shape[0]
image_stride = int(image_length / 2)
image_num = int(2 * image_input.shape[1] / image_length)
# our output here is 64*256
image_output = np.zeros((image_num, image_length * image_length), dtype='int32')

for i in range(image_num - 1):
array = np.reshape(image_input[:, i * image_stride:i * image_stride + image_length], newshape=(-1,))
image_output[i, :] = array

new_array = np.hstack((image_input[:, -image_stride:], image_input[:, :image_stride]))
image_output[-1, :] = new_array.reshape(-1, )

return image_output


def parse_data(training_num=500, validation_num=100, test_num=100):
print("parsing image into mat data...")
check_path("data/")

set_size = [training_num, validation_num, test_num]
set_name = ["training", "validation", "test"]

start_index = 0

for turn in range(3):
num = set_size[turn]

input_0 = np.asarray(Image.open((image_path + "line_%d.png") % start_index).convert('L'), dtype='int32')
input = crop_image(input_0)

output_0 = np.asarray(Image.open((image_path + "line_truth_%d.png") % start_index).convert('L'), dtype='int32')
output = crop_image(output_0)

for i in range(start_index + 1, start_index + num):
train_image = np.asarray(Image.open((image_path + "line_%d.png") % i).convert('L'), dtype='int32')
test_image = np.asarray(Image.open((image_path + "line_truth_%d.png") % i).convert('L'), dtype='int32')

# print(train_image)
train_crop = crop_image(train_image)
test_crop = crop_image(test_image)

input = np.vstack((input, train_crop))
output = np.vstack((output, test_crop))

start_index += num

# store the data into mat_file
file_name = "data/" + set_name[turn] + ".mat"
print("... saving mat " + file_name)
sio.savemat(file_name, {"set_input": input, "set_output": output})


def load_data():
train_file_name = "data/training.mat"
valid_file_name = "data/validation.mat"
test_file_name = "data/test.mat"

print("... load data")

train_set = sio.loadmat(train_file_name)
valid_set = sio.loadmat(valid_file_name)
test_set = sio.loadmat(test_file_name)

train_set = (train_set['set_input'], train_set['set_output'])
valid_set = (valid_set['set_input'], valid_set['set_output'])
test_set = (test_set['set_input'], test_set['set_output'])

rval = [train_set, valid_set, test_set]

return rval


def reform_image(image_matrix, image_length, name_index=0, recover_path="recover_result/"):
# recover the image with the correspond cropping method, the image_length is the edge length of image
# we constrain our input as 63*(16*16) -__-
assert image_matrix.shape[0] == 64 and image_matrix.shape[1] == image_length * image_length

image_stride = int(image_length / 2)
recover_img = np.zeros((image_length, image_length * 32))

for i in range(63):
array = image_matrix[i].reshape((image_length, image_length))
recover_img[:, i * image_stride:i * image_stride + image_length] += array

last_row = image_matrix[-1].reshape((image_length, image_length))
recover_img[:image_length, -image_stride:] += last_row[:, :image_stride]
recover_img[:image_length, :image_stride] += last_row[:, -image_stride:]
recover = np.asarray(recover_img / 2, dtype=np.uint8)

print("#%d image of test result successfully!" % name_index)
img = Image.fromarray(recover.astype(np.uint8))
img.save(recover_path + 'test_result_#%d.png' % name_index)
return recover


def batch_recover(batch_input, image_length=16, rows_in_single_image=64, recover_path="recover_result/"):
assert batch_input.shape[0] % rows_in_single_image == 0

check_path(recover_path)

turns = int(batch_input.shape[0] / rows_in_single_image)

out_image = []
for i in range(turns):
input_matrix = batch_input[i * rows_in_single_image:(i + 1) * rows_in_single_image, :]
recover = reform_image(image_matrix=input_matrix,
image_length=image_length,
name_index=i,
recover_path=recover_path)
if i == 0:
out_image = recover
else:
out_image = np.vstack((out_image, recover))

img = Image.fromarray(out_image.astype(np.uint8))
img.save(recover_path + "batch_result.png")


if __name__ == '__main__':
parse_data(400, 100, 100)
rval = load_data()
train_input, train_output = rval[0]
valid_input, valid_output = rval[1]
test_input, test_output = rval[2]

# batch_recover(train_output, image_length=16, rows_in_single_image=64)
Loading

0 comments on commit 4df1b13

Please sign in to comment.