Overview
Theextract_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
Parameters
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
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
- Base64 Encoding: The image file is read in binary mode and encoded to base64 format for transmission to the OpenAI API.
-
GPT-4o Vision Analysis: The encoded image is sent to the
gpt-4o-minimodel with a specialized prompt in Spanish that instructs the model to extract only the license plate text without additional commentary. -
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
-
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:Code Example
Usage in Application
This function is called in the/guardar route after an image is uploaded:
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
"NO_DETECTADA", allowing the application to attempt fallback OCR methods (like pytesseract).
Environment Requirements
Dependencies
Configuration
The function requires a valid OpenAI API key to be configured:.env file contains:
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
- Image Quality: Ensure uploaded images are clear and well-lit for optimal extraction accuracy
- Error Recovery: Always implement fallback OCR methods (e.g., pytesseract) for robustness
- Logging: Monitor console output for OCR errors to track API issues
- Validation: Validate extracted plate numbers against expected formats for your region
- Caching: Consider caching results to avoid reprocessing the same images
Related Functions
read_csv()- Read stored license plate recordswrite_csv()- Save extracted plates to persistent storageensure_csv()- Initialize CSV storage structure
