Untitled
user_3431143
plain_text
3 years ago
2.9 kB
11
Indexable
Install the following Python libraries:
1. OpenCV: For video processing and analysis.
2. Pytesseract: For OCR (Optical Character Recognition) to read the number plates.
3. Requests: To send the results to a webhook.
Install them using the following commands:
pip install opencv-python-headless
pip install opencv-contrib-python-headless
pip install pytesseract
pip install requests
Python code:
import cv2
import pytesseract
import requests
import re
# Configuration
rtsp_url = "rtsp://username:password@ip_address:port/path"
webhook_url = "https://your-webhook-url.com"
country = "eu" # Choose the appropriate country code for number plate detection
motion_detection = True
motion_threshold = 30 # Adjust this value based on your environment
# Initialize the required variables
prev_frame = None
motion_count = 0
# Load the OpenCV's built-in Haar Cascade for License Plate detection
cascade_path = "path/to/your/cascade.xml" # Update this path with your cascade file
cascade = cv2.CascadeClassifier(cascade_path)
# Connect to the RTSP stream
cap = cv2.VideoCapture(rtsp_url)
def send_to_webhook(plate_number):
data = {"plate_number": plate_number}
response = requests.post(webhook_url, json=data)
if response.status_code == 200:
print("Sent to webhook successfully:", plate_number)
else:
print("Error sending to webhook:", response.status_code, response.text)
def detect_number_plate(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
plates = cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in plates:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
plate_roi = gray[y:y + h, x:x + w]
text = pytesseract.image_to_string(plate_roi, config=f"-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 --psm 6 --oem 3 -l {country}")
plate_number = re.sub(r'\W+', '', text)
if plate_number:
send_to_webhook(plate_number)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if motion_detection:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if prev_frame is None:
prev_frame = gray
continue
frame_delta = cv2.absdiff(prev_frame, gray)
thresh = cv2.threshold(frame_delta, motion_threshold, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
motion_detected = len(contours) > 0
if motion_detected:
motion_count += 1
if motion_count >= 5: # Adjust this value to minimize false positives
detect_number_plate(frame)
else:
motion_count = 0
prev_frame = gray
else:
detect_number_plate(frame)
cap.release()
cvEditor is loading...