成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用 YOLO 和 EasyOCR 從視頻文件中檢測車牌

開發 后端 深度學習
本文中的代碼展示了如何綜合運用YOLO和EasyOCR技術,從視頻文件中檢測并識別車牌。

本文將介紹如何通過Python中的YOLO(ou Only Look Once)和EasyOCR(光學字符識別)技術來實現從視頻文件中檢測車牌。本技術依托于深度學習,以實現車牌的即時檢測與識別。

從視頻文件中檢測車牌

先決條件

在我們開始之前,請確保已安裝以下Python包:

pip install opencv-python ultralytics easyocr Pillow numpy

實施步驟

步驟1:初始化庫

我們將首先導入必要的庫。我們將使用OpenCV進行視頻處理,使用YOLO進行目標檢測,并使用EasyOCR讀取檢測到的車牌上的文字。

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:處理視頻幀

我們將從視頻文件中讀取每一幀,處理它以檢測車牌,然后應用OCR來識別車牌上的文字。為了提高性能,我們可以跳過每第三幀的處理。

# 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()

代碼解釋:

  • 啟動EasyOCR:設置EasyOCR以識別英文字符。
  • 導入YOLO模型:從特定路徑載入YOLO模型,需替換為模型的實際路徑。
  • 視頻幀讀取:利用OpenCV打開視頻文件,若需保存輸出,則啟動VideoWriter。
  • 幀尺寸調整與處理:逐幀讀取并調整尺寸,隨后使用模型預測車牌位置。
  • 繪制識別結果:在視頻幀上標出識別到的車牌邊界框,并裁剪出車牌區域以進行OCR識別。
  • 執行OCR:EasyOCR識別裁剪后的車牌圖像中的文本,并在幀上展示識別結果及置信度。
  • 視頻輸出:處理后的視頻幀可顯示在窗口中,也可以選擇保存為視頻文件。

結論

本段代碼展示了如何綜合運用YOLO和EasyOCR技術,從視頻文件中檢測并識別車牌。遵循這些步驟,你可以為自己的需求構建相似的系統。根據實際情況,靈活調整參數和優化模型性能。

責任編輯:趙寧寧 來源: 小白玩轉Python
相關推薦

2023-01-29 14:29:59

Python識別車牌

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目標檢測

2023-02-07 09:09:48

視頻文件存儲

2024-01-29 08:21:59

AndroidOpenCV車牌

2017-09-22 11:45:10

深度學習OpenCVPython

2009-09-18 08:50:14

Windows 7Real文件識別

2022-09-27 10:07:01

要使用 source

2024-10-09 17:02:34

2021-01-03 14:43:43

深度學習人工智能視頻

2024-12-23 06:30:00

目標檢測圖像分類YOLO

2013-03-06 09:41:29

2023-11-20 09:47:14

自動駕駛視覺

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提取圖像
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品日韩欧美一区二区三区 | 一区二区日韩精品 | 成人免费一区二区三区牛牛 | av中文字幕在线 | 亚洲免费视频一区 | 久久另类 | 色橹橹欧美在线观看视频高清 | 日日精品 | 成人av网站在线观看 | 9999精品视频 | 日韩av在线免费 | 精品日韩在线观看 | 亚洲视频免费在线观看 | 欧美一级久久 | 高清视频一区 | 九九久久精品 | 黄色毛片在线观看 | 本道综合精品 | 综合久久av | 久久精品屋 | 国产成人精品久久二区二区91 | 国产高清精品一区二区三区 | 免费看国产一级特黄aaaa大片 | 在线观看成人小视频 | 91网站在线看| 国产精品久久久久久久久久免费看 | 欧美日韩1区2区 | 亚洲精品视频三区 | 久久精品一区 | 国产av毛片 | 岛国av免费观看 | 91精品无人区卡一卡二卡三 | 国产最好的av国产大片 | 日韩在线观看一区 | 99re99| 欧美国产视频 | 国产综合久久 | 亚洲一区网站 | 亚洲欧美一区二区三区国产精品 | 国产成人亚洲精品 | 久久久国产精品 |