python?獲取圖片中文字的四種辦法
在Python中,獲取圖片中的中文文本通常需要使用光學(xué)字符識別(OCR)技術(shù).
1.使用http請求庫獲取,分別主流有2種以下庫
- 使用百度OCR API:百度提供了OCR API服務(wù),可以通過API調(diào)用來識別圖片中的文本,包括中文。你需要注冊百度開發(fā)者賬號,獲取API密鑰,然后使用Python中的HTTP請求庫發(fā)送圖片并接收識別結(jié)果
- 使用微軟Azure OCR服務(wù):微軟Azure也提供了OCR服務(wù),可以用來提取中文文本。與百度API類似,你需要注冊Azure賬號,創(chuàng)建一個OCR服務(wù),然后使用Python中的HTTP請求庫發(fā)送請求并獲取結(jié)果。
2.使用第三方庫,下面推薦4種第三方庫及源碼
Tesseract OCR庫:
pip install pytesseract
from PIL import Image import pytesseract # 打開圖像 image = Image.open('your_image.png') # 使用Tesseract進(jìn)行文本提取 text = pytesseract.image_to_string(image, lang='chi_sim') # 輸出提取的中文文本 print(text)
EasyOCR庫:
pip install easyocr
import easyocr # 創(chuàng)建EasyOCR Reader reader = easyocr.Reader(['ch_sim']) # 打開圖像 image = 'your_image.png' # 使用EasyOCR進(jìn)行文本提取 results = reader.readtext(image) # 輸出提取的中文文本 for (bbox, text, prob) in results: print(text)
PyOCR庫:
pip install pyocr
import pyocr import pyocr.builders from PIL import Image # 獲取Tesseract OCR工具 tools = pyocr.get_available_tools() tool = tools[0] # 打開圖像 image = Image.open('your_image.png') # 使用PyOCR進(jìn)行文本提取 text = tool.image_to_string( image, lang='chi_sim', builder=pyocr.builders.TextBuilder() ) # 輸出提取的中文文本 print(text)
Google Cloud Vision API庫:
pip install google-cloud-vision
from google.cloud import vision_v1p3beta1 as vision from google.oauth2 import service_account # 設(shè)置認(rèn)證憑據(jù) credentials = service_account.Credentials.from_service_account_file( 'your-service-account-key.json' ) # 創(chuàng)建Vision API客戶端 client = vision.ImageAnnotatorClient(credentials=credentials) # 打開圖像 with open('your_image.png', 'rb') as image_file: content = image_file.read() # 創(chuàng)建圖像對象 image = vision.Image(content=content) # 使用Vision API進(jìn)行文本提取 response = client.text_detection(image=image) # 輸出提取的中文文本 for text in response.text_annotations: print(text.description)
請注意,對于Google Cloud Vision API,你需要替換 'your-service-account-key.json'
為你自己的服務(wù)賬戶密鑰文件路徑。確保在使用這些示例代碼之前,你已經(jīng)正確配置了相應(yīng)的庫和服務(wù)。
到此這篇關(guān)于python 獲取圖片中文字的四種辦法的文章就介紹到這了,更多相關(guān)python 獲取圖片文字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何實現(xiàn)SSH遠(yuǎn)程連接與文件傳輸
這篇文章主要介紹了Python如何實現(xiàn)SSH遠(yuǎn)程連接與文件傳輸問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05Python+OpenCV實現(xiàn)相機標(biāo)定的方法詳解
opencv中內(nèi)置了張正友的棋盤格標(biāo)定法,通過一些姿態(tài)各異的棋盤格圖像,可以標(biāo)定相機的內(nèi)外參數(shù),本文為大家介紹OpenCV進(jìn)行相機標(biāo)定的具體方法,希望對大家有所幫助2023-05-05python numpy.ndarray中如何將數(shù)據(jù)轉(zhuǎn)為int型
這篇文章主要介紹了python numpy.ndarray中如何將數(shù)據(jù)轉(zhuǎn)為int型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05