自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用 YOLO 檢測眼睛閉合 | 設(shè)定警報

開發(fā) 深度學(xué)習(xí)
我們將探討如何使用OpenCV和YOLO目標(biāo)檢測模型來檢測視頻中的眼睛閉合,并在眼睛閉合超過特定閾值時發(fā)出警告。我們還將討論將這種方法應(yīng)用于實時檢測的可能性。

我們將探討如何使用OpenCV和YOLO目標(biāo)檢測模型來檢測視頻中的眼睛閉合,并在眼睛閉合超過特定閾值時發(fā)出警告。我們還將討論將這種方法應(yīng)用于實時檢測的可能性。眼睛閉合檢測在許多現(xiàn)實世界的應(yīng)用中都是一個重要的用例,從監(jiān)控駕駛員疲勞到確保關(guān)鍵環(huán)境中的警覺性。

先決條件

在開始之前,請確保已安裝以下庫:

  • OpenCV:用于視頻處理和顯示結(jié)果。
  • Ultralytics YOLO:一個流行且高效的深度學(xué)習(xí)模型,用于目標(biāo)檢測。

要安裝必要的庫,你可以運行:

pip install opencv-python
pip install ultralytics

眼睛閉合檢測算法概述

  • 加載YOLO模型:我們加載一個預(yù)訓(xùn)練的YOLO模型(best.pt)用于眼睛檢測。這個模型被訓(xùn)練來識別兩種狀態(tài):“閉合”和“打開”的眼睛。下載模型
  • 讀取輸入視頻:使用OpenCV逐幀讀取視頻。
  • 執(zhí)行目標(biāo)檢測:使用YOLO檢測每幀中眼睛的狀態(tài)。
  • 跟蹤眼睛閉合持續(xù)時間:如果檢測到眼睛“閉合”超過指定閾值,將顯示警告。
  • 顯示結(jié)果:結(jié)果被寫入輸出視頻文件,如果需要,警告消息會被疊加在幀上。

逐步實現(xiàn)

以下是使用YOLO和OpenCV檢測視頻中眼睛閉合的完整Python代碼:

import cv2
from ultralytics import YOLO


# Load the pre-trained YOLO model
model = YOLO("best.pt")
names = model.names
# Open the video file
cap = cv2.VideoCapture("video.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Initialize video writer to save the output
video_writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Initialize variables for eye closure detection
eye_closed_frames = 0
eye_closed_threshold_seconds = 1  # Threshold in seconds
eye_closed_threshold_frames = eye_closed_threshold_seconds * fps  # Convert seconds to frames
while cap.isOpened():
    success, im0 = cap.read()
    if not success:
        print("Video frame is empty or video processing has been successfully completed.")
        break
    # Predict the state of the eyes using YOLO
    results = model.predict(im0, show=False)
    boxes = results[0].boxes.xyxy.cpu().tolist()
    clss = results[0].boxes.cls.cpu().tolist()
    annotator = Annotator(im0, line_width=2, example=names)
    eye_closed = False  # Flag to check if the eye is closed in the current frame
If boxes are not None:
        for box, cls in zip(boxes, clss):
            clsName = names[int(cls)]
            xmax = int(box[0])
            ymin = int(box[1])
            xmin = int(box[2])
            ymax = int(box[3])
            # Set color based on the class name
            if clsName == 'closed':
                clr = (0, 0, 255)
                eye_closed = True  # Mark eye as closed
            elif clsName == 'opened':
                clr = (0, 255, 0)
            # Draw the bounding box and label
cv2.FONT_HERSHEY_SIMPLEX
            Font_scale = 1
            Font_thickness = 2
            tw, th = cv2.getTextSize(clsName, font, font_scale, font_thickness)[0]
            cv2.rectangle(im0, (xmin, ymin), (xmax, ymax), color=clr, thickness=2)
            cv2.putText(im0, clsName, (xmax, ymin - 5), font, font_scale, color=clr, thickness=font_thickness)
    # Check for eye closure duration
    if eye_closed:
        Eye_closed_frames += 1
    else:
# Reset counter if the eye is not closed
    # Display warning if eye has been closed for more than the threshold
    if eye_closed_frames > eye_closed_threshold_frames:
        print("Warning: Eye has been closed for more than 2 seconds!")
        cv2.putText(im0, "WARNING: Eye closed for more than 2 seconds!", (50, 50), font, font_scale, (0, 0, 255), font_thickness)
    # Write the processed frame to the output video
    video_writer.write(im0)
# Release resources
cap.release()
video_writer.release()

代碼解釋

  • 模型加載:使用ultralytics庫中的YOLO類加載YOLO模型,并提取類名。
  • 視頻處理:使用cv2.VideoCapture打開輸入視頻,并初始化視頻寫入器以保存輸出。
  • 逐幀檢測:逐幀處理視頻。對于每一幀,使用YOLO模型檢測眼睛并將其分類為“閉合”或“打開”。
  • 眼睛閉合檢測:計數(shù)器(eye_closed_frames)跟蹤眼睛被檢測為“閉合”的連續(xù)幀數(shù)。如果這個計數(shù)超過閾值(在這種情況下,是1秒鐘的幀數(shù)),則顯示警告消息。
  • 警告消息顯示:每當(dāng)眼睛閉合超過閾值持續(xù)時間時,警告消息就會動態(tài)疊加在視頻上。

實時檢測的可能性

上述方法可以適應(yīng)使用網(wǎng)絡(luò)攝像頭或?qū)崟r視頻流進(jìn)行實時檢測。以下是如何實現(xiàn)這一點的方法:

  • 捕獲實時視頻:將視頻文件輸入更改為實時視頻流,方法是將cv2.VideoCapture參數(shù)更改為0(默認(rèn)網(wǎng)絡(luò)攝像頭)。
cap = cv2.VideoCapture(0)  # Use 0 for the default camera, or 1, 2, etc. for other cameras
  • 優(yōu)化實時性能:為確保流暢的實時性能,你可能需要通過使用較小的模型(如YOLOv5s)或在可用的情況下在GPU上運行來優(yōu)化模型推理速度。
  • 實時顯示結(jié)果:使用cv2.imshow()實時顯示視頻流:
cv2.imshow("Eye Closure Detection", im0) if cv2.waitKey(1) & 0xFF == ord('q'):  # Press 'q' to quit     break

結(jié)合YOLO和OpenCV,你可以有效地檢測視頻或甚至實時中的眼睛閉合。這項技術(shù)在許多實際應(yīng)用中都有應(yīng)用,包括駕駛員監(jiān)控系統(tǒng)、疲勞檢測和在各種安全關(guān)鍵領(lǐng)域中的警覺性驗證。通過利用像YOLO這樣的深度學(xué)習(xí)模型,你可以在檢測微妙的面部表情(如眼睛閉合)方面實現(xiàn)高準(zhǔn)確性和性能。

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

2017-08-02 07:36:06

大數(shù)據(jù)PythonOpenCV

2025-02-11 08:30:00

2025-02-18 08:00:00

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

2024-11-29 16:10:31

2024-10-09 17:02:34

2024-11-28 15:56:05

YOLOEasyOCRPython

2014-03-17 09:34:01

HTMLHTML閉合

2023-11-20 09:47:14

自動駕駛視覺

2022-03-03 10:40:25

VSaaS視頻監(jiān)控人工智能

2025-01-22 11:10:34

2024-08-20 09:30:00

2025-01-13 10:00:00

2024-06-21 10:40:00

計算機視覺

2025-01-14 08:30:00

YOLO目標(biāo)檢測YOLOv8

2024-06-21 14:55:22

LinuxShell腳本

2011-01-21 10:10:44

sendmail

2024-06-25 09:31:02

2024-07-11 12:30:00

2024-07-30 12:30:00

2010-11-04 14:11:11

點贊
收藏

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