Applying AI in ERP Application: OCR Implementation
In this story, we’ll continue on where we left off last time(https://medium.com/super-ai-engineer/applying-ai-in-erp-applications-55c9721cccdb). What we will do here is an Odoo application that takes a document by uploading an image or taking a photo right from the app and utilizes OCR (Optical Character Recognition) and object detection to extract text and logo from receipts. Once text and logo recognized and pulled from a receipt, they will be filled in a form for further use.
Our Plan
- Spinning up Odoo 14 instance and PostgreSQL, locally.
- Scaffolding and developing new Odoo module for Thai OCR & object detection features.
- Deploying on live server.
A. Local Development Preparation
- Make a docker-compose file.
touch docker-compose-local.yml
Here we'll use a minimum configuration for local development server in Docker. As you can see in the compose file, we need to create folder structure for docker volumes as follows:
- Also create
custom
folder too, as we'll put our new module in there. - Take a look inside
.env
file and feel free to change values to fit your needs.
2. Spin up the containers by
docker-compose -f docker-compose-local.yml up -d
- note if you need to see logs, you can omit the -d part or keep it running in the background (keep the -d part) and run the log command.
docker-compose -f docker-compose-local.yml logs --follow odoo
3. Navigate to localhost:8069 in your browser and create your new database.
4. Now you can install any apps you want by clicking on App menu. Here we’ll make our custom module to show up on this App page.
B. Scaffolding Odoo Module
We can execute the helper method odoo scaffold
from inside or outside the container by doing either one of the following:
B1 — Inside the Container
Note that ocr
is the name of our module.
- check the odoo container by
docker ps -a
- get inside it
docker exec -ti 1aobd bash
- execute scaffold command
odoo scaffold ocr /mnt/extra-addons
B2 — Outside the container
docker-compose -f docker-compose-local.yml exec odoo odoo scaffold ocr /mnt/extra-addons
C. Customize the OCR app
- Open the
__manifest__.py
file to fill necessary info.
2. Put your Odoo instance in Debugging mode by append ?debug=1
in the browser address bar. http://localhost:8069/web/?debug=1#action=35&cids=1&menu_id=5&model=ir.module.module&view_type=kanban
3. Update app list by clicking the Update Apps List button.
4. Clear ‘Apps’ filter in the search bar and type ‘ocr’ to find our app.
5. Install it.
Please note that after installing we will not see anything new since we need to make some menus and data models first. You can double check if the module is installed by doing the search like we did earlier.
Sketch up the App
Our module will be very simple, and we will continue to add features as we go on. Right now the app will read a receipt and fill out the form like so.
Making Models
- Add model for receipt class to store title and date billed information. This model relates to the built-in model called res.partner which refers to entities such as person, customers, companies, and etc. Also the product purchased (product_id) from the store will be related to built-in model called product.product. And lastly the store itself relates to res.partner model.
2. Since we need a built-in product model, we need to install ‘sale_management’ app from the App Store.
our module, we have to tell our module to grab the sale app automatically otherwise the installation will fail. We can do this by appending the app name in manifest file.
# any module necessary for this one to work correctly
'depends': ['base', 'sale_management'],
Add Menu
3. Top level menu called ‘Thai OCR’ is added via an xml file (views/views.xml). The menu needs ‘action window’ which is basically a way for a menu to open window. Please see the mentioned file.
Specify Security to Access the Model
4. At this point, if we upgrade the module, we won’t see any changes. But if you upgraded, you can see our model being populated in tools like PGAdmin4 or dropping into psql shell.
5. In order for us to see it, we need to grant access to our newly created model first by adding groups.xml and ir.model.access.csv files. In groups.xml, we are saying a group called group_thaiocr_admin derives from admin users in the system. And we allow full access to the thaiocr.receipt model to this group like so in csv file.
Add Views
6. The essential parts in the views.xml file are:
- action (to define what to do when a menu clicked)
action code
- list view (page that shows a list)
list view
- form view (page that shows when click ‘create’ button)
- search criteria (search bar where we can find or filter results for particular keyword)
Model Improvements
Here we add a few more fields such as image of a receipt and update form to reflect it. Please see the models.py and views.xml files.
Here is what we have so far for the form.
Okay, now we have some basic setup done and ready for the AI part. As you can see from above picture, our Thai OCR module is capable of uploading a receipt to the system, add products to the receipt. In the next part, we’ll put the EasyOCR and YOLO V.5 in the module for automatic form filling. Have a great one!