[Python] Pytesseract not able to extract vehicle number plate text

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have designed code to detect number plates succesfully,But problem is arising when i need to extract number plate information using pytesseteract and store it in excel,It is not extracting number plate.Tried multiple attempts but didn't work,It would be great if someone helps me out.below is the code:

    import cv2
    import pytesseract
    from ultralytics import YOLO
    import torch

    pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

    model=YOLO('C:\\users\\license_plate_detector.pt')
    video_path='C:\\users\\traffic_flow.mp4'
    cap = cv2.VideoCapture(video_path)

    def resize_frame(frame, size=(640,640)):
    return cv2.resize(frame, size, interpolation=cv2.INTER_LINEAR)

    license_plates = []

    while cap.isOpened():
    ret,frame = cap.read()
    if not ret:
    break

    frame_resized = resize_frame(frame)
    frame_tensor = torch.tensor(frame_resized).permute(2,0,1).unsqueeze(0).float() / 255.0
    results = model(frame_sensor)

    for result in results:
    for bbox in result.boxes.xyxy:
    bbox = bbox.int().tolist()
    roi = frame_resized[bbox[1]:bbox[3], bbox[0]:bbox[2]]
    roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    roi_thresh = cv2.threshold(roi_gray,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    plate_text = pytesseract.image_to_string(roi_thresh, config='--psm 8')
    license_plates.append(plate_text.strip())
    cv2.rectangle(frame_resized, (bbox[0],bbox[1]),(bbox[2],bbox[3]), (0,255,0),2)
    cv2.putText(frame_resized, 'License Plate',(bbox[0],bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)
    cv2.imshow('License plate detection', frame_resized)

    if cv2.waitKey(1) & 0xFF == ord('q'):
    break

    cap.release()
    cv2.destroyAllWindows()

    df = pd.DataFrame(license_plates, columns=['License Plate'])
    df.to_excel('extracted_license_plates.xlsx'. index=False)


    [​IMG]

    Continue reading...

Compartilhe esta Página