Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
WHALEEYE committed Nov 24, 2024
1 parent ee5a803 commit bc10497
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 235 deletions.
31 changes: 10 additions & 21 deletions camel/toolkits/video_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import re
import tempfile
from pathlib import Path
from typing import List, Optional, Union
from typing import List, Optional

from PIL import Image

Expand All @@ -41,12 +41,12 @@ def _standardize_url(url: str) -> str:
return url


def _capture_screenshot(video_file: str, timestamp: int) -> Image.Image:
def _capture_screenshot(video_file: str, timestamp: float) -> Image.Image:
r"""Capture a screenshot from a video file at a specific timestamp.
Args:
video_file (str): The path to the video file.
timestamp (int): The time in seconds from which to capture the
timestamp (float): The time in seconds from which to capture the
screenshot.
Returns:
Expand Down Expand Up @@ -93,8 +93,6 @@ def __init__(
download_directory or tempfile.mkdtemp()
).resolve()

logger.info(f"self._download_directory: {self._download_directory}")

try:
self._download_directory.mkdir(parents=True, exist_ok=True)
except FileExistsError:
Expand Down Expand Up @@ -133,7 +131,7 @@ def _download_video(self, url: str) -> str:
video_template = self._download_directory / "%(title)s.%(ext)s"
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': video_template,
'outtmpl': str(video_template),
'force_generic_extractor': True,
'cookiefile': self._cookies_path,
}
Expand Down Expand Up @@ -168,16 +166,14 @@ def get_video_bytes(
return video_bytes

def get_video_screenshots(
self, video_url: str, timestamps: Union[List[int], int]
self, video_url: str, amount: int
) -> List[Image.Image]:
r"""Capture screenshots from the video at specified timestamps or by
dividing the video into equal parts if an integer is provided.
Args:
video_url (str): The URL of the video to download. (required)
timestamps (Union[List[int], int]): A list of timestamps (in
seconds) from which to capture the screenshots, or an integer
specifying the number of evenly spaced screenshots to capture.
video_url (str): The URL of the video to take screenshots.
amount (int): the amount of evenly split screenshots to capture.
Returns:
List[Image.Image]: A list of screenshots as Image.Image.
Expand All @@ -194,17 +190,10 @@ def get_video_screenshots(
except ffmpeg.Error as e:
raise RuntimeError(f"Failed to determine video length: {e.stderr}")

if isinstance(timestamps, int):
if timestamps <= 0:
raise ValueError(
"Number of screenshots must be greater than zero."
)
interval = video_length // (timestamps + 1)
tss = [round((i + 1) * interval) for i in range(timestamps)]
else:
tss = [ts for ts in timestamps if 0 <= ts <= video_length]
interval = video_length / (amount + 1)
timestamps = [i * interval for i in range(1, amount + 1)]

images = [_capture_screenshot(video_file, ts) for ts in tss]
images = [_capture_screenshot(video_file, ts) for ts in timestamps]

return images

Expand Down
67 changes: 30 additions & 37 deletions examples/vision/duckduckgo_video_object_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,59 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from typing import List

from PIL import Image

from camel.agents import ChatAgent
from camel.configs import ChatGPTConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.prompts import PromptTemplateGenerator
from camel.toolkits import SearchToolkit, VideoDownloaderToolkit
from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
from camel.types import RoleType, TaskType


def detect_image_obj(image_list) -> None:
def detect_image_obj(image_list: List[Image.Image]) -> None:
sys_msg = PromptTemplateGenerator().get_prompt_from_key(
TaskType.OBJECT_RECOGNITION, RoleType.ASSISTANT
)
print("=" * 20 + " SYS MSG " + "=" * 20)
print(sys_msg)
print("=" * 49)

assistant_sys_msg = BaseMessage.make_assistant_message(
role_name="Assistant",
content=sys_msg,
)
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
assistant_sys_msg,
model=model,
)
agent = ChatAgent(sys_msg)

user_msg = BaseMessage.make_user_message(
role_name="User",
content="Please start the object detection for the following images!",
image_list=image_list,
image_detail="high",
)

assistant_response = agent.step(user_msg)
print("=" * 20 + " RESULT " + "=" * 20)
print(assistant_response.msgs[0].content)
print("=" * 48)


# Create an instance of the SearchToolkit
search_toolkit = SearchToolkit()
def main():
# Create an instance of the SearchToolkit
search_toolkit = SearchToolkit()

# Example query for DuckDuckGo video search
query = "What big things are happening in 2024?"
# Example query for DuckDuckGo video search
query = "The future of AI in education"

# Perform a DuckDuckGo search with the query, setting source to 'videos'
results = search_toolkit.search_duckduckgo(
query=query, source="videos", max_results=5
)
# Perform a DuckDuckGo search with the query, setting source to 'videos'
results = search_toolkit.search_duckduckgo(
query=query, source="videos", max_results=5
)

# Try to download videos from the search results
for result in results:
video_url = result['embed_url']
if not video_url:
print(f"No valid video URL provided for result: {result}")
continue
# Try to download videos from the search results
for result in results:
video_url = result['embed_url']
if not video_url:
print(f"No valid video URL provided for result: {result}")
continue

print(f"Trying to download video from: {video_url}")
try:
print(f"Trying to download video from: {video_url}")
downloader = VideoDownloaderToolkit()
image_list = downloader.get_video_screenshots(video_url, 3)
if image_list and len(image_list) > 0:
Expand All @@ -86,10 +76,13 @@ def detect_image_obj(image_list) -> None:
break
else:
print(f"Failed to capture screenshots from video: {video_url}")
except Exception as e:
print(f"Failed to download video from {video_url}: {e!s}")

print("Exited the video download loop.")
print("Exited the video download loop.")


if __name__ == "__main__":
main()

"""
===============================================================================
Successfully downloaded video and captured screenshots
Expand Down
20 changes: 4 additions & 16 deletions examples/vision/web_video_description_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,24 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from camel.agents import ChatAgent
from camel.configs import ChatGPTConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.prompts import PromptTemplateGenerator
from camel.toolkits import VideoDownloaderToolkit
from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
from camel.types import RoleType, TaskType

video_url = (
'https://sample-videos.com/video321/mp4/720/big_buck_bunny_720p_1mb.mp4'
"https://sample-videos.com/video321/mp4/720/big_buck_bunny_720p_1mb.mp4"
)
downloader = VideoDownloaderToolkit()

# Get the video bytes
video_bytes = downloader.get_video_bytes(video_url)

sys_msg_prompt = PromptTemplateGenerator().get_prompt_from_key(
sys_msg = PromptTemplateGenerator().get_prompt_from_key(
TaskType.VIDEO_DESCRIPTION, RoleType.ASSISTANT
)
sys_msg = BaseMessage.make_assistant_message(
role_name="Assistant",
content=sys_msg_prompt,
)

model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
model_config_dict=ChatGPTConfig().as_dict(),
)

camel_agent = ChatAgent(sys_msg, model=model)
camel_agent = ChatAgent(sys_msg)

# Create user message with video bytes
user_msg = BaseMessage.make_user_message(
Expand Down
18 changes: 1 addition & 17 deletions examples/vision/web_video_object_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@
import argparse

from camel.agents import ChatAgent
from camel.configs.openai_config import ChatGPTConfig
from camel.generators import PromptTemplateGenerator
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.toolkits.video_toolkit import VideoDownloaderToolkit
from camel.types import (
ModelPlatformType,
ModelType,
RoleType,
TaskType,
)
Expand Down Expand Up @@ -49,19 +45,7 @@ def detect_image_obj(image_list) -> None:
print(sys_msg)
print("=" * 49)

assistant_sys_msg = BaseMessage.make_assistant_message(
role_name="Assistant",
content=sys_msg,
)
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
assistant_sys_msg,
model=model,
)
agent = ChatAgent(sys_msg)

user_msg = BaseMessage.make_user_message(
role_name="User",
Expand Down
Loading

0 comments on commit bc10497

Please sign in to comment.