python识别图片指定位置文字_python批量识别图片指定区域文字内容
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
: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批量識別圖片指定區域文字內容
本文地址: http://www.cppcns.com/jiaoben/python/258124.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python识别图片指定位置文字_python批量识别图片指定区域文字内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云 Aliplayer高级功能介绍(
- 下一篇: 利用 Linux tap/tun 虚拟设