Odoo Development: OCR Module Creation & Deployment Guide

Updated on Apr 10,2025

Table of Contents

This comprehensive guide provides a detailed walkthrough of creating and deploying an Optical Character Recognition (OCR) module within the Odoo 14 framework. From setting up your local development environment to integrating object detection features and deploying to a live server, this guide aims to equip you with the knowledge and steps necessary to enhance Odoo with OCR capabilities. Whether you are a seasoned Odoo developer or just starting, this article provides value through clear instructions and practical examples.

Key Points

Setting up Odoo 14 with PostgreSQL locally for OCR module development.

Scaffolding a new Odoo module tailored for OCR and object detection functionalities.

Integrating OCR features using YOLOv5 for object detection within Odoo.

Deploying the newly developed OCR module to a live server for production use.

Understanding the configuration and dependencies required for OCR module development.

Odoo OCR Module Development

Setting Up Your Local Odoo Development Environment

To start developing your OCR module, the first critical step is to set up your local development environment with Odoo 14 and PostgreSQL. This setup allows for iterative testing and debugging without affecting live systems.

This involves configuring Docker Compose, creating necessary volume directories, and ensuring Odoo can connect to the PostgreSQL database.

Here’s how to prepare your local environment:

  1. Docker Compose Configuration: Create a docker-compose-local.yml file to define services, networks, and volumes for your Odoo and PostgreSQL instances. This file is crucial as it dictates how your containers interact with each other and persist data.
  2. Volume Directories: Set up directories for Odoo data, logs, and PostgreSQL data to ensure data persistence across container restarts. These volumes are mapped to specific directories on your host machine.
  3. Environment Variables: Utilize an .env file to manage sensitive information like database passwords and Odoo configurations. This keeps your Docker Compose file clean and secure.
  4. Spinning Up the Containers: Use the command docker-compose -f docker-compose-local.yml up -d to start your Odoo and PostgreSQL instances in detached mode. This command pulls the necessary images, creates the defined containers, and starts them in the background.

Important Configuration Parameters:

It’s crucial to understand the key parameters configured in your docker-compose-local.yml file:

  • Odoo-data and Odoo-logs volumes: These are essential for persisting Odoo's data and logs, respectively.
  • PostgreSQL configurations: Define environment variables for the PostgreSQL database, including the user, password, and database name.
  • Port mappings: Ensure that Odoo's port (usually 8069) is mapped to a port on your host machine, allowing you to access the Odoo instance from your web browser.

This setup enables you to make changes to your Odoo module, test them thoroughly, and debug effectively before deploying to a live server. Proper local setup is the foundation of efficient Odoo development.

Scaffolding Your New Odoo Module for OCR

Once your local environment is running, the next step involves scaffolding a new Odoo module tailored for OCR functionality.

This involves creating a module directory with essential files, such as __init__.py, __manifest__.py, models, and views. Odoo provides Helper scripts to automate this process, ensuring all necessary components are in place.

Steps for Scaffolding an Odoo Module:

  1. Access the Odoo Container: Use Docker to access the running Odoo container with the command docker ps -a to list all containers and then docker exec -ti <container_id> bash to enter the Odoo container's shell.
  2. Execute the Scaffold Command: Inside the container, execute the scaffold command using the odoo helper script: odoo scaffold <module_name> /mnt/extra-addons. Replace <module_name> with the desired name for your OCR module.

This command generates a basic module structure that includes:

  • __init__.py: An initialization file that makes the directory a Python Package.
  • __manifest__.py: A manifest file that defines the module's name, summary, description, and dependencies.
  • models/: A directory to store Python files defining your data models.
  • views/: A directory to store XML files defining the user interface.

Here’s an example of how to customize the manifest file (__manifest__.py) for an OCR module:

{
    'name': 'OCR & Document Detection',
    'summary': 'Thai OCR & Object Detection for Odoo',
    'description': 'This OCR & Object Detection for Odoo app. This is an example module to utilize AI in Odoo.',
    'author': 'Jakkit S.',
    'website': 'https://www.alot.or.th',
    'depends': ['base'],
    'data': [
        'security/ir.model.access.csv',
        'views/views.xml',
    ],
    'installable': True,
    'application': True,
}

By customizing this file, you define the essential metadata for your OCR module, ensuring Odoo recognizes and integrates it correctly.

Integrating OCR and Object Detection Features with YOLOv5

Integrating Optical Character Recognition (OCR) and object detection into your Odoo module using YOLOv5 enhances its ability to process and understand visual information from documents and images.

This involves setting up YOLOv5, creating necessary Python modules for handling image processing, and defining the logic for extracting data using OCR.

Steps for Integrating OCR and YOLOv5:

  1. Set Up YOLOv5:

    • Download or clone the YOLOv5 repository from GitHub: This repository contains all the necessary scripts and configurations for running YOLOv5.
    • Install the required dependencies using pip: Ensure you have installed all the necessary Python packages, including torch, torchvision, and other dependencies listed in the requirements.txt file of the YOLOv5 repository.
  2. Create Python Modules:

    • Define methods for preprocessing images, running object detection with YOLOv5, and extracting text using OCR: These methods should be encapsulated within Python modules for reusability and maintainability.
  3. Implement OCR Logic:

    • Incorporate OCR libraries like Tesseract to extract text from image regions identified by YOLOv5: This involves creating functions to pass image data to Tesseract and parse the resulting text.
  4. Customize the OCR App:

    • Modify the manifest file (__manifest__.py) to include necessary dependencies: Ensure that your module depends on the necessary libraries for OCR and object detection.

Code Example (models.py):

from odoo import models, fields

class OCRReceipt(models.Model):
    _name = 'thaiocr.receipt'
    _description = 'Thai OCR Module'
    name = fields.Char('Receipt Title', required=True)
    date_billed = fields.Date('Date Billed', required=True)
    store_id = fields.Many2one('res.partner', string='Store', ondelete='cascade')

This model defines the fields needed to store information extracted from receipts, such as the title, billing date, and store details. The fields.Binary field is used to store the scanned receipt image, which can then be processed using OCR and YOLOv5 for further Data Extraction.

By integrating YOLOv5, your Odoo module can automatically identify and extract key information from receipts, such as vendor names, dates, and amounts, streamlining data entry and improving accuracy.

Deploying Your OCR Module on a Live Server

After thoroughly testing your OCR module in a local environment, the final step is to deploy it to a live server for production use.

This involves packaging your module, transferring it to the server, installing it in Odoo, and configuring the server for OCR functionality.

Deployment Steps:

  1. Package Your Module:

    • Create a ZIP archive of your module directory: Ensure all necessary files, including the manifest file, models, views, and Python modules, are included in the Archive.
  2. Transfer the Module to the Server:

    • Use secure file transfer methods (e.g., SSH, SCP) to transfer the ZIP archive to a directory on your Odoo server: It’s best practice to place the module in the addons directory of your Odoo installation.
  3. Install the Module in Odoo:

    • Log in to your Odoo instance as an administrator, go to the Apps menu, and update the Apps List: This step ensures that Odoo recognizes the new module.
    • Search for your OCR module and install it: Follow the installation process as you would with any other Odoo module.
  4. Configure Server Security:

    • Specify Security to Access the Model: In Odoo, you should add ir.model.access.csv to specify the security to the model first by adding groups.xml and ir.model.access.csv files. You can also enable full access to the Thai OCR module by giving full access to the thaiocr.admin derive from admin users in the system.
  5. Configure Server for OCR:

    • Ensure that the server has the necessary dependencies for OCR and object detection: This may involve installing Tesseract, OpenCV, and other required libraries.

By following these steps, you can successfully deploy your OCR module to a live server, making it available for users to leverage its functionality within Odoo.

Technical Deep Dive: Docker Compose Configuration for Odoo

Dissecting docker-compose-local.yml

The docker-compose-local.yml file is at the heart of your local Odoo development environment. It defines the services, networks, and volumes required to run Odoo and PostgreSQL in Docker containers. Understanding this file is crucial for customizing and troubleshooting your development setup.

Here’s a closer look at the key components of the docker-compose-local.yml file:

  • Services: Define the Odoo and PostgreSQL services, specifying their images, ports, volumes, and dependencies.
  • Image Configuration: Set the appropriate Odoo and PostgreSQL images to ensure compatibility with Odoo 14.
  • Port Mapping: Expose ports to access Odoo and PostgreSQL from your host machine. Map port 8069 to the Odoo instance and port 5432 to the PostgreSQL database.
  • Environment Variables: Define critical environment variables for both services, including database credentials and Odoo configurations.
  • Volume Mounts: Specify volume mounts for data and logs to persist data across container restarts.

Detailed Example:

version: "3.8"
services:
  odoo:
    image: odoo:14
    depends_on:
      - db
    ports:
      - "8069:8069"
    volumes:
      - odoo-data:/var/lib/odoo
      - odoo-logs:/var/log/odoo
      - ./custom:/mnt/extra-addons
    environment:
      - ODOO_ADMIN_PASSWORD=odoo
    restart: unless-stopped
  db:
    image: postgres:11
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=odoo
      - POSTGRES_PASSWORD=odoo
      - POSTGRES_DB=odoo
    volumes:
      - db-data:/var/lib/postgresql/data
    restart: unless-stopped
volumes:
  odoo-data:
  odoo-logs:
  db-data:

This file is the blueprint for your local Odoo environment, defining how Odoo and PostgreSQL containers are created and configured. Customizations such as adding extra addons and configuring the environment parameters are made here.

How to Use

Accessing Your OCR Module in Odoo

After successfully deploying your OCR module, accessing it within Odoo is straightforward.

This process involves navigating to the Apps menu, installing the module, and locating the custom menu items.

Follow these steps to access and use your OCR module:

  1. Navigate to the Apps Menu:

    • Log in to your Odoo instance with administrative credentials.
    • Click on the Apps icon to open the applications menu.
  2. Install the OCR Module:

    • Search for your OCR module using keywords related to OCR or document detection: This ensures that the module is visible in the Apps List.
    • Click Install to deploy the module within your Odoo instance: This step triggers the installation process, which may take a few minutes.
  3. Locate Custom Menu Items:

    • Once the module is installed, navigate back to the main menu: Look for the custom menu items that you defined in your module's views. This menu should include actions for uploading receipts and running OCR processes.

Most people like