简单的车牌号识别

目录

  • 处理流程与界面
  • 各接口编写时遇到的一些问题
    • 上传图片
    • 识别结果标签显示中文

      处理流程与界面

      首先点击“上传图片”按钮,可以选择文件夹中含有汽车车牌的图片,并显示在“图片框”中。

      点击“检测车牌”按钮,会先对“图片框”中即含有汽车车牌的图片使用自训练的yolov5目标检测模型进行处理,框选出车牌号部分;然后将框选出的车牌号部分使用paddlehub进行处理,得到车牌号的识别结果;然后将识别结果和识别框添加到含有汽车车牌的图片上,并更新显示在“图片框”中。

      可以看到识别的结果中,中文显示为???,字母和数字正常显示,通过查询资料可知,yolov5 2.0分支的源代码中,detect.py中plot_one_box添加识别结果字符串的具体实现是通过cv2.putText()完成,而putText()不支持非ASCII码的字符。从OpenCV 5.0开始,putText()也支持这些非ASCII码的字符OpenCV 4.x也是可以用putText()绘制中文的,不过需要使用opencv_contrib仓库的freetype模块提供的putText()。freetype模块是对FreeType2的封装,在编译安装OpenCV时需要确保已安装第三方库freetype和harfbuzz,并且打开-DWITH_FREETYPE=ON。查找发现另一种使用稍简单的方法,使用ImageDraw实现,修改代码后界面如下:

      各接口编写时遇到的一些问题

      上传图片

      点击“上传图片”按钮后,通过信号和槽机制,触发open_image()方法

      self.upload_btn.clicked.connect(self.open_image)
      

      open_image()实现如下:

      def open_image(self):
              self.lic_label.setText('')
              self.imgName, self.imgType = QFileDialog.getOpenFileName(self.win, "打开图片", "img", "*.jpg;*.tif;*.png;;All Files(*)")
              if self.imgName == "":
                  return 0
              ori_img = QPixmap(self.imgName).scaled(self.pics[0].width(), self.pics[0].height(), Qt.KeepAspectRatio)
              self.pics[0].setPixmap(ori_img)
      

      需要注意的是使用QFileDialog.getOpenFileName()打开一个文件,传递的第一个参数的类型需为QWidget类型或其子类,否则控制台会出现如下提示,传入的参数类型不匹配

      self.imgName, self.imgType = QFileDialog.getOpenFileName(self, "打开图片", "img", "*.jpg;*.tif;*.png;;All Files(*)")
      TypeError: getOpenFileName(parent: Optional[QWidget] = None, caption: Optional[str] = '', directory: Optional[str] = '', filter: Optional[str] = '', initialFilter: Optional[str] = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'xxx'
      

      识别结果标签显示中文

      使用PIL库中的Image,ImageDraw,ImageFont实现puttext_zn.py

      import cv2
      import numpy as np
      from PIL import Image, ImageDraw, ImageFont
      class PutTextZn():
          def cv2ImgAddText(self, img, text, left, top, textColor=(0, 255, 0), textSize=20):
              if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
                  img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
              # 创建一个可以在给定图像上绘图的对象
              draw = ImageDraw.Draw(img)
              # 字体的格式
              fontStyle = ImageFont.truetype(
                  "simsun.ttc", textSize, encoding="utf-8")
              # 绘制文本
              draw.text((left, top), text, textColor, font=fontStyle)
              # 转换回OpenCV格式
              return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
      

      在yolov5 2.0分支的源代码detect.py中,检测结果中的xyxy为一个存有检测结果框的列表

      x1, y1, x2, y2 = xyxy
      

      其中x1,y1为检测结果框的左上角点的坐标;x2,y2为检测结果框的右下角点的坐标。x1,y1即分别对应cv2ImgAddText()方法的形参列表中的left,top

      put_text_zn = PutTextZn()
      lic_reco_img = put_text_zn.cv2ImgAddText(im0, ocr_res, int(x1), int(y1 - 30), (255, 0, 0), 30) #rgb
      

      im0为numpy.ndarray类型,其如何使用QLabel即self.pics[0]显示呢,可以使用以下方法

      lic_img = QImage(im0.data, im0.shape[1], im0.shape[0], im0.shape[1]*3, QImage.Format_BGR888)
      lic_pix = QPixmap.fromImage(lic_img)
      self.pics[0].setPixmap(lic_pix)