-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplaceFgAndBgMulti.py
64 lines (51 loc) · 1.95 KB
/
replaceFgAndBgMulti.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cv2
import sys
import os
import numpy as np
import skvideo
pathStr = str('C:/Users/jcloi/Downloads/ffmpeg-N-101017-gcfcc36240f-win64-gpl-shared/ffmpeg-N-101017-gcfcc36240f-win64-gpl-shared/bin')
skvideo.setFFmpegPath(pathStr)
import skvideo.io
import skvideo.datasets
import skvideo.utils
import time
from tqdm import tqdm
import concurrent.futures
from moviepy.video.io.ffmpeg_reader import FFMPEG_VideoReader
from moviepy.editor import *
# apply fg overlay to a list of videos over a bg
def replaceFgAndBg(fg, bg, alpha):
#print("replacing fg")
#Return an array of zeros with the same shape and type as a given array
#outvideo = np.zeros_like(bg)
#cv2.imshow('luma', cv2.resize(fg.astype(np.uint8), (300,900), interpolation = cv2.INTER_AREA))
#cv2.waitKey(1)
# create a positive mask based on the img
alpha = alpha.astype(np.float)/255.0
#print(np.shape(alpha))
#print(np.shape(fg))
foreground = cv2.multiply(alpha, fg, dtype=cv2.CV_8U) #remove from extant fg
#remove this mask from bg
background = cv2.multiply(1-alpha, bg, dtype=cv2.CV_8U)
retval = np.add(foreground, background)
return retval
if __name__ == "__main__":
if not len(sys.argv) % 2:
print("syntax: python replaceFgAndBg.py bgdir outdir fg0 mx0 fg1 mx1 ... fgn mxn")
bgdir = sys.argv[1]
outdir = sys.argv[2]
fgdirs = sys.argv[3::2]
mxdirs = sys.argv[4::2]
os.mkdir(outdir)
for bgfilename, fgfilename, mxfilename in tqdm(os.listdir(bgdir), os.listdir(fgdir), os.listdir(mxdir)):
outfilename = os.path.join(outdir, fgfilename)
if os.path.exists(outfilename):
continue
bg = cv2.imread(os.path.join(bgdir, bgfilename),1)
frameHeight = np.shape(fg)[0]
frameWidth = np.shape(fg)[1]
fg = cv2.imread(os.path.join(fgdir, fgfilename),1)
mx = cv2.imread(os.path.join(mxdir, mxfilename),1)
mx = cv2.resize(mx, (int(frameWidth ), int(frameHeight)), interpolation = cv2.INTER_AREA)
result = replaceFgAndBg(fg, bg, mx)
cv2.imwrite(outfilename,result)