1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GPT-4Vで画像を解析する

Posted at

はじめに

GPT-4Vは、テキストだけでなく画像も理解できる革新的なAIモデルです。

ここでは、初心者でも理解しやすいように、GPT-4Vを使って画像解析を行う方法を紹介します。

必要なもの

  • OpenAIのAPIキー: GPT-4を使用するためには、OpenAIのAPIキーが必要です。
  • Python: このコードはPythonで書かれています。

コードの説明

GPT-4Vを使って画像に関する質問に答えるためのPythonコードは以下の通りです。

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What’s in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "画像のURL",
                    },
                },
            ],
        }
    ],
    max_tokens=300,
)

print(response.choices[0])

処理詳細

  1. ライブラリのインポート: from openai import OpenAIでOpenAIライブラリをインポートします。
  2. クライアントの設定: client = OpenAI()でOpenAIのクライアントを設定します。
  3. リクエストの作成: chat.completions.createメソッドを使って、質問と画像URLを含むリクエストを作成します。
  4. 画像URLの指定: "url": "画像のURL"の部分に、解析したい画像のURLを指定します。
  5. 応答の出力: print(response.choices[0])でモデルからの応答を出力します。
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?