0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fast API + paddleocr + DockerでOCRを試してみる

Last updated at Posted at 2025-03-25

はじめに

個人開発でOCRを試してみたかったので、
Fast API + paddleocr + Dockerで環境構築してみました。

flutterから叩くWEB APIとして、ライブラリの豊富さと速度からFastAPIを勉強がてらに選定

ディレクトリ構成

.
│
├── docker-compose.yml
├── src
│   ├── __pycache__
│   │   └── main.cpython-310.pyc
│   ├── main.py
│   └── receipt_test.jpg
└── web
    ├── Dockerfile
    └── requirements.txt

Dockerfile

FROM python:3.10-slim

WORKDIR /app

RUN apt-get update &&\
    apt-get install -y libgomp1 libgl1 libglib2.0-0

COPY web/requirements.txt .

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt



COPY /web  .

CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8080"]

requirements.txt

requirements.txt
fastapi
uvicorn
paddlepaddle
pydantic
opencv-python-headless
paddleocr

main.py

お試しでいろいろ試したので、ゴミ残ってますが。

main.py
import base64
import numpy as np
import io
from typing import Union
from fastapi import FastAPI
from paddleocr import PaddleOCR, draw_ocr


app = FastAPI()

@app.post("/")
def read_root():

    ocr = PaddleOCR(lang="japan")

    result = ocr.ocr('./receipt_test.jpg')

    for idx in range(len(result)):
        res = result[idx]
        for line in res:
            print(line)

    return {"Hello": "World"}

docker-compose.yml

docker-compose.yml

services:
  web: 
    build: 
      context: .
      dockerfile: ./web/Dockerfile
    ports:
      - "8080:8080"
    environment:
      - DATABASE=postgresql
      - DB_USER=root
      - DB_PASSWORD=password
      - DB_HOST=
      - DB_PORT=
      - DB_NAME=receibo 
    volumes:
      - ./src:/app

動作確認

$ docker compose up -d

receipt_test.jpgは適当な画像を配置してください。

APIを動作させて

$ docker logs [コンテナ名]

以上で動作確認が取れました。

自分はセブンイレブンのレシートで試しましたが、
「セブン-イレブン」→「セフンーイレブツ」 
「ホットコーヒー L」→「ホットコ一ヒーレ」
「メビウスブラックCMプルーム用」→「メヒ`ウスフラックCMフプルーム用」
日本語対応させると、アルファベットと濁点半濁点がすこし弱めな印象です。

Cloud visionを使用したところ

領収書
7カフェ ホットコーヒーL
*167
メビウスブラックCMプルーム用
500
小計 (税抜 8%)
¥167
消費税等 (8%)
¥13
小計 (税込10%)
合計
¥500
¥680
(税率 8% 対象
¥180)
(税率 10% 対象
¥500)
(内消費税等 8%
¥13)
(内消費税等10%
¥45)

ここまできれいに取れました。

GOAT

以上。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?