Last active
July 4, 2021 08:31
-
-
Save Rishbah-76/ce4735b07cd30e06a1a2cad0f9932d41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import imutils | |
import numpy as np | |
import pytesseract | |
import re | |
import string | |
pytesseract.pytesseract.tesseract_cmd = r'<paste your pytesseract application loacation here>' | |
img = cv2.imread('photo.jpeg',cv2.IMREAD_COLOR) | |
img = cv2.resize(img, (500,600) ) | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
gray = cv2.bilateralFilter(gray, 13, 15, 15) | |
edged = cv2.Canny(gray, 30, 200) | |
contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
contours = imutils.grab_contours(contours) | |
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10] | |
screenCnt = None | |
for c in contours: | |
peri = cv2.arcLength(c, True) | |
approx = cv2.approxPolyDP(c, 0.018 * peri, True) | |
if len(approx) == 4: | |
screenCnt = approx | |
break | |
if screenCnt is None: | |
detected = 0 | |
print ("No contour detected") | |
else: | |
detected = 1 | |
if detected == 1: | |
cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3) | |
mask = np.zeros(gray.shape,np.uint8) | |
new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,) | |
new_image = cv2.bitwise_and(img,img,mask=mask) | |
(x, y) = np.where(mask == 255) | |
(topx, topy) = (np.min(x), np.min(y)) | |
(bottomx, bottomy) = (np.max(x), np.max(y)) | |
Cropped = gray[topx:bottomx+1, topy:bottomy+1] | |
text = pytesseract.image_to_string(Cropped, config='--psm 11') | |
img = cv2.resize(img,(500,800)) | |
Cropped = cv2.resize(Cropped,(400,200)) | |
text = re.sub('[^A-Za-z0-9]+', '', text) | |
s = text | |
result = s.translate(table) | |
final_text =result[-10:] | |
cv2.imshow('Cropped',Cropped) | |
cv2.imshow('car',img) | |
print("License Plate Recognition\n") | |
print("Detected license plate Number is:",final_text) | |
cv2.waitKey()== 13 | |
cv2.waitKey()== 13 | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment