这篇文章主要介绍树莓派如何实现超声波车牌识别系统,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
本系统使用树莓派4B
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1325, ssd1331, sh2106
from luma.core.virtual import terminal
import os
import time
from PIL import ImageFont
from aip import AipOcr
from picamera import PiCamera
from time import sleep
#导入 GPIO库
import RPi.GPIO as GPIO
import time
#设置 GPIO 模式为 BCM
GPIO.setmode(GPIO.BCM)
#定义 GPIO 引脚
GPIO_TRIGGER = 27
GPIO_ECHO = 17
#设置 GPIO 的工作方式 (IN / OUT)
GPIO.setwarnings(False)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
serial = i2c(port=1, address=0x3C)
device = sh2106(serial)
APP_ID = 'XXX'
API_KEY = 'YYY'
SECRET_KEY = 'ZZZ'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def make_font(name, size):
font_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), 'fonts', name))
return ImageFont.truetype(font_path, size, encoding="utf-8")
font = make_font("/home/pi/Python/1602/msyh.ttc", 20)
def distance():
# 发送高电平信号到 Trig 引脚
GPIO.output(GPIO_TRIGGER, True)
# 持续 10 us
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start_time = time.time()
stop_time = time.time()
# 记录发送超声波的时刻1
while GPIO.input(GPIO_ECHO) == 0:
start_time = time.time()
# 记录接收到返回超声波的时刻2
while GPIO.input(GPIO_ECHO) == 1:
stop_time = time.time()
# 计算超声波的往返时间 = 时刻2 - 时刻1
time_elapsed = stop_time - start_time
# 声波的速度为 343m/s, 转化为 34300cm/s。
distance = (time_elapsed * 34300) / 2
print("距离 = {:.2f} cm".format(distance))
return distance
def i2c_12864_print(x,y,text):
with canvas(device) as draw:
draw.text((x, y), text, fill="white", font=font)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
while True:
print("测量长度")
csblength = distance()
if csblength < 200:
print("程序开始,拍摄照片")
camera = PiCamera()
camera.resolution = (1024, 768)
camera.start_preview()
camera.capture('/home/pi/Python/1602/image.jpg')
camera.stop_preview()
print("拍摄结束")
image = get_file_content('image.jpg')
result = client.licensePlate(image);
print(result);
carNumber = result["words_result"]["number"]
i2c_12864_print(0,0,carNumber)
break
sleep(1)
以上是“树莓派如何实现超声波车牌识别系统”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/KWYDM/blog/4680515