我有一個關於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()