2
0

gptのvisionモデルを使って生成AI画像を分析して分析結果を使用してDALL·E -3で再度画像を生成してみた

Posted at

はじめに

記事の目的や背景を説明

生成AIの活用方法を模索する上でOPENAIが提供しているtext-to-speech, speech-to-text,image-genration,visionなどいろいろありますが自社のなかでAIの活用方法が明確化されていないので実際に触ってみてどのようなことができるのか試してみようと思っての試みです。

対象読者

OPENAIのAPIをPythonを使用して実行しますが基本的にPyhton自体には触れないのでVisionとImage-generatorのほんとに基礎の実装方法を知りたい方向けになるかと思います。

目次

  1. OPENAIのVisionとは
  2. OPENAIのImage-Generationとは
  3. Visionを使用して画像の解析をしてみる
  4. 解析された情報を元にプロンプトを作成して画像を再度生成してみる

1. OPENAIのVisionとは

VisionとはAIの目のようなものです。入力された画像データを元にどういった画像なのかを識別することができます。現在Visionを使用するにはGPT-4V or gpt-4-vision-previewのどちらかのモデルを使用することになりますがOPENAIの有料会員になる必要があります。

2. OPENAIのImage-Generationとは

・Image-generationはDALL·E を用いて画像の操作をすることができます。
・テキストプロンプトに基づいてゼロから画像を作成する(DALL·E 3およびDALL·E 2)
・既存の画像の一部を新しいテキストプロンプトに基づいて置き換え、画像の編集版を作成する(DALL·E 2のみ)
・既存の画像のバリエーションを作成する(DALL·E 2のみ)

3.Visionを使用して画像の解析をしてみる

今回は事前に解析する画像をAIで作成して準備しています。
スクリーンショット 2023-12-06 23.17.57.png

必要なライブラリのインストール

pip install openai
pip install python-dotenv

API_KEYの設定を行います

# OpenAI API Key
openai.api_key = os.getenv("OPENAI_API_KEY")

画像ファイルをbase64形式に変換します。

def encode_image(image_path):
 with open(image_path, "rb") as image_file:
   return base64.b64encode(image_file.read()).decode('utf-8')

vision modelを使用して画像の解析を行います。
promptは”show me the details of image”を入れています。
detailのところをhighにすることによって時間はさらにかかりますがより高いクオリティの画像解析を行っています。

headers = {
 "Content-Type": "application/json",
 "Authorization": f"Bearer {openai.api_key}"
}


payload = {
 "model": "gpt-4-vision-preview",
 "messages": [
   {
     "role": "user",
     "content": [
       {
         "type": "text",
         "text": "show me the details of the image"
       },
       {
         "type": "image_url",
         "image_url": {
           "url": f"data:image/jpeg;base64,{base64_image}",
           "detail": "high"
         },
       }
     ]
   }
 ],
 "max_tokens": 300
}


response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
result = response.json()

こちらの出力結果は以下のようになりました。

1. She has medium-length brown hair styled in a bob cut.
2. Her facial features include finely shaped eyebrows, almond-shaped eyes with dark pupils, a straight nose, and full lips.
3. She is wearing subtle makeup, with what looks like a touch of blush on her cheeks and possibly a natural-toned lipstick.
4. Her eyes have a gentle gaze and are looking directly at the viewer, giving her a poised and attentive appearance.
5. She is dressed in professional attire, wearing a gray suit with a modest neckline.
6. The background is blurred, providing a neutral backdrop that brings the focus to her.
7. The lighting is soft and diffused, hinting at an indoor setting with natural light, contributing to the overall polished and serene impression of the subject.

解析された情報を元にプロンプトを作成して画像を再度生成してみる
こちらも先ほどと同じように環境変数などを読み込んで初期設定を行います。
Dalleを使用したコード例になります。
先ほどの画像の解析データをプロンプトとして使用する想定です。

def generate_image(input):
   input = "以下の情報を元に画像を生成してください" + input
   response = client.images.generate(
   model="dall-e-3",
   prompt=input,
   size="1024x1024",
   quality="standard",
   n=1,
   )


   image_url = response.data[0].url
   print(image_url)

先ほどの画像解析をしたコードの最終行に上記を追加してください。

response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
result = response.json()
image_generator.generate_image(result['choices'][0]['message']['content'])

実行すると生成された画像のURLを取得することができます。
生成された画像はこちら
スクリーンショット 2024-01-23 10.11.41.png

結論
特徴はほぼほぼ捉えた上で画像生成できたのかなと思います。
今回はプロンプトを大分荒い形で実行したのでもっと精査するとより精度も高まるかなといった印象でした。
参考資料
https://platform.openai.com/docs/guides/vision

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0