-
Notifications
You must be signed in to change notification settings - Fork 11
/
video.py
50 lines (40 loc) · 1.26 KB
/
video.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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
import tensorflow as tf
if __name__ == "__main__":
""" Video Path """
video_path = "Elon_Musk.mp4"
""" Load the model """
model = tf.keras.models.load_model("unet.h5")
""" Reading frames """
vs = cv2.VideoCapture(video_path)
_, frame = vs.read()
H, W, _ = frame.shape
vs.release()
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
out = cv2.VideoWriter('output.avi', fourcc, 10, (W, H), True)
cap = cv2.VideoCapture(video_path)
idx = 0
while True:
ret, frame = cap.read()
if ret == False:
cap.release()
out.release()
break
H, W, _ = frame.shape
ori_frame = frame
frame = cv2.resize(frame, (256, 256))
frame = np.expand_dims(frame, axis=0)
frame = frame / 255.0
mask = model.predict(frame)[0]
mask = mask > 0.5
mask = mask.astype(np.float32)
mask = cv2.resize(mask, (W, H))
mask = np.expand_dims(mask, axis=-1)
combine_frame = ori_frame * mask
combine_frame = combine_frame.astype(np.uint8)
cv2.imwrite(f"video/{idx}.png", combine_frame)
idx += 1
out.write(combine_frame)