python识别图片指定位置文字_python批量识别图片指定区域文字内容
Python批量識(shí)別圖片指定區(qū)域文字內(nèi)容,供大家參考,具體內(nèi)容如下
簡(jiǎn)介
對(duì)于一張圖片,需求識(shí)別指定區(qū)域的內(nèi)容
1.截取原始圖上的指定圖片當(dāng)做模板
2.根據(jù)模板相似度去再原始圖片上識(shí)別準(zhǔn)確坐標(biāo)
3.根據(jù)坐標(biāo)剪切出指定位置圖片,也就是所需的內(nèi)容區(qū)域
4.對(duì)指定位置圖片進(jìn)行ocr識(shí)別
環(huán)境
Ubuntu18.04
Python2.7
所需Python模塊
1.aircv
用于識(shí)別模板再原始圖的位置坐標(biāo)
pip install aircv
2.Pillow
用于剪裁圖片
pip install Pillow
3.Tesseract
文字識(shí)別
在此也可以用平臺(tái)端的API進(jìn)行更精準(zhǔn)的識(shí)別
ubuntu下Tesseract環(huán)境安裝
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代碼
識(shí)別對(duì)應(yīng)位置
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import aircv
def matchImg(imgsrc, imgobj, confidence=0.2):
"""
圖片對(duì)比識(shí)別imgobj在imgsrc上的相對(duì)位置(批量識(shí)別統(tǒng)一圖片中需要的部分)
:param imgsrc: 原始圖片路徑(str)
:param imgobj: 待查找圖片路徑(模板)(str)
:param confidence: 識(shí)別度(0
:return: None or dict({'confidence': 相似度(float), 'rectangle': 原始圖片上的矩形坐標(biāo)(tuple), 'result': 中心坐標(biāo)(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):
"""
根據(jù)坐標(biāo)位置剪切圖片
:param imgsrc: 原始圖片路徑(str)
:param out_img_name: 剪切輸出圖片路徑(str)
:param coordinate: 原始圖片上的坐標(biāo)(tuple) egg:(x, y, w, h) ---> x,y為矩形左上角坐標(biāo), w,h為右下角坐標(biāo)
:return:
"""
image = Image.open(imgsrc)
region = image.crop(coordinate)
region = ImageEnhance.Contrast(region).enhance(1.5)
region.save(out_img_name)
圖片識(shí)別
#!/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)
對(duì)于三方API識(shí)別自行研究
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
本文標(biāo)題: python批量識(shí)別圖片指定區(qū)域文字內(nèi)容
本文地址: http://www.cppcns.com/jiaoben/python/258124.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python识别图片指定位置文字_python批量识别图片指定区域文字内容的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云 Aliplayer高级功能介绍(
- 下一篇: vscode安装python插件失败_v