본문 바로가기

Development/OCR

Google Cloud Vision API - OCR 사용하기 (2)

728x90
반응형

출처:  https://ko.wikipedia.org/wiki/광학_문자_인식

 

이전 포스트에 이어서 다음 단계를 진행한다.

 

이전 포스트는 아래 링크를 참고한다.

 

2021.04.07 - [Development/Machine Learning] - Google Cloud Vision API - OCR 사용하기 (1)

 

Google Cloud Vision API - OCR 사용하기 (1)

먼저 Google의 서비스를 이용해야 하므로 로그인부터 하고 진행한다. 1. Google Cloud 시작하기 먼저 다음 링크에서 Google Cloud에 가입하는 것부터 시작하자. cloud.google.com/gcp/ Google Cloud 컴퓨팅, 호스..

davelogs.tistory.com


3. 사용자 환경에서 서비스 계정 키 파일 사용

 


4. Vision API 클라이언트 라이브러리 설치

Python으로 진행할 예정이라 다음과 같이 클라이언트 라이브러리를 설치한다.

 

 

728x90

 


5. 클라이언트 라이브러리 사용

다음 링크에서 제공하는 예제코드를 사용해 보자.

cloud.google.com/vision/docs/libraries?hl=ko

 

 

아래는 실제로 위 예제를 수정해 직접 구현해 본 것으로, 키 정보 위치를 실제 다운로드 받은 사용자 계정 키의 위치를 입력한다.

더불어, Python 환경의 아래코드에서처럼 환경변수를 직접 설정할 수 있다면, 위의 환경변수 설정을 따로 하지 않아도 된다.

 

import io
import os

# Set environment variable
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "다운로드받은사용자계정키.json"

# Imports the Google Cloud client library
from google.cloud import vision

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.abspath('resources/picture1.png')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = vision.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)
    
# Performs text detection on the image file
response = client.text_detection(image=image)
texts = response.text_annotations

print('Texts:')
for text in texts:
    print(text.description)

 

 

위 예시 코드는 아래 경로의 Jupyter Notebook으로 작성된 페이지에서 결과를 직접 확인할 수 있다.

 

github.com/DHDaveB/TIL-Getting-Started-With-OCR/blob/main/google-cloud-vision-api/sample.ipynb

 

DHDaveB/TIL-Getting-Started-With-OCR

Contribute to DHDaveB/TIL-Getting-Started-With-OCR development by creating an account on GitHub.

github.com

 


 

아래 포스트는 네이버 CLOVA AI의 OCR 기술을 사용하는 방법이니 참고하기 바란다.

 

[Development/OCR] - Naver CLOVA AI - OCR 사용하기 (1) - NCP 가입하기

 

Naver CLOVA AI - OCR 사용하기 (1) - NCP 가입하기

이번에는 CLOVA OCR을 사용해 보고자 한다. Google Cloud Vision API를 이용한 OCR은 아래 이전 포스팅을 참고하기 바란다. 2021.04.07 - [Development/Machine Learning] - Google Cloud Vision API - OCR 사용..

davelogs.tistory.com

 

 

728x90
반응형