Reputation: 11
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
Upvotes: 1
Views: 15