import cv2 import numpy as np def DetectMotionWithOrb(FrameOne, FrameTwo, MotionThreshold=1.0): FrameOneGray = cv2.cvtColor(FrameOne, cv2.COLOR_BGR2GRAY) FrameTwoGray = cv2.cvtColor(FrameTwo, cv2.COLOR_BGR2GRAY) OrbDetector = cv2.ORB_create() #type: ignore Keypoints1, Descriptors1 = OrbDetector.detectAndCompute(FrameOneGray, None) Keypoints2, Descriptors2 = OrbDetector.detectAndCompute(FrameTwoGray, None) if Descriptors1 is None or Descriptors2 is None: return False, 0, 0 BfMatcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) Matches = BfMatcher.match(Descriptors1, Descriptors2) Vectors = [] for Match in Matches: Pt1 = Keypoints1[Match.queryIdx].pt Pt2 = Keypoints2[Match.trainIdx].pt Vector = (Pt2[0] - Pt1[0], Pt2[1] - Pt1[1]) Vectors.append(Vector) if not Vectors: return False, 0, 0 Vectors = np.array(Vectors) AvgVector = np.mean(Vectors, axis=0) AvgX, AvgY = AvgVector TotalMagnitude = np.hypot(AvgX, AvgY) DirectionAngle = np.degrees(np.arctan2(AvgY, AvgX)) IsMotion = TotalMagnitude >= MotionThreshold return IsMotion, TotalMagnitude, DirectionAngle