当前位置:首页 » 《关注互联网》 » 正文

简单的 OCR 识别验证码 Demo | Keras 实现_XianxinMao的博客

12 人参与  2022年01月21日 10:35  分类 : 《关注互联网》  评论

点击全文阅读


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 的网络,解决这种简单问题,看来没毛病哈。


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/33648.html

验证码  图片  展示  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 妻子辱我爸受贿自杀,我掏出一等军功章节选推荐_[陈素云辰朋友]小说精彩章节分享
  • 全书浏览苔藓爬满旧日诺言新上(顾砚廷慕晚夏)_苔藓爬满旧日诺言新上(顾砚廷慕晚夏)全书结局
  • 顾尘傅雅宁(神女老婆,却在背地承欢作乐+后续+结局)结局_(顾尘傅雅宁神女老婆,却在背地承欢作乐+后续+结局全书结局)结局列表_笔趣阁(顾尘傅雅宁)
  • 「老婆怀上助理的孩子后,助理要求我净身出户」章节限时抢先看‌_「黄秋雅秋雅姐刘嘉铭」后续完结版
  • 此去经年人未还,沈青禾霍沉洲_此去经年人未还,沈青禾霍沉洲
  • 我爸娶了九十九个媳妇都死了,这次准备娶我的女同学小说精彩章节免费试读_[小梅娶媳妇孤儿]全文免费在线阅读
  • 此去经年人未还结局+番外文章简述(沈青禾霍沉洲)列表_此去经年人未还结局+番外文章简述
  • 完结文寻你寻不到归期结局+完结列表_完结文寻你寻不到归期结局+完结(姜昭意盛西)
  • 江以蓁的潮起时问归期高分佳作江以蓁秦司礼全书在线
  • 「亲手逼死儿子后,男人悔不当初」后续全文免费阅读_[傅司衍轩轩佳佳]最新章节免费阅读
  • (番外)+(全书)寻你寻不到归期+后续+结局(姜昭意盛西辞)全书在线_寻你寻不到归期+后续+结局免费列表_笔趣阁(姜昭意盛西辞)
  • 全文他亲手埋葬的爱结局+番外(谢怀商温南枝)列表_全文他亲手埋葬的爱结局+番外宝藏文(谢怀商温南枝)全文他亲手埋葬的爱结局+番外在线

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1