iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👁️

Using OpenAI's GPT-4 Turbo with Vision for Japanese OCR

に公開
2024/05/14

At the recent OpenAI DevDay, GPT-4 Turbo with vision was announced. This is an API that allows you to pass image files as input to the Chat Completions API for image analysis.

Previously, I wrote entries evaluating the Japanese OCR capabilities of various cloud image recognition APIs in "Trying out the Japanese OCR function of the Azure Computer Vision API" and "Trying out the Japanese OCR function of the Google Cloud Vision API". Since OpenAI has also made an image recognition API available, I decided to evaluate it using the exact same verification method.

Using the GPT-4 Turbo with vision OCR Function

Prepare an OpenAI API Key. If you haven't obtained one yet, please search for "How to get OpenAI API Key".

For this verification code, I will use the Node.js (node) version of the Quick Start from the OpenAI API Document: Vision. Since the code in the aforementioned entries was in Node.js, I chose Node.js again this time, though I don't think there would be much difference with Python.

It is assumed that Node.js and npm are already installed. Install the openai package using npm.

$ npm install openai

Set the OpenAI API Key in the environment variable OPENAI_API_KEY. Below is an example of execution in the Mac terminal. The value is a dummy.

$ export OPENAI_API_KEY='sk-Y9z2x8j7w6e5t4r3u2i1o0pQ6y5t8z2u9v1w4x7r3e0a5t4r'

In the sample code, max_tokens is not specified, but the default value of 16 is insufficient and the response would be cut off midway, so I set it to 1024 this time. Also, since I want only the extracted characters to be output, I added an instruction as a system message: "You are an OCR machine, and you cannot respond with anything other than the extracted text." The image file is passed as a link for the url value.

index.js
import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const response = await openai.chat.completions.create({
    model: "gpt-4-vision-preview",
    max_tokens: 1024,
    messages: [
      {
        role: "system",
        content: "You are an Optical Character Recognition (OCR) machine. You will extract all the characters from the image file in the URL provided by the user, and you will only privide the extracted text in your response. As an OCR machine, You can only respond with the extracted text."
      },
      {
        role: "user",
        content: [
          { type: "text", text: "Please extract all characters within the image. Return the only extacted characters." },
          {
            type: "image_url",
            image_url: {
              "url": "https://sample.com/images/01.jpg",
            },
          },
        ],
      },
    ],
  });
  console.log(response.choices[0].message.content);
}
main();

Once ready, run the script with node index.js. As before, I will first try character recognition on this image as a test.

$ node index.js
一行目はようこそいます
二行目とんにちは
三行目こんばんは

Regardless of the recognition accuracy, I confirmed that the character recognition results are being returned.

Character Recognition Test and Measurement of Recognition Rates

As in previous entries, I used the opening text of "The Selfish Giant" by Oscar Wilde, translated by Hiroshi Yuki, and images featuring four types of fonts: Yu Gothic, Meiryo, MS P Mincho, and Anzumoji 2020 (the exact same images as in the previous entries). After removing line breaks and spaces, the character recognition rate was calculated using the same formula: (Number of correct characters - Levenshtein distance between correct characters and recognition results) / Number of correct characters.

The results are shown in the table below. The actual text output is included at the end of this entry.

Font Image Recognition Rate
Yu Gothic Image 93.7% (358/382)
Meiryo Image 78.8% (301/382)
MS P Mincho Image 81.2% (310/382)
Anzumoji 2020 Image 50.8% (194/382)

Recognition accuracy varied significantly depending on the font. While Yu Gothic had some errors, it recognized text well enough that the meaning was sufficiently readable. For the three fonts after Meiryo, the results were disorganized enough that I felt they might be a bit difficult to read. When comparing the image recognition APIs of various clouds from the perspective of OCR functionality, at least for now, it can be said that the dedicated image recognition APIs of each cloud come out on top. This is a clear case of "specialists know best."

That concludes the results of using GPT-4 Turbo with vision as a Japanese OCR.

Since GPT-4 Turbo with vision is not an API that puts OCR functionality at the forefront, this evaluation itself might have been a bit demanding. For instance, instead of just performing OCR, if you instruct the API to "extract the text from the image file and summarize it in one sentence," it might reply with something like, "In short, the content is about children playing in a giant's garden." This demonstrates that it is an undeniably powerful and flexible API, especially useful for executing a series of tasks with just a few lines of code.

On the other hand, it's worth noting that if the accuracy of your overall task depends on the precision of the OCR result, there appear to be limitations to its recognition capabilities as an OCR tool. Based on the results above, Gothic fonts currently seem to yield higher accuracy. Therefore, it might be beneficial to use Gothic fonts for images passed to OpenAI whenever possible (avoiding Meiryo or Mincho). Alternatively, you could consider a multi-API approach: using dedicated image recognition APIs like Azure or Google Cloud for the OCR task, and then providing that text as context to the OpenAI API.

Output Results
Yu Gothic
わがままな大男

子どもたちは毎日、午後になって学校から帰ってくると、大男の庭に行って遊ぶのが常でした。
そしては、柔らかい豪の実が生えた、広くて素敵な庭でした。昔おじのあちこちには、星に似た美しい花가咲っておりました。その庭には十二本の桜の木があり、春になると樹皮もと真珠色の樹液が花があふれるように咲き、秋には豊かな果実が実ります。彼たちは木の上でたいそうない
歌声を響かせるので、子どもたちは好ぶのをやめて聞きいるのでした。「ここで遊ぶのはなんて楽しいんだろう!」と、くちぐちは声をあげました。

ある日、大男が帰ってきました。彼はコーンウォールに住む先の友人を訪問し、そこで7年間
いっしょに過ごしていました。7年が過ぎ、話したいことは全部話しました、もう話題もなくな
てきたので、自分の城に帰ろうと思ったのでした。大男が戻ると、子どもたちが庭で遊んでいる
のが見えました。
Meiryo
わが町奇談大會

テビビたちは毎日、午後になって学校から帰ってくると、大男の庭に行って遊ぶのが習いだった。

そしては、家づかい櫻の守が稽古を、庭くて素振り煽てました。草むらのあちこちには、星に似た美しい花が咲っておりました。その庭にはキー本の桜の木があり、春になると蜜蜂と自然恋の蝶
紋紋がかぶれるように呼さ、桜には豊かな果実が実ります。皇たちは木々の上でいそう自い
語有を糾かせるので、テビビたちは嬉ぷのをやめて聞きいるのでした。「ここで遊ぶのはなんて
楽しいんだろう!! と、くちぐち語をあふました。

ある日、大男が帰ってきました。彼はコーツウォールに住む蘭の友人を訪問し、そこて7年間
いっしょに訥しくいました。7年が過ぎ、話したいことは全部話したし、もう話額もなくなって赴ので、自分の故に帰ることに決のでした。大男が戻ると、テビビたちが遊て遊んでいる
のが見えました。
MS P Mincho
わがままな大男

子どもたちは毎日、午後になって学校から帰ってくると、大男の泉に行って遊ぶのが常でした。

そしては、柔らかい霧の中点が生えた、広くて蔓莪な庭でした。草むらのあちこちには、星に似た美しい花が立って咲いていました。その庭には十二本の桜の木があり、春になると華麗な色と甘い香りの絨毯を法があふれるように咲き、秋には世間がな果実が実ります。葉たちは大木の上でたいそうな囀を競争するので、子どもたちは遊ぶのをやめて聞きいるのでした。「ここで遊ぶのはなんて楽しいんだろう!」と、彼らは声をあげました。

ある日、大男が湧いてきました。彼はコーリャンゴールに佇む背の大きな蒼間凛、そこで7年間いっしょに過ごしていました。7年が過ぎ、言葉をいっとは全粒話したし、お互い話なくなってきたので、自分の故郷に帰ろうと思ったのでした。大男が戻ると、子どもたちは湧で遊んでいるのが見えました。
Anzumoji 2020
わかりますか課

子どもたちは毎日、午後遅くなって学校から帰ってくると、木男の足に行って遊ぶのが常でした。

そこは、森とかい霊安の草むらになった、広くて素敵な広場でした。草むらのなかっちょちょには、黄色い花たくさんいたかいていました。その広場には二本の巨木があり、森になると遥か北とを連結させる鉄塔との繋牙布で木男がふんばるようにそそり立っていました。その広場のはずれには、木男の木があり、森になると遥か北とを連結させる鉄塔がふんばっていました。温もりは木男の足の上でだいぶうわもい温声を辺時なきあのです。子どもたちは温ものをわかり間いとのです。「ここで温ものはなんで素しいんだろう?」と、くっきりとた声をあげました。

ぬる日、木男が帰ってきました。彼はコークナーボルに住む魚の木人も訪問し、えって7年間いっしょに過ごしていました。7年が過ぎ、詰もたいさとは全員お亡したし、もう話題もなくなってきたのです。自分の腹に帰ってきたのです。木男が戻ると、子どもたちが広場で遊んでいるのが見えました。

Discussion