Google vision API : Label detection 파이썬으로 사용한 것 기록
Google cloud platform 가입 후 진행
새 프로젝트 만든 후 Cloud vision 검색
사용자 인증 정보 만든 후
>>> 파이썬 실행 (Pycharm 사용)
파이참의 경우 Preferences > Python Interpreter에 들어가서 googel-cloud-vision 설치하기
from google.cloud.vision import types는 ImportError: cannot import name 'types' from 'google.cloud.vision'가 발생하는데, 이 경우 그냥 from google.cloud.vision_v1 import types를 하면 됐다
from __future__ import print_function
from google.cloud import vision
from google.cloud.vision_v1 import types
import os
import io
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'my-proj-313508-434c967488d2.json'
client = vision.ImageAnnotatorClient()
filenames = os.path.join(os.path.dirname(__file__), 'pizza.jpg')
with io.open(filenames, 'rb') as image_file:
content = image_file.read()
image = types.Image()
image = vision.Image(content=content)
response = client.label_detection(image=image)
for label in response.label_annotations:
print("Label: ",label.description, "/ score: ", label.score)
os.environ['GOOGLE_APPLICATION_CREDENTIALS']를 통해서 앞에서 json파일로 다운받은 구글 API키를 환경변수에 저장
pizza.jpg 실행결과(description / score 프린트)