简体   繁体   中英

How to crop license plate from vehicles in a real time video file using python

I have build my custom object detection model using SSD-Mobilenet and Tensorflow . Now, I have to crop license plate number from the video file and display it along with the model name on top of the boundary box.

i5 processor NVIDIA GeForce MX150 with 2GB VRAM

import numpy as np
import cv2
import pytesseract
import matplotlib.pyplot as plt

img = cv2.imread('/home/dora/Desktop/image1.jpg')
#convert my image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#perform adaptive threshold so that I can extract proper contours from the image
#need this to extract the name plate from the image. 
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
contours,h = cv2.findContours(thresh,1,2)

#once I have the contours list, i need to find the contours which form rectangles.
#the contours can be approximated to minimum polygons, polygons of size 4 are probably rectangles
largest_rectangle = [0,0]
for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    if len(approx)==4: #polygons with 4 points is what I need.
        area = cv2.contourArea(cnt)
        if area > largest_rectangle[0]:
            #find the polygon which has the largest size.
            largest_rectangle = [cv2.contourArea(cnt), cnt, approx]

x,y,w,h = cv2.boundingRect(largest_rectangle[1])
#crop the rectangle to get the number plate.
roi=img[y:y+h,x:x+w]
#cv2.drawContours(img,[largest_rectangle[1]],0,(0,0,255),-1)
plt.imshow(roi, cmap = 'gray')
plt.show()

The above code crops license plate from images.

I want to crop from a video file.

Looks like you want to read a video. In this case I would use cv2's VideoCapture class.

If you put all your processing in a method, you can just call it on every frame.

.
.
.

video = cv2.VideoCapture("<filename>")

ret = True
while ret:                       # run while there are more frames
    frame, ret = video.read()    # get next frame

    process(frame)               # do your processing

video.release()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM