## About
This module contains a number plate reader, using a YOLO model for number plate extraction and a CNN trained from scratch on an OCR dataset for character recognition.
## How to setup
1. Create a Python virtual enviroment using `python3 -m venv venv`
2. Install using `pip install NumPlateVision`
## Example run
To detect and read a number plate from an image, here is an example script

## ML data processing pipeline
The number plate of a car can be obtained by using an object detection model. State of the art currently is YOLO (You Only Look Once) which is a deep CNN. The model used for number plate extraction can be found [here](https://huggingface.co/nickmuchi/yolos-small-finetuned-license-plate-detection) on The Hugging Face.
A number plate can be read by extracting each character from the number plate, passing it into a character recognition CNN model and then stringing together a word.
Each of these stages is explained in more detail below.
### Stage 1 - Plate extraction (YOLO)
The model finds a bounding box of what it thinks is a number plate. With a set confidence level, we can obtain the bounding box predicted by the model, and extract the number plate from the original image.


### Stage 2 - Character segmentation (OpenCV)
One similarity among all number plates is that the letters are black. Therefore, after applying image processing techniques such as edge sharpening using the Laplacian operator and Otsu thresholding, we can create a binary image with the characters as the foreground and everything else as the background.

### Stage 3 - Character extraction (OpenCV)
To extract a character, we can use the `contours` method from OpenCV, which identifies foreground objects within an image. This method creates a bounding box around each character, allowing us to extract that portion of the image.

### Stage 4 - Character recognition (CNN from scratch, see below)
Once we have the characters, we can feed each one into the model, obtain the predictions, and concatenate them into a string.

## CNN built from scratch for character recognition (See stage 4 above)
### Training
The neural network is trained on the standard [OCR dataset](https://www.kaggle.com/datasets/preatcher/standard-ocr-dataset) which contains 50k images of characters.
To increase the number of examples, the training data is augmented 5 times per image from a mix of rotation, translation and zooming. This attains a total train set of size 100k.
### Constants
Constants used during training:
- Loss: Categorical crossentropy
- Epochs: 10
- Optimiser: Adam
### Model evaluation
After training, the test set attains an accuracy of 96.7%.
Looking at the loss and accuracy per epoch we see that there are no signs of overfitting:

The confusion matrix shows excellent results overall, however classes 0, 4, and 24 had misclassifications with numbers 0, 3, and letter P; adding more training data or augmentation could help improve accuracy.

In addition, looking at a few test examples:

Raw data
{
"_id": null,
"home_page": "https://github.com/elliottcoops/Number-Plate-Recognition",
"name": "NumPlateVision",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": null,
"author": "Elliott Cooper",
"author_email": "elliottcoops@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a6/4a/d2f4080d65d26ee3f4d62d80ef9c6f68ed1a969958221daf5264ffbcf750/numplatevision-0.1.3.tar.gz",
"platform": null,
"description": "## About\n\nThis module contains a number plate reader, using a YOLO model for number plate extraction and a CNN trained from scratch on an OCR dataset for character recognition.\n\n## How to setup\n\n1. Create a Python virtual enviroment using `python3 -m venv venv`\n2. Install using `pip install NumPlateVision`\n\n## Example run\n\nTo detect and read a number plate from an image, here is an example script\n\n\n\n## ML data processing pipeline\n\nThe number plate of a car can be obtained by using an object detection model. State of the art currently is YOLO (You Only Look Once) which is a deep CNN. The model used for number plate extraction can be found [here](https://huggingface.co/nickmuchi/yolos-small-finetuned-license-plate-detection) on The Hugging Face.\n\nA number plate can be read by extracting each character from the number plate, passing it into a character recognition CNN model and then stringing together a word.\n\nEach of these stages is explained in more detail below.\n\n### Stage 1 - Plate extraction (YOLO)\n\nThe model finds a bounding box of what it thinks is a number plate. With a set confidence level, we can obtain the bounding box predicted by the model, and extract the number plate from the original image.\n\n \n\n\n\n\n### Stage 2 - Character segmentation (OpenCV)\n\nOne similarity among all number plates is that the letters are black. Therefore, after applying image processing techniques such as edge sharpening using the Laplacian operator and Otsu thresholding, we can create a binary image with the characters as the foreground and everything else as the background.\n\n\n\n### Stage 3 - Character extraction (OpenCV)\n\nTo extract a character, we can use the `contours` method from OpenCV, which identifies foreground objects within an image. This method creates a bounding box around each character, allowing us to extract that portion of the image.\n\n\n\n### Stage 4 - Character recognition (CNN from scratch, see below)\n\nOnce we have the characters, we can feed each one into the model, obtain the predictions, and concatenate them into a string.\n\n\n\n## CNN built from scratch for character recognition (See stage 4 above)\n\n### Training\n\nThe neural network is trained on the standard [OCR dataset](https://www.kaggle.com/datasets/preatcher/standard-ocr-dataset) which contains 50k images of characters.\n\nTo increase the number of examples, the training data is augmented 5 times per image from a mix of rotation, translation and zooming. This attains a total train set of size 100k.\n\n### Constants\n\nConstants used during training:\n\n- Loss: Categorical crossentropy\n- Epochs: 10\n- Optimiser: Adam\n\n### Model evaluation\n\nAfter training, the test set attains an accuracy of 96.7%.\n\nLooking at the loss and accuracy per epoch we see that there are no signs of overfitting:\n\n\n\nThe confusion matrix shows excellent results overall, however classes 0, 4, and 24 had misclassifications with numbers 0, 3, and letter P; adding more training data or augmentation could help improve accuracy.\n\n\n\nIn addition, looking at a few test examples: \n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Extracting a number plate from an image and recognising the characters on them",
"version": "0.1.3",
"project_urls": {
"Homepage": "https://github.com/elliottcoops/Number-Plate-Recognition"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b5507899987cb09375aa48275375d5d80147e61892a9fe35a7f4577e7ee98e35",
"md5": "3e4c8aadf3c42a3ebfdd7c53803ef556",
"sha256": "3a8f50e8fef9457552a95205f066fb7297c22a8de335aec9affe5d398f4fa3d9"
},
"downloads": -1,
"filename": "NumPlateVision-0.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3e4c8aadf3c42a3ebfdd7c53803ef556",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 3585564,
"upload_time": "2024-08-23T15:56:12",
"upload_time_iso_8601": "2024-08-23T15:56:12.335392Z",
"url": "https://files.pythonhosted.org/packages/b5/50/7899987cb09375aa48275375d5d80147e61892a9fe35a7f4577e7ee98e35/NumPlateVision-0.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a64ad2f4080d65d26ee3f4d62d80ef9c6f68ed1a969958221daf5264ffbcf750",
"md5": "15c0121a9eb91e1cd31acee6645e915b",
"sha256": "8aafe9a240198e09f64a0d662167a5afc4543c4951bef89e0a317537ba6e7537"
},
"downloads": -1,
"filename": "numplatevision-0.1.3.tar.gz",
"has_sig": false,
"md5_digest": "15c0121a9eb91e1cd31acee6645e915b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 3567133,
"upload_time": "2024-08-23T15:56:21",
"upload_time_iso_8601": "2024-08-23T15:56:21.372968Z",
"url": "https://files.pythonhosted.org/packages/a6/4a/d2f4080d65d26ee3f4d62d80ef9c6f68ed1a969958221daf5264ffbcf750/numplatevision-0.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-23 15:56:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "elliottcoops",
"github_project": "Number-Plate-Recognition",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "numplatevision"
}