Certainly!
Below is a Python code that utilizes a Raspberry Pi with an ultrasonic
sensor and a Pi camera to capture images of vehicles passing by. It uses OpenCV for
image processing, Tesseract for OCR (Optical Character Recognition), and requests
module for sending data to a website.
Make sure you have OpenCV ( cv2), Tesseract ( pytesseract), and requests ( requests)
installed on your Raspberry Pi. You can install them using pip:
pip install opencv-python pytesseract requests
Here's the code:
import cv2
import numpy as np
import pytesseract
import requests
from time import sleep
# Initialize ultrasonic sensor
# Assume you have connected the ultrasonic sensor and configured it properly
# Function to capture image using Pi camera
def capture_image():
camera = cv2.VideoCapture(0)
return_value, image = camera.read()
cv2.imwrite('vehicle_image.jpg', image)
del(camera)
# Function to perform OCR on the captured image
def ocr_image(image_path):
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply any additional preprocessing steps here if required
text = pytesseract.image_to_string(gray)
return text
# Function to send data to website
def send_to_website(license_plate):
url = "YOUR_WEBSITE_URL"
payload = {'license_plate': license_plate}
response = requests.post(url, data=payload)
if response.status_code == 200:
print("Data sent successfully!")
else:
print("Failed to send data. Status code:", response.status_code)
# Main loop
while True:
# Assuming the ultrasonic sensor triggers when a vehicle is within a certain range
# You need to implement the code to check the ultrasonic sensor and trigger
accordingly
if ultrasonic_sensor_trigger():
print("Vehicle detected!")
capture_image()
plate_text = ocr_image('vehicle_image.jpg')
print("Detected license plate:", plate_text)
send_to_website(plate_text)
sleep(2) # Delay to avoid multiple detections from the same vehicle
In this code:
The capture_image() function captures an image using the Pi camera and saves
it as 'vehicle_image.jpg'.
The ocr_image() function performs OCR on the captured image using Tesseract
and returns the text recognized.
The send_to_website() function sends the recognized license plate text to a
website. Replace 'YOUR_WEBSITE_URL' with the actual URL where you want
to send the data.
The main loop continuously checks the ultrasonic sensor. When it detects a
vehicle, it captures an image, performs OCR, sends the license plate data to
the website, and then waits for a short delay to avoid multiple detections from
the same vehicle.