Skip to content

Commit 83762d2

Browse files
[Add] easyOCR local module
1 parent a3dd984 commit 83762d2

154 files changed

Lines changed: 502766 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Framework/easyocr/DBNet/DBNet.py

Lines changed: 766 additions & 0 deletions
Large diffs are not rendered by default.

Framework/easyocr/DBNet/README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# DBNet - Inference Only
2+
This text detection module is adapted from [DBNet++](https://github.com/MhLiao/DB).
3+
4+
## 1. Overview
5+
DBNet works as an image segmentation which performs classification at pixel-level. The model classifies if each pixel from the input image is a part of a text region. This module uses dynamic import and class construction from a config file. Config files are expected to be found in `./configs/`. At the input, the input image is expected to have width and height as multiple of 32. Input images that does not have these dimension will be resized accordingly. In addition, minimum and maximum sizes can be specified in the config file.
6+
7+
### 1.1) Terminology
8+
* Probability Heatmap: A tensor represents classification confidence of each pixel for being a part of a text region.
9+
* Segmentation: A boolean-like tensor represents region that is determined as being a text region.
10+
* text_threshold: A threshold for each element of the probability heatmap to be considered as a text region.
11+
* detection_size: This term is used to refer to the size of the image on which the detection routine will be performed. Input images that are not of this size will be resized accordingly.
12+
13+
### 1.2) Changes from the original repo
14+
1. Scripts inside `./concerns/` and multiple `.yaml` files are consolidated and pruned for inference-only implementation and dependencies reduction.
15+
2. DCN operators, which are required to be compiled with Ahead-of-Time (AoT) in the original repo, are changed to compile with Just-in-Time (JIT) approach as the default. AoT approach is still support.
16+
3. DCN CPU version is provided in addition to the CUDA version from the original repo.
17+
4. Pretrained weights are renamed for easy referring and adding file extension.
18+
19+
| Original name | New name |
20+
|-----------------------------------------------------|---------------------------|
21+
|synthtext_finetune_ic15_res18_dcn_fpn_dbv2 |pretrained_ic15_resnet18.pt|
22+
|synthtext_finetune_ic15_res50_dcn_fpn_dbv2_thresh0.25|pretrained_ic15_resnet50.pt|
23+
24+
25+
## 2. Using and Compiling DCN operators
26+
DBNet requires DCN operators to be compiled. There are two versions of DCN provided; CPU version and CUDA version (original). CUDA version works significantly faster, but requires CUDA-support GPU and CUDA developer toolkit. The CPU version can work without GPU and CUDA. The compilation prerequisites and instruction can be found below.
27+
28+
Please not that, EasyOCR **can work** without DBNet and DCN operators by using CRAFT text detection (the default detector module).
29+
30+
### 2.1) Prerequisites
31+
##### CPU version
32+
* GCC compiler > 4.9
33+
34+
##### CUDA version
35+
* GCC compiler > 4.9
36+
* [CUDA Developer Toolkits](https://developer.nvidia.com/cuda-toolkit) > 9.0 (Tested on 11.3).
37+
38+
### 2.2) Installing Dependencies
39+
40+
Some step-by-step procedure to install the prerequisites is listed below. Please note that there are other methods that work as well. These methods are listed only to serve as a guideline.
41+
42+
#### Installing GCC Compiler
43+
*Step 1*: Check if your machine already has GCC installed.
44+
45+
On command line terminal (Linux/Mac/Windows);
46+
```
47+
> gcc --version
48+
```
49+
If you already have GCC installed, it will report the version of GCC on your machine. If the command gives an error message along the line of command not found, it implies you do not have GCC installed.
50+
51+
*Step 2*: Install GCC.
52+
53+
To install GCC, you can do one of the following commands, depending on the privileges of your user account on your machine (Linux/Debian/Ubuntu)
54+
```
55+
> apt-get install build-essential
56+
```
57+
or
58+
```
59+
> sudo apt-get install build-essential
60+
```
61+
For Mac and Windows users, please follow the respective official instructions.
62+
63+
*Step 3*: Verification
64+
Repeat Step 1 to make sure that you now have GCC installed.
65+
66+
#### Installing CUDA and NVCC Compiler
67+
*Step 1*: Check if your machine already has NVCC and CUDA toolkit installed.
68+
On command line terminal (Linux/Mac/Windows);
69+
```
70+
> nvcc --version
71+
```
72+
If you already have NVCC installed, it will report the NVCC version on your machine. If the command gives an error message along the line of command not found, it implies you do not have NVCC installed.
73+
74+
*Step 2*: Install NVCC and CUDA developer toolkit.
75+
76+
Option 1: The official instruction can be found [here](https://developer.nvidia.com/cuda-downloads).
77+
78+
Option 2:
79+
Alternatively, you can try install NVCC with [conda](https://docs.conda.io/projects/conda/en/latest/index.html) (package management system and environment management system).
80+
81+
To use conda to install NVCC, you can do;
82+
*Linux/Mac/Windows*
83+
```
84+
> conda install -c conda-forge cudatoolkit-dev
85+
```
86+
Note that the above command may fail if your machine is missing some library, such as libxml2. If such error occurs, please install the missing libraries and try again.
87+
88+
*Step 3*: Verification
89+
Repeat Step 1 to make sure that you now have NVCC installed.
90+
91+
#### Installing conda
92+
Step 1: Check if your machine already has conda installed.
93+
On command line terminal, (Linux/Mac/Windows)
94+
```
95+
> conda --version
96+
```
97+
If you already have conda installed, it will report the version of conda on your machine. If the command gives an error message along the line of command not found, it implies you do not have conda installed.
98+
99+
Step 2: Install conda
100+
Please follow the [official instruction](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html) to install it according to your OS.
101+
102+
Step 3: Verification
103+
Repeat Step 1 to make sure that you now have conda installed.
104+
105+
#### Using Docker image
106+
For Docker users, please use development level images. For example, pytorch/pytorch:x.xx.x-cudax.x-cudnnx-devel. You can verify if all prerequisites are provided by the image by checking
107+
```
108+
gcc --version
109+
```
110+
and
111+
```
112+
nvcc --version
113+
```
114+
115+
If you already have GCC/NVCC installed, each command will report the version of GCC/NVCC on your machine. If the command gives an error message along the line of command not found, it implies you do not have GCC/NVCC installed.
116+
117+
### 2.3 Compiling DCN Just-in-Time (JiT)
118+
Once all of the prerequisites have been installed, you can start using `dbnet18` as the detect_network for EasyOCR. The module will compile the source codes automatically when needed. The compilation may take a while if the modules are being loaded and compiled for the first time.
119+
120+
### 2.4 Compiling DCN Ahead-of-Time (AoT)
121+
You can also try compiling DCN with Ahead-of-Time approach. The following procedure may serve as a guideline.
122+
123+
#### 2.4.1 Locate EasyOCR and DBNet module inside it
124+
Start python console environment of your choice, such as Jupyter notebook and Spyder IDE. You can also start one from command line interface (Linux/Mac terminal, etc.) by calling `python` or `python3`;
125+
126+
In python console environment;
127+
```
128+
> import os
129+
> import easyocr
130+
> print(os.dirname(easyocr.__file__))
131+
```
132+
This should show the installation location of easyocr on your machine.
133+
134+
The exact output of the above command depends on many factors and will be likely unique for each user, especially the `username`. As an example, let's assuming the command above returns something like;
135+
```
136+
> /home/username/anaconda3/lib/python3.8/site-packages/easyocr
137+
```
138+
139+
We want to go into the directory where `DBNet` and the DCN source files are located within EasyOCR which can be done by appending `DBNet/assets/ops/dcn` to the path obtained above. For example;
140+
```
141+
/home/username/anaconda3/lib/python3.8/site-packages/easyocr/DBNet/assets/ops/dcn
142+
```
143+
Access the above directory with any File Manager app on your machine of your choice, for example, Explorer (Windows), Nautilus (Linux/GNOME), Finder (MAC). Or use the following command in the command line interface;
144+
```
145+
> cd /home/username/anaconda3/lib/python3.8/site-packages/easyocr/DBNet/assets/ops/dcn
146+
```
147+
148+
#### 2.4.2 Compiling DCN operator manually with setup.py script
149+
150+
First go to DCN operator subdirectory inside DBNet module inside EasyOCR directory (e.g. `/home/username/anaconda3/lib/python3.8/site-packages/easyocr/DBNet/assets/ops/dcn`) by;
151+
152+
Verify that a script `setup.py` is found in that directory. (This version of `setup.py` script is different from the original version from [DBNet++](https://github.com/MhLiao/DB) since the support for CPU has been added.) Once the script is located, run the following command;
153+
```
154+
> python setup.py build_ext --inplace
155+
```
156+
This will start the compilation process and you can monitor the progress, including error messages, if any, on the command line interface. If there is any error, please resolve them, and try again. Once the compilation has been completed, new files will be added to the current directory (i.e. `/home/username/anaconda3/lib/python3.8/site-packages/easyocr/DBNet/assets/ops/dcn`). If your machine has only CPU, but no CUDA device (GPU), two files will be added to the directory. **Please note that the exact names of the files will be different depending on the configuration of your machine.** The file names should look like;
157+
```
158+
deform_conv_cpu.******.so
159+
deform_pool_cpu.******.so
160+
```
161+
If your machine also has CUDA device, two additional files will be added (4 files in total). The file names of these files should look like;
162+
```
163+
deform_conv_cuda.******.so
164+
deform_pool_cuda.******.so
165+
```
166+
167+
### 3. Using DBNet Detector
168+
When initializing EasyOCR with DBNet as the detect network for the first time in the current working session, messages will be print to indicate if the DCN operators are loaded from objects compiled with AoT approach (pre-compiled) or the source codes are compiling with JiT approach.
169+
170+
171+
172+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .functions.deform_conv import deform_conv, modulated_deform_conv
2+
from .functions.deform_pool import deform_roi_pooling
3+
from .modules.deform_conv import (DeformConv, ModulatedDeformConv,
4+
DeformConvPack, ModulatedDeformConvPack)
5+
from .modules.deform_pool import (DeformRoIPooling, DeformRoIPoolingPack,
6+
ModulatedDeformRoIPoolingPack)
7+
8+
__all__ = [
9+
'DeformConv', 'DeformConvPack', 'ModulatedDeformConv',
10+
'ModulatedDeformConvPack', 'DeformRoIPooling', 'DeformRoIPoolingPack',
11+
'ModulatedDeformRoIPoolingPack', 'deform_conv', 'modulated_deform_conv',
12+
'deform_roi_pooling'
13+
]

Framework/easyocr/DBNet/assets/ops/dcn/functions/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)