diff --git a/.gitignore b/.gitignore index 0a3b4d2..02480b4 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ /ndlocr-lite-gui/userconf.yaml /ndlocr-lite-gui/debug.log /ndlocr-lite-gui/4ab7ecc3-53fb-b3e7-64e8-a809b5a483d2/ -/4ab7ecc3-53fb-b3e7-64e8-a809b5a483d2/ \ No newline at end of file +/4ab7ecc3-53fb-b3e7-64e8-a809b5a483d2/ diff --git a/LICENCE b/LICENCE index 394ad72..307375c 100644 --- a/LICENCE +++ b/LICENCE @@ -392,4 +392,4 @@ understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. -Creative Commons may be contacted at creativecommons.org. \ No newline at end of file +Creative Commons may be contacted at creativecommons.org. diff --git a/README.md b/README.md index e0007c8..0f6f06f 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,14 @@ Windows(Windows 11)、Mac(Apple M4, macOS Sequoia)及びLinux(Ubuntu 22.04)環 本プログラムは、国立国会図書館がCC BY 4.0ライセンスで公開するものです。詳細については[LICENCE](./LICENCE)をご覧ください。なお、本アプリケーションの実行時に利用するライブラリ等のライセンスについては[LICENCE_DEPENDENCIES](./LICENCE_DEPENDENCEIES)をご覧ください。 - ## デスクトップアプリケーションによる利用 **デスクトップアプリケーションを利用する際には、日本語(全角文字)を含まないパスにアプリケーションを配置してください。全角文字を含む場合に起動しないことがあります。** [releases](https://github.com/ndl-lab/ndlocr-lite/releases)からお使いのOS環境(Windows/Mac/Linux)に合ったファイルをダウンロードしてください。 - デスクトップアプリケーションの操作方法については[NDLOCR-Liteの使い方](https://lab.ndl.go.jp/data_set/ndlocrlite-usage/)、ビルド方法については[デスクトップアプリケーションの利用方法](./ndlocr-lite-gui/README.md)を参照してください。 - 次のgifアニメーションは、 [国立国会図書館総務部総務課 編『国立国会図書館年報』昭和27年度,国立国会図書館,1954. 国立国会図書館デジタルコレクション https://dl.ndl.go.jp/pid/3048008"](https://dl.ndl.go.jp/pid/3048008/1/24) @@ -32,80 +29,88 @@ Windows(Windows 11)、Mac(Apple M4, macOS Sequoia)及びLinux(Ubuntu 22.04)環 ## コマンドラインからの利用 + ※コマンドラインから操作を行うにはPython 3.10以上が必要です。 事前準備 -``` + +```sh git clone https://github.com/ndl-lab/ndlocr-lite cd ndlocr-lite pip install -r requirements.txt cd src ``` + 実行例1.(同階層にある「9892834_0001」という名称のディレクトリ内の画像を一括処理し、tmpdirという名称のディレクトリに結果を出力する。) -``` -python3 ocr.py --sourcedir 9892834_0001 --output tmpdir + +```sh +python3 ocr.py --sourcedir 9892834_0001 --output tmpdir ``` 実行例2.(同階層にある「digidepo_1287221_00000002.jpg」という名称の画像を処理し、tmpdirという名称のディレクトリに結果を出力する。) -``` + +```sh python3 ocr.py --sourceimg digidepo_1287221_00000002.jpg --output tmpdir ``` -uv(https://github.com/astral-sh/uv -)をお使いの環境であれば、以下のようにしても導入・実行可能です。この場合、「ndlocr-lite」というコマンドから実行できます。 +[uv](https://github.com/astral-sh/uv)をお使いの環境であれば、以下のようにしても導入・実行可能です。この場合、「ndlocr-lite」というコマンドから実行できます。 導入方法 -``` + +```sh git clone https://github.com/ndl-lab/ndlocr-lite cd ndlocr-lite uv tool install . ``` 実行例 -``` -ndlocr-lite --sourceimg digidepo_1287221_00000002.jpg --output tmpdir + +```sh +ndlocr-lite --sourceimg digidepo_1287221_00000002.jpg --output tmpdir ``` ### パラメータの説明 #### `--sourcedir`オプション + 処理したい画像の含まれるディレクトリを絶対パスまたは相対パスで指定する。ディレクトリ内の"jpg(jpegも可)"、"png"、"tiff(tifも可)"、"jp2"及び"bmp"の拡張子のファイルを順次処理する。 #### `--sourceimg`オプション + 処理したい画像を絶対パスまたは相対パスで直接指定する。"jpg(jpegも可)"、"png"、"tiff(tifも可)"、"jp2"及び"bmp"の拡張子のファイルを処理することが可能。 #### `--output`オプション + OCR結果を保存する出力先ディレクトリを相対パスまたは絶対パスで指定する。 #### `--viz`オプション + `--viz True`を指定することで、文字認識箇所を青枠で表示した画像を出力先ディレクトリに出力する。 #### `--device`オプション(ベータ) -対応GPUを搭載したサーバかつonnxruntime-gpuがインストールされている環境に限り、`--device cuda`を指定することでGPUを利用した処理に切り替える。 +対応GPUを搭載したサーバかつonnxruntime-gpuがインストールされている環境に限り、`--device cuda`を指定することでGPUを利用した処理に切り替える。 ## OCR結果の例 + |資料画像|OCR結果の冒頭(誤認識を含む)|OCR結果のxml| |---|---|---| -|
国立国会図書館総務部 編『国立国会図書館スタッフ・マニュアル』E-2,国立国会図書館,1963.8. 国立国会図書館デジタルコレクション https://dl.ndl.go.jp/pid/2531162/1/23|(ヱ)気送子送付管気送子送付には、上記気送響にて送付するものと、空気の圧縮を使用せず,直接落下させる装置の二通りがある。後者の送付管は山納台左側に設置されており.5|[OCR結果(xmlファイル)](./resource/digidepo_2531162_0024.xml)| -|
館内スコープ 次世代室の謎に迫れ!. 国立国会図書館月報. 2018, (683),  p.20. http://dl.ndl.go.jp/info:ndljp/pid/11048278|はじめまして!私は2017年4月に就職後、次世代システム開発研究室(次世代室)という場所で仕事をしています。でも、「次世代室」って何をするところか想像しにくいですよね。次世代室は、図書館の役割がインターネット等の情報技術で変化する中、より先進的なサービスを検討していくために作られた比較的新しい部署です。|[OCR結果(xmlファイル)](./resource/digidepo_11048278_po_geppo1803_00021.xml)| -|
国立国会図書館総務部総務課 編『国立国会図書館年報』昭和27年度,国立国会図書館,1954. 国立国会図書館デジタルコレクション https://dl.ndl.go.jp/pid/3048008/1/25|第8章職員、庁舍、財政、記念行事等1.職員A.司書職員の研修昭和26年度(第4回)研修に引続き、昭和27年度(第5回)司書職員研修を27年6月2日から28年4月10日まで320時間、研修生50名に実施した。本年度は、東京学芸大学の協力を得て、講師は、本館職員が専門分野の科目を担当した外、東京学芸大学の教授が担当した。本年度も單位科目ごとに試験を実施し、必修科目(11單位)選択科目(4單位)合わせて15單位以上の試験に合格した43名が修了した。|[OCR結果(xmlファイル)](./resource/digidepo_3048008_0025.xml)| - +|
国立国会図書館総務部 編『国立国会図書館スタッフ・マニュアル』E-2,国立国会図書館,1963.8. 国立国会図書館デジタルコレクション |(ヱ)気送子送付管気送子送付には、上記気送響にて送付するものと、空気の圧縮を使用せず,直接落下させる装置の二通りがある。後者の送付管は山納台左側に設置されており.5|[OCR結果(xmlファイル)](./resource/digidepo_2531162_0024.xml)| +|
館内スコープ 次世代室の謎に迫れ!. 国立国会図書館月報. 2018, (683),  p.20. |はじめまして!私は2017年4月に就職後、次世代システム開発研究室(次世代室)という場所で仕事をしています。でも、「次世代室」って何をするところか想像しにくいですよね。次世代室は、図書館の役割がインターネット等の情報技術で変化する中、より先進的なサービスを検討していくために作られた比較的新しい部署です。|[OCR結果(xmlファイル)](./resource/digidepo_11048278_po_geppo1803_00021.xml)| +|
国立国会図書館総務部総務課 編『国立国会図書館年報』昭和27年度,国立国会図書館,1954. 国立国会図書館デジタルコレクション |第8章職員、庁舍、財政、記念行事等1.職員A.司書職員の研修昭和26年度(第4回)研修に引続き、昭和27年度(第5回)司書職員研修を27年6月2日から28年4月10日まで320時間、研修生50名に実施した。本年度は、東京学芸大学の協力を得て、講師は、本館職員が専門分野の科目を担当した外、東京学芸大学の教授が担当した。本年度も單位科目ごとに試験を実施し、必修科目(11單位)選択科目(4單位)合わせて15單位以上の試験に合格した43名が修了した。|[OCR結果(xmlファイル)](./resource/digidepo_3048008_0025.xml)| ## モデルの再学習及びカスタマイズについて(開発者向け情報) -[学習及びモデル変換手順](/train/README.md)をご覧ください。 +[学習及びモデル変換手順](/train/README.md)をご覧ください。 ## 技術情報について(開発者向け情報) NDLOCR-Liteは「レイアウト認識」、「文字列認識」、「読み順整序」の3つの機能(モジュール)を組み合わせて実現しています。 -レイアウト認識にはDEIMv2[1]、文字列認識にはPARSeq[2]をそれぞれ用いており、読み順整序については当館が公開している[NDLOCR](https://github.com/ndl-lab/ndlocr_cli)と同様のモジュールを用いています。 +レイアウト認識にはDEIMv2[1]、文字列認識にはPARSeq[2]をそれぞれ用いており、読み順整序については当館が公開している[NDLOCR]()と同様のモジュールを用いています。 -[1]Shihua Huang and Yongjie Hou and Longfei Liu and Xuanlong Yu and Xi Shen. Real-Time Object Detection Meets DINOv3. arXiv preprint arXiv:2509.20787, 2025.(https://arxiv.org/abs/2509.20787) +[1]Shihua Huang and Yongjie Hou and Longfei Liu and Xuanlong Yu and Xi Shen. Real-Time Object Detection Meets DINOv3. arXiv preprint arXiv:2509.20787, 2025.() -[2]Darwin Bautista, Rowel Atienza. Scene text recognition with permuted autoregressive sequence models. arXiv:2212.06966, 2022. (https://arxiv.org/abs/2207.06966) +[2]Darwin Bautista, Rowel Atienza. Scene text recognition with permuted autoregressive sequence models. arXiv:2212.06966, 2022. () レイアウト認識及び文字列認識の機械学習モデルは、いずれもpytorchをフレームワークとした学習を行った後にONNX形式に変換して利用しています。詳しくは[学習及びモデル変換手順](/train/README.md)をご覧ください。 - - diff --git a/ndlocr-lite-gui/README.md b/ndlocr-lite-gui/README.md index aafc151..9493bd3 100644 --- a/ndlocr-lite-gui/README.md +++ b/ndlocr-lite-gui/README.md @@ -3,6 +3,7 @@ NDLOCR-Liteの使い方を説明します。 ## 起動方法 + お使いのOSに対応する圧縮ファイルを展開し、ndlocr_liteをダブルクリック等で実行してください。 なお、セキュリティに関する警告が出ることがあります。 @@ -11,13 +12,14 @@ Windows 11の場合は、「WindowsによってPCが保護されました」→ macOSの場合は、 -https://zenn.dev/nakamura196/articles/c62a465537ff20 + の手順に従ってください。 なお、初回の起動には1分程度時間を要することがあります。お待ちください。 ## 操作方法 + ①OCR処理をかけたいファイルまたはディレクトリを指定します。画像(jpg,png,tiff,bmp,jp2)に加えてPDFファイルに対する処理が可能です。 ②出力先となるディレクトリを指定します。 @@ -27,14 +29,17 @@ https://zenn.dev/nakamura196/articles/c62a465537ff20 ④ ディレクトリを指定した場合、OCR処理が完了した画像から処理結果のプレビューを表示できます。ファイルを指定した場合には当該ファイルの処理結果が表示されます。 ## 自分でアプリケーションをビルドする場合の方法(開発者向け情報) + 本アプリケーションは[Flet(外部サイト)](https://flet.dev/)を利用します。 いずれのOSの場合にも事前にFlutter-SDKの導入が必要です。依存関係のインストールに関する説明は省略します。 ### Windowsの場合 -https://flet.dev/docs/publish/windows/ + + も参照してください。 -``` + +```sh #(コマンドプロンプトを利用、ndlocr-lite-guiと同階層で実行する) python3 -m venv ocrenv .\ocrenv\Scripts\activate @@ -45,7 +50,7 @@ flet build windows ### Macの場合 -``` +```sh #(ndlocr-lite-guiと同階層で実行する) python3 -m venv ocrenv source ./ocrenv/bin/activate @@ -55,7 +60,8 @@ flet build macos ``` ### Linuxの場合 -``` + +```sh #(ndlkotenocr-lite-guiと同階層で実行する) python3 -m venv ocrenv source ./ocrenv/bin/activate diff --git a/ndlocr-lite-gui/pyproject.toml b/ndlocr-lite-gui/pyproject.toml index 587da46..9034946 100644 --- a/ndlocr-lite-gui/pyproject.toml +++ b/ndlocr-lite-gui/pyproject.toml @@ -7,22 +7,11 @@ requires-python = ">=3.10" authors = [ { name = "National_Diet_Library", email = "lab@ndl.go.jp" } ] -dependencies = [ - "flet==0.27.6", - "dill==0.3.8", - "lxml==5.4.0", - "networkx==3.3", - "onnxruntime==1.23.2", - "pillow==12.1.1", - "ordered-set==4.1.0", - "protobuf==6.31.1", - "pyparsing==3.1.2", - "PyYAML==6.0.1", - "tqdm==4.66.4", - "reportlab==4.2.5", - "pypdfium2==4.30.0", - "numpy==2.2.2" -] + +dynamic = ["dependencies"] + +[tool.setuptools.dynamic] +dependencies = { file = ["requirements.txt"] } [tool.flet] # org name in reverse domain name notation, e.g. "com.mycompany". diff --git a/ndlocr-lite-gui/requirements.txt b/ndlocr-lite-gui/requirements.txt new file mode 100644 index 0000000..b615261 --- /dev/null +++ b/ndlocr-lite-gui/requirements.txt @@ -0,0 +1,14 @@ +flet==0.27.6 +dill==0.3.8 +lxml==5.4.0 +networkx==3.3 +onnxruntime==1.23.2 +pillow==12.1.1 +ordered-set==4.1.0 +protobuf==6.31.1 +pyparsing==3.1.2 +PyYAML==6.0.1 +tqdm==4.66.4 +reportlab==4.2.5 +pypdfium2==4.30.0 +numpy==2.2.2 diff --git a/pyproject.toml b/pyproject.toml index cc26571..50abd8d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,22 +8,10 @@ version = "1.0.0" description = "NDLOCR-Lite CLI Application" readme = "README.md" requires-python = ">=3.10" -dependencies = [ - "flet==0.27.6", - "dill==0.3.8", - "lxml==5.4.0", - "networkx==3.3", - "onnxruntime==1.23.2", - "pillow==12.1.1", - "ordered-set==4.1.0", - "protobuf==6.31.1", - "pyparsing==3.1.2", - "PyYAML==6.0.1", - "tqdm==4.66.4", - "reportlab==4.2.5", - "pypdfium2==4.30.0", - "numpy==2.2.2" -] +dynamic = ["dependencies"] + +[tool.setuptools.dynamic] +dependencies = { file = ["requirements.txt"] } [project.scripts] ndlocr-lite = "ocr:main" @@ -37,4 +25,4 @@ where = ["src"] [tool.setuptools.package-data] "model" = ["*.onnx"] -"config" = ["*.yaml"] \ No newline at end of file +"config" = ["*.yaml"] diff --git a/requirements.txt b/requirements.txt index fe5b1d2..b615261 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,4 @@ PyYAML==6.0.1 tqdm==4.66.4 reportlab==4.2.5 pypdfium2==4.30.0 -numpy==2.2.2 \ No newline at end of file +numpy==2.2.2 diff --git a/train/README.md b/train/README.md index d6dc55e..d45e3a8 100644 --- a/train/README.md +++ b/train/README.md @@ -1,8 +1,9 @@ +# 学習について ## レイアウト認識(DEIMv2) -Shihua Huang and Yongjie Hou and Longfei Liu and Xuanlong Yu and Xi Shen. Real-Time Object Detection Meets DINOv3. arXiv preprint arXiv:2509.20787, 2025.(https://arxiv.org/abs/2509.20787) +Shihua Huang and Yongjie Hou and Longfei Liu and Xuanlong Yu and Xi Shen. Real-Time Object Detection Meets DINOv3. arXiv preprint arXiv:2509.20787, 2025.() を利用してレイアウト認識モデルを作成します。 @@ -13,7 +14,8 @@ Shihua Huang and Yongjie Hou and Longfei Liu and Xuanlong Yu and Xi Shen. Real-T この項で紹介する当館が作成したサンプルコードは[deimv2code](./deimv2code)ディレクトリ以下にあります。 ### 環境構築 -``` + +```sh python3 -m venv deimenv source ./deimenv/bin/activate git clone https://github.com/Intellindust-AI-Lab/DEIMv2 @@ -22,31 +24,32 @@ python3 -m pip install --upgrade pip pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu121 pip install faster-coco-eval>=1.6.7 PyYAML tensorboard scipy calflops transformers ``` -他、公式リポジトリhttps://github.com/Intellindust-AI-Lab/DEIMv2 + +他、[公式リポジトリ](https://github.com/Intellindust-AI-Lab/DEIMv2) を参考に、vitt_distill.ptをダウンロードして、ckptsディレクトリ以下に展開しておく必要があります。 -### 学習データの変換 +### レイアウト認識(DEIMv2)学習データの変換 + 準備中 ### 学習(1) -``` +```sh cp -r deimv2code/part1/* . CUDA_VISIBLE_DEVICES=0,1,2 torchrun --master_port=7777 --nproc_per_node=3 train.py -c configs/ndl_deimv2/deimv2_dinov3_s_coco_r4_800.yml --seed=0 ``` ### 学習(2) -``` +```sh cp -r deimv2code/part2/* . CUDA_VISIBLE_DEVICES=0,1,2 torchrun --master_port=7777 --nproc_per_node=3 train.py -c configs/ndl_deimv2/deimv2_dinov3_s_coco_r4_800.yml --seed=0 -t /data1/DEIMv2/outputs/deimv2_dinov3_s_coco_r4_800/last.pth ``` -### 学習済モデルのONNXへの変換 - +### レイアウト認識(DEIMv2)学習済モデルのONNXへの変換 -``` +```sh pip install numpy==1.21.6 onnx==1.16.2 onnxruntime-gpu==1.18.1 python tools/deployment/export_onnx.py --check -c ./configs/ndl_deimv2/deimv2_dinov3_s_coco_r4_800.yml -r outputs/deimv2_dinov3_s_coco_r4_800/last.pth ``` @@ -55,9 +58,9 @@ python tools/deployment/export_onnx.py --check -c ./configs/ndl_deimv2/deimv2_di NDLOCR-Liteで利用する場合は、--det-weightsオプションでonnxファイルのパスを指定してください。 - ## 文字列認識(PARSeq) -Darwin Bautista, Rowel Atienza. Scene text recognition with permuted autoregressive sequence models. arXiv:2212.06966, 2022. (https://arxiv.org/abs/2207.06966) + +Darwin Bautista, Rowel Atienza. Scene text recognition with permuted autoregressive sequence models. arXiv:2212.06966, 2022. () を利用して文字列認識モデルを作成します。 @@ -65,8 +68,9 @@ Darwin Bautista, Rowel Atienza. Scene text recognition with permuted autoregress この項で紹介する当館が作成したサンプルコードは[parseqcode](./parseqcode)ディレクトリ以下にあります。 -### 環境構築 -``` +### 文字列認識(PARSeq)の環境構築 + +```sh python3 -m venv parseqenv source ./parseqenv/bin/activate git clone https://github.com/baudm/parseq @@ -80,22 +84,27 @@ pip install tqdm ``` そのままではONNX変換時にエラーが発生することがあるので、parseq/strhub/models/parseq/model.py -の117行目(元リポジトリの次の箇所 -https://github.com/baudm/parseq/blob/1902db043c029a7e03a3818c616c06600af574be/strhub/models/parseq/model.py#L117) +の117行目([元リポジトリの次の箇所](https://github.com/baudm/parseq/blob/1902db043c029a7e03a3818c616c06600af574be/strhub/models/parseq/model.py#L117)) -```tgt_mask = query_mask = torch.triu(torch.ones((num_steps, num_steps), dtype=torch.bool, device=self._device), 1)``` +```python +tgt_mask = query_mask = torch.triu(torch.ones((num_steps, num_steps), dtype=torch.bool, device=self._device), 1) +``` を -```tgt_mask = query_mask = torch.triu(torch.ones((num_steps, num_steps), dtype=torch.float, device=self._device), 1)``` +```python +tgt_mask = query_mask = torch.triu(torch.ones((num_steps, num_steps), dtype=torch.float, device=self._device), 1) +``` + に変更してください。 -### 学習データの変換 +### 文字列認識(PARSeq)学習データの変換 [OCR学習用データセット(みんなで翻刻)](https://github.com/ndl-lab/ndl-minhon-ocrdataset)の「利用方法」を参考に画像とテキストデータを対応付けた1行データセットを作成してください。 honkoku_rawdataディレクトリ内に行ごとの切り出し画像とテキストデータが次のように配置されているとします。 -``` + +```sh 001E3C19A3E626EC382F86D201FEFB8C-001_0.jpg 001E3C19A3E626EC382F86D201FEFB8C-001_0.txt 001E3C19A3E626EC382F86D201FEFB8C-003_0.jpg @@ -105,12 +114,13 @@ honkoku_rawdataディレクトリ内に行ごとの切り出し画像とテキ [convertkotensekidata2lmdb.py](./parseqcode/convertkotensekidata2lmdb.py)を実行するとtraindataとvaliddataディレクトリにparseqの学習に利用するlmdb形式のデータセット(data.mdb、lock.mdb)が出力されます。 -``` +```sh python3 convertkotensekidata2lmdb.py ``` 出力されたデータセットは次のコマンドで所定の位置に配置します。 -``` + +```sh mkdir data mkdir data/train mkdir data/train/real @@ -121,20 +131,20 @@ cp validdata/* data/val/ ### 学習 -``` +```sh #1行当たり100文字まで読むモデル python3 train.py --config-name=main_tiny768.yaml #1行当たり50文字まで読むモデル python3 train.py --config-name=main_tiny384.yaml #1行当たり30文字まで読むモデル python3 train.py --config-name=main_tiny256.yaml - ``` -### 学習済モデルのONNXへの変換 +### 文字列認識(PARSeq)学習済モデルのONNXへの変換 + [convert2onnx.py](./parseqcode/convert2onnx.py)の「チェックポイントのパス」を書き換えて実行します。 -``` +```sh python3 convert2onnx.py ```