0

So basically I'm making this program that detects car plates from a camera. I want to write to a excel file the correct time that the camera detects the car plate in hh:mm:ss format and the time that the car plates disappears from the camera's view. So the excel file should contain the plate, the date that the car plate was captured, the time that the camera first captured the car plate, the time that the camera left the camera's view and the duration the refers to the length that the car plate stayed on the camera's view. For instance say the program detects the car with the plate AC 241BD at 19:01:43 on 22/02/2023. The car stays there for about 4 minutes and leaves at 19:06:43. So in the excel file I should write car_plate equal to AC 241BD the date to 22/02/2023 the starting_time to 19:01:43 the leaving_time to 19:06:43 and the duration to 00:04:00. How can I implement that? Also I need to implement ocr in this code so I can extract the car plate from the camera and print it out. I tried using pytesseract and easyocr but the ocr was incorrect most of the time. I don't know if I need to tune the images with more effects like canny or smth else or it's the library fault but I guess it's mine. I'm failing to apply the logic since I'm junior and had no previous experience with opencv tasks. My biggest challenge is the excel problem. Any help?

Here's the code:

import cv2
from openpyxl import Workbook, load_workbook
import os

# Set the cascade indetifier
plate_cascade = cv2.CascadeClassifier('C:/Users/35568/PycharmProjects/pythonProject1/venv/Lib/site-packages/cv2/data/haarcascade_russian_plate_number.xml')

# Activate the front camera
cap = cv2.VideoCapture(0)

min_area = 500

# Print the error message if the program fails to open
if not cap.isOpened():
    print('Error opening video file')
    exit()

# Retreive the desktop path of the user and create the filename variable to be created later
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
filename = os.path.join(desktop, 'plates.xlsx')


# Check if the filename exist
try:
    wb = load_workbook(filename)
    ws = wb.active
except FileNotFoundError:
    wb = Workbook()
    ws = wb.active
    ws.append(['Plate', 'Date', 'Start Time', 'End Time', 'Duration'])


while True:
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    plates = plate_cascade.detectMultiScale(gray, 1.1, 4)

    for (x, y, w, h) in plates:
        area = w * h

        if area > min_area:

            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            plate_img = gray[y:y + h, x:x + w]
            cv2.imshow("Car Plate", plate_img)

    cv2.imshow('Video', frame)
    key = cv2.waitKey(25)
    if key == ord('q') or key == 27:
        break
    if cv2.getWindowProperty('Video', cv2.WND_PROP_VISIBLE) < 1:
        break

cap.release()
cv2.destroyAllWindows()

Any help would be greatly appreciated. I apologize for the bad explanation but I really need help with this task since it's pretty important for me and my job.

I tried using pytesseract and easyocr to extract the car plate from the video but none of the seemed to work. It failed to convert the car plate to text and most of the time it printed out gibberish. About the excel problem I cannot think of a solution. Frames Example

s0ld13r
  • 11
  • 2
  • You might get some help if you show an example frame. Are you sure you have a good frame? You do an imshow(). Does it look OK? Is plate_img OK? You show that also. – fmw42 Feb 24 '23 at 20:04
  • I hope I did exactly what u requested. – s0ld13r Feb 27 '23 at 15:03

0 Answers0