树莓派超声波车牌识别系统

树莓派车牌识别系统

Image

本系统使用树莓派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, sh1106
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 = sh1106(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)

猜你喜欢

转载自blog.csdn.net/qq_33259323/article/details/109040669