Giter Club home page Giter Club logo

easyocr-cpp's Introduction

EasyOCR-cpp

alt text

Custom C++ implementation of EasyOCR

Built and tested on Windows 11, libtorch 1.13+cpu and OpenCV 4.6

This C++ project implements the pre/post processing to run a OCR pipeline consisting of a text detector CRAFT, and a CRNN based text recognizer. Unlike the EasyOCR python which is API based, this repo provides a set of classes to show how you can integrate OCR in any C++ program for maximum flexibility. The torchExample.cpp main program highlights how to utilize all elements of the EasyOCR-cpp pipeline. Because a test program is only provided, make sure to configure your input image within torchExample.cpp if you only plan to utilize the test program.

thrown together quickly within a week so many debug comments :)

Libtorch is being utilized with an in-house class I usually use for C++ inference TorchModel, and OpenCV for the pre/post processing steps. The TorchModel class can easily adapted to run inference on most Pytorch models converted to TorchScript. Provides some handy functions to pre process opencv::Mat and handle device usage (GPU,CPU). Great starting point for C++ based inference for Pytorch Models.

Some features that have yet to be implemented:

  • beam search, only implemented greedy decoding
  • .txt/.pdf output
  • exact bounding box merge alg from EasyOCR, opted for custom one which is less complex
  • support for other languages, atm only english is supported.

If you would like to support feel free to make a PR, or a issue if you are having trouble.

Setup with Docker (Thanks to @BBO-repo )

Docker container with VSCode

To build and run through visual studio code, make sure the remote development extension is installed.

The repository provides a .devcontainer.json which should allow you to directly reopen the cloned folder in a docker container using the bottom left icon and select Reopen in containeras illustrated below vs-open-in-container

When the container is finally built, you can open a terminal and your working folder should be /workspace/ocr-c++ as mentionned .devcontainer.json the field "workspaceFolder": "/workspaces/ocr-c++". You should have the two folders:

  • thirdparty containing the automatically downloaded libtorch library
  • EasyOCR-cpp containing the code

You can proceed as usual to build the cmake project

mkdir -p /workspaces/ocr-c++/EasyOCR-cpp/build
cd /workspaces/ocr-c++/EasyOCR-cpp/build
cmake ..
make

The binaries should be available in build folder, to run the example application just run:

cd /workspaces/ocr-c++/EasyOCR-cpp/build
./torchTest

Docker container with command line

Considering that the repository was cloned in the folder /my/working/directory/EasyOCR-cpp for illustration.

Build the docker container

Build a docker image named for example ocr_engine from the Dockerfile inside the /my/working/directory/EasyOCR-cpp directory.

docker build --progress=plain -t ocr_engine /my/working/directory/EasyOCR-cpp

Build a container named for example EasyOCR-cpp from the ocr_engine generated image

docker create --name EasyOCR-cpp --mount type=bind,source=/my/working/directory/EasyOCR-cpp,target=/workspaces/ocr-c++/EasyOCR-cpp ocr_engine:latest

You can now start, stop or restart the generated EasyOCR-cpp container.
From a terminal, enter the following command to start the container and getting a terminal inside the container with an interactive mode

docker container start EasyOCR-cpp
docker exec -ti EasyOCR-cpp bash

You should have a bash terminal inside the container, entering pwd and ls commands should output the following:

pwd
  /workspaces/ocr-c++
ls
  thirdparty EasyOCR-cpp

Similarly to previously to build cmake project, go to source code folder cd /workspaces/ocr-c++/EasyOCR-cpp, make a folder build and go inside, then cmake .. and make

cd /workspaces/ocr-c++/EasyOCR-cpp
mkdir build
cd build
cmake ..
make

Then similarly, the binaries should be available in build folder, to run the example application just run:

cd /workspaces/ocr-c++/EasyOCR-cpp/build
./torchTest

Setup to run without Docker

Dependencies

Click to Download libtorch - > download

Use OpenCV Windows installer and unzip (v4.6) - > OpenCV libs

Make sure to change the location in the Makefile for OpenCV to point to your OpenCV build dir

Create a build directory within the repo, cd to it and run

cmake -DCMAKE_PREFIX_PATH=<absolute-path-to-libtorch-folder> ..

This will generate a solution within the build folder you can open up in Visual Studio. Make sure to use the Release config when building

Running

Configure your input image here. Currently the test program is using the test.jpg which comes in the repo.

Launch from command-line, or within Visual Studio after building.

Since its designed to be used in a C++ program, text is not being written to disk at the moment An output image will be generated in the main repo dir containing an annotated version of the input image with detection bounding boxes

easyocr-cpp's People

Contributors

ksasso1028 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

easyocr-cpp's Issues

How do you export the EasyOCR models to TorchScript format?

I would like to deploy EasyOCR models using libtorch, and saw your repo does this, which is excellent.

But the official EasyOCR models are not in TorchScript format needed by this software.

Do you have a conversion script or similar?

Specifically, I am having a hard time exporting the recognizer model - I don't know how to create the input tensor.

Best regards,
Mikael

dockerization

Hello,
Thanks for your repo! You really did what you said there.
Some suggestions that I have is that your repo is too much dependent on your local computer configuration, I thought a Dockerfile to play with your repo would be nice to have.
If you think it is valuable to add it I can add my Dockerfile, it automatically gets all dependencies: get libtorch and build latest OpenCV and build the application.

Also I've tried your code with get but got an error, seems that some tensor are in CPU while other are in GPU. Let's dig this point later
I would have some C++ specific comments on the code but let it for later, a PR was requested to solve a linux build.

output text doesn't match with actual image

I run the torchTest program with input is test.jpg in the repo but the output doesn't match with actual image. Each character is shifted to right one unit.
Ex: text in image is THREE, output is UISFF
T -> U
H -> I
R -> S
E -> F
E -> F

How to ise GPU?

Hello, nice project. How can I run it with GPU support (have already installed lobtorch with cuda special version that I have on pc)?

output do not make sense in Linux

Hi,
could you please help me to understand what is happening? This is the output I obtain when I run it in linux. The text detected completely messed up.

0 [blank]
95 z
94 y
93 x
92 w
91 v
90 u
89 t
88 s
87 r
86 q
85 p
84 o
83 n
82 m
81 l
80 k
79 j
78 i
77 h
76 g
75 f
74 e
73 d
72 c
71 b
70 a
69 Z
68 Y
67 X
66 W
65 V
64 U
63 T
62 S
61 R
60 Q
29 =
28 <
27 ;
26 :
25 /
24 .
23 -
22 ,
21 +
20 *
19 )
18 (
17 '
16 &
15 %
14 $
1 0
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
11 !
12 "
13 #
30 >
31 ?
32 @
33 [
34
35 ]
36 ^
37 _
38 `
39 {
40 |
41 }
42 ~
43
44 A
45 B
46 C
47 D
48 E
49 F
50 G
51 H
52 I
53 J
54 K
55 L
56 M
57 N
58 O
59 P
MODEL TIME 45.7036 ms
LOCATION: [0, 1] [20, 20]
TEXT: Q
CONFIDENCE 0.999953
################################################
LOCATION: [21, 5] [68, 18]
TEXT: IPMPOJ
CONFIDENCE 0.999209
################################################
LOCATION: [71, 5] [108, 18]
TEXT: UISFF
CONFIDENCE 0.999888
################################################
LOCATION: [115, 5] [349, 18]
TEXT: 490054792 Pqfofe cw QXIpmpo NEDbsejp
CONFIDENCE 0.995813
################################################
LOCATION: [13, 29] [38, 40]
TEXT: Ubdl
CONFIDENCE 0.999953
################################################
LOCATION: [53, 29] [78, 42]
TEXT: Feju
CONFIDENCE 0.999371
################################################
LOCATION: [91, 29] [118, 40]
TEXT: Kjfm
CONFIDENCE 0.0257791
################################################
LOCATION: [135, 29] [176, 42]
TEXT: Qbujfou
CONFIDENCE 0.999988
################################################
LOCATION: [191, 29] [222, 42]
TEXT: Dibsu
CONFIDENCE 0.999963
################################################
LOCATION: [237, 29] [266, 40]
TEXT: Mjolf
CONFIDENCE 0.999889
################################################
LOCATION: [281, 29] [353, 40]
TEXT: Opujgjdbujpot
CONFIDENCE 0.982506
################################################
LOCATION: [367, 29] [427, 42]
TEXT: Obwjrbujpo
CONFIDENCE 0.996567
################################################
LOCATION: [443, 29] [473, 44]
TEXT: Ifmq
CONFIDENCE 0.998373
################################################
LOCATION: [15, 55] [28, 68]
TEXT: J
CONFIDENCE 0.010791
################################################
LOCATION: [29, 55] [64, 66]
TEXT: Ipnf
CONFIDENCE 0.987937
################################################
LOCATION: [89, 55] [168, 66]
TEXT: Dbsejpwbtdvmbs
CONFIDENCE 0.999805
################################################
LOCATION: [177, 55] [186, 68]
TEXT: Jb
CONFIDENCE 0.00381894
################################################
LOCATION: [191, 55] [377, 70]
TEXT: Qbujfou Mjtu Qfsjpqfsbujwf Usbdljoh
CONFIDENCE 0.999996
################################################
LOCATION: [401, 55] [603, 66]
TEXT: Qspwjefs Iboepgg F Nfttbef Dfoufs
CONFIDENCE 0.999982
################################################
LOCATION: [624, 55] [722, 66]
TEXT: Ewobnjd Xpslmjtu
CONFIDENCE 0.999048
################################################
LOCATION: [742, 55] [858, 68]
TEXT: Sfgfssbm Nbobhfnfou
CONFIDENCE 0.997839
################################################
LOCATION: [882, 55] [958, 68]
TEXT: NzFyqfsjfodf
CONFIDENCE 0.999368
################################################
LOCATION: [15, 81] [28, 94]
TEXT: M
CONFIDENCE 0.999945
################################################
LOCATION: [29, 79] [86, 94]
TEXT: VqUpEbuf
CONFIDENCE 0.999511
################################################
LOCATION: [29, 107] [56, 120]
TEXT: EBa
CONFIDENCE 0.999826
################################################
LOCATION: [13, 133] [28, 146]
TEXT: XpMb
CONFIDENCE 0.0275535
################################################
LOCATION: [35, 133] [74, 144]
TEXT: Qspop
CONFIDENCE 0.999905
################################################
LOCATION: [89, 133] [130, 144]
TEXT: Lcopo
CONFIDENCE 0.990057

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.