-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_filter.py
91 lines (79 loc) · 3.5 KB
/
manual_filter.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Script for manual filtering of images as follows '[Key]: [Tag]':
'p': PASS
'f': FAIL
'r': RECHECK
'o': OTHER
DEL: Delete
ESC: Quit
All other keys: Skip
"""
import argparse
from glob import glob
import os
import shutil
from tkinter import filedialog
from tkinter import *
import cv2 as cv
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--other-only", action="store_true", help="Only allow the OTHER and Skip actions.")
parser.add_argument("-c", "--copy", action="store_true", help="Copy images instead of moving them.")
args = parser.parse_args()
def ResizeWithAspectRatio(image, width=None, height=None, inter=cv.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
return cv.resize(image, dim, interpolation=inter)
def main():
root = Tk()
root.withdraw()
indir = filedialog.askdirectory(title="Input directory of UNFILTERED images")
if not args.other_only:
passdir = filedialog.askdirectory(title="Directory to move images that PASSED the filter")
faildir = filedialog.askdirectory(title="Directory to move images that FAILED the filter")
recheckdir = filedialog.askdirectory(title="Directory to move images that need to be RECHECKED")
otherdir = filedialog.askdirectory(title="Directory to move images that fulfill some OTHER criteria")
if args.copy:
fn = shutil.copy
else:
fn = shutil.move
imgs = glob(f"{indir}/*.jpg")
for ii, img_path in enumerate(imgs):
img = cv.imread(img_path, cv.IMREAD_UNCHANGED)
cv.namedWindow(img_path, cv.WINDOW_NORMAL)
cv.setWindowProperty(img_path, cv.WND_PROP_ASPECT_RATIO, cv.WINDOW_KEEPRATIO)
cv.setWindowProperty(img_path, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
# img = ResizeWithAspectRatio(img, height=1280)
cv.imshow(img_path, img)
cv.moveWindow(img_path, 0, 0)
pressedKey = cv.waitKey(0) & 0xFF
if (pressedKey == ord('p') or pressedKey == ord('P')) and not args.other_only:
fn(img_path, os.path.join(passdir, os.path.basename(img_path)))
print(f"[{ii + 1}] Marked as PASS: {os.path.basename(img_path)}")
elif (pressedKey == ord('f') or pressedKey == ord('F')) and not args.other_only:
fn(img_path, os.path.join(faildir, os.path.basename(img_path)))
print(f"[{ii + 1}] Marked as FAIL: {os.path.basename(img_path)}")
elif (pressedKey == ord('r') or pressedKey == ord('R')) and not args.other_only:
fn(img_path, os.path.join(recheckdir, os.path.basename(img_path)))
print(f"[{ii + 1}] Marked as RECHECK: {os.path.basename(img_path)}")
elif pressedKey == ord('o') or pressedKey == ord('O'):
fn(img_path, os.path.join(otherdir, os.path.basename(img_path)))
print(f"[{ii + 1}] Marked as OTHER: {os.path.basename(img_path)}")
# Helpful here: https://www.asciitable.com
elif pressedKey == 8 or pressedKey == 127: # Backspace or Delete keys
os.remove(img_path)
print(f"[{ii + 1}] Deleted: {os.path.basename(img_path)}")
elif pressedKey == 27: # Escape key
cv.destroyAllWindows()
break
else:
print(f"[{ii + 1}] Skipped: {os.path.basename(img_path)}")
cv.destroyAllWindows()
if __name__ == "__main__":
main()