如何将车牌提取为单独的图像,并在提取的车牌上使用OCR。

问题描述 投票:0回答:1

我是图像处理的新手,作为我最后一年项目的一部分,我应该创建一个号码牌识别系统。到目前为止,我能够,1.将一个视频分离成相应的帧。 2. 使用ImageAI与模型RetinaNet来检测和隔离车辆3. 通过迭代每个帧提取车辆,并设定参数(最小概率=75,加载模式=更快)目前,我的需求是从上述第3部分提取的车辆中分离出号牌,并将其转换为文本。

我发现下面的代码 https:/cppsecrets.comusers1001989710897107975054485364103109971051084699111109Automatic-Vehicle-Number-Plate-Recognition-using-OpenCV-and-Tesseract-OCR-A-Real-World-Python-project.php。

当我运行以下图片的代码时 测试图像

def main():
   import numpy as np
   import cv2
   import imutils
   import sys
   import pytesseract
   import pandas as pd
   import time

  pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

  img = cv2.imread('C:\\Users\\priyan\\Desktop\\Number plate Identification\\Test 
                   Detection\\imagenew.jpg-objects\\car-6.jpg')

  img = imutils.resize(img, width=500)
  cv2.imshow("Original Image", img)  #Show the original image
  cv2.waitKey(0)

  gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  cv2.imshow("Preprocess 1 - Grayscale Conversion", gray_img)  
  cv2.waitKey(0)

  gray_img = cv2.bilateralFilter(gray_img, 11, 17, 17)
  cv2.imshow("Preprocess 2 - Bilateral Filter", gray_img) 
  cv2.waitKey(0)

  c_edge = cv2.Canny(gray_img, 120, 200)
  cv2.imshow("Preprocess 3 - Canny Edges", c_edge)       
  cv2.waitKey(0)

  cnt, new = cv2.findContours(c_edge, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

  cnt = sorted(cnt, key = cv2.contourArea, reverse = True)[:30]
  NumberPlateCount = None
  im2 = img.copy()
  cv2.drawContours(im2, cnt, -1, (0,255,0), 2)
  cv2.imshow("Top 30 Contours", im2)
  cv2.waitKey(0)

  count = 0
  for c in cnt:
      perimeter = cv2.arcLength(c, True)      
      approx = cv2.approxPolyDP(c, 0.02 * perimeter, True)
      if len(approx) == 4:            
        NumberPlateCount = approx
        break

  masked = np.zeros(gray_img.shape,np.uint8)
  new_image1 = cv2.drawContours(masked,[NumberPlateCount],0,255,-1)
  new_image = cv2.bitwise_and(img,img,mask=masked)
  cv2.imshow("4 - Final_Image",new_image)     #The final image showing only the number plate.
  cv2.waitKey(0)

  #Configuration for tesseract
  configr = ('-l eng --oem 1 --psm 3')
  #Running Tesseract-OCR on final image.
  text_no = pytesseract.image_to_string(new_image, config=configr)
  #The extracted data is stored in a data file.
  data = {'Date': [time.asctime(time.localtime(time.time()))],'Vehicle_number': [text_no]}
  df = pd.DataFrame(data, columns = ['Date', 'Vehicle_number'])
  df.to_csv('Dataset_VehicleNo.csv')
  #Printing the recognized text as output.
  print(text_no)
  cv2.waitKey(0)

  if __name__ == '__main__':
      main()

最后的结果是,我只得到了 "原始图像到4-最终图像 "的图像集,并出现了错误信息。

new_image1 = cv2.drawContours(masked,[NumberPlateCount],0,255,-1)cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencvmodules\imgproc\src\drawing.cpp:2606: error: (-215:断言失败) reader.ptr != NULL in function 'cvDrawContours'

谁能帮助我在获得适当的投资回报率的车牌和所需的输出O。f CAU 2112 the text of the Numberplate.

python opencv python-tesseract
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.