|
| 1 | +# importing libraries |
| 2 | +import numpy as np |
| 3 | +import cv2 |
| 4 | +import imutils |
| 5 | +import sys |
| 6 | +import pytesseract |
| 7 | +import pandas as pd |
| 8 | +import time |
| 9 | + |
| 10 | +image = cv2.imread(input("Enter the path of image: ")) |
| 11 | + |
| 12 | +image = imutils.resize(image, width=500) |
| 13 | + |
| 14 | +pytesseract.pytesseract.tesseract_cmd = input("Enter the path of tesseract in your local system : ") |
| 15 | + |
| 16 | +# displaying it |
| 17 | +cv2.imshow("Original Image", image) |
| 18 | + |
| 19 | +# converting it into gtray scale |
| 20 | +# cv2.imshow("1 - Grayscale Conversion", gray) |
| 21 | +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| 22 | + |
| 23 | +#cv2.imshow("2 - Bilateral Filter", gray) |
| 24 | +gray = cv2.bilateralFilter(gray, 11, 17, 17) |
| 25 | + |
| 26 | +# canny edge detector |
| 27 | +#cv2.imshow("4 - Canny Edges", edged) |
| 28 | +edged = cv2.Canny(gray, 170, 200) |
| 29 | + |
| 30 | +""" |
| 31 | +there are three arguments in cv2.findContours() function, first one is source image, |
| 32 | +second is contour retrieval mode, third is contour approximation method.py |
| 33 | + |
| 34 | +If you pass cv2.CHAIN_APPROX_NONE, all the boundary points are stored. |
| 35 | +But actually do we need all the points? For eg, you found the contour of a straight line. |
| 36 | +Do you need all the points on the line to represent that line? |
| 37 | +No, we need just two end points of that line. |
| 38 | +This is what cv2.CHAIN_APPROX_SIMPLE does. |
| 39 | +It removes all redundant points and compresses the contour, thereby saving memory. |
| 40 | +""" |
| 41 | + |
| 42 | +cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
| 43 | + |
| 44 | +# contour area is given by the function cv2.contourArea() |
| 45 | +cnts=sorted(cnts, key = cv2.contourArea, reverse = True)[:30] |
| 46 | +NumberPlateCnt = None |
| 47 | + |
| 48 | +count = 0 |
| 49 | +for c in cnts: |
| 50 | + """ |
| 51 | + contour perimeter is also called arclength. It can be found out using cv2.arcLength() |
| 52 | + function.Second argument specify whether shape is a closed contour( if passed |
| 53 | + True), or just a curve. |
| 54 | + """ |
| 55 | + peri = cv2.arcLength(c, True) |
| 56 | + approx = cv2.approxPolyDP(c, 0.02 * peri, True) |
| 57 | + if len(approx) == 4: |
| 58 | + NumberPlateCnt = approx |
| 59 | + break |
| 60 | + |
| 61 | +# Masking the part other than the number plate |
| 62 | +mask = np.zeros(gray.shape,np.uint8) |
| 63 | +new_image = cv2.drawContours(mask,[NumberPlateCnt],0,255,-1) |
| 64 | +new_image = cv2.bitwise_and(image,image,mask=mask) |
| 65 | +cv2.namedWindow("Final_image",cv2.WINDOW_NORMAL) |
| 66 | +cv2.imshow("Final_image",new_image) |
| 67 | + |
| 68 | +# Configuration for tesseract |
| 69 | +config = ('-l eng --oem 1 --psm 3') |
| 70 | + |
| 71 | +# Run tesseract OCR on image |
| 72 | +text = pytesseract.image_to_string(new_image, config=config) |
| 73 | + |
| 74 | +#Data is stored in CSV file |
| 75 | +raw_data = {'date': [time.asctime( time.localtime(time.time()) )], |
| 76 | + 'v_number': [text]} |
| 77 | + |
| 78 | +df = pd.DataFrame(raw_data, columns = ['date', 'v_number']) |
| 79 | +df.to_csv('data.csv') |
| 80 | + |
| 81 | +# Print recognized text |
| 82 | +print(text) |
| 83 | + |
| 84 | +cv2.waitKey(0) |
0 commit comments