Skip to content

December 16, 2024 • TechSpherex AI Bot • 11 min read

Build a robust and accurate shipping container number identification system using SOTA machine learning techniques

Build a robust and accurate shipping container number identification system using SOTA machine learning techniques

Introduce

Recently, I worked on a project related to Smart Ports, where one of the tasks was to build a real-time shipping container number identification service. Simply put, this task is to identify container numbers from images of shipping containers. The container number is a string of 11 characters consisting of capital letters and numbers, unique worldwide. If you are not familiar with container numbers, maybe this article will be useful to you.

This service processes images of containers and outputs an 11-character container number and a 4-character ISO type size code. This article mainly focuses on identifying container numbers, because the method for handling them is the same, and handling ISO codes is simpler. The figure below shows some examples of container number identification, with the container number surrounded by a green box and the ISO code in a yellow box. The recognized container number is displayed in the upper left corner. As you can see, the container number can appear in many different places on the container, such as the door, the side, the top, etc.

Example of container number identification The accuracy and stability of the final system are very high, with accuracy exceeding 99.5% (in customer test data sets). Advanced models and high-quality image data collection were key factors behind these excellent results. For commercial reasons of the project, model parameters and final data sets will not be published. Not every detail can be shared, but I will try to share as many ideas and techniques as possible. The images and datasets used in this article were collected from public sources online in the early stages, so you can use them with confidence. The GIF below shows container number recognition in action, for demonstration and testing purposes only.

Demo This article is the first part in the series “Building a robust shipping container number identification system”. You can find all the parts here:

  • Part 1: Tasks, Challenges, System Design

  • Part 2: Tools, Image Annotation, Data Preparation

  • Part 3: Training, Building workflows, Deployment, Things to keep in mind

Challenge

This task may seem simple (that’s how I felt when I first heard about this project). However, in reality it brings many challenges and details that need to be considered. Completing this project may be simple, but achieving excellence is challenging. Here are some notable challenges:

1. Number of Containers in many directions

Container numbers can be arranged horizontally or vertically. Additionally, in photographic images, they are not always completely horizontal or vertical; Sometimes, there is an angle. Most text recognition models perform poorly at recognizing vertical text, so special processing or discrete training is required with large amounts of images containing vertical text.

![](https://techspherex.com/wp-content/uploads/2024/12/image.png)

2.Two Line Container Number

Images of containers are often taken in outdoor environments, so image quality may be poor, for example, due to uneven or dim lighting. Some characters may be obscured by strong lighting or shadows. At night, when there is only light from the port, the brightness and clarity of the image can be significantly reduced. These factors can lead to poor pattern recognition performance. This is a common challenge for most computer vision tasks. Fortunately, a high-quality data set, covering a wide variety of conditions, can mitigate this problem.

![](https://techspherex.com/wp-content/uploads/2024/12/image-1-1024x439.png)

1. Number of Containers with Different Lighting

Containers are often placed in outdoor environments where characters can be obscured by stains, scratches, rust, etc. These factors can easily cause errors, as even humans can have difficulty recognizing them in certain situations. For these cases, it is necessary to perform character correction (using check digit testing of the container number and practical experience) or cross-checking with existing port data, such as container arrival information.

![](https://techspherex.com/wp-content/uploads/2024/12/image-2-1024x352.png)

1.Number of Worn and Damaged Containers

Recognition systems need to operate in real time, so there needs to be a balance between speed and accuracy. This is a typical problem of trade-off between speed and accuracy. We need to maximize recognition speed while ensuring accuracy. Furthermore, the system’s redundant design must be robust enough to ensure stability, especially disaster recovery, as port operations occur continuously 24 hours a day. This identity system is deployed locally as Docker, providing API services. This part is not the focus of the article, as it goes into a completely different topic.

![](https://techspherex.com/wp-content/uploads/2024/12/image-3.png)

1. Realtime calculation

Recognition systems need to operate in real time, so there needs to be a balance between speed and accuracy. This is a typical problem of trade-off between speed and accuracy. We need to maximize recognition speed while ensuring accuracy. Furthermore, the system’s redundant design must be robust enough to ensure stability, especially disaster recovery, as port operations occur continuously 24 hours a day. This identity system is deployed locally as Docker, providing API services. This part is not the focus of the article, as it goes into a completely different topic.

Design the identification process

The diagram below roughly illustrates the simple workflow of the identification system. CN represents the Container Number, and TS represents the ISO type size code (e.g. 45G1, 22G1, etc.). The illustration only shows the container number identification process, the ISO type size code identification process is similar.

![](https://techspherex.com/wp-content/uploads/2024/12/Untitled-diagram-2024-12-16-101735-1-442x1024.png)

Here are some key points of the identification process:

Image preprocessing: Based on traditional image processing experience, performing some preprocessing steps, such as noise reduction and contrast improvement, can improve the overall performance of the system. However, in this project, according to actual test results, these preprocessing operations are unnecessary and do not improve the final performance of the system. Detection and recognition models trained with large amounts of high-quality data have strong robustness and can handle a fair amount of noise and illumination. In fact, this is one of the advantages of machine learning models, the same phenomenon is also observed in other image processing tasks using machine learning models. The only preprocessing operation that may be necessary is to resize the image. The system does not require the input images at their large original size to achieve good recognition results, so the images can be resized down to a suitable size to reduce the computational load.

Detect container number: This is the first step in the identification process. This detection model is responsible for determining the position of the container number (and ISO type size code…) in the image. The model is trained using the YOLOv8 detection algorithm. It outputs the coordinates of the container number in the image, and these coordinates are used to crop the container number regions from the original image.

Reorganize the container number area:

  • Combining into one line: Since some container numbers may be distributed across two lines, it is necessary to combine the number ranges on these multiple lines into a single line or column. This simplification helps with later identification and can increase accuracy. To accurately merge regions, it is important to accurately detect whether a line is part of a container number or not. Whether it is related to the container number or not is an issue that the detection model in the first step needs to solve. Specific details will be introduced in the following article.
![](https://techspherex.com/wp-content/uploads/2024/12/image-4.png)

Combine container numbers into one line

  • Horizontal Rearrangement: If the container number is arranged vertically, it needs to be reordered horizontally, because I want the system to only recognize horizontal text, as most OCR models perform poorly with vertically arranged text. Here, rearranging horizontally is not as simple as rotating the image 90 or 270 degrees, because such rotation would cause the characters in the image to not stand upright. Although OCR models can recognize rotated text, in terms of recognition performance and convenience in later processing, horizontally arranged text with characters standing upright is the best choice. The figure below illustrates the reordering process. During this process, a new character detection model is trained to detect individual characters (also using YOLOv8 detection), then the system reorders the detected characters in horizontal order.
![](https://techspherex.com/wp-content/uploads/2024/12/image-5.png)

Reorder container numbers

Container number recognition: Once the horizontally cropped image of the container number is ready, it can be recognized using a text recognition model. Here, PaddleOCR is used to train a text recognition model, including many popular text recognition algorithms; The details will be explained in the following article. Notably, our system uses two different recognition algorithm models to identify container numbers. If the results from the first recognition model fail the test, the system will use the second recognition model to do it again. If the second model also fails, the system compares and corrects the two results to output the final result. In real applications, using multiple models to improve accuracy is a common practice. Of course, this inevitably increases processing time, but fortunately this increase is acceptable in the overall system.

Note: In the identification system, the accuracy of the identification result can be checked through calculating the check digit of the container number. The last character of the container number is a check digit, calculated based on the previous ten characters. For check digit calculation rules, you can refer to this article.

Below is the Python function that calculates the check digit:

```

Calculate the check-digit of the container code

Input: container_code - The first 10 characters of the container code

Output: check_digit - 11th character of the container code

def calculate_check_digit(container_code): #Step 1: Map letters from A-Z to corresponding numeric values # According to international standards, each letter is assigned a fixed numerical value letter_values = { ‘A’: 10, ‘B’: 12, ‘C’: 13, ‘D’: 14, ‘E’: 15, ‘F’: 16, ‘G’: 17, ‘H’: 18, ‘I’: 19, ‘J’: 20, ‘K’: 21, ‘L’: 23, ‘M’: 24, ‘N’: 25, ‘O’: 26, ‘P’: 27, ‘Q’: 28, ‘R’: 29, ‘S’: 30, ‘T’: 31, ‘U’: 32, ‘V’: 34, ‘W’: 35, ‘X’: 36, ‘Y’: 37, ‘Z’: 38 }

Step 2: Convert each character in the container code to the corresponding value

values = []
for char in container_code:
    if char.isalpha(): # If the character is a letter
        values.append(letter_values[char]) # Get values from letter_values
    else: # If the character is a digit
        values.append(int(char)) # Convert directly to integer

Step 3: Calculate the weighted sum of the values

# Multiply each value by 2^(position-1), with positions starting at 0
total = sum(val * (2 ** i) for i, val in enumerate(values))

Step 4: Calculate the check digit

# The check digit is the remainder of the sum when divided by 11
check_digit = total % 11

If the remainder is 10, the check digit will be set to 0

if check_digit == 10:
    check_digit = 0

Returns the check digit

return check_digit

Function usage example:

container_code = “ABC1234567” # First 10 characters of the container code check_digit = calculate_check_digit(container_code) # Calculate the check digit print(f”Check digit for container code {container_code} is {check_digit}”)


### Step-by-step explanation:

- **Mapping letters to numeric values:**
Letters from <code>A-Z</code> are mapped to fixed values. For example, <code>A</code> corresponds to <code>10</code>, <code>B</code> to <code>12</code>, and so on. This is based on the transportation industry's coding standards.

- **Conversion of container code:**

<li>If the character is a letter (<code>isalpha()</code>), find the corresponding value in the <code>letter_values</code> dictionary.

- If the character is a number, convert it directly to an integer with <code>int()</code>.

</li>

- **Calculate total weight:**

<li>For each value, multiply it by <code>2^(position-1)</code>. The position (<code>i</code>) starts at 0. This is the standard formula for calculating the check digit.

</li>

- **Calculate check digit:**

<li>Divide the sum by 11 and get the remainder (<code>% 11</code>).

- If the remainder is <code>10</code>, the check digit assigned is <code>0</code>.

</li>

- **Example results:**
If <code>container_code = "ABC1234567"</code>, the converted values are:

<li><code>A = 10</code>, <code>B = 12</code>, <code>C = 13</code>, <code>1 = 1</code>, <code>2 = 2</code>, <code>3 = 3</code>, <code>4 = 4</code>, <code>5 = 5</code>, <code>6 = 6</code>, <code>7 = 7</code>.

- The total weight is:
<code>10*1 + 12*2 + 13*4 + 1*8 + 2*16 + 3*32 + 4*64 + 5*128 + 6*256 + 7*512 = 4067</code>.

- The check digit is <code>4067 % 11 = 10</code>, but because <code>10</code> is invalid, it is replaced by <code>0</code>.

</li>

### Result:

The function returns <code>check_digit = 0</code> for code <code>ABC1234567</code>.

### Summary

In this article, we explored the challenges and designed a system to identify container numbers from images using advanced machine learning techniques. The identification process includes many important steps such as image preprocessing, detecting and identifying container numbers, along with handling difficult situations such as uneven lighting, stains, or blurred or obscured container numbers. In particular, we looked at how the system identifies container numbers in real-world environments, where the requirements for speed and accuracy must always be balanced.

Our system achieves high accuracy, with the ability to process quickly even in complex conditions. As mentioned, we use machine learning models to improve error identification and mitigation. Although we do not share details about the model and dataset, the general methods we present can be applied to many text recognition problems in similar situations.

In the next article, we will dive into data preparation tools and methods, including how to mark images and prepare high-quality data sets to train recognition models. See you in the next part of this series of articles on building a container number identification system!
Telegram Zalo Facebook Messenger