使用tesseract-ocr实现图片文字识别 docker版

前言

工作需要,有大量图片上的文字需要识别(比如英文小说)。所以打算安装一个Tesseract,然后进行一点orc方面的开发。

docker版tesseract-ocr

通过使用docker镜像,可以省去安装环节,轻松的实现ocr。

单张图片识别,直接启动镜像,将图片文件夹挂载到容器内,调用entrypoint命令识别。

# 拉镜像
docker pull jitesoft/tesseract-ocr:20.04
# 直接指定图片地址,进行识别
docker run -v ~/文档/_04dockervolume/anaconda3/notebooks:/tmp jitesoft/tesseract-ocr:20.04 /tmp/1.png stdout

如果有大量图片需要处理,可以:

cd ~/文档/_04dockervolume/anaconda3
#创建一个init.sh,以便在镜像中执行,这里如果多层文件夹的话,改为函数,递归调用即可。
cat <<EOF > init.sh
#! /bin/bash
indir=\$1
for f in \$indir/*
do
  tesseract \$f stdout
done
EOF

# 输出内容合并到1.txt中
docker run  --rm \
-v ~/文档/_04dockervolume/anaconda3:/sh  \
-v ~/文档/_PIC2BOOK/BOOK4/book/1:/pic   \
--entrypoint /bin/sh \
jitesoft/tesseract-ocr:20.04 \
/sh/init.sh /pic >> 1.txt

根据镜像反推安装过程

我们通过history,反推这个镜像的Dockerfile:

docker history jitesoft/tesseract-ocr:20.04 --format "table {
    
    {.ID}}\t{
    
    {.CreatedBy}}" --no-trunc

# RUN |5 TESSERACT_VERSION=4.1.1 LEPTONICA_VERSION=1.79.0 TESSERACT_VERSION=4.1.1 LEPTONICA_VERSION=1.79.0 TARGETARCH=amd64 /bin/sh -c tar -xzhf /tmp/tess/tess-${TARGETARCH}.tar.gz -C /usr/local  && groupadd -g 472 -r tesseract  && useradd -u 472 -r -g tesseract tesseract  && apt-get update  && apt-get -y install libgomp1 libgif7 libwebpmux3 libwebp6 libopenjp2-7 libpng16-16 libjpeg9 libtiff5 zlib1g wget  && chmod -R +x /usr/local/bin  && chown -R tesseract:tesseract /usr/local/share  && rm -f /usr/local/lib/*.a  && apt-get autoremove -y  && apt-get clean -y

猜你喜欢

转载自blog.csdn.net/weixin_36572983/article/details/105478190