-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblurry_filter_node.py
More file actions
73 lines (51 loc) · 1.52 KB
/
blurry_filter_node.py
File metadata and controls
73 lines (51 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import shutil
import os
import cv2
def check_and_copy(filepath, output_path):
if os.path.isfile(filepath):
return shutil.copy2(filepath, output_path)
else:
return False
def laplace_variance(image):
return cv2.Laplacian(image, cv2.CV_64F).var()
def thresh_and_remove(path, output_path, threshold):
if not os.path.isfile(path):
return
img = cv2.imread(path)
try:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
except:
gray = img
blurmetric = laplace_variance(gray)
if(blurmetric >= threshold):
shutil.copy2(path, output_path)
min_args = 2
max_args = 3 # program, input, threshold
n = len(sys.argv)
print(n)
if n < min_args:
print("Too few arguments")
exit()
elif n > max_args:
print("Too many arguments")
exit()
input_path = sys.argv[1]
if n == 3:
try:
threshold = int(sys.argv[2])
except:
print("threshold must be an integer")
exit()
else:
threshold = 150
input_path = input_path.replace("\\", "/")
if input_path[-1] == "/":
input_path = input_path[:-1]
output_dir = input_path + f"/blur_thresh_{threshold}_output_imgs"
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
input_path_initial = os.listdir(input_path)
input_path_len_initial = len(input_path_initial)
in_dir_iter = list(map(os.path.join, [input_path]*input_path_len_initial, input_path_initial))
list(map(thresh_and_remove, in_dir_iter, [output_dir]*input_path_len_initial, [threshold]*input_path_len_initial))