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

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

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

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

從視頻文件中檢測車牌

先決條件

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

pip install opencv-python ultralytics easyocr Pillow numpy

實(shí)施步驟

步驟1:初始化庫

我們將首先導(dǎo)入必要的庫。我們將使用OpenCV進(jìn)行視頻處理,使用YOLO進(jìn)行目標(biāo)檢測,并使用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:處理視頻幀

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

結(jié)論

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

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

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目標(biāo)檢測

2023-02-07 09:09:48

視頻文件存儲

2024-01-29 08:21:59

AndroidOpenCV車牌

2017-09-22 11:45:10

深度學(xué)習(xí)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

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

2024-12-23 06:30:00

目標(biāo)檢測圖像分類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提取圖像
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 国产精品99久久免费观看 | 激情一区二区三区 | 97福利在线 | 久久av一区二区三区 | 欧美一区二区三区在线看 | 成人不卡视频 | 91看片在线 | 国产欧美三区 | 国产福利资源在线 | 一级片视频免费观看 | 91激情视频| 国产精品成人一区二区 | 免费观看黄色一级片 | 特黄色一级毛片 | 成人h电影在线观看 | 亚洲精品在线看 | 国产精品99久久久久久大便 | 欧美日韩国产在线 | 亚洲精品久久久一区二区三区 | 天天操天天插 | 午夜免费电影院 | 精品久久久久久亚洲国产800 | 欧美一区免费在线观看 | 日韩一区二区在线播放 | 日韩中文字幕一区二区 | 在线看日韩 | 国产精品一区二区三区免费观看 | 伊人免费在线 | 亚洲精品一区二区冲田杏梨 | 99福利视频 | 国产一级在线视频 | 国产精品久久久久久久久久三级 | 欧美日韩福利视频 | 欧美一区二区三区在线看 | 国产中文字幕网 | 黄网站免费在线观看 | 国精日本亚洲欧州国产中文久久 | 亚洲一区在线日韩在线深爱 | 欧美性猛交 | 精品久久电影 | 日韩在线一区二区三区 |