| import cv2
|
| import pandas as pd
|
| import numpy as np
|
| import pytesseract
|
| import os
|
| from ultralytics import YOLO
|
| from datetime import datetime
|
|
|
|
|
| pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
|
|
|
|
| model = YOLO('best.pt')
|
|
|
|
|
| area = [(27, 250), (16, 310), (1015, 310), (992, 250)]
|
|
|
|
|
| processed_numbers = set()
|
|
|
|
|
| cap = cv2.VideoCapture('mycarplate.mp4')
|
|
|
|
|
| with open("coco1.txt", "r") as file:
|
| class_list = file.read().splitlines()
|
|
|
|
|
| csv_file = "car_plate_data_stored.csv"
|
|
|
|
|
| try:
|
| pd.read_csv(csv_file)
|
| except FileNotFoundError:
|
| with open(csv_file, "w") as file:
|
| file.write("ImageFile,Date,Time,Confidence\n")
|
|
|
| frame_count = 0
|
|
|
|
|
| def correct_characters(text):
|
| replacements = {
|
| '0': 'O',
|
| 'O': 'D',
|
| 'I': '1',
|
| 'Q': '0'
|
| }
|
| corrected_text = ''.join(replacements.get(c, c) for c in text)
|
| return corrected_text
|
|
|
|
|
| def format_plate_text(text):
|
| text = ''.join(filter(str.isalnum, text))
|
| if len(text) == 10:
|
| return f"{text[:2]} {text[2:4]} {text[4:6]} {text[6:]}"
|
| return text
|
|
|
|
|
| def deskew_image(image):
|
|
|
| if len(image.shape) == 3:
|
| gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| else:
|
| gray = image
|
|
|
|
|
| edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
|
|
|
|
| lines = cv2.HoughLines(edges, 1, np.pi / 180, 100)
|
|
|
| if lines is not None:
|
| for line in lines:
|
| rho, theta = line[0]
|
| a = np.cos(theta)
|
| b = np.sin(theta)
|
| x0 = a * rho
|
| y0 = b * rho
|
| x1 = int(x0 + 1000 * (-b))
|
| y1 = int(y0 + 1000 * (a))
|
| x2 = int(x0 - 1000 * (-b))
|
| y2 = int(y0 - 1000 * (a))
|
| cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
|
|
| return image
|
|
|
|
|
| while True:
|
| ret, frame = cap.read()
|
| frame_count += 1
|
|
|
|
|
| if frame_count % 3 != 0:
|
| continue
|
| if not ret:
|
| break
|
|
|
|
|
| frame = cv2.resize(frame, (1020, 500))
|
|
|
|
|
| results = model.predict(frame)
|
| detected_boxes = results[0].boxes.data
|
| detections = pd.DataFrame(detected_boxes).astype("float")
|
|
|
| for _, row in detections.iterrows():
|
| x1, y1, x2, y2 = int(row[0]), int(row[1]), int(row[2]), int(row[3])
|
| class_id = int(row[5])
|
| class_name = class_list[class_id]
|
|
|
|
|
| cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
|
|
|
|
|
| if cv2.pointPolygonTest(np.array(area, np.int32), (cx, cy), False) >= 0:
|
|
|
| crop = frame[y1:y2, x1:x2]
|
| gray_crop = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
| gray_crop = cv2.bilateralFilter(gray_crop, 10, 20, 20)
|
| _, threshold_crop = cv2.threshold(gray_crop, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
|
|
|
|
| threshold_crop = deskew_image(threshold_crop)
|
|
|
|
|
| current_date = datetime.now().strftime("%d-%m-%Y")
|
| current_time = datetime.now().strftime("%H:%M:%S")
|
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
|
|
|
|
| detection_folder = "detected_plates"
|
| if not os.path.exists(detection_folder):
|
| os.makedirs(detection_folder)
|
|
|
| image_filename = f"plate_{timestamp}.jpg"
|
| image_path = os.path.join(detection_folder, image_filename)
|
| cv2.imwrite(image_path, crop)
|
|
|
|
|
| if image_filename not in processed_numbers:
|
| processed_numbers.add(image_filename)
|
| with open(csv_file, "a") as file:
|
| confidence = float(row[4])
|
| file.write(f"{image_filename},{current_date},{current_time},{confidence:.2f}\n")
|
|
|
|
|
| cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 1)
|
| cv2.imshow('Detected Plate', threshold_crop)
|
|
|
|
|
| cv2.polylines(frame, [np.array(area, np.int32)], True, (255, 0, 0), 2)
|
| cv2.imshow("RGB", frame)
|
|
|
|
|
| if cv2.waitKey(1) & 0xFF == 27:
|
| break
|
|
|
|
|
| cap.release()
|
| cv2.destroyAllWindows()
|
|
|