Computer_Vision/Chapter18/detecting_the_number_plate_of_a_car.ipynb
2024-02-13 03:34:51 +01:00

1.7 MiB
Raw Permalink Blame History

Open In Colab

!wget https://raw.githubusercontent.com/zeusees/HyperLPR/master/model/cascade.xml
--2020-10-05 17:11:19--  https://raw.githubusercontent.com/zeusees/HyperLPR/master/model/cascade.xml
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 356611 (348K) [text/plain]
Saving to: cascade.xml


cascade.xml           0%[                    ]       0  --.-KB/s               
cascade.xml         100%[===================>] 348.25K  --.-KB/s    in 0.02s   

2020-10-05 17:11:19 (16.1 MB/s) - cascade.xml saved [356611/356611]

!wget https://www.dropbox.com/s/4hbem2kxzqcwo0y/car1.jpg
--2020-10-05 17:11:19--  https://www.dropbox.com/s/4hbem2kxzqcwo0y/car1.jpg
Resolving www.dropbox.com (www.dropbox.com)... 162.125.65.1, 2620:100:6021:1::a27d:4101
Connecting to www.dropbox.com (www.dropbox.com)|162.125.65.1|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: /s/raw/4hbem2kxzqcwo0y/car1.jpg [following]
--2020-10-05 17:11:20--  https://www.dropbox.com/s/raw/4hbem2kxzqcwo0y/car1.jpg
Reusing existing connection to www.dropbox.com:443.
HTTP request sent, awaiting response... 302 Found
Location: https://uc85aa9564fd6c3f1f88a78860c5.dl.dropboxusercontent.com/cd/0/inline/BAucLfBUruvOnGDlym7B8dRyeUJN01uEbG_LnxBQ72AVGdWxoYU0h4Bd9_UdueGBStSdTo11wxQxVGQ4XI2gbBhHSakuVvJiVHfGLnnW2t-m_myAbp4pPbhwACxtEusPDu4/file# [following]
--2020-10-05 17:11:20--  https://uc85aa9564fd6c3f1f88a78860c5.dl.dropboxusercontent.com/cd/0/inline/BAucLfBUruvOnGDlym7B8dRyeUJN01uEbG_LnxBQ72AVGdWxoYU0h4Bd9_UdueGBStSdTo11wxQxVGQ4XI2gbBhHSakuVvJiVHfGLnnW2t-m_myAbp4pPbhwACxtEusPDu4/file
Resolving uc85aa9564fd6c3f1f88a78860c5.dl.dropboxusercontent.com (uc85aa9564fd6c3f1f88a78860c5.dl.dropboxusercontent.com)... 162.125.65.15, 2620:100:6021:15::a27d:410f
Connecting to uc85aa9564fd6c3f1f88a78860c5.dl.dropboxusercontent.com (uc85aa9564fd6c3f1f88a78860c5.dl.dropboxusercontent.com)|162.125.65.15|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1714295 (1.6M) [image/jpeg]
Saving to: car1.jpg

car1.jpg            100%[===================>]   1.63M  --.-KB/s    in 0.03s   

2020-10-05 17:11:20 (62.1 MB/s) - car1.jpg saved [1714295/1714295]

!pip install torch_snippets
from torch_snippets import *
plate_cascade = cv2.CascadeClassifier('cascade.xml')
image = read("car1.jpg", 1)
Collecting torch_snippets
  Downloading https://files.pythonhosted.org/packages/d6/56/1093531de48ccc64410a480def65c386d18c981cfac21e7e3f92c9496352/torch_snippets-0.228-py3-none-any.whl
Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from torch_snippets) (3.2.2)
Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from torch_snippets) (4.41.1)
Requirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from torch_snippets) (7.0.0)
Requirement already satisfied: dill in /usr/local/lib/python3.6/dist-packages (from torch_snippets) (0.3.2)
Collecting loguru
[?25l  Downloading https://files.pythonhosted.org/packages/6d/48/0a7d5847e3de329f1d0134baf707b689700b53bd3066a5a8cfd94b3c9fc8/loguru-0.5.3-py3-none-any.whl (57kB)
     |████████████████████████████████| 61kB 5.6MB/s 
[?25hCollecting opencv-python-headless
[?25l  Downloading https://files.pythonhosted.org/packages/e2/e2/6670da2b12544858657058a5db2f088a18c56d0144bef8d178ad4734b7a3/opencv_python_headless-4.4.0.44-cp36-cp36m-manylinux2014_x86_64.whl (36.7MB)
     |████████████████████████████████| 36.7MB 110kB/s 
[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch_snippets) (1.18.5)
Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from torch_snippets) (1.1.2)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->torch_snippets) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->torch_snippets) (1.2.0)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->torch_snippets) (2.8.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->torch_snippets) (2.4.7)
Collecting aiocontextvars>=0.2.0; python_version < "3.7"
  Downloading https://files.pythonhosted.org/packages/db/c1/7a723e8d988de0a2e623927396e54b6831b68cb80dce468c945b849a9385/aiocontextvars-0.2.2-py2.py3-none-any.whl
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->torch_snippets) (2018.9)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cycler>=0.10->matplotlib->torch_snippets) (1.15.0)
Collecting contextvars==2.4; python_version < "3.7"
  Downloading https://files.pythonhosted.org/packages/83/96/55b82d9f13763be9d672622e1b8106c85acb83edd7cc2fa5bc67cd9877e9/contextvars-2.4.tar.gz
Collecting immutables>=0.9
[?25l  Downloading https://files.pythonhosted.org/packages/99/e0/ea6fd4697120327d26773b5a84853f897a68e33d3f9376b00a8ff96e4f63/immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl (98kB)
     |████████████████████████████████| 102kB 8.6MB/s 
[?25hBuilding wheels for collected packages: contextvars
  Building wheel for contextvars (setup.py) ... [?25l[?25hdone
  Created wheel for contextvars: filename=contextvars-2.4-cp36-none-any.whl size=7666 sha256=34153ec7c2c1663ee9ebc472440fec3ce14338da61adbd30038633b3d82e0f99
  Stored in directory: /root/.cache/pip/wheels/a5/7d/68/1ebae2668bda2228686e3c1cf16f2c2384cea6e9334ad5f6de
Successfully built contextvars
Installing collected packages: immutables, contextvars, aiocontextvars, loguru, opencv-python-headless, torch-snippets
Successfully installed aiocontextvars-0.2.2 contextvars-2.4 immutables-0.14 loguru-0.5.3 opencv-python-headless-4.4.0.44 torch-snippets-0.228
image_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
plates = plate_cascade.detectMultiScale(image_gray, 1.08, 2, minSize=(40, 40),maxSize=(1000, 100))
image2 = image.astype('uint8')
for (x, y, w, h) in plates:
    print(x,y,w,h)
    x -= w * 0.14
    w += w * 0.75
    y -= h * 0.15
    h += h * 0.3
    cv2.rectangle(image2, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 0), 10)
show(image2, grid=True)
600 1273 359 91