mirror of https://github.com/ZHKKKe/MODNet.git
add alpha_matte option
parent
34334533ff
commit
6b4d1b1d29
|
|
@ -0,0 +1,6 @@
|
||||||
|
numpy
|
||||||
|
Pillow
|
||||||
|
opencv-python
|
||||||
|
torch >= 1.0.0
|
||||||
|
torchvision
|
||||||
|
tqdm
|
||||||
|
|
@ -36,7 +36,7 @@ else:
|
||||||
modnet.eval()
|
modnet.eval()
|
||||||
|
|
||||||
|
|
||||||
def offline_matting(video_path, save_path, fps=30):
|
def offline_matting(video_path, save_path, alpha_matte=False, fps=30):
|
||||||
# video capture
|
# video capture
|
||||||
vc = cv2.VideoCapture(video_path)
|
vc = cv2.VideoCapture(video_path)
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ def offline_matting(video_path, save_path, fps=30):
|
||||||
h, w = frame.shape[:2]
|
h, w = frame.shape[:2]
|
||||||
|
|
||||||
# video writer
|
# video writer
|
||||||
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
||||||
video_writer = cv2.VideoWriter(save_path, fourcc, fps, (w, h))
|
video_writer = cv2.VideoWriter(save_path, fourcc, fps, (w, h))
|
||||||
|
|
||||||
print('Start matting...')
|
print('Start matting...')
|
||||||
|
|
@ -61,7 +61,6 @@ def offline_matting(video_path, save_path, fps=30):
|
||||||
for c in t:
|
for c in t:
|
||||||
frame_np = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
frame_np = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||||
frame_np = cv2.resize(frame_np, (672, 512), cv2.INTER_AREA)
|
frame_np = cv2.resize(frame_np, (672, 512), cv2.INTER_AREA)
|
||||||
# frame_np = frame_np[:, 120:792, :]
|
|
||||||
|
|
||||||
frame_PIL = Image.fromarray(frame_np)
|
frame_PIL = Image.fromarray(frame_np)
|
||||||
frame_tensor = torch_transforms(frame_PIL)
|
frame_tensor = torch_transforms(frame_PIL)
|
||||||
|
|
@ -74,30 +73,34 @@ def offline_matting(video_path, save_path, fps=30):
|
||||||
|
|
||||||
matte_tensor = matte_tensor.repeat(1, 3, 1, 1)
|
matte_tensor = matte_tensor.repeat(1, 3, 1, 1)
|
||||||
matte_np = matte_tensor[0].data.cpu().numpy().transpose(1, 2, 0)
|
matte_np = matte_tensor[0].data.cpu().numpy().transpose(1, 2, 0)
|
||||||
fg_np = matte_np * frame_np + (1 - matte_np) * np.full(frame_np.shape, 255.0)
|
if alpha_matte:
|
||||||
fg_np = cv2.cvtColor(fg_np.astype(np.uint8), cv2.COLOR_RGB2BGR)
|
view_np = matte_np * np.full(frame_np.shape, 255.0)
|
||||||
fg_np = cv2.resize(fg_np, (w, h))
|
else:
|
||||||
|
view_np = matte_np * frame_np + (1 - matte_np) * np.full(frame_np.shape, 255.0)
|
||||||
|
view_np = cv2.cvtColor(view_np.astype(np.uint8), cv2.COLOR_RGB2BGR)
|
||||||
|
view_np = cv2.resize(view_np, (w, h))
|
||||||
|
video_writer.write(view_np)
|
||||||
|
|
||||||
video_writer.write(fg_np)
|
|
||||||
rval, frame = vc.read()
|
rval, frame = vc.read()
|
||||||
c += 1
|
c += 1
|
||||||
|
|
||||||
video_writer.release()
|
video_writer.release()
|
||||||
print('Save video to {}'.format(args.save_path))
|
print('Save video to {}'.format(save_path))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--video_path', type=str, default='./sample/video.mp4')
|
parser.add_argument('--video_path', type=str, default='./sample/video.mp4')
|
||||||
parser.add_argument('--save_path', type=str, default='./sample/matte.mp4')
|
parser.add_argument('--save_path', type=str, default='./sample/res.mp4', help='Video should be .mp4 format.')
|
||||||
|
parser.add_argument('--alpha_matte', action='store_true', default=False, help='If True, save alpha_matte video.')
|
||||||
parser.add_argument('--fps', type=int, default=30)
|
parser.add_argument('--fps', type=int, default=30)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if not args.save_path.endswith('avi'):
|
if not args.save_path.endswith('.mp4'):
|
||||||
args.save_path = os.path.splitext(args.save_path)[0] + '.avi'
|
args.save_path = os.path.splitext(args.save_path)[0] + '.mp4'
|
||||||
|
|
||||||
offline_matting(args.video_path, args.save_path, args.fps)
|
offline_matting(args.video_path, args.save_path, args.alpha_matte, args.fps)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue