iTranslated by AI
Using Anthropic's Claude 3 Opus Vision Capabilities for Japanese OCR
On the 4th of this month, Anthropic announced Claude 3. Evaluation results released by Anthropic demonstrate high performance, surpassing OpenAI's GPT-4 and Google's Gemini 1.0 Ultra. I have also used Claude 3 Sonnet for several tasks, such as translation and test case creation, and in my experience, its performance feels equivalent to or even better than GPT-4.
Recent generative AI models incorporate image recognition capabilities, and Claude 3 also provides a Vision feature. Specifically, it is designed for use cases such as reading and processing photos, graphs, and diagrams.
I have previously written articles evaluating the Japanese OCR capabilities of image recognition APIs: "Trying out the Japanese OCR function of the Azure Computer Vision API", "Trying out the Japanese OCR function of the Google Cloud Vision API", and "Trying out OpenAI's GPT-4 Turbo with vision as a Japanese OCR". Since Claude 3 also appears capable of Japanese OCR, I decided to evaluate it using the exact same verification method.
Using Claude 3 Opus Vision
Prepare an Anthropic API Key. Go to the Claude API page, click "Get API Access", and after logging in, create a key via "Get API Keys" → "Create Key". The default Evaluation Plan is fine for this purpose. This was my first time creating an Anthropic API Key, and I received a $5 free credit through phone number verification, so I didn't incur any costs for using the API.
The verification code used here is based on the Python code from the Anthropic Client SDKs and the Claude User Manual: Vision, with modifications to the prompt section. If you don't have the necessary Python packages in your environment, please run pip install anthropic.
The Anthropic API Key is entered directly into the api_key variable in the code. Note that the API Key in the code below is a dummy. For each execution, update the image_url value with the URL of the image file you want to process.
import anthropic
import base64
import httpx
client = anthropic.Anthropic(
# defaults to os.environ.get("ANTHROPIC_API_KEY")
api_key="sk-ant-Zy9X-Jw7V_Qr6FTvBn8LCs2DUp3EYtMiGk7ONm5P1Lq2Mn0Eo3Dp4Ht5Kx6Ry7Sz8Tn9Pu0Iv1Jw2Kx3Ly4Mz5Nx6Oy7Pz8Qv9Rw0Sx1Ty2",
)
image_url = "https://sample.com/images/01.jpg"
image_media_type = "image/png"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
message = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=4096,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": image_media_type,
"data": image_data,
},
},
{
"type": "text",
"text": "Please extract all characters within the image. They might be several lines. Return the only extracted characters."
}
],
}
],
)
print(message.content[0].text)
Once prepared, execute the script with python ocr.py. Similar to previous cases, let's first test the character recognition on this image.
$ python ocr.py
一行目おはようございます
二行目こんにちは
三行目こんばんは
I confirmed that the character recognition results were returned correctly.
Character Recognition Test and Accuracy Measurement
As in previous posts, I used the opening text of "The Selfish Giant" (original by Oscar Wilde, translated by Hiroshi Yuki) with four different fonts: Yu Gothic, Meiryo, MS P Mincho, and Anzumoji 2020. The images used are the exact same ones from my previous entries. After removing line breaks and half-width spaces, I calculated the character recognition rate using the same formula: (Number of characters in the correct answer - Levenshtein distance between the correct answer and the recognition result) / Number of characters in the correct answer.
The results are shown in the table below. I will also include the actual output strings at the end of this entry.
| Font | Image | Recognition Rate |
|---|---|---|
| Yu Gothic | Image | 97.4% (372/382) |
| Meiryo | Image | 99.2% (379/382) |
| MS P Mincho | Image | 98.7% (377/382) |
| Anzumoji 2020 | Image | 99.5% (380/382) |
All of them showed excellent character recognition accuracy. It was particularly surprising that there was no significant difference in accuracy between fonts, and that Anzumoji 2020—a unique, non-standard font—achieved the highest precision. Since the mechanical measurement method classifies cases where full-width numbers in the original text are recognized as half-width numbers as errors, achieving a 100% recognition rate is nearly impossible. Therefore, these results are essentially near-perfect scores.
One of the few areas where a difference occurred was in the Yu Gothic image, where the first line "わがままな大男" (The Selfish Giant) was not recognized. This remained the same after several attempts, though the reason is unknown. In images with other fonts, this line was recognized correctly.
Since line breaks are collectively removed during the recognition rate calculation, it doesn't affect the measured results, but there was some fluctuation in how line breaks were placed in the raw output. Specifically, as you can see more clearly in the output results at the bottom of this page, the pattern for handling text wrapping based on image width varied between adding line breaks for visual fidelity and keeping the text continuous for semantic flow. Since this could change even when processing the same image twice, this variation in behavior might be bothersome depending on the use case.
That concludes the results of using the Anthropic Claude 3 Opus Vision feature as a Japanese OCR.
While I felt that OpenAI's GPT-4 with Vision still had some way to go compared to leading cloud image recognition APIs, I was simply amazed by the high precision of Claude 3's Japanese OCR functionality. With the news that such powerful models for both text and image processing will be available on AWS Bedrock and Google Cloud Vertex AI, it feels like a formidable rival has emerged to challenge the dominance of OpenAI's ChatGPT and Azure OpenAI Service. Personally, I am a ChatGPT Plus subscriber and use it daily, but after seeing these verification results, I decided to subscribe to Claude Pro and use Claude 3 Opus for a while.
Output Results
子どもたちは毎日、午後になって学校から帰ってくると、大男の庭に行って遊ぶのが常でした。
そこは、柔らかい緑の草が生えた、広くて素敵な庭でした。草むらのあちこちには、星に似た美しい花が立っておりました。その庭には十二本の桃の木があり、春になると薄桃色と真珠色の繊細な花があふれるように咲き、秋には豊かな果実が実ります。鳥たちは木々の上でたいそう甘い歌声を響かせるので、子どもたちは遊ぶのをやめて聞きいるのでした。「ここで遊ぶのはなんて楽しいんだろう!」と、くちぐちに声をあげました。
ある日、大男が帰ってきました。彼はコーンウォールに住む鬼の友人を訪問し、そこで7年間いっしょに過ごしていました。7年が過ぎ、話したいことは全部話したし、もう話題もなくなってきたので、自分の城に帰ろうと思ったのでした。大男が戻ると、子どもたちが庭で遊んでいるのが見えました。
わがままな大男
子どもたちは毎日、午後になって学校から帰ってくると、大男の庭に行って遊ぶのが常でした。
そこは、柔らかい緑の草が生えた、広くて素敵な庭でした。草むらのあちこちには、星に似た美しい花が立っておりました。その庭には十二本の桃の木があり、春になると薄桃色と真珠色の繊細な花があふれるように咲き、秋には豊かな果実が実ります。鳥たちは木々の上でたいそう甘い歌声を響かせるので、子どもたちは遊ぶのをやめて聞きいるのでした。「ここで遊ぶのはなんて楽しいんだろう!」と、くちぐちに声をあげました。
ある日、大男が帰ってきました。彼はコーンウォールに住む鬼ের友人を訪問し、そこで7年間いっしょに過ごしていました。7年が過ぎ、話したいことは全部話したし、もう話題もなくなってきたので、自分の城に帰ろうと思ったのでした。大男が戻ると、子どもたちが庭で遊んでいるのが見えました。
わがままな大男
子どもたちは毎日、午後になって学校から帰ってくると、大男の庭に行って遊ぶのが常でした。
そこは、柔らかい緑の草が生えた、広くて素敵な庭でした。昔むらのあちこちには、星に似た美しい花が
立っておりました。その庭には十二本の桃の木があり、春になると薄桃色と真珠色の繊細な花があふれる
ように咲き、秋には豊かな果実が実ります。鳥たちは木々の上でたいそう甘い歌声を響かせるので、子ど
もたちは遊ぶのをやめて聞きいるのでした。「ここで遊ぶのはなんで楽しいんだろう!」と、くちぐちに言を
あげました。
ある日、大男が帰ってきました。彼はコーンウォールに住む鬼の友人を訪問し、そこで7年間いっしょに過
ごしていました。7年が過ぎ、話したいことは全部話したし、もう話題もなくなってきたので、自分の城に帰
ろうと思ったのでした。大男が戻ると、子どもたちが庭で遊んでいるのが見えました。
わがままな大男
子どもたちは毎日、午後になって学校から帰ってくると、大男の庭に行って遊ぶのが常でした。
そこは、柔らかい緑の草が生えた、広くて素敵な庭でした。草むらのあちこちには、星に似に美しい花が立っておりました。その庭には十二本の桃の木があり、春になると薄桃色と真珠色の繊細な花があふれるように咲き、秋には豊かな果実が実ります。鳥たちは木々の上でたいそう甘い歌声を響かせるので、子どもたちは遊ぶのをやめて聞きいるのでした。「ここで遊ぶのはなんて楽しいんだろう!」と、くちぐちに声をあげました。
ある日、大男が帰ってきました。彼はコーンウォールに住む負の友人を訪問し、そこで7年間いっしょに過ごしていました。7年が過ぎ、話したいことは全部話したし、もう話題もなくなってきたので、自分の城に帰ろうと思ったのでした。大男が戻ると、子どもたちが庭で遊んでいるのが見えました。
Discussion