c001.手書き文字-日本語-共有ソース

サイトの紹介と使い方動画


created: 2021/10/20 modified: 2021/11/02

概要

  1. とりあえずの共有ソースコードです。
  2. 時期を見てクラス化したいと思います。

共有コード

1グループ対応バージョン:c001_env.py

import sys,glob
import numpy as np
from PIL import Image

cls      = sys.argv[1]

root_dir    = '/home/kurodon/handwritten/dendo'
file_type   = '/*.jpg'
img_mode    = 'RGB'
img_size    = 60

if( cls=="F01-01" ):
    group_dir = "numbers"
    groups    = "0123456789"
elif( cls=="F01-02" ):
    group_dir = "katakana"
    groups    = "アイウエオカキクケコサシスセソタツテトナニヌネノハヒフヘホ"
#チ抜き
else:
    group_dir = ""
    groups    = ""

train_dir   = root_dir+'/'+group_dir+'/train/'
test_dir    = root_dir+'/'+group_dir+'/test/'
family_io   = root_dir+'/io/'+group_dir+'_'
nb_classes  = len(groups)

npy_x_train = family_io+'x_train.npy'
npy_y_train = family_io+'y_train.npy'
fs_json     = family_io+'model.json'
fs_hdf5     = family_io+'model.hdf5'

best_eval   = 0.9
better_eval = 0.7
multi_eval  = 0.3

def load_img( rdir ):
    x1 = []
    y1 = []

    for idx,group in enumerate( groups ):
        img_dir = rdir + group
        files = glob.glob( img_dir+file_type )
        print( 'char {} '.format(group),format(len(files)) )
        for ii,ff in enumerate( files ):
            img = Image.open( ff )
            img = img.resize( (img_size,img_size) )
            img = img.convert( img_mode )
            data = np.asarray( img )
            x2   = data.tolist()
            x1.append( x2 )
            y1.append( idx )

    ximg  = np.array( x1,dtype='int8' )
    yimg  = np.array( y1,dtype='int8' )

    return ximg,yimg

def best_char( pred ):
    max = 0.0
    idx = -1

    for ii in range(len(pred)):
        if( max<pred[ii] ):
           max = pred[ii]
           idx = ii

    return idx,max

def idx_to_char( idx,groups ):
    return groups[idx]

def eval_char( model,c_eval ):
    img_dir = test_dir + c_eval
    files   = glob.glob( img_dir+file_type )
    nfiles  = len(files)
    for ii,ff in enumerate(files):
        img = Image.open( ff )
        img = img.resize( (img_size,img_size) )
        img = img.convert( img_mode )
        data = np.asarray( img )
        x2   = data.tolist()
        x1 = []
        x1.append( x2 )
        data = np.asarray( x1,dtype='int8' )
        data = data.astype( 'float32') /255

        pre = model.predict( data )
        cc = str(ii) + " " + c_eval + " "
        idx2,max = best_char(pre[0])
        cc += groups[idx2]
        cc += " "
        for jj in range(len(pre[0])):
            if( pre[0][jj]>0.01 ):
                cc += str(pre[0][jj])
                cc += ","
        print( cc )

  1. コマンドラインの第1引数でfamilyを指定します。
  2. root_dir、file_type、img_mode、img_sizeは固定です。
  3. group_dirとgroupsはfamilyにより決定されます。
  4. train_dir、test_dir、family_ioは、ディレクトリ構造に従います。
  5. npy_x_train、npy_y_trainは、画像⇒numpyデータファイルです。
  6. fs_json、fs_hdf5は、モデルファイルです。
  7. load_img()は、画像データをnumpyデータに変換します。

Family対応バージョン:c002_env.py

import sys,glob
import numpy as np
from PIL import Image

cls      = sys.argv[1]

root_dir    = '/home/kurodon/handwritten/dendo'
file_type   = '/*.jpg'
img_mode    = 'RGB'
img_size    = 60

if( cls=="F02-01" ):
    family_dir    = [ "numbers","katakana" ]
    f_numbers     = "0123456789"
    f_katakana    = "アイウエオカキクケコサシスセソタツテトナニヌネノハヒフヘホ"
#チ抜き
    f_all         = f_numbers + f_katakana
    family_member = [ f_numbers,f_katakana ]
    family_name   = "01"
    nb_classes    = len( f_all )
else:
    group_dir = ""
    groups    = ""

#train_dir   = root_dir+'/'+group_dir+'/train/'
#test_dir    = root_dir+'/'+group_dir+'/test/'

family_io   = root_dir+'/io/'+family_name+'_'
npy_x_train = family_io+'x_train.npy'
npy_y_train = family_io+'y_train.npy'
fs_json     = family_io+'model.json'
fs_hdf5     = family_io+'model.hdf5'

best_eval   = 0.9
better_eval = 0.7
multi_eval  = 0.3

def load_img2( tdir ):
    x1 = []
    y1 = []
    idx2 = 0

    for ii,f_dir in enumerate( family_dir ):
        groups = family_member[ii]
        rdir   = root_dir+'/'+f_dir+tdir
        print( groups,rdir )
        for idx,group in enumerate( groups ):
            img_dir = rdir + group
            files = glob.glob( img_dir+file_type )
            print( 'char {} '.format(group),format(len(files)) )
            for ii,ff in enumerate( files ):
                img = Image.open( ff )
                img = img.resize( (img_size,img_size) )
                img = img.convert( img_mode )
                data = np.asarray( img )
                x2   = data.tolist()
                x1.append( x2 )
                y1.append( idx2 )
            idx2 += 1

    ximg  = np.array( x1,dtype='int8' )
    yimg  = np.array( y1,dtype='int8' )

    return ximg,yimg

def best_char( pred ):
    max = 0.0
    idx = -1

    for ii in range(len(pred)):
        if( max<pred[ii] ):
           max = pred[ii]
           idx = ii

    return idx,max

#def idx_to_char( idx,groups ):
#    return groups[idx]

def eval_char2( model,group,c_eval ):
    img_dir = root_dir + '/'+ group + '/test/' + c_eval
    files   = glob.glob( img_dir+file_type )
    nfiles  = len(files)
    for ii,ff in enumerate(files):
        img = Image.open( ff )
        img = img.resize( (img_size,img_size) )
        img = img.convert( img_mode )
        data = np.asarray( img )
        x2   = data.tolist()
        x1 = []
        x1.append( x2 )
        data = np.asarray( x1,dtype='int8' )
        data = data.astype( 'float32') /255

        pre = model.predict( data )
        cc = str(ii) + " " + c_eval + " "
        idx2,max = best_char(pre[0])
        cc += f_all[idx2]
        cc += " "
        for jj in range(len(pre[0])):
            if( pre[0][jj]>0.01 ):
                cc += str(pre[0][jj])
                cc += ","
#        print( cc,pre.shape,f_all )
        print( cc )

HDF5ファイル名指定バージョン:c003_env.py

import sys,glob
import numpy as np
from PIL import Image

cls         = sys.argv[1]
m_name      = sys.argv[4]

root_dir    = '/home/kurodon/handwritten/dendo'
file_type   = '/*.jpg'
img_mode    = 'RGB'
img_size    = 60

if( cls=="F02-01" ):
    family_dir    = [ "numbers","katakana" ]
    f_numbers     = "0123456789"
    f_katakana    = "アイウエオカキクケコサシスセソタツテトナニヌネノハヒフヘホ"
#チ抜き
    f_all         = f_numbers + f_katakana
    family_member = [ f_numbers,f_katakana ]
    family_name   = "01"
    nb_classes    = len( f_all )
else:
    group_dir = ""
    groups    = ""

#train_dir   = root_dir+'/'+group_dir+'/train/'
#test_dir    = root_dir+'/'+group_dir+'/test/'

family_io   = root_dir+'/io/'+family_name+'_'
npy_x_train = family_io+'x_train.npy'
npy_y_train = family_io+'y_train.npy'
fs_json     = family_io+m_name+'model.json'
fs_hdf5     = family_io+m_name+'model.hdf5'

best_eval   = 0.9
better_eval = 0.7
multi_eval  = 0.3

def load_img2( tdir ):
    x1 = []
    y1 = []
    idx2 = 0

    for ii,f_dir in enumerate( family_dir ):
        groups = family_member[ii]
        rdir   = root_dir+'/'+f_dir+tdir
        print( groups,rdir )
        for idx,group in enumerate( groups ):
            img_dir = rdir + group
            files = glob.glob( img_dir+file_type )
            print( 'char {} '.format(group),format(len(files)) )
            for ii,ff in enumerate( files ):
                img = Image.open( ff )
                img = img.resize( (img_size,img_size) )
                img = img.convert( img_mode )
                data = np.asarray( img )
                x2   = data.tolist()
                x1.append( x2 )
                y1.append( idx2 )
            idx2 += 1

    ximg  = np.array( x1,dtype='int8' )
    yimg  = np.array( y1,dtype='int8' )

    return ximg,yimg

def best_char( pred ):
    max = 0.0
    idx = -1

    for ii in range(len(pred)):
        if( max<pred[ii] ):
           max = pred[ii]
           idx = ii

    return idx,max

#def idx_to_char( idx,groups ):
#    return groups[idx]

def eval_char2( model,group,c_eval ):
    img_dir = root_dir + '/'+ group + '/test/' + c_eval
    files   = glob.glob( img_dir+file_type )
    nfiles  = len(files)
    for ii,ff in enumerate(files):
        img = Image.open( ff )
        img = img.resize( (img_size,img_size) )
        img = img.convert( img_mode )
        data = np.asarray( img )
        x2   = data.tolist()
        x1 = []
        x1.append( x2 )
        data = np.asarray( x1,dtype='int8' )
        data = data.astype( 'float32') /255

        pre = model.predict( data )
        cc = str(ii) + " " + c_eval + " "
        idx2,max = best_char(pre[0])
        cc += f_all[idx2]
        cc += " "
        for jj in range(len(pre[0])):
            if( pre[0][jj]>0.01 ):
                cc += str(pre[0][jj])
                cc += ","
#        print( cc,pre.shape,f_all )
        print( cc )
トップへ戻る
タイトルとURLをコピーしました