135 lines
1.9 MiB
Plaintext
135 lines
1.9 MiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Working perfectly 3 8 12 14\n",
|
||
|
"\n",
|
||
|
"# Import dependencies\n",
|
||
|
"import cv2 \n",
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"import pytesseract\n",
|
||
|
"import re\n",
|
||
|
"pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'\n",
|
||
|
"# Setup function to detect car plate\n",
|
||
|
"def carplate_detect(image, carplate_haar_cascade):\n",
|
||
|
" carplate_overlay = image.copy() # Create overlay to display red rectangle of detected car plate\n",
|
||
|
" carplate_rects = carplate_haar_cascade.detectMultiScale(carplate_overlay,scaleFactor=1.1, minNeighbors=5) \n",
|
||
|
"\n",
|
||
|
" for x,y,w,h in carplate_rects: \n",
|
||
|
" cv2.rectangle(carplate_overlay, (x,y), (x+w,y+h), (255,0,0), 5) \n",
|
||
|
" \n",
|
||
|
" return carplate_overlay\n",
|
||
|
"\n",
|
||
|
"def enlarge_plt_display(image, scale_factor):\n",
|
||
|
" width = int(image.shape[1] * scale_factor / 100)\n",
|
||
|
" height = int(image.shape[0] * scale_factor / 100)\n",
|
||
|
" dim = (width, height)\n",
|
||
|
" plt.figure(figsize = dim)\n",
|
||
|
" plt.axis('off') \n",
|
||
|
" plt.imshow(image)\n",
|
||
|
"\n",
|
||
|
" cv2.imwrite(\"result.png\", image)\n",
|
||
|
"\n",
|
||
|
"def carplate_extract(image, carplate_haar_cascade):\n",
|
||
|
" \n",
|
||
|
" carplate_rects = carplate_haar_cascade.detectMultiScale(image,scaleFactor=1.1, minNeighbors=5) \n",
|
||
|
"\n",
|
||
|
" for x,y,w,h in carplate_rects: \n",
|
||
|
" carplate_img_extract = image[y+15:y+h-10 ,x+15:x+w-20] \n",
|
||
|
" \n",
|
||
|
" return carplate_img_extract\n",
|
||
|
"\n",
|
||
|
"def enlarge_img(image, scale_percent):\n",
|
||
|
" width = int(image.shape[1] * scale_percent / 100)\n",
|
||
|
" height = int(image.shape[0] * scale_percent / 100)\n",
|
||
|
" dim = (width, height)\n",
|
||
|
" resized_image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)\n",
|
||
|
" return resized_image\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"def convertToText(carplate_img):\n",
|
||
|
" #Detection \n",
|
||
|
" carplate_img_rgb = cv2.cvtColor(carplate_img, cv2.COLOR_BGR2RGB)\n",
|
||
|
"\n",
|
||
|
" carplate_haar_cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml')\n",
|
||
|
" detected_carplate_img = carplate_detect(carplate_img_rgb, carplate_haar_cascade)\n",
|
||
|
" enlarge_plt_display(detected_carplate_img, 1.2) \n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
" #OCR\n",
|
||
|
" carplate_extract_img = carplate_extract(carplate_img_rgb, carplate_haar_cascade)\n",
|
||
|
" carplate_extract_img = enlarge_img(carplate_extract_img, 150)\n",
|
||
|
" carplate_extract_img_gray = cv2.cvtColor(carplate_extract_img, cv2.COLOR_RGB2GRAY)\n",
|
||
|
" carplate_extract_img_gray_blur = cv2.medianBlur(carplate_extract_img_gray,3) # Kernel size 3\n",
|
||
|
"\n",
|
||
|
" text = pytesseract.image_to_string(carplate_extract_img_gray_blur, \n",
|
||
|
" config = f'--psm 8 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
" print(\"text : \", text)\n",
|
||
|
" text = re.sub('[^A-Za-z0-9]+', '', text)\n",
|
||
|
" return text\n",
|
||
|
"\n",
|
||
|
"# carplate_img = cv2.imread('Cars19.png')\n",
|
||
|
"# print(convertToText(carplate_img))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"text : 0200163\n",
|
||
|
"\n",
|
||
|
"0200163\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAABOQAAAOwCAYAAACeeatzAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOz9SZcsubbnh/02ADNz92hOl81tXleNSEoqNjOJkyJXzcQJ1+Kn4EDfQR+AA4454EwacCRpyJlYEiUNuCiSYoksktW89+rdJk+eJjp3NzMAW4MNMzf38DgnTt7Me18V8V95MiLczWAwGAzY+OO/9xZVVSoqKioqKioqKioqKioqKioqKip+L3B/6ApUVFRUVFRUVFRUVFRUVFRUVFT8zwmVkKuoqKioqKioqKioqKioqKioqPg9ohJyFRUVFRUVFRUVFRUVFRUVFRUVv0dUQq6ioqKioqKioqKioqKioqKiouL3iErIVVRUVFRUVFRUVFRUVFRUVFRU/B5RCbmKioqKioqKioqKioqKioqKiorfIyohV1FRUVFRUVFRUVFRUVFRUVFR8XtEJeQqKioqKioqKioqKioqKioqKip+j6iEXEVFRUVFRUVFRUVFRUVFRUVFxe8R4acrWkEzkO1PsY9A7Hek/CsfL/6ekOfvHmM62qHlWlPZpUw9HKgn5Zxe6fiqFRUVFRUVFRUVFRUVFRUVFRUVPx1+QkJugjz6VT8rzDP6zDHReY8pMzdRbLqk22T+VY4/XZT6I+BXv4J/+A9/rNIqKj4PEfi3/q2ZcK6oqKioqKioqKioqKioqPjnF6KqPxpPdQpdaNOUiRh7rIQ7PQvyglBbkndLak0R8kEKpwDhEWGhp6eew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]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"carplate_img = cv2.imread('img/26417135.jpg')\n",
|
||
|
"print(convertToText(carplate_img))"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": ".venv",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.9.13"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|