Created
November 28, 2023 20:20
-
-
Save Poiuytrezay1/de3af1406b7ff3a6a2db032cd6daa3dd to your computer and use it in GitHub Desktop.
Remove adversarial noise from images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
import os | |
import argparse | |
from pathlib import Path | |
from cv2.ximgproc import guidedFilter | |
def main(args): | |
input = Path(args.input_file_or_dir) | |
output = input | |
if input.is_dir(): | |
image_files = [ | |
file | |
for file in input.iterdir() | |
if file.suffix in [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".avif"] | |
] | |
else: | |
image_files = [input] | |
for image_file in image_files: | |
img = cv2.imread(str(image_file.resolve())).astype(np.float32) | |
y = img.copy() | |
for _ in range(64): | |
y = cv2.bilateralFilter(y, 5, 8, 8) | |
for _ in range(4): | |
y = guidedFilter(img, y, 4, 16) | |
cv2.imwrite(str(image_file.resolve()), y.clip(0, 255).astype(np.uint8)) | |
if __name__ == "__main__": | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument( | |
"--input_file_or_dir", help="Input file or directory to load the images from" | |
) | |
args = argparser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment