国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

基于Python的OCR實(shí)現(xiàn)示例

瀏覽:5日期:2022-07-31 09:58:14

摘要:

近幾天在做一個東西,其中需要對圖像中的文字進(jìn)行識別,看了前輩們的文章,找到兩個較簡單的方法:使用python的pytesseract庫和調(diào)用百度AI平臺接口。寫下這篇文章做一個比較簡短的記錄和學(xué)習(xí),后期如果有新內(nèi)容再行補(bǔ)充。

1、使用python的pytesseract庫

主要是安裝庫,比較簡單,直接使用 pip install 安裝即可;另外,如果進(jìn)行中文識別,需要下載語言包,并配置好相應(yīng)環(huán)境,具體操作可以進(jìn)行百度,教程有不少。因?yàn)檫@個識別方法比較簡單(但效果并不是很理想),下面直接貼出測試代碼:

import pytesseractfrom PIL import Imageimg = 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)行英文識別print(s)

2、調(diào)用百度AI平臺接口(有調(diào)用次數(shù)限制,通用50000次/天,學(xué)習(xí)完全夠用)

這個類似于調(diào)用接口實(shí)現(xiàn)詞法分析等操作,首先通過注冊獲得APP_ID、API_KEY、SECRET_KEY,然后調(diào)用接口實(shí)現(xiàn)OCR。由于是在線API,如果圖片體積比較大,涉及到上傳數(shù)據(jù)、分析數(shù)據(jù)、返回?cái)?shù)據(jù)等一系列操作,需要一定的時間。此外,因?yàn)榉祷氐氖?dict 類型數(shù)據(jù),所以需要對結(jié)果進(jìn)行處理(這套算法是按行識別文字的,準(zhǔn)確率較高,基本可以直接將結(jié)果進(jìn)行提取和拼接)。實(shí)現(xiàn)起來比較簡單,下面直接貼出代碼:

from aip import AipOcrAPP_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這個領(lǐng)域所做的一些筆記,后續(xù)再深入進(jìn)行學(xué)習(xí)。

python實(shí)現(xiàn)的ocr接口

import pytesseractimport requestsfrom PIL import Imagefrom PIL import ImageFilterfrom StringIO import StringIOfrom werkzeug.utils import secure_filenamefrom gevent import monkeyfrom gevent.pywsgi import WSGIServermonkey.patch_all()from flask import Flask,render_template,jsonify,request,send_from_directoryimport timeimport osimport base64import randomapp = Flask(__name__)UPLOAD_FOLDER=’upload’app.config[’UPLOAD_FOLDER’] = UPLOAD_FOLDERbasedir = 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 泰州市| 文化| 嘉定区| 乌鲁木齐市| 青神县| 那曲县| 綦江县| 略阳县| 大荔县| 正定县| 洞口县| 莲花县| 铁力市| 延边| 车致| 广水市| 金川县| 莲花县| 巴青县| 万安县| 永宁县| 涿鹿县| 济南市| 普安县| 清水县| 罗源县| 大田县| 四川省| 高州市| 遂宁市| 桓台县| 封开县| 全州县| 吉安市| 苏州市| 宣汉县| 任丘市| 嘉义市| 明光市| 大荔县| 隆化县|