728x90

Microsoft Azure Cognitive Services의 Computer Vision을 이용하여 이미지 분석(Image Analysis) 하기

[Microsoft Azure Cognitive Services] Computer Vision

  • Microsoft Azure Cognitive Services의 서비스 중 하나로, 컴퓨터 비전(Computer Vision) 관련 작업을 수행할 수 있다.
 

Computer Vision | Microsoft Azure

Azure Cognitive Service인 Computer Vision으로 이미지에서 풍부한 정보를 추출하고 콘텐츠를 분석합니다.

azure.microsoft.com

 

Azure Cognitive Services 란?

  • AI를 활용하여 다양한 기능을 구현할 때, Microsoft에 의해 미리 구현된 여러가지 기능들을 API를 이용하여 불러올 수 있는 서비스이다.
 

Cognitive Services - AI 솔루션용 API | Microsoft Azure

Azure Cognitive Services는 기계 학습 전문 지식이 필요하지 않은 API를 통해 개발자에게 AI를 제공합니다. 30일 만에 AI 솔루션을 빌드하는 방법을 알아보세요.

azure.microsoft.com

 

방법

① Microsoft Azure에서 키 값과 엔드포인트 값 긁어오기

키 값(키 1과 키 2중에서 선택)과 엔드포인트를 복사한다.

 

② 코드 작성하기

  • ✅ 표시된 부분에 자신의 ①분석할 이미지, ②키 값, ③엔드포인트 값을 넣어준다.
import requests
from io import BytesIO
from PIL import Image

# 분석할 이미지를 불러온다.
image_url = '_______________'                                    # ✅ 분석할 이미지의 주소를 넣는다.
image = Image.open(BytesIO(requests.get(image_url).content))
image                                                            # 불러온 이미지를 출력한다.

# 필요한 값들을 준비한다.
key = '________________'                                         # ✅ Azure의 키 값을 넣는다.
endpoint = '______________'                                      # ✅ Azure의 엔드포인트 값을 넣는다.

# 컴퓨터 비전 2.0 버전을 사용할 것이기 때문에 endpoint 뒤에 붙여준다.
endpoint += 'vision/v2.0/'

# endpoint로 이미지 분석과, 이미지 감지 작업을 할 수 있다.
analyze_endpoint = endpoint + 'analyze'                          # 분석을 위해 끝에 analyze 붙인다.

# 웹 통신할 때는 header, parameter, data 3가지가 필요하다.
header = { 'Ocp-Apim-Subscription-Key' : key }
params = { 'visualFeatures' : 'Categories,Description,Color' }
data = { 'url' : image_url }

# 외부로 요청하기 (클라우드에서 분석 후, 분석값을 가져온다.)
response = requests.post(analyze_endpoint,
                         headers=header,
                         params=params,
                         json=data)

# 분석 결과 확인하기
result = response.json()
result                                                            # JSON으로 결과를 출력시킨다.

 

실행 결과

키 값과 엔드포인트 값은 모자이크 처리하였다.
이미지 분석 결과가 JSON 형태로 출력된다.

728x90