基于Python的OCR實(shí)現(xiàn)示例
摘要:
近幾天在做一個(gè)東西,其中需要對(duì)圖像中的文字進(jìn)行識(shí)別,看了前輩們的文章,找到兩個(gè)較簡(jiǎn)單的方法:使用python的pytesseract庫(kù)和調(diào)用百度AI平臺(tái)接口。寫(xiě)下這篇文章做一個(gè)比較簡(jiǎn)短的記錄和學(xué)習(xí),后期如果有新內(nèi)容再行補(bǔ)充。
1、使用python的pytesseract庫(kù)
主要是安裝庫(kù),比較簡(jiǎn)單,直接使用 pip install 安裝即可;另外,如果進(jìn)行中文識(shí)別,需要下載語(yǔ)言包,并配置好相應(yīng)環(huán)境,具體操作可以進(jìn)行百度,教程有不少。因?yàn)檫@個(gè)識(shí)別方法比較簡(jiǎn)單(但效果并不是很理想),下面直接貼出測(cè)試代碼:
import pytesseract from PIL import Image img = Image.open('./testImages/test01.jpg') pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe' s = pytesseract.image_to_string(img, lang='chi_sim') #不加lang參數(shù)的話,默認(rèn)進(jìn)行英文識(shí)別 print(s)
2、調(diào)用百度AI平臺(tái)接口(有調(diào)用次數(shù)限制,通用50000次/天,學(xué)習(xí)完全夠用)
這個(gè)類(lèi)似于調(diào)用接口實(shí)現(xiàn)詞法分析等操作,首先通過(guò)注冊(cè)獲得APP_ID、API_KEY、SECRET_KEY,然后調(diào)用接口實(shí)現(xiàn)OCR。由于是在線API,如果圖片體積比較大,涉及到上傳數(shù)據(jù)、分析數(shù)據(jù)、返回?cái)?shù)據(jù)等一系列操作,需要一定的時(shí)間。此外,因?yàn)榉祷氐氖?nbsp;dict 類(lèi)型數(shù)據(jù),所以需要對(duì)結(jié)果進(jìn)行處理(這套算法是按行識(shí)別文字的,準(zhǔn)確率較高,基本可以直接將結(jié)果進(jìn)行提取和拼接)。實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,下面直接貼出代碼:
from aip import AipOcr APP_ID = '00000000' API_KEY = '00000000000000000000' SECRET_KEY = '00000000000000000000' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() def image2text(fileName): image = get_file_content(fileName) dic_result = client.basicGeneral(image) res = dic_result['words_result'] result = '' for m in res: result = result + str(m['words']) return result getresult = image2text('./test01.jpg') print(getresult)
小結(jié):
主要是初次接觸OCR這個(gè)領(lǐng)域所做的一些筆記,后續(xù)再深入進(jìn)行學(xué)習(xí)。
python實(shí)現(xiàn)的ocr接口
import pytesseract import requests from PIL import Image from PIL import ImageFilter from StringIO import StringIO from werkzeug.utils import secure_filename from gevent import monkey from gevent.pywsgi import WSGIServer monkey.patch_all() from flask import Flask,render_template,jsonify,request,send_from_directory import time import os import base64 import random app = Flask(__name__) UPLOAD_FOLDER='upload' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER basedir = os.path.abspath(os.path.dirname(__file__)) ALLOWED_EXTENSIONS = set(['png','jpg','JPG','PNG']) def allowed_file(filename): return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS @app.route('/',methods=['GET'],strict_slashes=False) def indexpage(): return render_template('index.html') @app.route('/',methods=['POST'],strict_slashes=False) def api_upload(): log = open("error.log","w+") file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER']) if not os.path.exists(file_dir): os.makedirs(file_dir) print request.headers print >> log, request.headers f = request.files['file'] postLang = request.form.get("lang", type=str) log.close() if f and allowed_file(f.filename): fname = secure_filename(f.filename) ext = fname.rsplit('.',1)[1] unix_time = int(time.time()) new_filename = str( random.randrange(0, 10001, 2))+str(unix_time)+'.'+ext f.save(os.path.join(file_dir,new_filename)) if cmp(postLang, "chi_sim"): strboxs = pytesseract.image_to_boxes(Image.open("/var/OCRhtml/upload/" + new_filename), lang="chi_sim") strdata = pytesseract.image_to_string(Image.open("/var/OCRhtml/upload/" + new_filename), lang="chi_sim") print "Chinese" else: strboxs = pytesseract.image_to_boxes(Image.open("/var/OCRhtml/upload/"+new_filename)) strdata = pytesseract.image_to_string(Image.open("/var/OCRhtml/upload/"+new_filename)) return jsonify({"errno":0, "msg":"succeed ","data":strdata,"info":strboxs}) else: return jsonify({"errno":1001, "errmsg":u"failed"}) if __name__ == '__main__': http_server = WSGIServer(('', 80), app) http_server.serve_forever()
到此這篇關(guān)于基于Python的OCR實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python OCR 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python 實(shí)現(xiàn)任意區(qū)域文字識(shí)別(OCR)操作
- Python3使用tesserocr識(shí)別字母數(shù)字驗(yàn)證碼的實(shí)現(xiàn)
- 如何使用Python進(jìn)行PDF圖片識(shí)別OCR
- python3.7中安裝paddleocr及paddlepaddle包的多種方法
- Python調(diào)用百度OCR實(shí)現(xiàn)圖片文字識(shí)別的示例代碼
- python圖片驗(yàn)證碼識(shí)別最新模塊muggle_ocr的示例代碼
- 如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具
- Python基于百度AI實(shí)現(xiàn)OCR文字識(shí)別
- python3安裝OCR識(shí)別庫(kù)tesserocr過(guò)程圖解
- python 如何做一個(gè)識(shí)別率百分百的OCR
相關(guān)文章
如何用python實(shí)現(xiàn)復(fù)制粘貼功能
這篇文章主要介紹了如何用python實(shí)現(xiàn)復(fù)制粘貼功能,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python基于紋理背景和聚類(lèi)算法實(shí)現(xiàn)圖像分割詳解
這篇文章將詳細(xì)講解Python圖和基于紋理背景的圖像分割和聚類(lèi)算法實(shí)現(xiàn)圖像分割效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01python神經(jīng)網(wǎng)絡(luò)Keras實(shí)現(xiàn)GRU及其參數(shù)量
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Keras實(shí)現(xiàn)GRU及其參數(shù)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python3開(kāi)發(fā)監(jiān)控自動(dòng)化觸發(fā)聲光報(bào)警
使用python制作一個(gè)自動(dòng)監(jiān)控并觸發(fā)聲光報(bào)警是不是感覺(jué)很高端,很多人都會(huì)認(rèn)為只是一件很難的事情,但實(shí)際很簡(jiǎn)單就能實(shí)現(xiàn)。2023-07-07關(guān)于django python manage.py startapp 應(yīng)用名出錯(cuò)異常原因解析
這篇文章主要介紹了關(guān)于django python manage.py startapp 應(yīng)用名出錯(cuò)異常原因解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12python和c語(yǔ)言的主要區(qū)別總結(jié)
在本篇文章里小編給各位整理了關(guān)于python和c語(yǔ)言的主要區(qū)別的相關(guān)知識(shí)帖內(nèi)容,有需要的朋友們學(xué)習(xí)閱讀下。2019-07-07