1# MIT License 2# 3# Copyright (c) 2017 OsciiArt 4# 5# Permission is hereby granted, free of charge, to any person obtaining a copy 6# of this software and associated documentation files (the "Software"), to deal 7# in the Software without restriction, including without limitation the rights 8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9# copies of the Software, and to permit persons to whom the Software is 10# furnished to do so, subject to the following conditions: 11# 12# The above copyright notice and this permission notice shall be included in all 13# copies or substantial portions of the Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21# SOFTWARE. 22 23#!/usr/bin/env python 24# -*- coding: utf-8 -*- 25 26from keras.models import model_from_json 27import numpy as np 28import pandas as pd 29from PIL import Image 30import pickle 31import os 32 33 34# parameters 35model_path = "model/model.json" 36weight_path = "model/weight.hdf5" 37image_path = 'sample images/original images/21 original.png' # put the path of the image that you convert. 38new_width = 0 # adjust the width of the image. the original width is used if new_width = 0. 39input_shape = [64, 64, 1] 40 41 42def add_mergin(img, mergin): 43 if mergin!=0: 44 img_new = np.ones([img.shape[0] + 2 * mergin, img.shape[1] + 2 * mergin], dtype=np.uint8) * 255 45 img_new[mergin:-mergin, mergin:-mergin] = img 46 else: 47 img_new = img 48 return img_new 49 50 51def pickleload(path): 52 with open(path, mode='rb') as f: 53 data = pickle.load(f) 54 return data 55 56 57# load model 58json_string = open(model_path).read() 59model = model_from_json(json_string) 60model.load_weights(weight_path) 61print("model load done") 62 63char_list_path = "data/char_list.csv" 64char_list = pd.read_csv(char_list_path, encoding="cp932") 65print("len(char_list)", len(char_list)) 66# print(char_list.head()) 67char_list = char_list[char_list['frequency']>=10] 68char_list = char_list['char'].as_matrix() 69 70for k, v in enumerate(char_list): 71 if v==" ": 72 space = k 73 break 74print("class index of 1B space:", space) 75 76 77mergin = (input_shape[0] - 18) // 2 78img = Image.open(image_path) 79orig_width, orig_height = img.size 80if new_width==0: new_width = orig_width 81new_height = int(img.size[1] * new_width / img.size[0]) 82img = img.resize((new_width, new_height), Image.LANCZOS) 83img = np.array(img) 84if len(img.shape) == 3: 85 img = img[:, :, 0] 86 87img_new = np.ones([img.shape[0]+2*mergin+18, img.shape[1]+2*mergin+18], 88 dtype=np.uint8) * 255 89img_new[mergin:mergin+new_height, mergin:mergin+new_width] = img 90img = (img_new.astype(np.float32)) / 255 91 92char_dict_path = "data/char_dict.pkl" 93char_dict = pickleload(char_dict_path) 94 95print("len(char_dict)", len(char_dict)) 96 97output_dir = "output/" 98if not os.path.isdir(output_dir): 99 os.makedirs(output_dir) 100 101for slide in range(18): 102 print("converting:", slide) 103 num_line = (img.shape[0] - input_shape[0]) // 18 104 img_width = img.shape[1] 105 new_line = np.ones([1, img_width]) 106 img = np.concatenate([new_line, img], axis=0) 107 predicts = [] 108 text = [] 109 for h in range(num_line): 110 w = 0 111 penalty = 1 112 predict_line = [] 113 text_line = "" 114 while w <= img_width - input_shape[1]: 115 input_img = img[h*18:h*18+ input_shape[0], w:w+input_shape[1]] 116 input_img = input_img.reshape([1,input_shape[0], input_shape[1], 1]) 117 predict = model.predict(input_img) 118 if penalty: predict[0, space] = 0 119 predict = np.argmax(predict[0]) 120 penalty = (predict==space) 121 char = char_list[predict] 122 predict_line.append(char) 123 char_width = char_dict[char].shape[1] 124 w += char_width 125 text_line += char 126 predicts.append(predict_line) 127 text.append(text_line+'\r\n') 128 # print(text) 129 130 img_aa = np.ones_like(img, dtype=np.uint8) * 0xFF 131 132 for h in range(num_line): 133 w = 0 134 for char in predicts[h]: 135 # print("w", w) 136 char_width = char_dict[char].shape[1] 137 char_img = 255 - char_dict[char].astype(np.uint8) * 255 138 img_aa[h*18:h*18+16, w:w+char_width] = char_img 139 w += char_width 140 141 img_aa = Image.fromarray(img_aa) 142 img_aa = img_aa.crop([0,slide,new_width, new_height+slide]) 143 save_path = output_dir + os.path.basename(image_path)[:-4] + '_'\ 144 + 'w' + str(new_width) \ 145 + '_slide' + str(slide) + '.png' 146 img_aa.save(save_path) 147 148 f=open(save_path[:-4] + '.txt', 'w') 149 f.writelines(text) 150 f.close() 151print('http://example.com?a=') 152print('''http://example.com?a='b'&''') 153