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

