メインコンテンツまでスキップ

プロンプトで判定ロジックを変えられる動画像認識エンジン

Version: 1
  • GenerativeAI
Download Canvas JSON
logo

ユーザーが指定したプロンプトに従って、動画内のオブジェクトを認識し、特定の条件が満たされた場合においてコールバック関数によるトリガーを発出します。

概要

想定ユーザー

  • プロンプトを書き換えるだけで、任意の動画解析を行いたいStudio利用者
  • 解析結果に応じてアクションのトリガーを実現したいStudio利用者

テンプレート動作

  • Input

    • 解析したい動画ファイル
      • サンプル
    • 環境変数
      • video-frame-parser

        環境変数内容備考
        FILENAME入力ビデオファイルのパス
        OUTPUT出力イメージtarファイルのパス
        WIDTH出力フレームイメージの幅
        HEIGHT出力フレームイメージの高さ
      • agentization-llm-function

        環境変数内容備考
        PATH_INSTRUCTIONS入力命令ファイルのパス
        PATH_IMAGES入力画像ファイルまたはそれらを含むtarファイルのパス
        PATH_OUTPUT出力ファイルのパス
        MODEすべてのフレームを一度に分析(multi)または1つずつ分析(single)Singleモードでは動画から取り出されたフレーム1つに対して1つの回答を返しますが、Multiモードでは複数のフレームに対して1つの回答を返します。これによりアップロードされた動画から取り出される複数のフレームを入力として回答を返すことができます。
        SUBSAMPLEnフレームごとに1フレームを取得
        AI使用するLLMプロバイダ:openaiまたはanthropicOpenAIのGPT4-visionとAnthropicのClaude3 Opusが利用できます。
        MODEL_ANALYZEOpenAIによる画像検出に使用するモデルopenai選択時にのみ使用
        MAX_TOKENS_ANALYZEOpenAIによる画像検出に使用する最大トークン数openai選択時にのみ使用
        MODEL_INTERPRETOpenAIの分析と関数呼び出しに使用されるモデルopenai選択時にのみ使用
        MAX_TOKENS_INTERPRETOpenAIの画像分析に使用する最大トークン数openai選択時にのみ使用
        MODELAnthropicで使用されるモデルanthropic選択時にのみ使用
        MAX_TOKENSAnthropicで使用する最大トークン数anthropic選択時にのみ使用
        OPENAI_API_KEYOpenAI APIキーopenai, anthropicのうち片方のみ使用の場合は、シークレット登録していない方のキーを環境変数から削除すること
        ANTHROPIC_API_KEYAnthropic APIキーopenai, anthropicのうち片方のみ使用の場合は、シークレット登録していない方のキーを環境変数から削除すること
  • Output

    • 動画解析の結果
      • JSON形式

動作の流れ

  1. Endpoint /video に動画ファイルが送られると、Entity video に格納されます。
  2. Triggerが発動し、Pipelineが実行されます。
    • TriggerのスクリプトはEntity video に変更があった場合にPipelineを実行するように設定されています。
  3. Pipelineからの出力は、Entity output に解析結果が格納されます。接続されているEndpoint /outputより解析結果をダウンロードすることができます。

Pipelineは2つのFunctionから構成されています。

  • video-frame-parser
    • 動画ファイルをフレームに分割し、そのフレームを圧縮ファイル(frames.tar)として出力します。これにより、動画の各フレームが次のFunctionで分析可能な形式に変換されます。
  • agentization-llm-function
    • 先に生成されたフレームセット(frames.tar)を入力として受け取り、指定されたプロンプトに基づいてフレームの内容を分析します。プロンプトは、例えば「画像内のすべてのオブジェクトを列挙する」といった内容で、特定の条件(例:画像にリンゴが含まれているかどうか)に基づいてコールバック関数からをトリガーを発動するように設定されています。分析結果は接続されているEntityに保存されます。

利用方法

単体での動作確認

デプロイ方法

  1. 下記のシークレットをCanvasに設定します。

    • OPENAI_API_KEY
    • ANTHROPIC_API_KEY
    注記

    片方だけ使う場合は、シークレットとして登録していない環境変数をFunction agentization-llm-funcitonから削除してください(現行仕様では、存在していないシークレットが環境変数に設定されていると、デプロイがうまくいかない場合があります)

  2. デプロイボタンを押します。

実行方法

  1. Endpoint /videoにローカルディレクトリから動画ファイルをアップロードします。

    下記はTerminalからの実行例です。<env-name><namespace-id><動画のファイルパス>はご自身のものに書き換えてください。

    curl -X POST https://studio.<env-name>.studio.exabase.ai/<namespace-id>/ws/video/file \
    -H "Content-Type: multipart/form-data" \
    -F "file=@<動画のファイルパス>"
  2. Pipelineによる処理が完了したら、Endpoint /outputから解析結果を取得します。

    下記はTerminalからの実行例です。<env-name><namespace-id>、はご自身のものに書き換えてください。

    curl -X GET https://studio.<env-name>.studio.exabase.ai/<namespace-id>/ws/upload/data -H "Content-Type: application/json"

    下記は出力例です。今回の例では、画像にリンゴが含まれているかどうかに基づいてコールバック関数からをトリガーを発動するように設定されています。リンゴが映っている画像に対してはTrueを返し、リンゴが映らなくなるとFalseを返すようになります。

    {
    "data": {
    "ai": "openai",
    "mode": "single",
    "subsample": 50,
    "frames": [
    "000.jpg",
    "050.jpg",
    "100.jpg",
    "150.jpg",
    "200.jpg",
    "250.jpg",
    "300.jpg",
    "350.jpg"
    ],
    "prompt": [
    "Enumerate all the visible objects on the image.",
    "Set the trigger state value to true, using the callback function, if there is an apple on the image description."
    ],
    "responses": [
    {
    "analysis": "In the image, I see:\n\n1. Two apples with a predominantly red and yellow color, displaying some natural blemishes.\n2. A wooden surface, likely a table or countertop, with a visible wood grain pattern.\n3. Shadows cast on the wooden surface, indicating a light source on the upper left side, outside of the frame.\n\nThat covers all the visible objects in the image.",
    "message": null,
    "functions": [
    {
    "trigger": true
    }
    ]
    },
    {
    "analysis": "In the image, there are two apples on a wooden surface. The wood appears to have a fine grain, typical of a wooden table or countertop. The lighting casts a soft shadow for each apple on the surface.",
    "message": null,
    "functions": [
    {
    "trigger": true
    }
    ]
    },
    {
    "analysis": "The image shows two apples on a wooden surface. The surface appears to be a table or countertop with light shining on it, possibly from a nearby window, as indicated by the shadows cast by the apples. The apples have a reddish hue with some yellow and green spots, indicating they might be of a variety that isn't uniformly red.",
    "message": null,
    "functions": [
    {
    "trigger": true
    }
    ]
    },
    {
    "analysis": "The image shows a red apple with some yellowish hues on a wooden surface. The surface appears smooth, with the grain of the wood running horizontally from the perspective of the photo. There are also patterns of light and shadow, likely created by sunlight, casting over the apple and wooden surface, suggesting that the photo may have been taken near a window or under a light source that creates a similar effect.",
    "message": null,
    "functions": [
    {
    "trigger": true
    }
    ]
    },
    {
    "analysis": "In the image, I see a single red apple resting on a wood-grained surface where the light is creating a pattern of shadows and highlights across the surface, suggesting a light source above or to one side of the scene.",
    "message": null,
    "functions": [
    {
    "trigger": true
    }
    ]
    },
    {
    "analysis": "In the image, I can see the following objects:\n\n1. A human hand reaching for or holding an object.\n2. A red apple with part of it hidden by the hand.\n3. A flat surface with a wood grain pattern that appears to be a table or countertop.",
    "message": null,
    "functions": [
    {
    "trigger": true
    }
    ]
    },
    {
    "analysis": "The image shows a wooden surface with a visible wood grain pattern. There are no distinct objects other than the wood itself. The wood has variations in color, with lighter and darker brown tones. The grain patterns suggest it might be a floor or a table, but without additional context, it's not possible to determine its exact use or the presence of any other objects.",
    "message": null,
    "functions": [
    {
    "trigger": false
    }
    ]
    },
    {
    "analysis": "I'm sorry, but you haven't provided an image for me to analyze. Please upload an image, and I'll help you identify the visible objects.",
    "message": null,
    "functions": [
    {
    "trigger": false
    }
    ]
    }
    ]
    }
    }

Injection機能によるカスタマイズ方法

Canvas上から、Function agentization-llm-funciton/app/data/instructions.json を編集します。 このファイルには、LLMが画像を分析して関数を呼び出すためのプロンプトが書かれています。プロンプトは {"prompt": [...]} 形式である必要があります。OpenAIとAnthropicではプロンプトがわずかに異なるため、デフォルトでサンプルのプロンプトがそれぞれに対して提供されています。

/app/data/instructions.json の設定例

{
"prompt": [
"Enumerate all the visible objects on the image.",
"Set the trigger state value to true, using the callback function, if there is an apple on the image description."
],
"prompt_anthopic": "Enumerate all the visible objects on the image. Set the callback trigger state value to true if there is an apple on the image."
}

編集が完了したら、Circuitを再度デプロイし、前述と同様に動画をEndpointにアップロードしてください。

他アセットとの連携

本アセットは他アセットとの連携が可能です。以下に実装例を紹介します。

  • Function agentization-llm-funciton の後ろに別のFunctionを繋げることで、更なる機能拡張が期待できます。例えば、JSON形式の解析結果と元々の動画を入力として、トリガーの値やLLMによる画像の解説を字幕化した動画の出力する機能をもったFunctionが考えられます。前述のSideAppによる実装と合わせることも可能です。

  • SideAppを用いたWebApp(別途作成の必要があります)にFrontendとBackendで責務を分けた互いに疎結合なサービスの開発が可能です。例えば、先ほどのFunction追加と併せて以下の機能を実装することが可能です。

    • ローカルから動画ファイルをアップロードする
    • 動画をEntity videoに送信する
    • Pipelineによる解析結果をEntity outputから取得する
    • 解析結果(JSON形式)と解析結果の字幕付き動画をローカルにダウンロードする

関連情報

動画をフレームに分割してLLMで解析する with Claude3 Opus