Untitled

 avatar
user_3431143
plain_text
a year ago
1.6 kB
1
Indexable
Never
import torch
import requests
import cv2
import re

# Configuration
rtsp_url = "rtsp://username:password@ip_address:port/path"
webhook_url = "https://your-webhook-url.com"
model_path = "path/to/yolov5s.pt"  # Update this path with your YOLOv5 model file
conf_threshold = 0.25

# Load YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path)
model.conf = conf_threshold

# 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):
    results = model(frame)

    for *xyxy, conf, cls in results.xyxy[0]:  # xyxy are the box coordinates
        x1, y1, x2, y2 = int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])
        cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
        plate_roi = frame[y1:y2, x1:x2]
        gray = cv2.cvtColor(plate_roi, cv2.COLOR_BGR2GRAY)
        text = pytesseract.image_to_string(gray, config="-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 --psm 6 --oem 3")
        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

    detect_number_plate(frame)

cap.release()
cv2.destroyAllWindows()