1010fourcc = 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' )
3217def 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