Number Plate Recognition and Automatic Gate Opening System using Raspberry Pi

Introduction : It would be great to have an automatic gate opening system so that the human intervention is decreased and also account for the fast and safety of the parking lot. It would also record the Number plate reading of the car so that it could be useful.

So in this project we are building a prototype of the automatic gate opening system for which we’ll be using the concepts of Computer Vision.

Components : Raspberry Pi, Pi camera, Motor(for gate)

Description : Initially we are using the Computer Vision Algorithms and concepts inorder to obtain the image of the Number Plate and also extract the value of it from the surrounding noise. Then once we have the desired numberplate value, we can actuate the motor that is responsible for the gate movement.

So, ill be explaining the step by step working of the Algorithm through the code.

1st we have to blur the image at background leaving only the numberplate. Next we have to convert the image to grayscale and extract the numberplate details(for that we search for a rectangular shape object, as all numberplates are rectangular.) Once we extract that we can save them as a separate image or leave it as it is.

Then finally actuate the Motor.

We will use the contour option in OpenCV to detect for rectangular objects to find the number plate.

It is better to resize the image because it would be easier for the algorithm to operate on the images of same size.

import cv2
import imutils
import numpy as np
import pytesseract
from PIL import Image
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.board)
while(1)
  c = cv2.VideoCapture()
  cv2.imread(c, c.jpg)
img = cv2.imread('c.jpg',cv2.IMREAD_COLOR)              

img = cv2.resize(img, (620,480) )

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey scale
gray = cv2.bilateralFilter(gray, 11, 17, 17) #Blur to reduce noise
edged = cv2.Canny(gray, 30, 200) #Perform Edge detection (other edge detection functions can be used)


cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)   FINDING THE CONTOURS AND KEEPING THE LARGEST ONE.
cnts = imutils.grab_contours(cnt)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None

# loop over our contours
for S in cnt:
 # approximate the contour
 peri = cv2.arcLength(S, True)
 approx = cv2.approxPolyDP(S, 0.018 * peri, True)
 
 
 if len(approx) == 4:
  Cnt = app
  break

if Cnt is None:
 detected = 0
 print "No contour detected"
else:
 detected = 1

if detected == 1:
 cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3)

# Masking the part other than the number plate
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)


#Read the number plate
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("Detected Number is:",text)
for( int i=0; i<1 ;i++)
    motoron()
    delay(100000)
    motoroff() 

cv2.imshow('new_image',img)


cv2.waitKey(0)
cv2.destroyAllWindows()

def motoron()
     
     GPIO.setup(27,GPIO.out)
     GPIO.setup(25.GPIO.out)
     GPIO.output(27,high)
     GPIO.output(25,low)

def motoroff()
     GPIO.setup(27,GPIO.out)
     GPIO.setup(25.GPIO.out)
     GPIO.output(27,low)
     GPIO.output(25,high)
you get a connector pin with the camera and that has to be connected.(I,ve shown it using a single wire).

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.