Comments (48)
In case it is helpful to others looking for a solution, posting below a bash script I use for -
- taking a scanned page image,
- running tesseract with hocr option on it,
- running hocr tools to split it into lines.
The ground truth needs to be updated manually, if there is an existing page level ground truth file, copy line by line into the lines ground truth.
#!/bin/bash
SOURCE="./myfiles/"
lang=san
set -- "$SOURCE"*.png
for img_file; do
echo -e "\r\n File: $img_file"
OMP_THREAD_LIMIT=1 tesseract --tessdata-dir ../tessdata_fast "${img_file}" "${img_file%.*}" --psm 6 --oem 1 -l $lang -c page_separator='' hocr
source venv/bin/activate
PYTHONIOENCODING=UTF-8 ./hocr-extract-images -b ./myfiles/ -p "${img_file%.*}"-%03d.exp0.tif "${img_file%.*}".hocr
deactivate
done
rename s/exp0.txt/exp0.gt.txt/ ./myfiles/*exp0.txt
echo "Image files converted to tif. Correct the ground truth files and then run ocr-d train to create box and lstmf files"
from tesstrain.
Thank you. I think the following workflow will do the trick.
./ocropus-nlbin bookpages/*.png -o book
./ocropus-gpageseg 'book/????.bin.png'
./ocropus-gtedit html -f 20 -H 48 ./book/*/*.png
writing correction.html
Transfer and browse correction.html on Windows. Add the ground truth text for each line image. Save HTML as complete webpage. Transfer file back to Linux.
./ocropus-gtedit extract -p bookgt correction.html
from tesstrain.
In my case to make it run, have made some minor changes in @Shreeshrii script, I put the image file of page in myfiles and run the script with bash generate_training_data.sh
- Created training virtualenv
- sudo apt-get install hocr-tools
- sudo apt-get install rename
#!/bin/bash
SOURCE="./myfiles/"
lang=eng
set -- "$SOURCE"*.jpg
for img_file; do
echo -e "\r\n File: $img_file"
OMP_THREAD_LIMIT=1 tesseract "${img_file}" "${img_file%.*}" --psm 6 --oem 1 -l $lang -c page_separator='' hocr
source training_env/bin/activate
PYTHONIOENCODING=UTF-8 hocr-extract-images -b ./myfiles/ -p "${img_file%.*}"-%03d.exp0.tif "${img_file%.*}".hocr
deactivate
done
rename s/exp0.txt/exp0-gt.txt/ ./myfiles/*exp0.txt
echo "Image files converted to tif. Correct the ground truth files and then run ocr-d train to create box and lstmf files"
Special thanks to Shreeshri!
from tesstrain.
you can also use tesseract for creating a hocr output and then use hocr-extract-images to create the line images and texts.
@zuphilip Thank you. I was able to use it for Devanagari script files also. The commands which worked for me (it took a little experimenting to get it right).
:~/hocr-tools$ PYTHONIOENCODING=UTF-8 ./hocr-extract-images -b ./shree/ -p ./shree/san.pothi-%03d.png ./shree/Mudgala-Test-01.hocr
:~/hocr-tools$ PYTHONIOENCODING=UTF-8 ./hocr-extract-images -b ./shree/ -p ./shree/san.pothi-%03d.tif ./shree/Mudgala-Test-01.hocr
from tesstrain.
I want to run it on my system but am confused on what to change
Assuming that you have tesseract and hocr-tools installed, put your image (png) files in ./myfiles/ folder. Change lang=san in the bash script to whichever language you need eg. lang=eng
save and run the bash script.
for each image file
runs tesseract on image file to produce hocr output
runs hocr-extract-images to split the image to line images with the OCRed text for the line
done
rename generated text file from *.txt to *.gt.txt
Correct command will be the following (. instead of - in filename)
rename s/exp0.txt/exp0.gt.txt/ ./myfiles/*exp0.txt
After this the *.gt.txt files need to be manually corrected to match the line images.
from tesstrain.
Nitpick:
rename s/exp0.txt/exp0-gt.txt/ ./myfiles/*exp0.txt
should be
rename s/exp0.txt/exp0.gt.txt/ ./myfiles/*exp0.txt
from tesstrain.
In case it is helpful to others looking for a solution, posting below a bash script I use for -
- taking a scanned page image,
- running tesseract with hocr option on it,
- running hocr tools to split it into lines.
The ground truth needs to be updated manually, if there is an existing page level ground truth file, copy line by line into the lines ground truth.
#!/bin/bash SOURCE="./myfiles/" lang=san set -- "$SOURCE"*.png for img_file; do echo -e "\r\n File: $img_file" OMP_THREAD_LIMIT=1 tesseract --tessdata-dir ../tessdata_fast "${img_file}" "${img_file%.*}" --psm 6 --oem 1 -l $lang -c page_separator='' hocr source venv/bin/activate PYTHONIOENCODING=UTF-8 ./hocr-extract-images -b ./myfiles/ -p "${img_file%.*}"-%03d.exp0.tif "${img_file%.*}".hocr deactivate done rename s/exp0.txt/exp0.gt.txt/ ./myfiles/*exp0.txt echo "Image files converted to tif. Correct the ground truth files and then run ocr-d train to create box and lstmf files"
For a simpler and more efficient way, I recommend gnu parallel. The above stuff becomes 2 lines. First generate the hocr files:
parallel --bar -j 4 'OMP_THREAD_LIMIT=1 tesseract {} {/.} --psm 4 --oem 1 -l eng hocr' ::: *.png
Then extract the tif/txt pairs:
parallel --bar -j 4 'hocr-extract-images {} -p {/.}-%03d.tif' ::: *.hocr
Even faster (around 10% for me) if you recompile tesseract without openMP (./configure --disable-openmp)
from tesstrain.
You would have to align them manually or semi-automatically (i.e. you could try to OCR the images to get the line segmentation and than heuristically match the text on the lines) on the line level. Tesseract text recognition has to be trained on the level of lines. No other way (cf. e.g. https://ieeexplore.ieee.org/abstract/document/6628705).
from tesstrain.
Unfortunately, not yet. we are working on something in this direction to align the fulltexts from the German Text Archive with the corresponding images. Hopefully, I can get back to you soon with some tool.
Additionally, @jbaiter does some things along these lines...
from tesstrain.
Thanks. It will be a useful tool.
I am trying to use some ocropus tools to split the page into line images. I will either ocr the line images to create text to be corrected for ground truth, or type it fully,
from tesstrain.
@Shreeshrii , you could try this approach:
- Split the page image into line images with ocropus/kraken
- Run the most suitable OCR model on the line images
- For each line in the resulting OCR, find the ground truth line with the lowest edit distance (e.g. Levenshtein)
- Every matching line with an edit distance above a certain threshold should have fairly high chance of being a correct match
One problem with this approach is that segmentation errors (e.g. a line gets cut in two, a few words at the beginning/end are missing, etc) lead to false positives.
This also assumes that your ground-truth is split into lines. If not, you will have to modify step 3 to slide each OCR line over the ground truth and determine the best match that way, with some added heuristics to not match partial words, etc.
from tesstrain.
I want to use it for Devanagari script. I had looked at ocropus quite sometime back. I am not sure if ocropus/kraken supports Devanagari.
Do you know if it has support for complex scripts?
from tesstrain.
@Shreeshrii There are some papers with text recognition results with Ocropus on Devanagari script. However, I am not aware of any shared model you could reuse. You can find some models for Ocropus here https://github.com/tmbdev/ocropy/wiki/Models
However, instead of 1.+2. you can also use tesseract for creating a hocr output and then use hocr-extract-images to create the line images and texts.
Moreover, if you have the ground truth in hocr format you can use hocr-eval for the evaluation with your recognition format. Or, do you have the ground truth only as a text with the geometric information?
from tesstrain.
@zuphilip I have also read about Devanagari training for ocropus but the models are not available (I had looked couple of years ago or so).
Thank you for the link to specific HOCR tools. I will give them a try.
The ground truth I have are plain text files matching the scanned images without any positional info. I was able to use them to eval OCR accuracy by comparing to recognized output.
from tesstrain.
https://github.com/Shreeshrii/imagessan/tree/master/groundtruthimages
Sanskrit language samples in Devanagari script.
from tesstrain.
Ping @adnanulhasan who may still have some sources from the Ocropus training with Deviangari script texts.
from tesstrain.
The other option which I had used was
# perform binarization
./ocropus-nlbin tests/devatest?.png -o devatest -n -g
# perform page layout analysis
./ocropus-gpageseg 'devatest/????.bin.png' -n
And then running tesseract to get text and correcting it.
from tesstrain.
Occasionally, the line images are a bit wider than the text and so they catch the letters from the preceding or the subsequent lines. Is this a problem for the training (i.e. should such images be fixed to ensure that they do not contain top/bottom of the neighbouring lines)?
from tesstrain.
I think this is a problem. It would be great if you could provide a corresponding example, maybe in a specific GitHub issue. Many thanks in advance!
from tesstrain.
Please see tesseract-ocr/tesseract#2231 for the WordStr format box files.
from tesstrain.
@bertsky: Concerning the comment by @SultanOrazbayev, clipping may help here, right? Is it possible, to get polygonal line shapes from tesseract?
from tesstrain.
It is possible to get polygon-based segmentation from Tesseract: with BlockPolygon
from the page iterator delivered by AnalyseLayout
. There is a bug somewhere though: sometimes, paths self-intersect, which even Tesseract itself does not cope with very well (as can be seen by the mask images produced internally, available with GetImage
when also passing the raw image again). Maybe by postprocessing one can circumvent this issue – using shapely.geometry
functions to self-disjoin paths, or similar.
But even without polygon masked line images you could try clipping to rid of the intrusions from neighbours, yes. Or alternatively, do resegmentation (i.e. increase coherence via another line segmentation). Both methods are already available as OCR-D processors, as is Tesseract region segmentation (optionally with polygons).
But you want line segmentation with polygons here, right? I am afraid Tesseract's API does not offer that – only for the "block" level!
Should I give details (what/where/how) on using clipping and resegmentation?
from tesstrain.
Hi,
I am using ocr-D for preparing traindata and i am try to extract data from dot matrix font pdf.i created some sample in dot matrix tif image and gt.txt then i am using tesseract to extract my pdf but it extract only some letters and some time its consider 0 as 8.please give solution to fix this issue
from tesstrain.
@kabilankiruba This is clearly not related to this thread. Pls. consider to contact the Tesseract user group.
from tesstrain.
Is there any tool which will display the line images and gt.txt side by side for easy correction after generating the files from HOCR output (as suggested here).
I do not want to run a web server to do this.
Can it be done via javascript/html - show an image and its gt.txt - save corrected gt.txt and have an arrow/option to display next image and gt.txt.
Basically, i would like to run this on my windows10 desktop.
from tesstrain.
@kba @cneud @stweil Can you recommend a tool for this purpose? Wasn't there such a thing in OCRopy?
from tesstrain.
https://github.com/OpenArabic/OCR_GS_Data/blob/master/_doublecheck_viewer.py creates HTML5 based webpage for Reviewing OCR Training/Testing Data.
from tesstrain.
Can you recommend a tool for this purpose?
- ocropus-gtedit
- ketos transcribe (in kraken)
- https://github.com/qurator-spk/neath (server based)
- https://github.com/UB-Mannheim/ocr-gt-tools (server-based ocropus-gtedit)
Can it be done via javascript/html - show an image and its gt.txt - save corrected gt.txt and have an arrow/option to display next image and gt.txt.
Both kraken's and ocropy's transcription do that. the hocrjs viewer has an option to make items contenteditable
but no way to save it.
from tesstrain.
@Shreeshrii could you please clarify how you match the extracted ground truth txt files from ocropy/ocropus with the line level images obtained with your script? After using ocropus-nlbin, the original filename is "lost" (ocropus uses numerical increasing values).
Using tesstrain, I assume that you don't train tesseract on the line level images and gt obtained with ocropus? These images are slightly different compared to the line images obtained with your script (which uses tesseract directly) because of preprocessing with ocropus-nlbin. But please correct me if I am wrong.
I am confused what the current workflow is to correct the extracted ground truth:
- Using just your script and edit the gt files manually
- Use only ocropy
- Combine your script together with the tools from ocropus?
- Change the filenames to the same numerical increasing values that ocropus uses
- Run script
- Work with ocropus commands on the line images obtained from your script
- use the line images obtaind from tesseract script with the edited gt from ocropus
from tesstrain.
@fjp These are two different approaches.
I have used both separately, only on experimental basis, mostly for testing.
from tesstrain.
Hello,
are there still any plans to integrate some kind of tool into tesstrain?
I was facing similar requirements for generation of training data in a windows-env, which ended up in a small Script that extracts both coords and textdata from an existing ALTO-file and writes training-data-pairs.
from tesstrain.
@M3ssman This would be a great contribution. Especially, since it opens up a way to use Aletheia-created GT with tesstrain.
from tesstrain.
@wrznr I must confess: There are some caveats.
It adds another dependency, python-opencv. Pillow kept complaining about images >80 MB.
Further, on Windows 10, one needs additionally to install C++14.0-Buildtools, which varies according to the used Python Version which is used by numpy which in turn is used by opencv.
from tesstrain.
In case it is helpful to others looking for a solution, posting below a bash script I use for -
1. taking a scanned page image, 2. running tesseract with hocr option on it, 3. running hocr tools to split it into lines.
The ground truth needs to be updated manually, if there is an existing page level ground truth file, copy line by line into the lines ground truth.
#!/bin/bash SOURCE="./myfiles/" lang=san set -- "$SOURCE"*.png for img_file; do echo -e "\r\n File: $img_file" OMP_THREAD_LIMIT=1 tesseract --tessdata-dir ../tessdata_fast "${img_file}" "${img_file%.*}" --psm 6 --oem 1 -l $lang -c page_separator='' hocr source venv/bin/activate PYTHONIOENCODING=UTF-8 ./hocr-extract-images -b ./myfiles/ -p "${img_file%.*}"-%03d.exp0.tif "${img_file%.*}".hocr deactivate done rename s/exp0.txt/exp0-gt.txt/ ./myfiles/*exp0.txt echo "Image files converted to tif. Correct the ground truth files and then run ocr-d train to create box and lstmf files"
Could you please explain what each line does. I want to run it on my system but am confused on what to change @Shreeshrii
from tesstrain.
I want to run it on my system but am confused on what to change
Assuming that you have tesseract and hocr-tools installed, put your image (png) files in ./myfiles/ folder. Change lang=san in the bash script to whichever language you need eg. lang=eng
save and run the bash script.for each image file
runs tesseract on image file to produce hocr output
runs hocr-extract-images to split the image to line images with the OCRed text for the line
done
rename generated text file from *.txt to *.gt.txt
Correct command will be the following (. instead of - in filename)
rename s/exp0.txt/exp0.gt.txt/ ./myfiles/*exp0.txtAfter this the *.gt.txt files need to be manually corrected to match the line images.
Thank You. It has solved some issues but still a problem persists. I'm attaching a screenshot. Please look into the matter @Shreeshrii
from tesstrain.
Do you have tesseract and hocr-tools installed correctly?
It is not finding hocr config file. Is your tessdata_prefix directory setup correctly?
Are hocr-tools working fine?
Change the paths based on your setup.
from tesstrain.
i installed hocr-tools using "sudo pip3 install hocr-tools". And as of tesseract i cloned the tesstrain repo aur used make leptonica tesseract since i had to train tesseract manually on data.
i guess my tessdata_prefix if fine. its ./usr/share/tessdata .
I tried several steps but am not able to run it correctly @Shreeshrii
from tesstrain.
Take one image file. Run tesseract on it, see if you get text output. Try again with pdf at end of command and see if you get pdf output. Then try with hocr.
Similarly test the hocr-tools. Check that you can run the hocr-extract-images command.
If you have installed it, then you may not need ./ before command.
Once you can do this for one file, use the appropriate commands in a for loop for all files.
from tesstrain.
./usr/share/tessdata
Check the files and folders in that directory. Do you have a newer set of files under /usr/share/tessdata/4.00
from tesstrain.
both tesseract and hocr are working.
So, i manually ran tesseract hocr on a jpg file "img.png" and it provided ne with an output "img.hocr".
Now, to extract line data from this i ran hocr-extract-images but am faced with an error which i have attached below. Please help @Shreeshrii
from tesstrain.
@rraina97 Please open an issue at https://github.com/tmbdev/hocr-tools for help on invoking hocr-extract-images
to keep this issue uncluttered.
It looks like img.hocr
is not in the current directory. Make sure you are in the right location, i.e. ls img.hocr
is successful.
from tesstrain.
Note that @M3ssman has proposed a set of python scripts to generate line image/text pairs from PAGE and ALTO in #205.
from tesstrain.
If we only have page images and page ground truth text, can we use them to train tesseract instead of line images and line ground truth? I imagine page images/text are closer to the tesseract input/output format?
from tesstrain.
@whisere The question is whether your page images/texts are aligned on line-level. I.e. for each text line the coordinates of the corresponding part of the page image have to be annotated. If not, training Tesseract with your data is not possible.
from tesstrain.
Thanks, That's not good, There is no text line information in page texts at all.. only multiple blocks with <p>
..
from tesstrain.
How about block images and block ground truth text?
from tesstrain.
Many thanks for the information!
from tesstrain.
This is a helpful script. Thank you. However, it ends with "run ocr-d train to create box and lstmf files". Can someone tell me how to do this? Thanks.
from tesstrain.
Related Issues (20)
- fine tuning arabic traineddata to solve extended words issue HOT 2
- Error while compiling tesseract within tesstrain HOT 2
- Maths OCR
- Can't open lstm.train despite (probably) having all training tools HOT 1
- Training a model from scratch with own imgs + txts? HOT 1
- Trying to train Tesseract for a different font, unable to get CER under 50%
- File not found - *.gt.txt HOT 3
- Error fine tuning new font for Thai Language
- What if my ground truth includes characters not found in a *.unicharset?
- Error generate text2image using khm.training_text HOT 1
- make training not building traineddata file HOT 1
- `make lists -j32` doesn't seem to be honoring the thread count. (Also happens when calling `make training -j32`) HOT 3
- deu_latf wordfile HOT 4
- unicharset_extractor stuck HOT 1
- How to train captcha? HOT 4
- winget install GnuWin32.Make error HOT 10
- make tesseract-langdata error HOT 7
- A question about missing dependency warnings when compiling and installing tesseract on centos using source code HOT 1
- How to train Chinese tradtional vertical in Tesseract 5? HOT 1
- "Compute CTC targets failed for xyz.lstmf!" for custom NET_SPECs HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tesstrain.