Untitled
unknown
plain_text
a year ago
882 B
8
Indexable
class OpticalFlow():
def __init__(self, s_opt=320):
self.prev = None
self.s_opt = s_opt
def resize_bbox(self, box, d_size):
x1, y1, x2, y2 = box
x_scale = self.s_opt / d_size[1]
y_scale = self.s_opt / d_size[0]
x1 = int(np.round(x1 * x_scale))
y1 = int(np.round(y1 * y_scale))
x2 = int(np.round(x2 * x_scale))
y2 = int(np.round(y2 * y_scale))
return x1, y1, x2, y2
def get(self, frame):
frame = cv2.resize(frame, (self.s_opt, self.s_opt))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if self.prev is None:
self.prev = frame
flow = cv2.calcOpticalFlowFarneback(self.prev, frame, None, 0.5, 3, 15, 3, 5, 1.2, 0)
self.prev = frame
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
return mag
Editor is loading...
Leave a Comment