如何使用python在实时视频文件中裁剪车辆牌照

如何使用python在实时视频文件中裁剪车辆牌照,python,ocr,tesseract,object-detection,Python,Ocr,Tesseract,Object Detection,我已经使用SSD-Mobilenet和Tensorflow构建了我的自定义对象检测模型。现在,我必须从视频文件中裁剪车牌号,并将其与车型名称一起显示在边界框顶部 i5处理器 NVIDIA GeForce MX150,配备2GB VRAM import numpy as np import cv2 import pytesseract import matplotlib.pyplot as plt img = cv2.imread('/home/dora/Desktop/image1.jpg')

我已经使用
SSD-Mobilenet
Tensorflow
构建了我的自定义对象检测模型。现在,我必须从视频文件中裁剪车牌号,并将其与车型名称一起显示在边界框顶部

i5处理器 NVIDIA GeForce MX150,配备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()
以上代码从图像中裁剪车牌


我想从视频文件中剪切。

看起来您想读取视频。在本例中,我将使用cv2的
VideoCapture

如果将所有处理放在一个方法中,则可以在每一帧上调用它

。
.
.
视频=cv2.视频捕获(“”)
ret=真
while ret:#在有更多帧时运行
帧,ret=video.read()#获取下一帧
处理(帧)#进行处理
video.release()

看起来你想看视频。在本例中,我将使用cv2的
VideoCapture

如果将所有处理放在一个方法中,则可以在每一帧上调用它

。
.
.
视频=cv2.视频捕获(“”)
ret=真
while ret:#在有更多帧时运行
帧,ret=video.read()#获取下一帧
处理(帧)#进行处理
video.release()

我试试这个。谢谢。我试试这个。谢谢。