def load_train_mat_label(trainpath, files): res_list = [] for file in files: names = file.split('.') _ord = int(names[-2]) label = chr(_ord) img = Image.open('{}/{}'.format(trainpath, file)) mat = img2mat(img) res_list.append((mat, label)) return res_list
def load_train_mat_label(trainpath,files): res_list = [] for file in files: names = file.split('.') _ord = int(names[-2]) label = chr(_ord) img = Image.open('{}/{}'.format(trainpath,file)) mat = img2mat(img) res_list.append((mat,label)) return res_list
def split_img(bin_img,col_rate=0.7,row_rate=0.4,count=None): mat = img2mat(bin_img) splits = [] res_list = split_mat(mat,col_rate=col_rate,row_rate=row_rate,count=count) for res in res_list: tp,_ = res r1,r2,c1,c2 = tp _img = bin_img.crop((c1,r1,c2,r2)) splits.append((tp,_img)) return splits
def split_img(bimg, **kwargs): if kwargs == {}: kwargs = split_kwargs mat = img2mat(bimg) splits = [] results = split_mat(mat, **kwargs) for result in results: indexes, _ = result r1, r2, c1, c2 = indexes img = bimg.crop((c1, r1, c2, r2)) splits.append((indexes, img)) return splits
def split_img(bin_img, col_rate=0.7, row_rate=0.4, count=None): mat = img2mat(bin_img) splits = [] res_list = split_mat(mat, col_rate=col_rate, row_rate=row_rate, count=count) for res in res_list: tp, _ = res r1, r2, c1, c2 = tp _img = bin_img.crop((c1, r1, c2, r2)) splits.append((tp, _img)) return splits
def rename(splitpath, trainpath, chars, cnt=3): _chars = {} for char in chars: print(char, cnt) _chars[char] = cnt files = os.listdir(splitpath) for file in files: img = Image.open('{}/{}'.format(splitpath, file)) #img = img mat = img2mat(img) show_pixel(mat) while True: char = raw_input( 'enter the char (if want pass just press `Enter`):') if char == '': break if char not in _chars: print('char `{}` not in the chars,please check'.format(char)) elif _chars[char] <= 0: break else: _chars[char] -= 1 i = _chars[char] fname = '.'.join([file, str(i), char, str(ord(char)), 'bmp']) img.save('{}/{}'.format(trainpath, fname)) break #check miss print('still miss chars : ', end='') miss = False for char in _chars: need = _chars[char] if need > 0: miss = True print('`{}`#{},'.format(char, need), end='') print('') if miss == False: break
def rename(splitpath, trainpath, count, **kwargs): chars = get_chars(**kwargs) chars_map = {} for char in chars: chars_map[char] = count files = os.listdir(splitpath) files = filter(lambda x: not x.startswith('.'), files) for file in files: img = Image.open('{}/{}'.format(splitpath, file)) mat = img2mat(img) show_pixel(mat) while True: char = raw_input('enter the char (if want pass just press `Enter`):') if char == '': break if char not in chars_map: click.echo('!!! char `{}` not in the chars,please check'.format(char)) continue elif chars_map[char] > 0: chars_map[char] -= 1 i = chars_map[char] fname = '.'.join([file, str(i), char, str(ord(char)), 'bmp']) img.save('{}/{}'.format(trainpath, fname)) break miss = filter(lambda x: x[1] > 0, chars_map.iteritems()) # check miss if not miss: break miss = map(lambda x: '`{0}`#{1}'.format(*x), miss) click.echo('still miss chars : {}'.format(','.join(miss)))
def rename(splitpath,trainpath,chars,cnt=3): _chars = {} for char in chars: print(char,cnt) _chars[char] = cnt files = os.listdir(splitpath) for file in files: img = Image.open('{}/{}'.format(splitpath,file)) #img = img mat = img2mat(img) show_pixel(mat) while True: char = raw_input('enter the char (if want pass just press `Enter`):') if char == '': break if char not in _chars: print('char `{}` not in the chars,please check'.format(char)) elif _chars[char] <= 0: break else: _chars[char] -= 1 i = _chars[char] fname = '.'.join([file,str(i),char,str(ord(char)),'bmp']) img.save('{}/{}'.format(trainpath,fname)) break #check miss print('still miss chars : ',end='') miss = False for char in _chars: need = _chars[char] if need >0: miss = True print('`{}`#{},'.format(char,need),end='') print('') if miss == False: break
def read_raw_img(raw_img): bin_img = chg_img(raw_img) mat = img2mat(bin_img) #print_mat(mat,replace=lambda x:' ' if x<200 else 'M') result = read_mat(mat) return result
return result def read_mat(mat): res_list = split_mat(mat,**split_kwargs) result = [] for res in res_list: tp,_mat = res #print_mat(_mat,replace=lambda x:' ' if x<200 else 'M') label,sim = classfiy(_mat) result.append((label,sim)) return result if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-c','--classfiy',action='store',help='give a split image ') parser.add_argument('-r','--readraw',action='store',help='give a raw image') args = parser.parse_args() if args.classfiy != None: img = Image.open(args.classfiy) mat = img2mat(img) result = classfiy(mat) print(result) if args.readraw != None: raw_img = Image.open(args.readraw) labels = read_raw_img(raw_img) print(labels)
def read_img(img): bimg = bin_img(img) mat = img2mat(bimg) result = read_mat(mat) return result
for res in res_list: tp, _mat = res #print_mat(_mat,replace=lambda x:' ' if x<200 else 'M') label, sim = classfiy(_mat) result.append((label, sim)) return result if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-c', '--classfiy', action='store', help='give a split image ') parser.add_argument('-r', '--readraw', action='store', help='give a raw image') args = parser.parse_args() if args.classfiy != None: img = Image.open(args.classfiy) mat = img2mat(img) result = classfiy(mat) print(result) if args.readraw != None: raw_img = Image.open(args.readraw) labels = read_raw_img(raw_img) print(labels)