python利用pytesseract 實(shí)現(xiàn)本地識別圖片文字
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import glob from os import path import os import pytesseract from PIL import Image from queue import Queue import threading import datetime import cv2 def convertimg(picfile, outdir): '''調(diào)整圖片大小,對于過大的圖片進(jìn)行壓縮 picfile: 圖片路徑 outdir: 圖片輸出路徑 ''' img = Image.open(picfile) width, height = img.size while (width * height > 4000000): # 該數(shù)值壓縮后的圖片大約 兩百多k width = width // 2 height = height // 2 new_img = img.resize((width, height), Image.BILINEAR) new_img.save(path.join(outdir, os.path.basename(picfile))) def baiduOCR(ts_queue): while not ts_queue.empty(): picfile = ts_queue.get() filename = path.basename(picfile) outfile = 'D:\Study\pythonProject\scrapy\IpProxy\port_zidian.txt' img = cv2.imread(picfile, cv2.IMREAD_COLOR) print("正在識別圖片:\t" + filename) message = pytesseract.image_to_string(img,lang = 'eng') message = message.replace('', '') message = message.replace('\n', '') # message = client.basicAccurate(img) # 通用文字高精度識別,每天 800 次免費(fèi) #print("識別成功!")) try: filename1 = filename.split('.')[0] filename1 = ''.join(filename1) with open(outfile, 'a+') as fo: fo.writelines('\'' + filename1 + '\'' + ':' + message + ',') fo.writelines('\n') # fo.writelines("+" * 60 + '\n') # fo.writelines("識別圖片:\t" + filename + "\n" * 2) # fo.writelines("文本內(nèi)容:\n") # # 輸出文本內(nèi)容 # for text in message.get('words_result'): # fo.writelines(text.get('words') + '\n') # fo.writelines('\n' * 2) os.remove(filename) print("識別成功!") except: print('識別失敗') print("文本導(dǎo)出成功!") print() def duqu_tupian(dir): ts_queue = Queue(10000) outdir = dir # if path.exists(outfile): # os.remove(outfile) if not path.exists(outdir): os.mkdir(outdir) print("壓縮過大的圖片...") # 首先對過大的圖片進(jìn)行壓縮,以提高識別速度,將壓縮的圖片保存與臨時(shí)文件夾中 try: for picfile in glob.glob(r"D:\Study\pythonProject\scrapy\IpProxy\tmp\*"): convertimg(picfile, outdir) print("圖片識別...") for picfile in glob.glob("tmp1/*"): ts_queue.put(picfile) #baiduOCR(picfile, outfile) #os.remove(picfile) print('圖片文本提取結(jié)束!文本輸出結(jié)果位于文件中。' ) #os.removedirs(outdir) return ts_queue except: print('失敗') if __name__ == "__main__": start = datetime.datetime.now().replace(microsecond=0) t = 'tmp1' s = duqu_tupian(t) threads = [] try: for i in range(100): t = threading.Thread(target=baiduOCR, name='th-' + str(i), kwargs={'ts_queue': s}) threads.append(t) for t in threads: t.start() for t in threads: t.join() end = datetime.datetime.now().replace(microsecond=0) print('刪除耗時(shí):' + str(end - start)) except: print('識別失敗')
實(shí)測速度慢,但用了多線程明顯提高了速度,但準(zhǔn)確度稍低,同樣高清圖片,90百分識別率。還時(shí)不時(shí)出現(xiàn)亂碼文字,亂空格,這里展現(xiàn)不了,自己實(shí)踐吧,重點(diǎn)免費(fèi)的,隨便識別,通向100張圖片,用時(shí)快6分鐘了,速度慢了一倍,但是是免費(fèi)的,挺不錯(cuò)的了。
以上就是python利用pytesseract 實(shí)現(xiàn)本地識別圖片文字的詳細(xì)內(nèi)容,更多關(guān)于python 識別圖片文字的資料請關(guān)注腳本之家其它相關(guān)文章!
- python pytesseract庫的實(shí)例用法
- python3光學(xué)字符識別模塊tesserocr與pytesseract的使用詳解
- Python基于內(nèi)置庫pytesseract實(shí)現(xiàn)圖片驗(yàn)證碼識別功能
- python下調(diào)用pytesseract識別某網(wǎng)站驗(yàn)證碼的實(shí)現(xiàn)方法
- Python 圖片文字識別的實(shí)現(xiàn)之PaddleOCR
- Python調(diào)用百度OCR實(shí)現(xiàn)圖片文字識別的示例代碼
- Python圖像處理之圖片文字識別功能(OCR)
- Python3一行代碼實(shí)現(xiàn)圖片文字識別的示例
- python利用 pytesseract快速識別提取圖片中的文字((圖片識別)
相關(guān)文章
python使用adbapi實(shí)現(xiàn)MySQL數(shù)據(jù)庫的異步存儲
這篇文章主要為大家詳細(xì)介紹了python使用adbapi實(shí)現(xiàn)MySQL數(shù)據(jù)庫的異步存儲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03python 列出面板數(shù)據(jù)所有變量名的示例代碼
在Python中,處理面板數(shù)據(jù)(Panel Data)通常使用pandas庫,特別是當(dāng)數(shù)據(jù)以DataFrame或Panel,這篇文章主要介紹了python 列出面板數(shù)據(jù)所有變量名,需要的朋友可以參考下2024-06-06Python區(qū)塊鏈創(chuàng)建Block Class教程
這篇文章主要為大家介紹了Python區(qū)塊鏈創(chuàng)建Block Class教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05利用PyQt5模擬實(shí)現(xiàn)網(wǎng)頁鼠標(biāo)移動特效
不知道大家有沒有發(fā)現(xiàn),博客園有些博客左側(cè)會有鼠標(biāo)移動特效。通過移動鼠標(biāo),會形成類似蜘蛛網(wǎng)的特效,本文將用PyQt5實(shí)現(xiàn)這一特效,需要的可以參考一下2022-03-03Python argparse 解析命令行參數(shù)模塊詳情
這篇文章主要介紹了Python argparse 解析命令行參數(shù)模塊詳情,argparse是python用于解析命令行參數(shù)和選項(xiàng)的標(biāo)準(zhǔn)模塊,用于代替已經(jīng)過時(shí)的optparse模塊2022-07-07