Pengenalan karakter

Bekerja dengan gambar adalah salah satu tugas paling umum dalam pembelajaran mesin. Kami akan menunjukkan contoh pemrosesan gambar, memperoleh matriks (tensor) angka, menyiapkan data set pelatihan, contoh arsitektur jaringan saraf.





Bekerja dengan gambar adalah salah satu tugas paling umum dalam pembelajaran mesin. Gambar biasa, yang dapat dilihat oleh seseorang secara jelas, tidak memiliki arti dan interpretasi untuk komputer hanya jika tidak ada jaringan saraf terlatih yang dapat menetapkan gambar tersebut ke satu kelas tertentu. Agar jaringan saraf seperti itu berfungsi, perlu untuk melatihnya pada data pelatihan, gambar yang sebelumnya diolah dan diumpankan ke masukan jaringan saraf dalam bentuk matriks angka yang mencirikan nada (warna) tertentu pada posisi tertentu di foto. Artikel ini memberikan contoh pemrosesan gambar, memperoleh matriks (tensor) angka, menyiapkan data set pelatihan, contoh arsitektur jaringan saraf.





: (CAPTCHA). , . :





  • ;





  • ;





  • ;





  • , .





Gambar 1 contoh gambar (CAPTCHA)
.1 (CAPTCHA)

100 Β«.pngΒ». 29 Β«12345789Β». ( -1Β° –+15Β°), , . , . ( ). , python 3 opencv, matplotlib, pillow. :





import cv2 #    . 
image = cv2.imread('.\Captcha.png') #  .  numpy array
#   (img, (x1, y1), (x2, y2), (255, 255, 255), 4) – 
    #    ,   ,   , 
    #     BGR,  .
image = cv2.line(image, (14, 0), (14, 50), (0, 0, 255), 1)
    …
#     ,   .,  
def view_image(image, name_wind='default'):
    cv2.namedWindow(name_wind, cv2.WINDOW_NORMAL) # #   
    cv2.imshow(name_wind, image) # #     image
    cv2.waitKey(0) # #    , 0  .
    cv2.destroyAllWindows() # #  ()  
view_image(image)  # #       
      
      



Angka:  2 contoh mendefinisikan rentang karakter
. 2

matplotlib, RGB: BGR , , . matplotlib ( ) .





, . . 3 , 47. ( ): 14–44 , : 32–62 , : 48 –72. opencv numpy array, (50, 100, 3). 3 , 50 100 . BGR (blue , green , red ),   3- 0-255.





Gambar 3 model warna RGB
.3 RGB

. , , , . B(n-m) G(k-l) R(y-z). HSV (Hue, Saturation, Value β€” , , ). opencv Heu 0 – 179, S 0 – 255, V 0 –255. Heu S 10 – 255, V 0 – 234, , .





Gambar 4 Model warna RGB (BGR) dan HSV
.4 RGB (BGR) HSV
# #   BGR    HSV
image = cv2.imread('.\captcha_png')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
      
      



HSV (50, 100, 3) (3 numpy array (50, 100), 50 , 100 ). β€” [:, :, 0] Hue, [:, :, 1] Saturation, [:, :, 2] Value.





Gambar asli

(0 – 255 – ).





[:,:, 0] , .. 179, 160 – 179 0~30 , 60 ~ 100 , 110 ~ 150 . 9 .. 160, «» .. 0~30





[:,:, 1] , , 0~10, >10





[:,:, 2] , 240 ~ 255, < 240.





S V ( ), Hue ().





mask_S = image[:, :, 1]&lt; 10; mask_V = image[:, :, 1] > 240
      
      



: (50, 100) [[True, False, ..,], …, [..]]. . (Hue) , 255, 0-179, 255 – ( , Hue).





#  255      0 - 179
image[:, :, 0][mask_S] = 255 ; image[:, :, 0][mask_V] = 255
      
      



Gambar 5 Hasil background dan bagian noise adalah 255
.5 255

β€” . , .





Gbr. 6 Memisahkan karakter menjadi rentang tertentu
.6
img_char1 = image[3: 47, 14: 44, 0].copy()
img_char2 = image[3: 47, 32: 62, 0].copy()
img_char3 = image[3: 47, 48: 78, 0].copy()
      
      



, , ( 255 (500 – 800 ), , ). N -10, N + 10.





Gambar 7 Definisi bidang karakter 1 dan 3 yang tidak memiliki data karakter ke-2
.7 1 3 , 2-

1 3 2 . , .





#     ,   
val_count_1 = img_char1[3: 47, 14: 32, 0].copy().reshape(-1) 
val_color_hue_1 = pd.Series(val_count_1).value_counts()
# val_color_hue_1 ->255 – 741, 106 – 11, 104 – 11, 20 – 1, 99 – 1.
val_color_hue_1 = pd.Series(val_count_1).value_counts().index[1] 
#    ,    Hue -10, +10.
val_color_char_hue_1_min = val_base_hue_1 – 10 = 106 - 10 = 96
val_color_char_hue_1_max = val_base_hue_1 + 10 = 106+ 10 = 116
      
      



Hue 1, 3 , 0, 255.





mask_char1 = (img_char1> 96) &amp; (img_char1&lt;116)
img_char1[~mask_char1] = 255 #    (  ) img_char1[mask_char1] = 0 #  
      
      



Gbr. 8 Menampilkan hasil sebagai bingkai data panda
.8 pandas dataframe

0 1 .





img_char1[img_char1 == 0] = 1; img_char1[img_char1 == 255] = 0
      
      



2- , , 1 3 , 2- 255 2, 1 3 .





Gambar 9 Menghapus dari data ke-2 karakter 1 dan 3 karakter
.9 2- 1 3-

2. 1, 2, 3 – 0, 1. , . ,   opencv, ( ) ,





kernel = np.ones((3, 3), np.uint8)
closing = cv2.morphologyEx(np_matrix, cv2.MORPH_CLOSE, kernel)
      
      



Gbr. 10 Koreksi data, mengisi celah
.10 ,

, , , .





Gambar 11 Lokasi simbol di tengah matriks
.11

. . ~100 , . 300 ( 44Γ—30 0 1). . , . pillow python, 44Γ—30, , nympy array. .





shift_x = [1, 1, -1, -1, -2, 2, 0, 0, 0]
shift_y = [1, 1, -1, -1, -2, 2, 0, 0, 0]
rotor_char = [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1]
char = '12345789'
#     – ~10_000 – 60_000
shift _x_r = random.choice(shift_x)
shift _y_r = random.choice(shift_y)
rotor_r = random.choice(rotor_char)
char_r = random.choice(char)
      
      



Gambar. 13 Contoh teks yang cocok dan penempatan simbol dalam matriks
.13
train_x = []
train_x.append(char)
train_x = np.array(train_x)
train_x = train_x.reshape(train_x.shape[0], train_x[1], train_x[2], 1)

      
      



(50000, 44, 30, 1), (1) .





: char_y = [0, 4, …, 29] – 50_000 ( 0-29





char = '12345789' # 29 
dict_char = {char[i]: i for i in range(len(char))}
dict_char_reverse = {i[1]: i[0] for i in dict_char.items()}

      
      



(one-hot encoding). , 29. . . , «» β€˜000000000100000000000000000000’.





Img_y = utils.to_categorical(Img_y)
#  1 -> (array( [1, 0, 0, 0, …, 0, 0],  dtype=float32)
#  2 -> (array( [0, 1, 0, 0, …, 0, 0],  dtype=float32)

      
      



.





x_train, x_test, y_train, y_test = sklearn.train_test_split(
                             out_train_x_rsh, out_train_y_sh, 
                             test_size=0.1, shuffle=True)

      
      



, mnist ( 28Γ—28) kaggle . :





#   
Import tensorflow as tf

def model_detection():
    model=tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(input_shape=(44,30, 1), filters=32, 
                kernel_size=(5, 5), padding='same', activation='relu'),
        tf.keras.layers.Conv2D( filters=32, kernel_size=(5, 5), 
                               padding='same', activation='relu'),
        tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Conv2D( filters=64, kernel_size=(3, 3), 
                padding='same', activation='relu'),
        tf.keras.layers.Conv2D( filters=64, kernel_size=(3, 3), 
                padding='same', activation='relu'),
        tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2)),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(256, activation='relu'),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(29, activation=tf.nn.softmax)])

    model.compile(optimizer='adam', loss='categorical_crossentropy',
                   metrics=['accuracy'])
    returnmodel

#   
model = model_detection()

      
      



, (valaccuracy).





checkpoint = ModelCheckpoint('captcha_1.hdf5', monitor='val_accuracy',
                                        save_best_only=True, verbose=1)

model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test), 
          verbose=1, callbacks=[checkpoint])

      
      



valaccuracy, . : , . β€” numpy array (). , . (1, 2, 3 ). . Β« – Β» .





model2 = model_detection() # 
model2.load_weights('captcha_1.hdf5') #  
prediction_ch_1 = model2.predict(char_1) #  29  
#     ,    
prediction_ch_1 = np.argmax(prediction_ch_1, axis=1)
#    ,      
dict_char_reverse[prediction_ch_1]

      
      



Algoritma ini mengolah citra berwarna yang mengandung huruf dan angka, hasil pengenalan karakter oleh neural network 95% (akurasi), dan pengenalan captcha 82% (akurasi). Dengan menggunakan contoh penguraian algoritma pengenalan karakter, Anda dapat melihat bahwa bagian utama pengembangan ditempati oleh persiapan, pemrosesan, dan pembuatan data. Memilih arsitektur dan melatih jaringan saraf adalah bagian penting dari tugas, tetapi bukan yang paling memakan waktu. Pilihan untuk memecahkan masalah mengenali angka, huruf, gambar objek, dll. himpunan, artikel ini hanya memberikan satu contoh solusi, menunjukkan langkah-langkah solusi, kesulitan yang mungkin ditemui sebagai akibat dari pekerjaan dan contoh mengatasinya. Bagaimana Anda bekerja dengan captcha?








All Articles