2017-12-20 1 views
1

Google Vision API의 가장 기본적인 텍스트 감지 및 OCR (광학 문자 인식) 프로그램을 Python으로 실행하려고합니다. 내 소스 코드는이 API에 대한 Google 클라우드 튜토리얼에서 가져온 것입니다 그리고 그것은 다음과 같다 :Python 프로그램에서 Google Vision API를 사용하는 방법은 무엇입니까?

나는 다음과 같은 오류가 발생하지만
import io 
from google.cloud import vision 
from google.cloud.vision import types 

def detect_text(file): 
    """Detects text in the file.""" 
    client = vision.ImageAnnotatorClient() 

    with io.open(file, 'rb') as image_file: 
     content = image_file.read() 

    image = types.Image(content=content) 

    response = client.text_detection(image=image) 
    texts = response.text_annotations 
    print('Texts:') 

    for text in texts: 
     print('\n"{}"'.format(text.description)) 

     vertices = (['({},{})'.format(vertex.x, vertex.y) 
        for vertex in text.bounding_poly.vertices]) 

     print('bounds: {}'.format(','.join(vertices))) 

file_name = "prescription.jpg" 
detect_text(file_name) 

:

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or 
explicitly create credential and re-run the application. For more 
information, please see 
https://developers.google.com/accounts/docs/application-default-credentials. 

이 이상한 이유는

1) 새로운 서비스 계정을 만들었습니다.

2) .bash_profile에 export GOOGLE_APPLICATION_CREDENTIALS="/Users/User/PycharmProjects/GCP-OCR/Trial-e046e4bc6ce1.json"을 추가했습니다. (이 p의 Pycharm 파일에 json 파일을 넣었습니다. 방금 한 가지 이상한 일은 json 파일의 개인 키가 약 20 줄인 반면 아마도 약 1 줄이 될 것이라고 예상하는 것입니다.

이 버그를 해결하고 프로그램을 실행하려면 어떻게해야합니까? 나는 단순히 내 소스 코드에

import os 
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/GCP-OCR/Trial-e046e4bc6ce1.json" 

를 추가하면 문제가 해결되는 방식으로

.

답변

1

파이썬 코드에서 변수를 설정할 때 작동하면 자격 증명이 유효합니다. 나는 그것을 직접 테스트했고 당신의 초기 접근 방식을 사용하여 작동한다. 당신의 .bash_profile을 업데이트하는 것을 잊지 마십시오

echo $GOOGLE_APPLICATION_CREDENTIALS 

하는해야 출력 올바른 경로 : How to reload .bash_profile from the command line?

에 설명 된대로

source ~/.bash_profile 

환경 변수가 올바르게 설정되어 있는지 확인 귀하의 자격 증명

+0

감사합니다. 예, 확실히 .bash_profile을 업데이트했지만 'echo'로 체크 아웃하지 않았습니다. 가까운 시일 내에 다시이 문제를 해결할 수 있다면이 사소한 기술적 인 문제를 해결할 수있을 것입니다. – Universalis

관련 문제