Crafting a Custom License Plate Dataset: Blender Scripting Unleashed

Naethan Jacob
Toyota Connected India
9 min readOct 10, 2023

1.1 Introduction

In the world of machine learning and artificial intelligence, data is the bedrock upon which models are built. When I embarked on a journey to train an Optical Character Recognition (OCR) model specifically for reading license plates, the first and most crucial challenge was sourcing the data. Real-world data collection, with its necessity for annotations, not only proved to be an expensive endeavour but also posed various logistical challenges. How could I obtain a rich dataset of license plate images without breaking the bank or spending endless hours in data collection? The solution lay in a tool not conventionally associated with data generation: Blender. A free and open-source 3D creation suite, Blender became my go-to for creating a vast and diverse dataset of license plate images. In this guide, we’ll explore how to harness Blender’s powerful scripting tools to automate this process.

1.2 Prerequisites: Familiarity with Blender

While this guide delves into the intricacies of scripting within Blender to automate license plate image generation, it assumes the reader has a foundational understanding of Blender’s interface and basic functionalities. If you’re new to Blender or feel the need for a refresher, I highly recommend Blender Guru’s “Beginner Donut Tutorial” on YouTube. This comprehensive playlist offers an engaging introduction to Blender, ensuring you have the necessary groundwork to grasp the concepts discussed in this article.

2.1 Setting Up the Blender Scene

One of the foundational steps in generating a realistic dataset is ensuring the digital representation closely mirrors the real world. While I collaborated with a 3D artist to refine the intricacies, it’s important to understand the basics:

The License Plate Model: Designing a photorealistic license plate goes beyond just a simple rectangular shape. Elements such as embossing, where certain characters or patterns are raised, can add a touch of authenticity. Additionally, ensuring the plate has a slightly reflective surface can emulate the way real license plates catch and reflect light.

Diverse Text Styles: License plates around the world come in a plethora of fonts and styles. To replicate this diversity, multiple fonts were incorporated. Programmatically changing fonts ensures a wide range of plate styles, aiding in creating a dataset that’s robust and varied.

Camera and Lighting Dynamics: Capturing the license plate from various angles and under different lighting conditions is crucial for a comprehensive dataset. This not only provides diversity in terms of perspective but also simulates different times of the day and conditions under which an OCR model might operate in the real world. By programmatically adjusting the camera angles and lighting setups, we ensured the dataset was as diverse as possible, laying the groundwork for optimal model generalization during training.

2.2 Scene Setup: License Plate and Text

Before diving deep into the scripting intricacies, it’s crucial to understand the basic setup of our Blender scene. The license plate generation hinges on two primary objects: the base license plate and the text.

Base License Plate: This is the foundational 3D model representing the physical license plate. Crafted with attention to detail, it provides a realistic backdrop upon which the dynamic text will be displayed. Depending on your requirements, this model can have unique characteristics such as embossing, reflective surfaces, or specific color patterns. Remember, the more realistic this base plate, the more authentic the final renders will appear.

Dynamic Text Object: Positioned above the base license plate, this object is the centerpiece of our automation. It’s where different license plate numbers or texts will be displayed. Being a separate object allows for greater flexibility, letting us change the text content without affecting the base license plate. It’s essential to ensure this text object is well-aligned, centered, and sized appropriately to fit within the boundaries of the base plate.

Having these two objects separate but aligned offers a modular approach. While the base license plate remains consistent, the text object can be dynamically updated, allowing for the generation of countless license plate variations with ease.

3. Blender Scripting Basics

As someone who dabbles in technology, I’ve always been intrigued by the interplay between graphics and programming. While Blender is renowned for its graphical prowess, I hadn’t quite ventured into its scripting domain until this project. Embarking on this was, in essence, a steep learning curve, but the potential rewards in terms of automating the license plate generation process were too alluring to pass up.

Diving into Blender’s Python API, bpy, was initially daunting. The intricate web of classes, functions, and properties can be overwhelming for a beginner. But with persistence and the right resources, the fog began to clear.

  1. Code-Generating LLMs: These proved to be invaluable tools, helping bridge the gap between my conceptual understanding and practical application of scripting in Blender.
  2. Official Blender Documentation: While sometimes a bit dense, the official Blender documentation was my compass, guiding me through the vast sea of bpy. It’s a treasure trove of information for anyone keen on understanding the nuts and bolts of Blender’s Python API.

With these resources at hand and a bit of trial and error, I was ready to venture into automating the generation of our license plate images.

4.1 The Scripting Process

This section will delve into the technical details, illustrating how scripting played a pivotal role in automating the generation of license plate images.

4.2 Selecting the Text Object

Every Blender scene consists of a collection of objects, each with its unique properties and characteristics. For our project, the text object, responsible for displaying the license plate numbers, was our primary target. But how do we single out this object among potentially many others in a scene?

In Blender’s Python API, objects can be accessed through bpy.data.objects. By providing the name of our text object, we can select and manipulate it:

text_object = bpy.data.objects["NameOfTextObject"]

4.3 Changing the Text

With our text object selected, the next step was to update its content dynamically. This would allow us to generate different license plate numbers without manual intervention:

text_object.data.body = "NewLicenseNumber"

Replace NewLicenseNumber with the desired license plate text. By looping through a list of license plate numbers, we can programmatically generate numerous variations.

4.4 Rendering the Scene

Rendering is the process of converting our 3D scene into a 2D image, capturing all the details, lighting, and nuances. With our text object updated, the scene is ready for rendering:

bpy.ops.render.render(write_still=True)

This command instructs Blender to render the current scene, producing an image of our license plate.

4.5 Saving the Image

Having rendered the scene, the final step is to save this image for our dataset:

image_path = "/path/to/save/images/NewLicenseNumber.png" 
bpy.data.images['Render Result'].save_render(filepath=image_path)

Replace /path/to/save/images/ with the directory where you’d like to save your images. By incorporating this step into our loop, each rendered license plate image is automatically saved with a distinct name.

5. Challenges and Optimizations

While the core scripting process might seem straightforward, any developer knows that it’s rare for a script to run flawlessly on the first try. Here are some considerations and optimizations to keep in mind:

Text Overflow: Depending on the length of the license plate text and the font used, there’s a possibility of text overflowing the plate. It’s wise to set character limits or adjust font sizes dynamically to ensure the text fits snugly within the plate’s boundaries.

Rendering Quality vs. Speed: High-quality renders can take time, especially if the scene is complex. For dataset generation, where thousands of images might be needed, consider striking a balance. You could reduce the rendering quality for speed, ensuring you still retain enough detail for the OCR model to train effectively.

Batch Rendering: Instead of rendering images one by one, consider batch rendering. This allows Blender to render multiple images in a sequence, which can be more efficient and reduce overall rendering time.

Randomization: To add more diversity to the dataset, consider randomizing elements like lighting, slight color variations to the plate, or even minor deformations to simulate wear and tear. This ensures the dataset is rich and varied, closely mimicking real-world conditions.

With these considerations in mind, the scripting process becomes not just about automating a task, but refining it for the best possible outcome.

6. Potential Enhancements and Considerations

Even with a robust scripting process in place, there’s always room for improvement and adaptation. Here are some potential enhancements and considerations to make the license plate generation even more versatile and effective:

Dynamic Backgrounds and Environments: While our main focus is on license plates, introducing diverse backgrounds can add another layer of realism. Consider using HDRIs or procedural backgrounds to simulate various environmental conditions like daytime, nighttime, or rainy weather.

Motion Blur: In real-world scenarios, especially for surveillance cameras, license plates may often be captured in motion. Introducing motion blur programmatically can simulate this effect, making the dataset more representative of real-life captures.

some examples of we’ve encountered in our ALPR product (cropped to preserve privacy)

Dirt and Wear Simulation: A brand-new license plate is a rarity in the real world. Using Blender’s particle systems or texture overlays, you can introduce dirt, scratches, and general wear to the plates, adding to the realism.

Customizable Plate Designs: Different regions or countries have distinct license plate designs. Consider building a library of plate designs and programmatically selecting them based on desired regions.

Integration with External Databases: If you have access to a database of license plate numbers or patterns, consider integrating it with the script. This ensures that the generated plates are not just random but also follow real-world patterns.

Optimizing for Larger Datasets: If you’re looking to generate extremely large datasets, consider streamlining the process further by using Blender’s headless mode, which allows for rendering without the GUI, potentially speeding up the process.

Feedback Loops: Once the OCR model is trained, consider using it to verify the generated images. This feedback loop can help identify and rectify any systematic errors in the generation process, ensuring higher quality data.

In essence, while the foundation we’ve laid out provides a robust start, the possibilities for enhancement are vast. By understanding the requirements and continuously iterating, you can tailor the process to suit specific needs, ensuring that the generated dataset is both diverse and representative.

7. Conclusion

Setting out to craft a rich dataset of license plate images using Blender was both challenging and rewarding. What started as a necessity to train an OCR model became an exploration into the depths of Blender’s scripting capabilities. The process not only underscored the importance of automation in today’s digital age but also showcased the versatility of tools like Blender, which transcend their primary functions to offer solutions in unexpected domains.

For those looking to dive into similar projects or any form of procedural generation, remember that the key lies in understanding the tool, iterating based on feedback, and continuously seeking optimizations. The world of 3D graphics and scripting is vast, and the solutions it can offer are limited only by our imagination. Here’s to more innovations and explorations in this exciting realm!

8. References

  1. Blender’s Official Documentation: A comprehensive guide to Blender’s Python API.
  2. Blender Guru’s Beginner Donut Tutorial: An engaging introduction to the basics of Blender.

--

--