偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

使用 YOLO 和 EasyOCR 從視頻文件中檢測(cè)車牌

開發(fā) 后端 深度學(xué)習(xí)
本文中的代碼展示了如何綜合運(yùn)用YOLO和EasyOCR技術(shù),從視頻文件中檢測(cè)并識(shí)別車牌。

本文將介紹如何通過(guò)Python中的YOLO(ou Only Look Once)和EasyOCR(光學(xué)字符識(shí)別)技術(shù)來(lái)實(shí)現(xiàn)從視頻文件中檢測(cè)車牌。本技術(shù)依托于深度學(xué)習(xí),以實(shí)現(xiàn)車牌的即時(shí)檢測(cè)與識(shí)別。

從視頻文件中檢測(cè)車牌

先決條件

在我們開始之前,請(qǐng)確保已安裝以下Python包:

pip install opencv-python ultralytics easyocr Pillow numpy

實(shí)施步驟

步驟1:初始化庫(kù)

我們將首先導(dǎo)入必要的庫(kù)。我們將使用OpenCV進(jìn)行視頻處理,使用YOLO進(jìn)行目標(biāo)檢測(cè),并使用EasyOCR讀取檢測(cè)到的車牌上的文字。

import cv2
from ultralytics import YOLO
import easyocr
from PIL import Image
import numpy as np

# Initialize EasyOCR reader
reader = easyocr.Reader(['en'], gpu=False)

# Load your YOLO model (replace with your model's path)
model = YOLO('best_float32.tflite', task='detect')

# Open the video file (replace with your video file path)
video_path = 'sample4.mp4'
cap = cv2.VideoCapture(video_path)

# Create a VideoWriter object (optional, if you want to save the output)
output_path = 'output_video.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, 30.0, (640, 480))  # Adjust frame size if necessary

步驟2:處理視頻幀

我們將從視頻文件中讀取每一幀,處理它以檢測(cè)車牌,然后應(yīng)用OCR來(lái)識(shí)別車牌上的文字。為了提高性能,我們可以跳過(guò)每第三幀的處理。

# Frame skipping factor (adjust as needed for performance)
frame_skip = 3  # Skip every 3rd frame
frame_count = 0

while cap.isOpened():
    ret, frame = cap.read()  # Read a frame from the video
    if not ret:
        break  # Exit loop if there are no frames left

    # Skip frames
    if frame_count % frame_skip != 0:
        frame_count += 1
        continue  # Skip processing this frame

    # Resize the frame (optional, adjust size as needed)
    frame = cv2.resize(frame, (640, 480))  # Resize to 640x480

    # Make predictions on the current frame
    results = model.predict(source=frame)

    # Iterate over results and draw predictions
    for result in results:
        boxes = result.boxes  # Get the boxes predicted by the model
        for box in boxes:
            class_id = int(box.cls)  # Get the class ID
            confidence = box.conf.item()  # Get confidence score
            coordinates = box.xyxy[0]  # Get box coordinates as a tensor

            # Extract and convert box coordinates to integers
            x1, y1, x2, y2 = map(int, coordinates.tolist())  # Convert tensor to list and then to int

            # Draw the box on the frame
            cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)  # Draw rectangle

            # Try to apply OCR on detected region
            try:
                # Ensure coordinates are within frame bounds
                r0 = max(0, x1)
                r1 = max(0, y1)
                r2 = min(frame.shape[1], x2)
                r3 = min(frame.shape[0], y2)

                # Crop license plate region
                plate_region = frame[r1:r3, r0:r2]

                # Convert to format compatible with EasyOCR
                plate_image = Image.fromarray(cv2.cvtColor(plate_region, cv2.COLOR_BGR2RGB))
                plate_array = np.array(plate_image)

                # Use EasyOCR to read text from plate
                plate_number = reader.readtext(plate_array)
                concat_number = ' '.join([number[1] for number in plate_number])
                number_conf = np.mean([number[2] for number in plate_number])

                # Draw the detected text on the frame
                cv2.putText(
                    img=frame,
                    text=f"Plate: {concat_number} ({number_conf:.2f})",
                    org=(r0, r1 - 10),
                    fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=0.7,
                    color=(0, 0, 255),
                    thickness=2
                )

            except Exception as e:
                print(f"OCR Error: {e}")
                pass

    # Show the frame with detections
    cv2.imshow('Detections', frame)

    # Write the frame to the output video (optional)
    out.write(frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break  # Exit loop if 'q' is pressed

    frame_count += 1  # Increment frame count

# Release resources
cap.release()
out.release()  # Release the VideoWriter object if used
cv2.destroyAllWindows()

代碼解釋:

  • 啟動(dòng)EasyOCR:設(shè)置EasyOCR以識(shí)別英文字符。
  • 導(dǎo)入YOLO模型:從特定路徑載入YOLO模型,需替換為模型的實(shí)際路徑。
  • 視頻幀讀?。豪肙penCV打開視頻文件,若需保存輸出,則啟動(dòng)VideoWriter。
  • 幀尺寸調(diào)整與處理:逐幀讀取并調(diào)整尺寸,隨后使用模型預(yù)測(cè)車牌位置。
  • 繪制識(shí)別結(jié)果:在視頻幀上標(biāo)出識(shí)別到的車牌邊界框,并裁剪出車牌區(qū)域以進(jìn)行OCR識(shí)別。
  • 執(zhí)行OCR:EasyOCR識(shí)別裁剪后的車牌圖像中的文本,并在幀上展示識(shí)別結(jié)果及置信度。
  • 視頻輸出:處理后的視頻幀可顯示在窗口中,也可以選擇保存為視頻文件。

結(jié)論

本段代碼展示了如何綜合運(yùn)用YOLO和EasyOCR技術(shù),從視頻文件中檢測(cè)并識(shí)別車牌。遵循這些步驟,你可以為自己的需求構(gòu)建相似的系統(tǒng)。根據(jù)實(shí)際情況,靈活調(diào)整參數(shù)和優(yōu)化模型性能。

責(zé)任編輯:趙寧寧 來(lái)源: 小白玩轉(zhuǎn)Python
相關(guān)推薦

2023-01-29 14:29:59

Python識(shí)別車牌

2021-01-11 08:00:00

工具軟件視頻

2024-10-29 16:18:32

YOLOOpenCV

2024-11-29 16:10:31

2025-02-11 08:30:00

2025-02-18 08:00:00

C++YOLO目標(biāo)檢測(cè)

2023-02-07 09:09:48

視頻文件存儲(chǔ)

2024-01-29 08:21:59

AndroidOpenCV車牌

2017-09-22 11:45:10

深度學(xué)習(xí)OpenCVPython

2009-09-18 08:50:14

Windows 7Real文件識(shí)別

2022-09-27 10:07:01

要使用 source

2024-10-09 17:02:34

2021-01-03 14:43:43

深度學(xué)習(xí)人工智能視頻

2024-12-23 06:30:00

目標(biāo)檢測(cè)圖像分類YOLO

2013-03-06 09:41:29

2023-11-20 09:47:14

自動(dòng)駕駛視覺(jué)

2025-01-20 07:00:00

2025-01-22 11:10:34

2024-08-20 09:30:00

2025-02-17 12:00:00

PythonOpenCV提取圖像
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)