主题
OCR 工具
OCRTool使用具备视觉能力的 LLM 从本地图像或图像 URL 中提取文本。
OCRTool
描述
从图像(本地路径或 URL)中提取文本。通过 CrewAI 的 LLM 接口使用支持视觉的 LLM。
安装
除 crewai-tools 外无需额外安装。请确保所选 LLM 支持视觉。
参数
运行参数
image_path_url(str,必填):本地图像路径或 HTTP(S) URL。
示例
直接使用
python
from crewai_tools import OCRTool
print(OCRTool().run(image_path_url="/tmp/receipt.png"))配合智能体使用
python
from crewai import Agent, Task, Crew
from crewai_tools import OCRTool
ocr = OCRTool()
agent = Agent(
role="OCR",
goal="Extract text",
tools=[ocr],
)
task = Task(
description="Extract text from https://example.com/invoice.jpg",
expected_output="All detected text in plain text",
agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()说明
- 确保所选 LLM 支持图像输入。
- 对于大图像,考虑缩小尺寸以减少 token 消耗。
- 如有需要,可以向工具传入特定的 LLM 实例(例如
LLM(model="gpt-4o")),与 README 指南保持一致。
示例
python
from crewai import Agent, Task, Crew
from crewai_tools import OCRTool
tool = OCRTool()
agent = Agent(
role="OCR Specialist",
goal="Extract text from images",
backstory="Vision‑enabled analyst",
tools=[tool],
verbose=True,
)
task = Task(
description="Extract text from https://example.com/receipt.png",
expected_output="All detected text in plain text",
agent=agent,
)
crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()