6 from typing import Callable, List, NamedTuple, Optional, Set
12 logger = logging.getLogger(__name__)
13 parser = config.add_commandline_args(
14 f"ML Quick Labeler ({__file__})",
15 "Args related to quick labeling of ML training data",
18 "--ml_quick_label_skip_list_path",
19 default="./qlabel_skip_list.txt",
21 type=argparse_utils.valid_filename,
22 help="Path to file in which to store already labeled data.",
25 "--ml_quick_label_use_skip_lists",
27 action=argparse_utils.ActionNoYes,
28 help='Should we use a skip list file to speed up execution?',
31 "--ml_quick_label_overwrite_labels",
33 action=argparse_utils.ActionNoYes,
34 help='Enable overwriting existing labels; default is to not relabel.',
38 class InputSpec(NamedTuple):
39 image_file_glob: Optional[str]
40 image_file_prepopulated_list: Optional[List[str]]
41 image_file_to_features_file: Callable[[str], str]
43 valid_keystrokes: List[str]
45 keystroke_to_label: Callable[[str], str]
48 def read_skip_list() -> Set[str]:
50 if config.config['ml_quick_label_use_skip_lists']:
51 quick_skip_file = config.config['ml_quick_label_skip_list_path']
52 if os.path.exists(quick_skip_file):
53 with open(quick_skip_file, 'r') as f:
59 logger.debug(f'Read {quick_skip_file} and found {len(ret)} entries.')
63 def write_skip_list(skip_list) -> None:
64 if config.config['ml_quick_label_use_skip_lists']:
65 quick_skip_file = config.config['ml_quick_label_skip_list_path']
66 with open(quick_skip_file, 'w') as f:
67 for filename in skip_list:
68 filename = filename.strip()
70 f.write(f'{filename}\n')
71 logger.debug(f'Updated {quick_skip_file}')
74 def label(in_spec: InputSpec) -> None:
78 if in_spec.image_file_glob is not None:
79 images += glob.glob(in_spec.image_file_glob)
80 elif in_spec.image_file_prepopulated_list is not None:
81 images += in_spec.image_file_prepopulated_list
84 'One of image_file_glob or image_file_prepopulated_list is required'
87 skip_list = read_skip_list()
89 if image in skip_list:
90 logger.debug(f'Skipping {image} because of the skip list')
92 features = in_spec.image_file_to_features_file(image)
93 if features is None or not os.path.exists(features):
94 msg = f'File {image} yielded file {features} which does not exist, SKIPPING.'
99 # Render features and image.
101 with open(features, "r") as f:
102 lines = f.readlines()
106 if in_spec.label not in line:
107 filtered_lines.append(line)
111 if not saw_label or config.config['ml_quick_label_overwrite_labels']:
112 logger.info(features)
113 os.system(f'xv {image} &')
114 keystroke = input_utils.single_keystroke_response(
115 in_spec.valid_keystrokes,
116 prompt=in_spec.prompt,
118 os.system('killall xv')
119 label_value = in_spec.keystroke_to_label(keystroke)
120 filtered_lines.append(f"{in_spec.label}: {label_value}\n")
121 with open(features, 'w') as f:
122 f.writelines("%s\n" % line for line in filtered_lines)
124 write_skip_list(skip_list)