LoginSignup
1
3

画像認識と画像生成が同時に学べる記事【OpenAI API】

Last updated at Posted at 2023-12-07

はじめに

この記事は、下記のOpenAI APIについての記事の発展的な内容を取り扱います。

本日のお題は、「画像認識により(架空の)ポケモンの特徴を分析⇒対になる伝説のポケモンを生成」です。

前提として、OpenAI APIの使い方講座【公式Quickstart】のStep2まで完了している必要があります。

架空のポケモンの画像は、DALL·E APIで実在しないポケモンを創るで生成したライトニュメラを用います。

image.png

本日のお題:画像認識により(架空の)ポケモンの特徴を分析⇒対になる伝説のポケモンを生成

出来上がったソースコード

image_recognition_generation.py
from image_recognition import image_recognition
from image_generation import image_generation

def recognize_and_generate_image(image_url, recognition_prompt, model_name):
    # 画像認識を行い、結果を取得
    recognition_result = image_recognition.recognize_image(image_url, recognition_prompt)
    print(f"Recognition result: {recognition_result.message.content}")

    # 生成する画像のプロンプトを設定
    generation_prompt = recognition_result.message.content

    # 画像を生成
    generated_image_response = image_generation.generate_image(model_name, generation_prompt)
    print(generated_image_response)

    return generated_image_response

if __name__ == "__main__":
    # 画像認識と画像生成のパラメータ設定
    image_url = "https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2627015/ee531ce6-6e85-4cbd-66e4-4fcb5870af4c.png"
    recognition_prompt = "ポケモンの特徴をなるべく詳しく分析し、その対となるポケモンを生成するプロンプトを教えてください"
    model_name = "dall-e-3"

    # 画像認識と画像生成の処理を実行
    response = recognize_and_generate_image(image_url, recognition_prompt, model_name)
    print(response)

柿ピーで学ぶGPT-4V(OpenAI Vison API)」のimage_recognition.pyと「DALL·E APIで実在しないポケモンを創る」のimage_generation.pyをimportし、実装しています。
前半のGPT-4V(OpenAI Vison API)を用いた画像認識関数で(架空の)ポケモンの特徴を分析し、後半のDALL·E APIを用いた画像生成関数に引数として渡し、対となるポケモンを生成しています。

実行結果(生成されたテキスト)

Recognition result: 画像に表示されているのは、光元素がテーマの神話級の獣、ライト・ヌムインナスと呼ばれる架空の生物です。この生物は「ポケモン」とは異なり、オリジナルのコンセプトアートやファンアートに近いスタイルになっていますが、ポケ
モンに関連する特徴を分析して、その特徴に基づくポケモンのプロンプトを作成することが求められています。
この神話級獣の特徴は以下の通りです:

  • 羽根のある象徴的な飛行生物
  • 白と青の光輝くオーラや模様
  • エレガントで流れるような形状
  • 光や星に関連した装飾要素

これらの特徴を反映させた対となるポケモンを想像するために、次のようなプロンプトが考えられます

'Imagine a mythical beast called the Light Numuinnas. It is not a Pokemon creature but has many characteristics that are similar to creatures in the Pokemon series. The Light Numuinnas is a symbol of light elements. It is an iconic flying creature with generalized wings. Its body is infused with a glowing aura and patterns consisting of white and blue colors that give it an ethereal appearance. It possesses an elegant and flowing form, maintaining a sublime balance of beauty and power. Further, it is adorned with decorative elements related to light and stars. Keep these characteristics in mind as you visualize a corresponding Pokemon creature.'

実行結果(生成された画像)

image.png

カッコいい、、!
たとえ架空のポケモンであっても特徴を分析し、対となるポケモンが生成できていますね。

おわりに

この記事では、OpenAIのAPIを活用した画像認識と画像生成の応用について学びました。題材として、架空のポケモン「ライトニュメラ」の特徴を分析し、それに対応する伝説のポケモンを生成するという課題に取り組みました。

カレンダーの購読や、QiitaとTwitterをフォローしてくださると励みになります。

ソースコード

本記事で使用したソースコードは、下記のGitHubレポジトリに格納しています。

1
3
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
3