Advertisement
webbersof

Checking car plates

Feb 13th, 2022
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import cv2
  2. import imutils
  3. import numpy as np
  4. import pytesseract
  5.  
  6. pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
  7.  
  8. img = cv2.imread('kia.jpg', cv2.IMREAD_COLOR)
  9. img = cv2.resize(img, (600, 400))
  10.  
  11. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. gray = cv2.bilateralFilter(gray, 13, 15, 15)
  13.  
  14. edged = cv2.Canny(gray, 30, 200)
  15. contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  16. contours = imutils.grab_contours(contours)
  17. contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
  18. screenCnt = None
  19.  
  20. for c in contours:
  21.  
  22.     peri = cv2.arcLength(c, True)
  23.     approx = cv2.approxPolyDP(c, 0.018 * peri, True)
  24.  
  25.     if len(approx) == 4:
  26.         screenCnt = approx
  27.         break
  28.  
  29. if screenCnt is None:
  30.     detected = 0
  31.     print("No contour detected")
  32. else:
  33.     detected = 1
  34.  
  35. if detected == 1:
  36.     cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)
  37.  
  38. mask = np.zeros(gray.shape, np.uint8)
  39. new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, )
  40. new_image = cv2.bitwise_and(img, img, mask=mask)
  41.  
  42. (x, y) = np.where(mask == 255)
  43. (topx, topy) = (np.min(x), np.min(y))
  44. (bottomx, bottomy) = (np.max(x), np.max(y))
  45. Cropped = gray[topx:bottomx + 1, topy:bottomy + 1]
  46.  
  47. text = pytesseract.image_to_string(Cropped, config='--psm 11')
  48. print("programming_fever's License Plate Recognition\n")
  49. print("Detected license plate Number is:", text)
  50. img = cv2.resize(img, (500, 300))
  51. Cropped = cv2.resize(Cropped, (400, 200))
  52. cv2.imshow('car', img)
  53. cv2.imshow('Cropped', Cropped)
  54.  
  55. cv2.waitKey(0)
  56. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement