Skip to content

Commit e856e9f

Browse files
committed
fixed generator memory issue
1 parent 36c6c8e commit e856e9f

File tree

2 files changed

+88
-18
lines changed

2 files changed

+88
-18
lines changed

deblur_video.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
from PIL import Image
3+
import click
4+
import cv2
5+
6+
from deblurgan.model import generator_model
7+
from deblurgan.utils import deprocess_image, preprocess_image
8+
9+
# Define the codec and create VideoWriter object
10+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
11+
12+
13+
@click.command()
14+
@click.option('--weight_path', help='Model weight')
15+
@click.option('--input_video', help='Video to deblur path')
16+
@click.option('--output_video', help='Deblurred video path')
17+
def deblur_video(weight_path, input_video, output_video):
18+
g = generator_model()
19+
g.load_weights(weight_path)
20+
# Read input video
21+
cap = cv2.VideoCapture(input_video)
22+
# Get frame count, possible apriori if reading a file
23+
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
24+
print("Number of frames in file are {0}".format(n_frames))
25+
fps = int(cap.get(cv2.CAP_PROP_FPS))
26+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
27+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
28+
# Desired shape into which output is to be reshaped
29+
desired_shape = (512, 288)
30+
# output writer object
31+
out = cv2.VideoWriter(output_video, fourcc, fps, desired_shape)
32+
# Frame number
33+
ctr = 1
34+
# While frame keep coming in
35+
while cap.isOpened():
36+
if (ctr - 1) % 25 == 0:
37+
print("Now processing frame number {0}".format(ctr))
38+
# Read input frame by frame
39+
ret, in_frame = cap.read()
40+
if ret:
41+
# Shape = (w, h), np array = rows (height), cols (width)
42+
in_frame1 = in_frame[60:-60, :, :]
43+
# Out_frame is the 256x256 de-blurred numpy array frame
44+
image = np.array([preprocess_image(Image.fromarray(in_frame1))])
45+
generated_images = g.predict(x=image)
46+
generated = np.array([deprocess_image(img) for img in generated_images])
47+
image = deprocess_image(image)
48+
for i in range(generated_images.shape[0]):
49+
x = image[i, :, :, :]
50+
img = generated[i, :, :, :]
51+
output_frame = np.concatenate((x, img), axis=1)
52+
# Hardcoded [:, 256, :] because shape of output numpy array is (256, 512, 3)
53+
# out_cv is still a numpy array since cv2 keeps it as numpy after reshape instead of converting to cv2 form
54+
out_cv = cv2.resize(output_frame[:, 256:, :], desired_shape, interpolation=cv2.INTER_CUBIC)
55+
# We can write a numpy array directly and it is written with a transformed shape
56+
out.write(out_cv)
57+
else:
58+
break
59+
ctr += 1
60+
# Release everything if job is finished
61+
cap.release()
62+
out.release()
63+
return
64+
65+
66+
if __name__ == "__main__":
67+
deblur_video()

scripts/deblur_video.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,13 @@
1010
fourcc = cv2.VideoWriter_fourcc(*'XVID')
1111

1212

13-
def deblur(weight_path, input_frame):
14-
g = generator_model()
15-
g.load_weights(weight_path)
16-
image = np.array([preprocess_image(Image.fromarray(input_frame))])
17-
x_test = image
18-
generated_images = g.predict(x=x_test)
19-
generated = np.array([deprocess_image(img) for img in generated_images])
20-
x_test = deprocess_image(x_test)
21-
for i in range(generated_images.shape[0]):
22-
x = x_test[i, :, :, :]
23-
img = generated[i, :, :, :]
24-
output_frame = np.concatenate((x, img), axis=1)
25-
# Hardcoded bacuse shape of output numpy array is (256, 512, 3)
26-
return output_frame[:, 256:, :]
27-
2813
@click.command()
2914
@click.option('--weight_path', help='Model weight')
3015
@click.option('--input_video', help='Video to deblur path')
3116
@click.option('--output_video', help='Deblurred video path')
3217
def deblur_video(weight_path, input_video, output_video):
18+
g = generator_model()
19+
g.load_weights(weight_path)
3320
# Read input video
3421
cap = cv2.VideoCapture(input_video)
3522
# Get frame count, possible apriori if reading a file
@@ -38,8 +25,10 @@ def deblur_video(weight_path, input_video, output_video):
3825
fps = int(cap.get(cv2.CAP_PROP_FPS))
3926
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
4027
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
28+
# Desired shape into which output is to be reshaped
29+
desired_shape = (512, 288)
4130
# output writer object
42-
out = cv2.VideoWriter(output_video, fourcc, fps, (w, h))
31+
out = cv2.VideoWriter(output_video, fourcc, fps, desired_shape)
4332
# Frame number
4433
ctr = 1
4534
# While frame keep coming in
@@ -49,8 +38,22 @@ def deblur_video(weight_path, input_video, output_video):
4938
# Read input frame by frame
5039
ret, in_frame = cap.read()
5140
if ret:
52-
out_frame = deblur(weight_path, in_frame)
53-
out.write(cv2.resize(out_frame, (w, h), interpolation=cv2.INTER_CUBIC))
41+
# Shape = (w, h), np array = rows (height), cols (width)
42+
in_frame1 = in_frame[60:-60, :, :]
43+
# Out_frame is the 256x256 de-blurred numpy array frame
44+
image = np.array([preprocess_image(Image.fromarray(in_frame1))])
45+
generated_images = g.predict(x=image)
46+
generated = np.array([deprocess_image(img) for img in generated_images])
47+
image = deprocess_image(image)
48+
for i in range(generated_images.shape[0]):
49+
x = image[i, :, :, :]
50+
img = generated[i, :, :, :]
51+
output_frame = np.concatenate((x, img), axis=1)
52+
# Hardcoded [:, 256, :] because shape of output numpy array is (256, 512, 3)
53+
# out_cv is still a numpy array since cv2 keeps it as numpy after reshape instead of converting to cv2 form
54+
out_cv = cv2.resize(output_frame[:, 256:, :], desired_shape, interpolation=cv2.INTER_CUBIC)
55+
# We can write a numpy array directly and it is written with a transformed shape
56+
out.write(out_cv)
5457
else:
5558
break
5659
ctr += 1

0 commit comments

Comments
 (0)