python批量识别图片指定区域文字内容
作者:林圈圈的 时间:2021-04-26 13:59:54
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识别自行研究
来源:https://blog.csdn.net/qq_41616397/article/details/87980510
标签:python,识别图片
0
投稿
猜你喜欢
Python实现的金山快盘的签到程序
2022-12-05 21:14:51
详解Windows下安装Nodejs步骤
2024-05-03 15:56:34
python库sklearn常用操作
2022-06-24 13:44:33
Python接口测试文件上传实例解析
2023-04-19 15:59:28
Go语言数据结构之选择排序示例详解
2024-04-26 17:25:33
Python判断是否json是否包含一个key的方法
2023-02-25 02:58:40
浅淡BANNER设计
2010-09-25 18:37:00
用python开发一款操作MySQL的小工具
2024-01-26 08:55:19
如何增大MYSQL数据库连接数
2010-10-25 20:28:00
Python:__eq__和__str__函数的使用示例
2023-01-12 02:45:31
探讨:如何查看和获取SQL Server实例名
2024-01-18 03:47:42
MSSQL数据库获取TEXT字段的内容长度
2024-01-24 12:50:54
Python之requests的使用(二)
2021-10-30 16:44:51
Django之创建引擎索引报错及解决详解
2023-06-11 10:55:15
Javascript技术栈中的四种依赖注入小结
2024-04-18 10:54:08
将Python代码打包为jar软件的简单方法
2023-10-25 17:46:34
jquery实现表单验证并阻止非法提交
2023-07-02 05:31:28
asp如何创建目录?
2009-11-14 20:51:00
Golang与其他语言不同的九个特性
2024-05-10 13:57:41
Python装饰器的练习题
2023-12-07 13:11:48