亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python?獲取圖片中文字的四種辦法

 更新時(shí)間:2023年11月13日 11:32:34   作者:憂傷的玩不起  
本文主要介紹了python?獲取圖片中文字的幾種辦法,主要使用光學(xué)字符識(shí)別(OCR)技術(shù),本文主要介紹了4種第三方庫,具有一定的參考價(jià)值,感興趣的可以了解一下

在Python中,獲取圖片中的中文文本通常需要使用光學(xué)字符識(shí)別(OCR)技術(shù).

1.使用http請(qǐng)求庫獲取,分別主流有2種以下庫

  • 使用百度OCR API:百度提供了OCR API服務(wù),可以通過API調(diào)用來識(shí)別圖片中的文本,包括中文。你需要注冊(cè)百度開發(fā)者賬號(hào),獲取API密鑰,然后使用Python中的HTTP請(qǐng)求庫發(fā)送圖片并接收識(shí)別結(jié)果
  • 使用微軟Azure OCR服務(wù):微軟Azure也提供了OCR服務(wù),可以用來提取中文文本。與百度API類似,你需要注冊(cè)Azure賬號(hào),創(chuàng)建一個(gè)OCR服務(wù),然后使用Python中的HTTP請(qǐng)求庫發(fā)送請(qǐng)求并獲取結(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)建圖像對(duì)象
image = vision.Image(content=content)

# 使用Vision API進(jìn)行文本提取
response = client.text_detection(image=image)

# 輸出提取的中文文本
for text in response.text_annotations:
    print(text.description)

請(qǐng)注意,對(duì)于Google Cloud Vision API,你需要替換 'your-service-account-key.json' 為你自己的服務(wù)賬戶密鑰文件路徑。確保在使用這些示例代碼之前,你已經(jīng)正確配置了相應(yīng)的庫和服務(wù)。

到此這篇關(guān)于python 獲取圖片中文字的四種辦法的文章就介紹到這了,更多相關(guān)python 獲取圖片文字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論