開源gosseract
地址 gosseract
1. 首先下載docker 和docker-compose ,具體的檢視官網注意 docker版本和docker-compose 版本一致
2. gosseract 在原始碼中找到dockerfile 檔案如下(做了稍微的修改,只安裝golang 和 tesseract-ocr/):
##### docker build -f docker_file -t otiai10:0.1 .
# This is a working example of setting up tesseract/gosseract,
# and also works as an example runtime to use gosseract package.
# You can just hit `docker run -it --rm otiai10/gosseract`
# to try and check it out!
#####
FROM golang:latest
LABEL maintainer="Hiromu Ochiai <otiai10@gmail.com>"
RUN apt-get update -qq
# You need librariy files and headers of tesseract and leptonica.
# When you miss these or LD_LIBRARY_PATH is not set to them,
# you would face an error: "tesseract/baseapi.h: No such file or directory"
RUN apt-get install -y -qq libtesseract-dev libleptonica-dev
RUN apt update \
&& apt install -y \
ca-certificates \
libtesseract-dev=4.1.1-2+b1 \
tesseract-ocr=4.1.1-2+b1
# In case you face TESSDATA_PREFIX error, you minght need to set env vars
# to specify the directory where "tessdata" is located.
ENV TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00/tessdata/
# Load languages.
# These {lang}.traineddata would b located under ${TESSDATA_PREFIX}/tessdata.
# 安裝中文
RUN apt-get install -y -qq \
tesseract-ocr-eng \
tesseract-ocr-deu \
#tesseract-ocr-chi_sim \
tesseract-ocr-jpn
#檢查「tesseract」支援的語言
# tesseract --list-langs
# See https://github.com/tesseract-ocr/tessdata for the list of available languages.
# https://github.com/tesseract-ocr/tessdata/tree/4.00 或者下載 https://tesseract-ocr.github.io/tessdoc/Data-Files
# If you want to download these traineddata via `wget`, don't forget to locate
# downloaded traineddata under ${TESSDATA_PREFIX}/tessdata.
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
#RUN go get -t github.com/otiai10/gosseract
RUN /go/src
#RUN cd ${GOPATH}/src/github.com/otiai10/gosseract && go test
# Now, you've got complete environment to play with "gosseract"!
# For other OS, check https://github.com/otiai10/gosseract/tree/main/test/runtimes
3. 在Dockerfile 目錄下面編譯映象檔案,命令如下(注意如果檔案不是Dockerfile 需要 -f xxxxfile 指定編排檔案)
docker build -t orc .
4. 下載過來檢視映象
docker images
5. 從官網下載原始碼執行,新建 docker-compose.yml 檔案建立網路
docker network create go_app_orc
編排 掛在你的專案目錄 orc_test 掛在到容器裡面 /go/src/orc_test
version: '3'
# docker network create go_app_orc
# docker cp /Users/gitxuzan/Downloads/chi_sim.traineddata orc:/usr/share/tesseract-ocr/4.00/tessdata
# docker exec -it orc_v2 bash
services:
golang:
image: orc
container_name: orc
networks:
- web
volumes:
- "../orc_test:/go/src/orc_test"
tty: true
environment:
TZ: "Asia/Shanghai"
networks:
web:
external:
name: go_app_orc
6. 啟動容器
docker-compose up -d
7. 進入容器
docker exec -it orc bash
8. 進入掛載專案執行
程式碼如下:
package main
import (
"fmt"
"github.com/otiai10/gosseract/v2"
"log"
)
func main() {
client := gosseract.NewClient()
defer client.Close()
//client.SetLanguage("eng","chi_sim")
//client.SetLanguage("chi_sim")
client.SetImage("test2.png")
text, _ := client.Text()
fmt.Println(text)
// Hello, World!
}
我用的go mod 管理的 go mod tidy , go run main.go,識別結果如下
9. 最後如果需要識別中文,需要下載中文包拷貝到容器
中文包下載地址1
中文包下載地址2
拷貝到容器
docker cp /Users/gitxuzan/Downloads/chi_sim.traineddata orc:/usr/share/tesseract-ocr/4.00/tessdata
# 檢視是否有中文包
tesseract --list-langs
設定識別語言
```go
package main
import (
"fmt"
"github.com/otiai10/gosseract/v2"
"log"
)
func main() {
client := gosseract.NewClient()
defer client.Close()
client.SetLanguage("eng","chi_sim")
client.SetImage("test2.png")
text, _ := client.Text()
fmt.Println(text)
// Hello, World!
}
識別中文結果:
現在存在的問題[^1]
[^1]: 4.0.0 不能設定白名單,需要升級成4.1.1
我的部落格地址 blog.csdn.net/qq_36517296/article/...
本作品採用《CC 協議》,轉載必須註明作者和本文連結