文字转录
从图片中提取和转录文字内容
🌐 查看英文原文 | 源码 Notebook
如何使用 Claude 转录文档
Claude 3 非常擅长读取图像和 PDF 中的非结构化文本和信息,并将其转换为结构化文本。我们将看几个示例,但首先让我们设置运行 notebook 所需的代码。
%pip install anthropic IPythonimport base64
from anthropic import Anthropic
client = Anthropic()
MODEL_NAME = "claude-opus-4-1"
def get_base64_encoded_image(image_path):
with open(image_path, "rb") as image_file:
binary_data = image_file.read()
base_64_encoded_data = base64.b64encode(binary_data)
base64_string = base_64_encoded_data.decode("utf-8")
return base64_string转录打印文本
与传统 OCR 系统相比,使用 Claude 3 的优势在于,由于 Claude 3 具有高级推理能力,你可以精确指定要转录的内容。对于这张图片,让我们只转录答案中的代码。
from IPython.display import Image
Image(filename="../images/transcribe/stack_overflow.png")<IPython.core.display.Image object>
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": get_base64_encoded_image("../images/transcribe/stack_overflow.png"),
},
},
{"type": "text", "text": "Transcribe the code in the answer. Only output the code."},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print(response.content[0].text)import os
import base64
image = 'test.jpg'
encoded_string = ""
with open(image, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
file = encoded_string
转录手写文本
效果不错,但让我们尝试一些更难的任务。Claude 3 同样擅长转录手写文本。让我们让 Claude 3 转录这张手写的处方笔记。
Image(filename="../images/transcribe/school_notes.png")<IPython.core.display.Image object>
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": get_base64_encoded_image("../images/transcribe/school_notes.png"),
},
},
{
"type": "text",
"text": "Transcribe this text. Only output the text and nothing else.",
},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print(response.content[0].text)Levels of Cellular Organization
1) Cells group together to make tissue.
2) Tissues group together to make an organ.
3) Organs group together to make an organ system
4) Organ systems group together to make an organism
Organism -> a living thing that can
carry out life processes by itself.
- Multicellular organisms have specialized
cells to perform specific functions.
> This makes them more efficient
and typically have a longer life span.
Tissue = a group of similar cells
that perform a common function.
1) Animals are made of four
basic types of tissue
> nervous, epithelial, connective,
and muscle
2) Plants have three types
of tissue
> transport, protective, and
ground
转录表格
不如我们尝试一下打印文本和手写文本的组合?这在保险和报告表格等各种文档中很常见。
Image(filename="../images/transcribe/vehicle_form.jpg")<IPython.core.display.Image object>
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": get_base64_encoded_image("../images/transcribe/vehicle_form.jpg"),
},
},
{"type": "text", "text": "Transcribe this form exactly."},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print(response.content[0].text)VEHICLE INCIDENT REPORT FORM
Use this form to report accidents, injuries, medical situations, criminal activities, traffic incidents, or student behavior incidents. If possible, a report should be completed within 24 hours of the event.
Date of Report: 02/29, 2024
PERSON INVOLVED
Full Name: John Doe Address: 123 Main St
Identification: ■ Driver's License No. 474921 □ Passport No. ___________
□ Other: ____________________
Phone: (678) 999-8212 E-Mail: john@gmail.com
THE INCIDENT
Date of Incident: 02/29/2024 ■ Time: 9:01 ■ AM □ PM
Location: Corner of 2nd and 3rd
Describe the Incident: Red car t-boned blue car
_______________________________________________________
_______________________________________________________
INJURIES
Was anyone injured? □ Yes ■ No
If yes, describe the injuries: ________________________________________
_______________________________________________________________
_______________________________________________________________
WITNESSES
Were there witnesses to the incident? □ Yes ■ No
If yes, enter the witnesses' names and contact info: __________________________
_______________________________________________________________
_______________________________________________________________
Page 1 of 2
复杂文档问答
借助 Claude 3,我们可以超越单纯的转录,针对非结构化文档中的信息提出具体问题。
Image(filename="../images/transcribe/page.jpeg")<IPython.core.display.Image object>
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": get_base64_encoded_image("../images/transcribe/page.jpeg"),
},
},
{"type": "text", "text": "Which is the most critical issue for live rep support?"},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print(response.content[0].text)According to the hierarchy of importance pyramid for Live Rep Support shown in the image, the most critical issue is Product Quality/Liability Issues. This is positioned at the very bottom of the pyramid, indicating it is the most critical or important issue for live rep support to handle.
非结构化信息 → JSON
让我们看看如何使用 Claude 将图像中的非结构化信息转换为结构化的 JSON 输出。
Image(filename="../images/transcribe/org_chart.jpeg")<IPython.core.display.Image object>
message_list = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": get_base64_encoded_image("../images/transcribe/org_chart.jpeg"),
},
},
{
"type": "text",
"text": "Turn this org chart into JSON indicating who reports to who. Only output the JSON and nothing else.",
},
],
}
]
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=message_list)
print(response.content[0].text){
"President": {
"name": "John Smith",
"directReports": [
{
"name": "Susan Jones",
"title": "VP Marketing",
"directReports": [
{
"name": "Alice Johnson",
"title": "Manager"
},
{
"name": "Tim Moore",
"title": "Manager"
}
]
},
{
"name": "Rachel Parker",
"title": "VP Sales",
"directReports": [
{
"name": "Michael Gross",
"title": "Manager"
},
{
"name": "Kim Dole",
"title": "Manager"
}
]
},
{
"name": "Tom Allen",
"title": "VP Production",
"directReports": [
{
"name": "Kathy Roberts",
"title": "Manager"
},
{
"name": "Betsy Foster",
"title": "Manager"
}
]
}
]
}
}
Tip
中文读者提示:本章节的代码和输出为英文原文。如需理解具体实现细节,可参考上方中文导读和代码注释。如有疑问,欢迎在 GitHub Issues 讨论。