{
 "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": "[encoded data, 2035264 characters]",
      "text/plain": [
       "<Figure size 1700x1200 with 1 Axes>"
      ]
     },
     "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
}
