Skip to main content

Overview

The extract_plate_from_image() function is the core OCR implementation for the CoroNet License Plate Scanner. It uses OpenAI’s GPT-4o-mini vision model to intelligently extract license plate text from vehicle images, providing superior accuracy compared to traditional OCR methods.

Function Signature

def extract_plate_from_image(image_path: str) -> str

Parameters

image_path
string
required
The file system path to the image containing the license plate. Must be a valid image file (JPEG, PNG, etc.) that can be read and base64-encoded.

Return Value

plate_text
string
The extracted license plate text in uppercase format, cleaned and sanitized. Returns "NO_DETECTADA" if extraction fails or no plate is detected.
  • Maximum length: 10 characters
  • Format: Alphanumeric characters and hyphens only
  • All other characters are filtered out

Implementation Details

Image Processing Pipeline

  1. Base64 Encoding: The image file is read in binary mode and encoded to base64 format for transmission to the OpenAI API.
  2. GPT-4o Vision Analysis: The encoded image is sent to the gpt-4o-mini model with a specialized prompt in Spanish that instructs the model to extract only the license plate text without additional commentary.
  3. Text Sanitization: The response is processed through multiple cleaning steps:
    • Stripped of whitespace
    • Converted to uppercase
    • Filtered to keep only alphanumeric characters and hyphens
    • Truncated to maximum 10 characters
  4. Error Handling: Any exceptions during the OCR process are caught, logged to console, and result in returning the fallback value "NO_DETECTADA".

OpenAI API Configuration

The function uses the following API parameters:
model="gpt-4o-mini"
messages=[
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Extrae únicamente el texto de la matrícula visible..."
            },
            {
                "type": "image_url",
                "image_url": {"url": "data:image/jpeg;base64,..."}
            }
        ]
    }
]

Code Example

from app import extract_plate_from_image

# Extract plate from a saved image
image_path = "/path/to/uploads/matricula_20240315_143022.jpg"
plate_number = extract_plate_from_image(image_path)

if plate_number != "NO_DETECTADA":
    print(f"License plate detected: {plate_number}")
    # Save to database or process further
else:
    print("No license plate could be detected")
    # Fallback to alternative OCR method

Usage in Application

This function is called in the /guardar route after an image is uploaded:
@app.route("/guardar", methods=["POST"])
def guardar():
    # ... file upload handling ...
    
    # Primary OCR with GPT-4o
    matricula = extract_plate_from_image(path)
    
    # Fallback to pytesseract if GPT-4o fails
    if matricula == "NO_DETECTADA":
        image = Image.open(path)
        ocr_text = pytesseract.image_to_string(image, lang="eng")
        ocr_text = ocr_text.strip().replace(" ", "").replace("\n", "").upper()
        matricula = "".join([c for c in ocr_text if c.isalnum() or c == "-"])[:10]
    
    # ... save to CSV ...

Error Handling

The function implements comprehensive error handling:
All exceptions are caught and logged to the console with the prefix “Error OCR con OpenAI:”. Common error scenarios include:
  • Invalid image path or file not found
  • Image encoding failures
  • OpenAI API connection errors
  • API authentication failures (invalid API key)
  • Rate limiting or quota exceeded errors
  • Invalid API response format
When any error occurs, the function gracefully returns "NO_DETECTADA", allowing the application to attempt fallback OCR methods (like pytesseract).

Environment Requirements

Dependencies

from openai import OpenAI
import base64
import os
from dotenv import load_dotenv

Configuration

The function requires a valid OpenAI API key to be configured:
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
Ensure your .env file contains:
OPENAI_API_KEY=sk-your-api-key-here

Performance Considerations

  • API Latency: Typical response time is 2-5 seconds depending on image size and API load
  • Cost: Each call consumes OpenAI API credits (gpt-4o-mini pricing applies)
  • Image Size: Larger images take longer to encode and transmit; consider resizing images before processing
  • Rate Limits: OpenAI enforces rate limits; implement request queuing for high-volume scenarios

Best Practices

  1. Image Quality: Ensure uploaded images are clear and well-lit for optimal extraction accuracy
  2. Error Recovery: Always implement fallback OCR methods (e.g., pytesseract) for robustness
  3. Logging: Monitor console output for OCR errors to track API issues
  4. Validation: Validate extracted plate numbers against expected formats for your region
  5. Caching: Consider caching results to avoid reprocessing the same images
  • read_csv() - Read stored license plate records
  • write_csv() - Save extracted plates to persistent storage
  • ensure_csv() - Initialize CSV storage structure

Build docs developers (and LLMs) love