Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions add_distortion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import cv2
from tqdm import tqdm

func_dict = {'CS': color_saturation, 'CC': color_contrast,
'JPEG': jpeg_compression, 'GB': gaussian_blur}
param_dict = {'CS': 0.4, 'CC': 0.85,
Expand All @@ -16,18 +17,25 @@
help='path to the input video')
parser.add_argument('--img_out', '-o',
type=str,
default='./perturbations/CS',
help='path to the output video')
default=None,
help='path to the output video (defaults to ./perturbations/[type])')
Comment on lines 18 to +21
parser.add_argument(
'--type', '-t',
type=str,
default='CS',
help='distortion type: CS | CC | JPEG | GB')
Comment on lines 22 to 26
args = parser.parse_args()

# Dynamic feature: Automatically matches output folder name to the chosen type if not specified
output_root = args.img_out or os.path.join('./perturbations', args.type)

for dir in tqdm(os.listdir(args.img_in)):
for img in os.listdir(os.path.join(args.img_in, dir)):
ori_img = cv2.imread(os.path.join(args.img_in, dir, img))
img_path = os.path.join(args.img_in, dir, img)
ori_img = cv2.imread(img_path)
per_img = func_dict[args.type](ori_img, param_dict[args.type])
if not os.path.exists(os.path.join(args.img_out, dir)):
os.makedirs(os.path.join(args.img_out, dir))
cv2.imwrite(os.path.join(args.img_out, dir, img), per_img)

save_dir = os.path.join(output_root, dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
Comment on lines +39 to +40
cv2.imwrite(os.path.join(save_dir, img), per_img)