Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for i in range(n):
im[i] = imresize(imSeq[i], (flowSz, flowSz))
# compute Motion Saliency per frame
salImSeq = np.zeros((n, flowSz, flowSz))
numDomFrames = 0
for i in range(n):
isFrameDominant = 0
for j in range(-flowF, flowF + 1):
if j == 0 or i + j < 0 or i + j >= n:
continue
# flow = calcOpticalFlowFarneback(
# color.rgb2gray(im[i]), color.rgb2gray(im[i + j]), 0.5, 4,
# flowWinSz, 10, 5, 1.1, OPTFLOW_FARNEBACK_GAUSSIAN)
# pyflow needs im: float in [0,1]
u, v, _ = pyflow.coarse2fine_flow(
im[i].astype(float) / 255., im[i + j].astype(float) / 255.,
alpha, ratio, minWidth, nOuterFPIterations, nInnerFPIterations,
nSORIterations, 0)
flow = np.concatenate((u[..., None], v[..., None]), axis=2)
dominant, _, target, salIm = isDominant(
flow, flowMagTh, flowDirTh, dirBins=flowDirBins)
if False:
odir = '/home/dpathak/local/data/trash/my_nlc/nlc_out/'
np.save(odir + '/np/outFlow_%d_%d.npy' % (i, i + j), flow)
import cv2
hsv = np.zeros((100, 100, 3), dtype=np.uint8)
hsv[:, :, 0] = 255
hsv[:, :, 1] = 255
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
def get_flow(im1, im2):
im1 = np.array(im1)
im2 = np.array(im2)
im1 = im1.astype(float) / 255.
im2 = im2.astype(float) / 255.
# Flow Options:
alpha = 0.012
ratio = 0.75
minWidth = 20
nOuterFPIterations = 7
nInnerFPIterations = 1
nSORIterations = 30
colType = 0 # 0 or default:RGB, 1:GRAY (but pass gray image with shape (h,w,1))
u, v, im2W = pyflow.coarse2fine_flow(im1, im2, alpha, ratio, minWidth, nOuterFPIterations, nInnerFPIterations,nSORIterations, colType)
flow = np.concatenate((u[..., None], v[..., None]), axis=2)
#flow = rescale_flow(flow,0,1)
return flow
def get_flow(im1, im2):
im1 = np.array(im1)
im2 = np.array(im2)
im1 = im1.astype(float) / 255.
im2 = im2.astype(float) / 255.
# Flow Options:
alpha = 0.012
ratio = 0.75
minWidth = 20
nOuterFPIterations = 7
nInnerFPIterations = 1
nSORIterations = 30
colType = 0 # 0 or default:RGB, 1:GRAY (but pass gray image with shape (h,w,1))
u, v, im2W = pyflow.coarse2fine_flow(im1, im2, alpha, ratio, minWidth, nOuterFPIterations, nInnerFPIterations,nSORIterations, colType)
flow = np.concatenate((u[..., None], v[..., None]), axis=2)
#flow = rescale_flow(flow,0,1)
return flow
im1 = np.array(Image.open('examples/car1.jpg'))
im2 = np.array(Image.open('examples/car2.jpg'))
im1 = im1.astype(float) / 255.
im2 = im2.astype(float) / 255.
# Flow Options:
alpha = 0.012
ratio = 0.75
minWidth = 20
nOuterFPIterations = 7
nInnerFPIterations = 1
nSORIterations = 30
colType = 0 # 0 or default:RGB, 1:GRAY (but pass gray image with shape (h,w,1))
s = time.time()
u, v, im2W = pyflow.coarse2fine_flow(
im1, im2, alpha, ratio, minWidth, nOuterFPIterations, nInnerFPIterations,
nSORIterations, colType)
e = time.time()
print('Time Taken: %.2f seconds for image of size (%d, %d, %d)' % (
e - s, im1.shape[0], im1.shape[1], im1.shape[2]))
flow = np.concatenate((u[..., None], v[..., None]), axis=2)
np.save('examples/outFlow.npy', flow)
if args.viz:
import cv2
hsv = np.zeros(im1.shape, dtype=np.uint8)
hsv[:, :, 0] = 255
hsv[:, :, 1] = 255
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)