File size: 6,350 Bytes
87337b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
#
# This file is part of TEN Framework, an open source project.
# Licensed under the Apache License, Version 2.0.
# See the LICENSE file for more information.
#
import json
from ten import (
AudioFrame,
VideoFrame,
AsyncTenEnv,
Cmd,
Data,
)
from PIL import Image
from io import BytesIO
from base64 import b64encode
from ten_ai_base.const import CMD_CHAT_COMPLETION_CALL
from ten_ai_base.types import (
LLMChatCompletionUserMessageParam,
LLMToolMetadata,
LLMToolMetadataParameter,
LLMToolResult,
LLMToolResultLLMResult,
)
from ten_ai_base.llm_tool import AsyncLLMToolBaseExtension
def rgb2base64jpeg(rgb_data, width, height):
# Convert the RGB image to a PIL Image
pil_image = Image.frombytes("RGBA", (width, height), bytes(rgb_data))
pil_image = pil_image.convert("RGB")
# Resize the image while maintaining its aspect ratio
pil_image = resize_image_keep_aspect(pil_image, 512)
# Save the image to a BytesIO object in JPEG format
buffered = BytesIO()
pil_image.save(buffered, format="JPEG")
# pil_image.save("test.jpg", format="JPEG")
# Get the byte data of the JPEG image
jpeg_image_data = buffered.getvalue()
# Convert the JPEG byte data to a Base64 encoded string
base64_encoded_image = b64encode(jpeg_image_data).decode("utf-8")
# Create the data URL
mime_type = "image/jpeg"
base64_url = f"data:{mime_type};base64,{base64_encoded_image}"
return base64_url
def resize_image_keep_aspect(image, max_size=512):
"""
Resize an image while maintaining its aspect ratio, ensuring the larger dimension is max_size.
If both dimensions are smaller than max_size, the image is not resized.
:param image: A PIL Image object
:param max_size: The maximum size for the larger dimension (width or height)
:return: A PIL Image object (resized or original)
"""
# Get current width and height
width, height = image.size
# If both dimensions are already smaller than max_size, return the original image
if width <= max_size and height <= max_size:
return image
# Calculate the aspect ratio
aspect_ratio = width / height
# Determine the new dimensions
if width > height:
new_width = max_size
new_height = int(max_size / aspect_ratio)
else:
new_height = max_size
new_width = int(max_size * aspect_ratio)
# Resize the image with the new dimensions
resized_image = image.resize((new_width, new_height))
return resized_image
class VisionAnalyzeToolExtension(AsyncLLMToolBaseExtension):
image_data = None
image_width = 0
image_height = 0
async def on_init(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_init")
async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_start")
await super().on_start(ten_env)
async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")
# TODO: clean up resources
async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_deinit")
async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
cmd_name = cmd.get_name()
ten_env.log_debug("on_cmd name {}".format(cmd_name))
await super().on_cmd(ten_env, cmd)
async def on_data(self, ten_env: AsyncTenEnv, data: Data) -> None:
data_name = data.get_name()
ten_env.log_debug("on_data name {}".format(data_name))
async def on_audio_frame(
self, ten_env: AsyncTenEnv, audio_frame: AudioFrame
) -> None:
audio_frame_name = audio_frame.get_name()
ten_env.log_debug("on_audio_frame name {}".format(audio_frame_name))
async def on_video_frame(
self, ten_env: AsyncTenEnv, video_frame: VideoFrame
) -> None:
video_frame_name = video_frame.get_name()
ten_env.log_debug("on_video_frame name {}".format(video_frame_name))
self.image_data = video_frame.get_buf()
self.image_width = video_frame.get_width()
self.image_height = video_frame.get_height()
def get_tool_metadata(self, ten_env: AsyncTenEnv) -> list[LLMToolMetadata]:
return [
LLMToolMetadata(
name="get_vision_chat_completion",
description="Get the image analyze result from camera. Call this whenever you need to understand the input camera image like you have vision capability, for example when user asks 'What can you see in my camera?' or 'Can you see me?'",
parameters=[
LLMToolMetadataParameter(
name="query",
type="string",
description="The vision completion query.",
required=True,
),
],
),
]
async def run_tool(
self, ten_env: AsyncTenEnv, name: str, args: dict
) -> LLMToolResult | None:
if name == "get_vision_chat_completion":
if self.image_data is None:
raise ValueError("No image data available")
if "query" not in args:
raise ValueError("Failed to get property")
query = args["query"]
base64_image = rgb2base64jpeg(
self.image_data, self.image_width, self.image_height
)
# return LLMToolResult(message=LLMCompletionArgsMessage(role="user", content=[result]))
cmd: Cmd = Cmd.create(CMD_CHAT_COMPLETION_CALL)
message: LLMChatCompletionUserMessageParam = (
LLMChatCompletionUserMessageParam(
role="user",
content=[
{"type": "text", "text": query},
{"type": "image_url", "image_url": {"url": base64_image}},
],
)
)
cmd.set_property_from_json("arguments", json.dumps({"messages": [message]}))
ten_env.log_info("send_cmd {}".format(message))
[cmd_result, _] = await ten_env.send_cmd(cmd)
result = cmd_result.get_property_to_json("response")
return LLMToolResultLLMResult(
type="llmresult",
content=json.dumps(result),
)
|