code import cv2 from filterpy.kalman import KalmanFilter from ultralytics import YOLO…
- User
code import cv2 from filterpy.kalman import KalmanFilter from ultralytics import YOLO import numpy as np
model = YOLO('/Users/surabhi/Documents/kalman/best.pt') dt = 1.0 kf = KalmanFilter(dim_x=6, dim_z=2) kf.x = np.array([0, 0, 0, 0,0,0]) # initial state estimate kf.P = np.eye(6) * 1000 # initial error covariance matrix kf.F = np.array([[1, 0, dt, 0, 0.5 * (dt ** 2), 0], [0, 1, 0, dt, 0, 0.5 * (dt ** 2)], [0, 0, 1, 0, dt, 0], [0, 0, 0, 1, 0, dt], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]) # state transition matrix kf.H = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0]]) # measurement matrix kf.R = np.diag([0.1, 0.1]) # measurement noise covariance matrix kf.Q= np.array([[dt4/4, 0, dt3/2, 0, dt2, 0], [0, dt4/4, 0, dt3/2, 0, dt2], [dt3/2, 0, dt2, 0, dt, 0], [0, dt3/2, 0, dt2, 0, dt], [dt2, 0, dt, 0, 1, 0], [0, dt2, 0, dt, 0, 1]]) # process noise covariance matrix
u = np.zeros((4, 1))
cap = cv2.VideoCapture("1_1.mp4") frame_num = 0 predicted_points = [] bounce_detected = False last_bounce_frame = -10
while True: ret, frame = cap.read() if ret is False: break bbox = model(frame, show=True) frame_num += 1 for boxes_1 in bbox: result = boxes_1.boxes.xyxy if len(result) == 0: print("not detected") else: cx = int((result[0][0] + result[0][2]) / 2) cy = int((result[0][1] + result[0][3]) / 2) centroid = np.array([cx, cy]) kf.predict() kf.update(centroid) next_point = (kf.x).tolist() #predicted_velocity.append((int(next_point[2]),int(next_point[3]))) predicted_points.append((int(next_point[0]), int(next_point[1]))) if len(predicted_points) > 10: predicted_points.pop(0) print("next_point", next_point) print("frame_number", frame_num) if(next_point[2]>0): vx="positive" else: vx="negative" if(next_point[3]>0): vy="positive" else: vy="negative" cv2.putText(frame, f'Frame: {frame_num}', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.putText(frame, f': {next_point}', (10,205), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) #cv2.putText(frame, f'vx:{vx}',(10,205), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) #cv2.putText(frame, f'vy:{vy}',(10,230), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) cv2.circle(frame, (cx, cy), 5, (0,0,255), 5) cv2.circle(frame, (int(next_point[0]), int(next_point[1])), 5, (255, 0, 0), 10) for i, p in enumerate(predicted_points): color = (255,255,255) cv2.circle(frame, p, 5, color, 2) if not bounce_detected and frame_num - last_bounce_frame > 10: if kf.x[2] < -5 and kf.x[3]<-5 and kf.x[4]<-5 and kf.x[5] < - 5: # If Y acceleration is less than the negative threshold, say -15 bounce_detected = True last_bounce_frame = frame_num print("Bounce detected")
code - Assistant
One possible solution to eliminate false bounce detection from player hits could be to use object tracking to keep track of the ball's movement and only trigger bounce detection if the ball's position and velocity meet certain criteria after a certain number of frames.
For example, instead of solely relying on acceleration to detect a bounce, you could also consider the ball's position and velocity at the time of impact. You could also set a minimum distance threshold between the ball and the player to exclude hits from bounce detection.
Additionally, you could also implement a filter to remove any outliers or noisy data points that may be causing false bounce detections.