YOLOv8 with counting time when object is stopped moving

Sorry I’m new to programming here and tried to combine one code with another, I’m trying to create a program which would counting how long a mouse stay idle (i mean not moving). so the timer must start when the mouse didn’t move and stop when the mouse is moving again. but i get this error line 38, in <module>
bboxes = results.xyxy[0].cpu().numpy()[:, :4] if len(results.xyxy) else []
AttributeError: 'list' object has no attribute 'xyxy'
sorry can anybody tell me whats wrong and what should I do?

here’s my code

import cv2
from ultralytics import YOLO
import os
import time
from datetime import datetime

**# Load the model**
model_path = "YOLOv8/model/path"
model = YOLO(model_path)

**# Open video**
cap = cv2.VideoCapture('target/video/path')
frame_rate = cap.get(cv2.CAP_PROP_FPS)
show=True

start_time = time.time()  **# Use time.time() to count the time**
stop_duration = 0
is_movement_detected = False
is_object_detected = False

prev_x, prev_y = 0, 0

position_changes = []
threshold_frame_count = 10
threshold_factor = 1.5

while True:
    ret, frame = cap.read()
    if not ret:
        break

    **# Perform object detection on the current frame**
    results = model(frame)

   ** # Get the bounding boxes of detected objects**
    bboxes = results.xyxy[0].cpu().numpy()[:, :4] if len(results.xyxy) else []

    if len(bboxes) == 0:  **# No object detected**
        if is_object_detected:
            cv2.putText(frame, "No Object", (50, 50),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            print("No Object")
        else:
            cv2.putText(frame, "No Object", (50, 50),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            print("No Object")
            cv2.imshow('Tracking', frame)
            output_video.write(frame)
            continue  

    is_object_detected = True

    for bbox in bboxes:
        (x_min, y_min, x_max,
         y_max) = bbox.astype(int)  **# Take the coordinate of the bounding boxes
**
        cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)

        **# Count the big position difference of the object**
        position_change = ((x_min - prev_x)**2 + (y_min - prev_y)**2)**0.5

        **# Reset the time if there's big difference of position (object is moving)**
        if position_change > 10:  **# Set the limit of position change that are considered big**
            if is_stopped:
                is_stopped = False
                start_time = time.time()  **# Reset start time**
                print('bergerak')

                **# Set is_movement_detected become True when object is moving**
                is_movement_detected = True

            if stop_duration >= 1.00 and is_movement_detected:
                **# Create a name with timestamp**
                timestamp = datetime.now().strftime("%Y%m%d%H%M%S%f")
                output_filename = os.path.join(output_directory,
                                               f'frame_{timestamp}.jpg')

                text_duration = f'Stop Duration: {stop_duration:.2f}s'
                text_pos_x = f'x: {x_min:.2f}'
                text_prev_x = f'prev_x: {prev_x:.2f}'
                text_pos_y = f'y: {y_min:.2f}'
                text_prev_y = f'prev_y: {prev_y:.2f}'
                (text_width, text_height), baseline = cv2.getTextSize(
                    text_duration, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
               ** # Count the position so the text is in the middle of the image**
                text_x = (frame.shape[1] - text_width) // 2
                text_y = text_height + 10  # Adjust to the desired position
                cv2.putText(frame, text_duration, (text_x, text_y),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_pos_x, (text_x, text_y + 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_prev_x, (text_x, text_y + 60),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_pos_y, (text_x, text_y + 90),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_prev_y, (text_x, text_y + 120),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

                **# Save frame when the object is started moving again**
                cv2.imwrite(output_filename, frame)
                print(
                    f"The frame when the object moves again is saved as {output_filename}"
                )
           ** # Count the position so the text is in the middle of the image**
                text_x = (frame.shape[1] - text_width) // 2
                text_y = text_height + 10  # Adjust to the desired position
                cv2.putText(frame, text_duration, (text_x, text_y),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_pos_x, (text_x, text_y + 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_prev_x, (text_x, text_y + 60),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_pos_y, (text_x, text_y + 90),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
                cv2.putText(frame, text_prev_y, (text_x, text_y + 120),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

               ** # Save frame when the object is started moving again**
                cv2.imwrite(output_filename, frame)
                print(
                    f"The frame when the object moves again is saved as {output_filename}"
                )

               ** # Reset is_movement_detected and stop_duration**
                is_movement_detected = False
                stop_duration = 0

        else:
            if not is_stopped:
                is_stopped = True
                start_time = time.time()  # Start counting stop duration

           ** # Count stop duration**
            stop_duration = time.time() - start_time
            print('stop')

       ** # Show stop duration on the top of bounding boxes**
        text_duration = f'Stop Duration: {stop_duration:.2f}s'
        text_pos_x = f'x: {x_min:.2f}'
        text_prev_x = f'prev_x: {prev_x:.2f}'
        text_pos_y = f'y: {y_min:.2f}'
        text_prev_y = f'prev_y: {prev_y:.2f}'
        (text_width,
         text_height), baseline = cv2.getTextSize(text_duration,
                                                  cv2.FONT_HERSHEY_SIMPLEX, 1,
                                                  2)
       ** # Count the position so the text is in the middle of the image**
        text_x = (frame.shape[1] - text_width) // 2
        text_y = text_height + 10  # Adjust to the desired position
        cv2.putText(frame, text_duration, (text_x, text_y),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.putText(frame, text_pos_x, (text_x, text_y + 30),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.putText(frame, text_prev_x, (text_x, text_y + 60),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.putText(frame, text_pos_y, (text_x, text_y + 90),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.putText(frame, text_prev_y, (text_x, text_y + 120),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

   ** # Update previous position**
    prev_x, prev_y = x_min, y_min

    **# Show frame with bounding boxes**
    cv2.imshow('Tracking', frame)

   ** # Write the frame to the video**
    output_video.write(frame)

    **# Stop if 'q' is pressed**
    **#if cv2.waitKey(5) or 0x1B == ord('q'):
        #break
    # Press 'q' or 'Q' to exit**
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q") or key == ord("Q"):
        break


cap.release()
output_video.release()
cv2.destroyAllWindows()

I hope this code would count the stopping time and show it on the videos, and in the future it would save the last frame where the object is stopped so the stopped time could be saved as image and data

New contributor

Kaizennewbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật