| import numpy as np |
| import argparse |
| import sys |
| import math |
| from PIL import ImageFilter, Image |
| import cv2 |
|
|
| ''' |
| Enhance video by applying denoise and sharpen filters |
| ''' |
|
|
| |
| parser = argparse.ArgumentParser(description='Use block shuffle') |
| parser.add_argument('--denoise', action='store_true', default=False) |
| parser.add_argument('--sharpen', action='store_true', default=False) |
| parser.add_argument("--input_video", required=True, help="path to input video") |
| args = vars(parser.parse_args()) |
|
|
| |
| print('\nEnhancing video ...\n') |
|
|
|
|
| |
| def update_progress(current_frame, total_frames): |
| progress = math.ceil((current_frame / total_frames) * 100) |
| sys.stdout.write('\rProgress: [{0}] {1}%'.format('>' * math.ceil(progress / 10), progress)) |
|
|
|
|
| |
| vid_cap = cv2.VideoCapture(args['input_video']) |
|
|
| |
| num_frames = int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| print("Total frames in input video:", num_frames) |
|
|
| |
| enhanced_video = cv2.VideoWriter('results/enhanced_secret_300.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 15, |
| (300, 300)) |
|
|
| |
| frames = [] |
|
|
| |
| while vid_cap.isOpened(): |
| success, image = vid_cap.read() |
| if success: |
| frames.append(image) |
| else: |
| break |
|
|
| |
| start_frame = 5 |
|
|
| |
| for i in range(start_frame, len(frames) - (start_frame + 1)): |
| output = frames[i] |
| if args["denoise"]: |
| output = cv2.fastNlMeansDenoisingColoredMulti(frames, i, 11) |
| if args["sharpen"]: |
| output = np.array(Image.fromarray(output).filter(ImageFilter.DETAIL)) |
| enhanced_video.write(output) |
| update_progress(i, num_frames - (start_frame + 1)) |
|
|
| |
| print('\n\nSuccessfully enhanced video !!!\n') |
|
|
| ''' |
| Sample run:- |
| python enhance.py --input_video results/secret_outvid_300.avi --denoise --sharpen |
| ''' |
|
|