Re: google drive api for python を使用した ocr - end0tknr's kipple - web写経開発
以前、上記entryにて、 google drive api for python を使用した ocr を行いましたが、 今回は、Google Cloud Vision API による OCR 。
google.cloud.vision が殆どのことを処理してくれますので、 python scriptは以下のように、小規模で済みます
参考url
- Vision AI | Cloud Vision API | Google Cloud
- Vision クライアント ライブラリ | Cloud Vision API | Google Cloud
- ローカル ファイル内のテキストを検出する | Cloud Vision API | Google Cloud
pip install
DOS> .\python.exe -m pip install google-cloud-vision
python script
#!python # -*- coding: utf-8 -*- import sys from google.cloud import vision from google.oauth2 import service_account from PIL import Image def main(): text_detection() def text_detection(): tiff_file_path = sys.argv[1] img = Image.open(tiff_file_path) img_file_path = tiff_file_path img_file_path = "tmp.png" img.save( img_file_path ) credentials = service_account.Credentials.from_service_account_file( 'brave-airship-ないしょ.json' ) client = vision.ImageAnnotatorClient(credentials=credentials) with open(img_file_path ,'rb') as f: content = f.read() image = vision.Image(content=content) response = client.text_detection(image=image) #response = client.label_detection(image=image) #response = client.document_text_detection(image=image) if response.error.message: print( response ) return for text in response.text_annotations: disp_str = text.description.encode('cp932',"ignore").decode('cp932') print( disp_str ) if __name__ == '__main__': main()