How to implement an OCR model using CNNs, RNNs, and CTC loss.
This example demonstrates a simple OCR model built with the Functional API. Apart from combining CNN and RNN, it also illustrates how you can instantiate a new layer and use it as an “Endpoint layer” for implementing CTC loss. For a detailed guide to layer subclassing, please check out this page in the developer guides.
我把本次用到的資料集下載過來了,我們要解決的是驗證碼識別問題,然后每張圖片里的驗證碼就是圖片名稱

The dataset contains 1040 captcha files as png images. The label for each sample is a string, the name of the file (minus the file extension). We will map each character in the string to an integer for training the model. Similary, we will need to map the predictions of the model back to strings. For this purpose we will maintain two dictionaries, mapping characters to integers, and integers to characters, respectively.
關于這篇 OCR 識別驗證碼文章,我只會解讀關鍵代碼部分,完整的代碼我會放在 Github 倉庫: https://github.com/MaoXianXin/Tensorflow_tutorial/blob/ViT/OCR/demo.py,大家可以自取,
# Get list of all the images
images = sorted(list(map(str, list(data_dir.glob("*.png")))))
labels = [img.split(os.path.sep)[-1].split(".png")[0] for img in images]
characters = set(char for label in labels for char in label)
print("Number of images found: ", len(images))
print("Number of labels found: ", len(labels))
print("Number of unique characters: ", len(characters))
print("Characters present: ", characters)
images 的展示如下所示,所以此處我們得到的 images 其實是一個圖片路勁串列

labels 的展示結果如下所示,這里每個 labels 里的元素都是和上面的 images 的元素一一對應的,

這里我們通過 set 進行去重,最后得到了 1040 張圖片的 label name 所用到的所有字符集合

# Mapping characters to integers
char_to_num = layers.StringLookup(
vocabulary=list(characters), mask_token=None
)
# Mapping integers back to original characters
num_to_char = layers.StringLookup(
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
)
下圖展示上面代碼中的 vocabulary,也就是我們基于 characters 的 19 個字符,建立起來的字典,用于 StringLookup

_, ax = plt.subplots(4, 4, figsize=(10, 5))
for batch in train_dataset.take(1):
images = batch["image"]
labels = batch["label"]
for i in range(16):
img = (images[i] * 255).numpy().astype("uint8")
label = tf.strings.reduce_join(num_to_char(labels[i])).numpy().decode("utf-8")
ax[i // 4, i % 4].imshow(img[:, :, 0].T, cmap="gray")
ax[i // 4, i % 4].set_title(label)
ax[i // 4, i % 4].axis("off")
plt.show()
下圖是對原始驗證碼圖片的展示結果:

從圖片來看,這個驗證碼是 N 多年前的了,現在的難多了,不過也算一個 Demo 供大家學習吧
最后放一下預測的結果吧,因為我才剛接觸 OCR 所以很多實驗也沒做,更多的經驗心得方面估計要后面才能分享

從預測結果來看,用這個 Demo 的網路,解決這種簡單問題,看來沒毛病哈,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302958.html
標籤:AI
上一篇:【Halcon影像】形狀模板匹配
下一篇:【論文翻譯】DeepWalk: Online Learning of Social Representations
