树莓派应用--AI项目实战篇来啦-10.OpenCV进行车牌检测

1. 介绍

本项目使用 esseract、OpenCV和Python探索光学字符识别(OCR)的神奇世界,本项目将

带你了解最受欢迎的OCR引擎 Tesseract 背后的技术,以及如何用 Pytesseract 和 OpenCV实现字符识别。

从图像中检测字符的技术称为 OCR(光学字符识别),使用 Pytesseract 库很容易实现。

2.Python pytesseract 字符识别OCR库介绍

Python 提供了支持 Tesseract-OCR 引擎的 Python 版本的库 pytesseract, pytesseract是一款用于光学字符识别(OCR)的python工具,即从图片中识别出和"读取"其中嵌入的文字。

pytesseract 是对 Tesseract-OCR的一层封装,同时也可以单独作为对 Tesseract 引擎的调用脚本,支持使用 PIL库(Python Imaging Library)读取各种图片文件类型,包括jpeg、png、gif、bmp、tiff 等其它格式。作为脚本使用时,pytesseract 将打印识别出的文字,而不是将其写入文件。

2.1 Pytesseract 字符识别库安装

复制代码
sudo apt-get update 
sudo apt-get install libleptonica-dev 
sudo apt-get install tesseract-ocr 
sudo apt-get install libtesseract-dev 
pip3 install pytesseract 
pip3 install pillow

2.2 车牌识别相关操作

1.车牌检测:第一步是从汽车上检测车牌所在位置。使用OpenCV中矩形的轮廓检测来寻找车牌,如果我们知道车牌的确切尺寸,颜色和大致位置,则可以提高准确性。通常,也会将根据摄像机的位置和该特定国家/地区所使用的车牌类型来训练检测算法。但是图像可能并没有汽车的存在,在这种情况下我们将先进行汽车的,然后是车牌。

2.字符分割:检测到车牌后,必须将其裁剪并保存为新图像。同样,这可以使用OpenCV 来完成。

  1. 字符识别,在上一步中获得的新图像肯定可以写上一些字符(数字/字母)。因此,我们可以对其执行 OCR(光学字符识别)以检测数字。

3.源程序代码

python 复制代码
import cv2
import imutils
import numpy as np
import pytesseract

img  = cv2.imread('./images/car2.jpg',cv2.IMREAD_COLOR)

img = cv2.resize(img, (600,400) )

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
gray = cv2.bilateralFilter(gray, 13, 15, 15)

edged = cv2.Canny(gray, 30, 200) 
contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None

# 载入显示库
def bgr8_to_jpeg(value, quality=75):
    return bytes(cv2.imencode('.jpg', value)[1])
    
import ipywidgets.widgets as widgets
from IPython.display import display
imagecar = widgets.Image(format='jpeg', width=600, height=400)
imageCropped = widgets.Image(format='jpeg', width=600, height=400)
display(imagecar)
display(imageCropped)

for c in contours:
    
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.018 * peri, True)
 
    if len(approx) == 4:
        screenCnt = approx
        break

if screenCnt is None:
    detected = 0
    print ("No contour detected")
else:
     detected = 1

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

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)

(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]

text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("programming_fever's License Plate Recognition\n")
print("Detected license plate Number is:",text)
img = cv2.resize(img,(500,300))
Cropped = cv2.resize(Cropped,(400,200))
# 显示图像
imagecar.value = bgr8_to_jpeg(img)
imageCropped.value = bgr8_to_jpeg(Cropped)

相关推荐
运器1238 分钟前
【一起来学AI大模型】PyTorch DataLoader 实战指南
大数据·人工智能·pytorch·python·深度学习·ai·ai编程
超龄超能程序猿23 分钟前
(5)机器学习小白入门 YOLOv:数据需求与图像不足应对策略
人工智能·python·机器学习·numpy·pandas·scipy
卷福同学24 分钟前
【AI编程】AI+高德MCP不到10分钟搞定上海三日游
人工智能·算法·程序员
帅次31 分钟前
系统分析师-计算机系统-输入输出系统
人工智能·分布式·深度学习·神经网络·架构·系统架构·硬件架构
AndrewHZ1 小时前
【图像处理基石】如何入门大规模三维重建?
人工智能·深度学习·大模型·llm·三维重建·立体视觉·大规模三维重建
5G行业应用1 小时前
【赠书福利,回馈公号读者】《智慧城市与智能网联汽车,融合创新发展之路》
人工智能·汽车·智慧城市
悟空胆好小1 小时前
分音塔科技(BABEL Technology) 的公司背景、股权构成、产品类型及技术能力的全方位解读
网络·人工智能·科技·嵌入式硬件
探讨探讨AGV1 小时前
以科技赋能未来,科聪持续支持青年创新实践 —— 第七届“科聪杯”浙江省大学生智能机器人创意竞赛圆满落幕
人工智能·科技·机器人
cwn_1 小时前
回归(多项式回归)
人工智能·机器学习·数据挖掘·回归
聚客AI2 小时前
🔥 大模型开发进阶:基于LangChain的异步流式响应与性能优化
人工智能·langchain·agent