| | """ |
| | # Copyright 2020 Adobe |
| | # All Rights Reserved. |
| | |
| | # NOTICE: Adobe permits you to use, modify, and distribute this file in |
| | # accordance with the terms of the Adobe license agreement accompanying |
| | # it. |
| | |
| | """ |
| |
|
| | import os, glob |
| | import numpy as np |
| | import cv2 |
| | import scipy.ndimage |
| |
|
| | fs = ['suit1_pred_fls_t7_audio_embed.mp4' ] |
| |
|
| | for f in fs: |
| |
|
| | os.system('ffmpeg -y -i examples/{} -filter:v crop=256:256:256:0 -strict -2 examples/crop_{}'.format(f, f)) |
| |
|
| | cap = cv2.VideoCapture('examples/crop_{}'.format(f)) |
| | writer = cv2.VideoWriter('examples/tmp_{}.mp4'.format(f[:-4]), |
| | cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 62.5, (256, 256)) |
| |
|
| | length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| |
|
| | ret, frame1 = cap.read() |
| | prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) |
| | fir = np.copy(prvs) |
| |
|
| | |
| | feature_params = dict( maxCorners = 100, |
| | qualityLevel = 0.9, |
| | minDistance = 3, |
| | blockSize = 3) |
| |
|
| | |
| | lk_params = dict( winSize = (15,15), |
| | maxLevel = 2, |
| | criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) |
| |
|
| | |
| | color = np.random.randint(0,255,(100,3)) |
| |
|
| | |
| | ret, old_frame = cap.read() |
| | old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) |
| |
|
| | mask = np.zeros_like(old_gray) |
| | mask[-50:, 128:] = 1 |
| |
|
| | p0 = cv2.goodFeaturesToTrack(old_gray, mask = mask, **feature_params) |
| | p0 = p0[0:1] |
| |
|
| | ori_ab = None |
| |
|
| | |
| | mask = np.zeros_like(old_frame) |
| | ii = 0 |
| | while(ii>-1): |
| | print(f, ii, length) |
| | ii += 1 |
| |
|
| | ret,frame = cap.read() |
| | if(not ret): |
| | break |
| | frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| |
|
| | |
| | p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) |
| |
|
| | |
| | good_new = p1[st==1] |
| | good_old = p0[st==1] |
| |
|
| | |
| | for i,(new,old) in enumerate(zip(good_new,good_old)): |
| | a,b = new.ravel() |
| | c,d = old.ravel() |
| | |
| | |
| | if(ori_ab is None): |
| | ori_ab = [a, b] |
| |
|
| | |
| | |
| |
|
| | |
| | rgb = scipy.ndimage.shift(frame, shift=[ori_ab[1]-b, ori_ab[0]-a, 0], mode='reflect') |
| |
|
| | |
| | writer.write(rgb) |
| |
|
| | |
| | old_gray = frame_gray.copy() |
| | p0 = good_new.reshape(-1,1,2) |
| |
|
| | cv2.destroyAllWindows() |
| | cap.release() |
| | writer.release() |
| |
|
| | f = f[:-4] |
| | os.system('ffmpeg -loglevel error -y -i {} -vn {}'.format( |
| | os.path.join('../examples', '{}.mp4'.format(f)), os.path.join('../examples', 'a_' + f + '.wav') |
| | )) |
| |
|
| | os.system('ffmpeg -loglevel error -y -i {} -i {} -pix_fmt yuv420p -shortest -strict -2 {}'.format( |
| | os.path.join('../examples', 'tmp_{}.mp4'.format(f)), os.path.join('../examples', 'a_' + f + '.wav'), |
| | os.path.join('../examples', 'f_' + f + '.mp4') |
| | )) |
| | os.remove(os.path.join('../examples', 'tmp_{}.mp4'.format(f))) |
| | os.remove(os.path.join('../examples', 'a_' + f + '.wav')) |
| |
|