Извините, я новичок в программировании и пытался объединить один код с другим. Я пытаюсь создать программу, которая будет подсчитывать, как долго мышь бездействует (то есть не движется). поэтому таймер должен запускаться, когда мышь не двигалась, и останавливаться, когда мышь снова движется. но я получаю эту строку ошибки 38 в
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()
Я надеюсь, что этот код будет подсчитывать время остановки и показывать его на видео, а в будущем он будет сохранять последний кадр, в котором объект остановлен, чтобы время остановки можно было сохранить как изображение и данные
Извините, я новичок в программировании и пытался объединить один код с другим. Я пытаюсь создать программу, которая будет подсчитывать, как долго мышь бездействует (то есть не движется). поэтому таймер должен запускаться, когда мышь не двигалась, и останавливаться, когда мышь снова движется. но я получаю эту строку ошибки 38 в [code]bboxes = results.xyxy[0].cpu().numpy()[:, :4] if len(results.xyxy) else [][/code] [code]AttributeError: 'list' object has no attribute 'xyxy'[/code] извините, может кто-нибудь сказать мне, что не так и что мне делать? вот мой код [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
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}" )
**# 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() [/code] Я надеюсь, что этот код будет подсчитывать время остановки и показывать его на видео, а в будущем он будет сохранять последний кадр, в котором объект остановлен, чтобы время остановки можно было сохранить как изображение и данные