python 利用百度API識(shí)別圖片文字(多線程版)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jun 12 09:37:38 2018 利用百度api實(shí)現(xiàn)圖片文本識(shí)別 @author: XnCSD """ import glob from os import path import os from aip import AipOcr from PIL import Image from queue import Queue import threading import datetime def convertimg(picfile, outdir): '''調(diào)整圖片大小,對(duì)于過大的圖片進(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): """利用百度api識(shí)別文本,并保存提取的文字 picfile: 圖片文件名 outfile: 輸出文件 """ while not ts_queue.empty(): picfile = ts_queue.get() filename = path.basename(picfile) outfile = 'D:\Study\pythonProject\scrapy\IpProxy\port_zidian.txt' APP_ID = '' # 剛才獲取的 ID,下同 API_KEY = '' SECRECT_KEY = '' client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在識(shí)別圖片:\t" + filename) message = client.basicGeneral(img) # 通用文字識(shí)別,每天 50 000 次免費(fèi) # message = client.basicAccurate(img) # 通用文字高精度識(shí)別,每天 800 次免費(fèi) #print("識(shí)別成功!") i.close() try: filename1 = filename.split('.')[0] filename1 = ''.join(filename1) with open(outfile, 'a+') as fo: for text in message.get('words_result'): fo.writelines('\'' + filename1 + '\'' + ':' + text.get('words') + ',') fo.writelines('\n') # fo.writelines("+" * 60 + '\n') # fo.writelines("識(shí)別圖片:\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("識(shí)別成功!") except: print('識(shí)別失敗') 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("壓縮過大的圖片...") # 首先對(duì)過大的圖片進(jìn)行壓縮,以提高識(shí)別速度,將壓縮的圖片保存與臨時(shí)文件夾中 try: for picfile in glob.glob(r"D:\Study\pythonProject\scrapy\IpProxy\端口\*"): convertimg(picfile, outdir) print("圖片識(shí)別...") for picfile in glob.glob("tmp/*"): 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 = 'tmp' s = duqu_tupian(t) threads = [] 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))
速度快,準(zhǔn)確率99百分,100里必回出錯(cuò)一張。
實(shí)測,識(shí)別1500張圖片,還是小圖片驗(yàn)證碼大小,高清,用時(shí)30秒,不能識(shí)別150張,出錯(cuò)14張左右。 但總體快,不會(huì)出現(xiàn)亂碼啥的。
以上就是python 利用百度API識(shí)別圖片文字(多線程版)的詳細(xì)內(nèi)容,更多關(guān)于python 識(shí)別圖片文字的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python基于百度AI的文字識(shí)別的示例
- python利用百度AI實(shí)現(xiàn)文字識(shí)別功能
- python使用百度文字識(shí)別功能方法詳解
- Python3調(diào)用百度AI識(shí)別圖片中的文字功能示例【測試可用】
- Python基于百度云文字識(shí)別API
- Python基于百度AI實(shí)現(xiàn)OCR文字識(shí)別
- python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識(shí)別
- Python調(diào)用百度OCR實(shí)現(xiàn)圖片文字識(shí)別的示例代碼
- Python基于百度API識(shí)別并提取圖片中文字
- Python調(diào)用百度AI實(shí)現(xiàn)圖片上文字識(shí)別功能實(shí)例
相關(guān)文章
Python PaddleOCR模型訓(xùn)練及使用超詳細(xì)教程
OCR英文全稱是Optical Character Recognition,中文叫做光學(xué)字符識(shí)別,是通過掃描等光學(xué)技術(shù)與計(jì)算機(jī)技術(shù)結(jié)合的方式直接從影像中提取各類數(shù)據(jù),省去人工錄入,節(jié)約成本,這篇文章主要介紹了Python PaddleOCR模型訓(xùn)練及使用超詳細(xì)教程,需要的朋友可以參考下2024-06-06Python全角與半角之間相互轉(zhuǎn)換的方法總結(jié)
全角與半角轉(zhuǎn)換在處理漢語語料中會(huì)經(jīng)常出現(xiàn),這里分別說明漢字、數(shù)字、字母的unicode編碼范圍,下面這篇文章主要給大家介紹了關(guān)于Python全角與半角之間相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2022-03-03python實(shí)現(xiàn)決策樹分類算法代碼示例
決策樹分類算法是最為常見的一種分類算法,通過屬性劃分來建立一棵決策樹,測試對(duì)象通過在樹上由頂向下搜索確定所屬的分類,下面這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)決策樹分類算法的相關(guān)資料,需要的朋友可以參考下2022-06-06Python自動(dòng)化開發(fā)學(xué)習(xí)之三級(jí)菜單制作
這篇文章主要為大家詳細(xì)介紹了Python自動(dòng)化開發(fā)學(xué)習(xí)之三級(jí)菜單的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07