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


Posted in Python onApril 30, 2019

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

简介

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

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

环境

Ubuntu18.04
Python2.7

所需Python模块

1.aircv

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

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识别自行研究

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python3基础之函数用法
Aug 13 Python
python安装oracle扩展及数据库连接方法
Feb 21 Python
基于python的字节编译详解
Sep 20 Python
python使用标准库根据进程名如何获取进程的pid详解
Oct 31 Python
一看就懂得Python的math模块
Oct 21 Python
python 实现语音聊天机器人的示例代码
Dec 02 Python
深入理解Django-Signals信号量
Feb 19 Python
Django shell调试models输出的SQL语句方法
Aug 29 Python
检测tensorflow是否使用gpu进行计算的方式
Feb 03 Python
关于tf.TFRecordReader()函数的用法解析
Feb 17 Python
python实现输入三角形边长自动作图求面积案例
Apr 12 Python
Python干货实战之八音符酱小游戏全过程详解
Oct 24 Python
Python3.5装饰器典型案例分析
Apr 30 #Python
python如何制作缩略图
Apr 30 #Python
Python3.5装饰器原理及应用实例详解
Apr 30 #Python
11个Python Pandas小技巧让你的工作更高效(附代码实例)
Apr 30 #Python
python制作图片缩略图
Apr 30 #Python
python获取微信企业号打卡数据并生成windows计划任务
Apr 30 #Python
使用Python实现企业微信的自动打卡功能
Apr 30 #Python
You might like
php中批量修改文件后缀名的函数代码
2011/10/23 PHP
处理(php-cgi.exe - FastCGI 进程超过了配置的请求超时时限)的问题
2013/07/03 PHP
jQuery Mobile + PHP实现文件上传
2014/12/12 PHP
WordPress中设置Post Type自定义文章类型的实例教程
2016/05/10 PHP
php实现算术验证码功能
2018/12/05 PHP
javaScript 数值型和字符串型之间的转换
2009/07/25 Javascript
几个有趣的Javascript Hack
2010/07/24 Javascript
封装的原生javascript弹出层代码
2010/09/24 Javascript
XMLHTTPRequest的属性和方法简介
2010/11/23 Javascript
js+css实现的圆角边框TAB选项卡滑动门代码分享(2款)
2015/08/26 Javascript
JS+Ajax实现百度智能搜索框
2017/08/04 Javascript
javascript与PHP动态往类中添加方法对比
2018/03/21 Javascript
Vue.js最佳实践(五招助你成为vuejs大师)
2018/05/04 Javascript
[36:52]DOTA2真视界:基辅特锦赛总决赛
2017/05/21 DOTA
python读取浮点数和读取文本文件示例
2014/05/06 Python
在Python中使用HTMLParser解析HTML的教程
2015/04/29 Python
Python标准库06之子进程 (subprocess包) 详解
2016/12/07 Python
python用pickle模块实现“增删改查”的简易功能
2017/06/07 Python
Python实现计算圆周率π的值到任意位的方法示例
2018/05/08 Python
python学生信息管理系统(初级版)
2018/10/17 Python
pycharm 在windows上编辑代码用linux执行配置的方法
2018/10/27 Python
Python实现的删除重复文件或图片功能示例【去重】
2019/04/23 Python
django 微信网页授权认证api的步骤详解
2019/07/30 Python
Python中如何将一个类方法变为多个方法
2019/12/30 Python
pycharm工具连接mysql数据库失败问题
2020/04/01 Python
python对指定字符串逆序的6种方法(小结)
2020/04/02 Python
django 数据库 get_or_create函数返回值是tuple的问题
2020/05/15 Python
Python实现LR1文法的完整实例代码
2020/10/25 Python
AmazeUI中模态框的实现
2020/08/19 HTML / CSS
一套C++笔试题面试题
2012/06/06 面试题
机工车间主任岗位职责
2014/03/05 职场文书
2015年党员干部承诺书
2015/01/21 职场文书
2015年妇幼卫生工作总结
2015/05/23 职场文书
大学军训口号大全
2015/12/24 职场文书
2019年大学生职业生涯规划书最新范文
2019/03/25 职场文书
中国古风插画师排行榜:夏达第一,第三是阴阳师姑获鸟皮肤创作者
2022/03/18 国漫