当前位置:   article > 正文

Python实现批量图像文字识别并指定部分提取_怎么批量识别图片上固定字段

怎么批量识别图片上固定字段

当我们在工作中需要批量识别同一区域内的一些数字时候可以用Python实现

需要用到这些库 easyocr openvc os matplotlib 核心是前两个

opevc实现图象裁剪,easyocr实现文字识别

安装方法

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

pip install opencv-contrib-python

pip install opencv-python

pip install opencv-python-headless

pip install easyocr

  1. # -*- coding: utf-8 -*-
  2. import easyocr
  3. import os
  4. import cv2
  5. import pandas as pd

遍历图片并裁剪

  1. # im_path:图片路径
  2. def clip_image(im_path):
  3. i=0
  4. filelist = os.listdir(im_path)
  5. for file in filelist:
  6. file_path=os.path.join(im_path,file)
  7. im=cv2.imread(file_path)
  8. #[h,w]根据自己图片中目标的位置修改
  9. im=im[19:38,256:353]
  10. b=str(i) #数字变为字符串方便后面命名
  11. save_path = r'E:\pythonprograms\easyocr\img\img2' #裁剪后路径
  12. save_path_file = os.path.join(save_path,b+".jpg")
  13. cv2.imwrite(save_path_file,im)
  14. i=i+1
  15. im_path = r'E:\pythonprograms\easyocr\img\img' #裁剪前路径
  16. clip_image(im_path)

批量文字识别、拼接合并、转为excel存储

  1. render = easyocr.Reader(['ch_sim','en'])
  2. filepath =r'E:\pythonprograms\easyocr\img\img2' #裁剪后路径
  3. file = os.listdir(filepath)
  4. spa =[]
  5. for f in file :
  6. url =os.path.join(filepath,f)
  7. content = render.readtext(url,detail=0) #detail=0 表示去掉细节
  8. s = ' '.join(content)
  9. spa.append(s)
  10. b2=pd.DataFrame(spa )
  11. b2.to_excel("results.xls")
  12. b2
04.818 ruicms
18.907 ruicms
22.556 ruicms
33.280 ruicms
43.189 rnicms
53.028 rnicm

 相关知识

读取图片有三种方式,分别是matplotlib,opencv,PIL,展示用matplotlib比较简单

定位图像想要提取的区域,可以通过画图软件查看

比如鼠标移动在上面可以看到定位像素详细。就可以对应像素位置提取图片了

  1. image1=r'E:\pythonprograms\easyocr\img\img\R-0148_01692_Screenshot.png'
  2. im=cv2.imread(image1)
  3. im2=im[19:38,256:353]

cv2读取图片后,可以获取图像属性,可以裁剪图像

https://blog.csdn.net/yukinoai/article/details/86423937

  1. # 获取图像属性
  2. shape = im.shape
  3. print('图像的形状为: ', shape) # 打印图像形状,包括行、列、通道
  4. size = im.size
  5. print('图像的像素数目为: ', size) # 打印图像的像素数目
  6. dtype = im.dtype
  7. print('图像的数据类型为: ', dtype) # 打印图像的数据类型

用matplotlib读取图片 

  1. import matplotlib.image as mpimg#读取图片
  2. import matplotlib.pyplot as plt #显示图片
  3. %matplotlib inline
  4. image = mpimg.imread(image1)
  5. plt.title('展示部分')
  6. plt.axis('off')# 不显示坐标轴
  7. plt.imshow(im2)
  8. plt.show()

  1. import matplotlib.image as mpimg#读取图片
  2. import matplotlib.pyplot as plt #显示图片
  3. %matplotlib inline
  4. image = mpimg.imread(image1)
  5. plt.title('Read Image by Matplotlib')
  6. plt.axis('off')# 不显示坐标轴
  7. plt.imshow(image)
  8. plt.show()

过程中遇到的问题记录

easyocr import 报错 

easyocr安装没问题 但  import 报错

py运行错误为:

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.

OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see Intel® Product Support

ipynb 同样会报错

方法:

删除了E:\Anaconda\anacanda3\Library\bin下的这个libiomp5md.dll文件。也可以修改后缀名。

然后就可以了。

cv2 展示图片报错

使用,cv2.imshow(" ", img)  一直报错,最终也没解决,用的matplotlib展示图片。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/295978
推荐阅读
相关标签
  

闽ICP备14008679号