Annotating images for fine tuning the LayoutLM model requires a lot of manual work which includes the three major steps:
- Drawing bounding boxes around the texts
- Labelling the bounding boxes
- Manually type in the text in those bounding boxes
There are a lot of ML assisted annotation tools in the market that can make the job much easier but they can cost the user money. Thus I present the community with an annotation tool that automates steps 1 and 3. This can be acheived using Label Studio and PaddleOCR, both of them are Open Source but the advantage of my tool is that you can use your own Models for Text Detection and/or Text Recognition Model (you have to overwrite a method in the code)
- Copy-Paste all your images into the
imagesfolder. - Run the
generate.pyscript to create a Label Studio Annotation Job (A JSON file). (In this step the PaddleOCR model will detect the Text Bounding boxes and the Recognisied text. Thus completing steps 1 and 3, mentioned above). - Upload this JSON file to Label Studio to start the Annotation Job.
- All you have to do is to label each bounding box and fix the bounding boxes and the OCR text.
- After the Annotation Job is done, export the annotation using
JSON-MINexport option. - Use
distill.pyto postprocess the annotations so that thet can be ingested by the LayoutLM model without any preporcessing.
$ git clone 'https://github.com/arnabd64/LayoutLM-Annotator.git'
$ cd LayoutLM-Annotator
# create a virtual environment
# using conda
$ conda create -n label python=3.10
$ conda activate label
# using venv
$ python3 -m venv label
$ source label/bin/activate
# install packages
$ pip3 install -r requirements.txtOnce the process is done then the annotation tool has been installed.
Put all your image data inside the images folder.
The server.py script will run a HTTP file server on http://localhost:9000/ if you want to change the port from 9000 to a port of your choice replace the PORT variable on line 11.
Open generate.py and change the ANNOTATION_NAME variable to what to want. Run the script
Run server.py
Run the Label Studio server byb executing: label-studio --data-dir ./data
- Do the usual Sign Up and Login In stuff.
- Once Inside the application, create a project
- Give it in an appropiate name
- In
Data ImportTab, Upload the.jsonfile that was created bygenerate.py. - In the
AnnotationTab, choose Optical Character Recognition. - Start the Project
Once the Annotation Job is done then hit the Export button and export it under the JSON-MIN option.
Open distill.py and the variables LABEL_STUDIO_JSON_PATH to the file name of the JSON export from label studio and optionally you can change the variable DISTILLED_ANNOIATIONS variable to change the name of the postprocessed JSON file.
Run distill.py
If you are facing any issues with the code and is unable to solve then do raise an issue on the github repo issues section. You have a faced an issue and have modified the code to solve then you can share it with other community members and me by raising an issue and writing down the solution in the comments section.
Other members odf the community feel free to Fork this repo and do leave a star on this repo.
- @AIOdysseyhub YouTube channel for the inspiration as well as the first draft of the code.
- Label Studio Documentation
- PaddleOCR