iT邦幫忙

0

我有一個python yolov8的問題

  • 分享至 

  • xImage

我有一個關於Yolov8的問題 我想要做出畫面上同時出現人和車子10秒鐘觸發警報
我的程式碼長這樣 但達不到我的要求 要怎麼修改呢?
我的yolov8是用vscode的python執行的 我是直接在TERMINALpip install ultralytics
pip install time
pip install opencv-python的

import cv2
from ultralytics import YOLO
import time
import os
import requests
model = YOLO(r'D:\yolo\alarm\yolov8n.pt')
cap = cv2.VideoCapture(r'D:\video\不下車.mp4')
start_time = None
alert_triggered = False
screenshot_dir = r'D:\screenshot'
os.makedirs(screenshot_dir, exist_ok=True)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model(frame, classes=[0, 2])
annotated_frame = results[0].plot()
if len(results[0]) > 0:
if start_time is None:
start_time = time.time()

        elapsed_time = time.time() - start_time

        if elapsed_time >= 25 and not alert_triggered:
            print("警報!警報!警報!警報!警報!警報!警報!警報!警報!\n"*100)
            alert_triggered = True
            screenshot_filename = os.path.join(screenshot_dir, f"screenshot_{time.strftime('%Y%m%d_%H%M%S')}.jpg")
            cv2.imwrite(screenshot_filename, frame)
            print(f"Screenshot captured and saved as {screenshot_filename}")
            url = 'https://notify-api.line.me/api/notify'
            token = 'dTsdo4Uf6ZjtF8aHHTjh7KYaTZsvo4vBbR7dRrhREiL'
            headers = {
 'Authorization': 'Bearer ' + token    
}
            data = {
'message':'警報!'     
}
            data = requests.post(url, headers=headers, data=data)
    else:
        start_time = None
        alert_triggered = False         
    cv2.imshow('YOLOv8 Inference', annotated_frame)
    if cv2.waitKey(25) & 0xFF == ord("1"):
        break
else:
    break

cap.release()
cv2.destroyAllWindows()

貓虎皮 iT邦新手 3 級 ‧ 2024-07-09 15:47:46 檢舉
如果py程式碼中出現兩行if,請確保發文時,所有程式碼皆被包在代碼框(<pre>和<code>)中,這樣他人才能檢查你的程式邏輯是否出問題。
感謝!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答