728x90

안녕하세요, 오늘은 Gemini API를 이용해서 이미지를 AutoLabling 하는 방법에 대해 소개해드리려고 합니다. 이미지와 예시를 제공하면, 그에 따른 이미지에 대한 설명을 제공하는데 이를 바탕으로 StableDiffusion Model을 Fine-tunning 할 수 있습니다.  순서는 다음과 같습니다.

  1. Gemini API Key 발급받기
  2. 이미지 입력 예시 작성하기
  3. 코드 작성하기
  4. 결과 확인하기

 

Gemini API Key 발급받기


여기를 누르고 프로젝트 설정을 하면, 다음과 같이 화면이 나오게 됩니다. Create API KEY 버튼을 누르고, Create API Key in exsting project 버튼을 누르면 API 키가 발급됩니다.

 

 

다음과 같이 API Key가 생성됩니다. Copy 버튼을 누르거나, 메모장에 미리 저장합니다.

 

이미지 입력 예시 작성하기


Stable Diffusion 예시등을 예시로 제공해 주면서, AutoLabeling의 Example을 제공해 줍니다. 이번에 저는 Dreambooth method를 사용하여 사자를 잘 생성하고 싶어서 다음과 같은 예시를 제공했습니다. Lion을 TOK라고 바꾸어서 AutoLabeling을 진행하려고 합니다.  그래서 Input Text는 다음과 같이 설정했습니다.

INPUT_TEXT = '''
I want to train a text-to-image Stable Diffusion model with dreambooth method. Please generate an appropriate text prompt for this image. This cartoon character's names is "TOK". You don't have to describe the character's appearance. Instead, Focus on the image style, character's action, clothing, other objects, and background.
[Examples]
* Simplified illustration of TOK cartoon character, one TOK, wearing a blue business suit with red tie and gold INU pin, holding a brown briefcase, on a black background with blue waterdrop shapes and gold stars.
* 3D Image of TOK cartoon character, one TOK, wielding a spatula with wooden handle in a light blue chef's jacket with darker blue trim and matching tall chef's hat, dark navy blue background.

'''

영어로 작성하는 것이 한국어로 작성하는 것보다 성능이 좋게 나오므로 영어로 설정해주었습니다.

 

코드 작성하기


Gemini API를 사용하기 위해서 SDK를 설치합니다.

pip install -q -U google-generativeai

이미지를 입력으로 넣기 위해 pillow(PIL)을 설치합니다.

pip install pillow

 

그리고 다음과 같이 image와 text를 입력하면 출력 결과가 나옵니다.

import google.generativeai as genai
import PIL.Image, local

## gemini
text = "describe this image"
api_key = "api_key"

genai.configure(api_key=api_key)
file_path = f'C:\\User\\1.png'
img = PIL.Image.open(file_path)
model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content([text,img])

print(response.text)

다음과 같이 발급받은 api_key와 file_path를 입력하고 실행하면 됩니다.

 

 

결과 확인하기


다음은 코드를 실행하고 나온 출력결과입니다. 맨 밑에 출력결과가 나온 것을 확인할 수 있습니다.

이와 같이 Gemini API를 이용해서 image를 AutoLabeling을 할 수 있습니다. 

728x90