当前位置: 移动技术网 > IT编程>脚本编程>Python > python批量识别图片指定区域文字内容

python批量识别图片指定区域文字内容

2019年06月05日  | 移动技术网IT编程  | 我要评论

清妃官方网站,血族德鲁依,日记大全500字

python批量识别图片指定区域文字内容,供大家参考,具体内容如下

简介

对于一张图片,需求识别指定区域的内容

1.截取原始图上的指定图片当做模板
2.根据模板相似度去再原始图片上识别准确坐标
3.根据坐标剪切出指定位置图片,也就是所需的内容区域
4.对指定位置图片进行ocr识别

环境

ubuntu18.04
python2.7

所需python模块

1.

用于识别模板再原始图的位置坐标

pip install aircv

2.pillow

用于剪裁图片

pip install pillow

3.tesseract

文字识别
在此也可以用平台端的api进行更精准的识别
ubuntu下tesseract环境安装

sudo apt-get install libpng12-dev 
sudo apt-get install libjpeg62-dev 
sudo apt-get install libtiff4-dev 
sudo apt-get install gcc 
sudo apt-get install g++ 
sudo apt-get install automake

1.tesseract-ocr安装

sudo apt-get install tesseract-ocr

2.pytesseract安装

pip install pytesseract

python代码

识别对应位置

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
import aircv


def matchimg(imgsrc, imgobj, confidence=0.2):
 """
  图片对比识别imgobj在imgsrc上的相对位置(批量识别统一图片中需要的部分)
 :param imgsrc: 原始图片路径(str)
 :param imgobj: 待查找图片路径(模板)(str)
 :param confidence: 识别度(0<confidence<1.0)
 :return: none or dict({'confidence': 相似度(float), 'rectangle': 原始图片上的矩形坐标(tuple), 'result': 中心坐标(tuple)})
 """
 imsrc = aircv.imread(imgsrc)
 imobj = aircv.imread(imgobj)

 match_result = aircv.find_template(imsrc, imobj,
         confidence) # {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)}
 if match_result is not none:
  match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽

 return match_result

图片剪裁

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
from pil import image, imageenhance

def cutimg(imgsrc, out_img_name, coordinate):
 """
  根据坐标位置剪切图片
 :param imgsrc: 原始图片路径(str)
 :param out_img_name: 剪切输出图片路径(str)
 :param coordinate: 原始图片上的坐标(tuple) egg:(x, y, w, h) ---> x,y为矩形左上角坐标, w,h为右下角坐标
 :return:
 """
 image = image.open(imgsrc)
 region = image.crop(coordinate)
 region = imageenhance.contrast(region).enhance(1.5)
 region.save(out_img_name)

图片识别

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
import pytesseract
from pil import image

image = image.open('bb.png')
code = pytesseract.image_to_string(image)
print(code)

对于三方api识别自行研究

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网