def extract_optical_flow(fn, n_frames=34):
img = dd.image.load(fn)
if img.shape != (128*34, 128, 3):
return []
frames = np.array_split(img, 34, axis=0)
grayscale_frames = [fr.mean(-1) for fr in frames]
mags = []
skip_frames = np.random.randint(34 - n_frames + 1)
middle_frame = frames[np.random.randint(skip_frames, skip_frames+n_frames)]
im0 = grayscale_frames[skip_frames]
for f in range(1+skip_frames, 1+skip_frames+n_frames-1):
im1 = grayscale_frames[f]
flow = cv2.calcOpticalFlowFarneback(im0, im1,
None, # flow
0.5, # pyr_scale
3, # levels
np.random.randint(3, 20), # winsize
3, #iterations
5, #poly_n
1.2, #poly_sigma
0 # flags
)
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
mags.append(mag)
im0 = im1
mag = np.sum(mags, 0)
mag = mag.clip(min=0)
#norm_mag = np.tanh(mag * 10000)
norm_mag = (mag - mag.min()) / (mag.max() - mag.min() + 1e-5)
outputs = []
outputs.append((middle_frame, norm_mag))
return outputs
video_jpeg_rolls_flow_saliency.py 文件源码
python
阅读 27
收藏 0
点赞 0
评论 0
评论列表
文章目录