-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel.py
More file actions
77 lines (60 loc) · 2.18 KB
/
label.py
File metadata and controls
77 lines (60 loc) · 2.18 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
74
75
76
77
#!/usr/bin/env python3
"""
Label captured images by typing the display reading for each.
Usage:
python label.py # label images in captures/
python label.py --dir my_captures # custom directory
python label.py --labels my_labels.json
Controls:
Type the reading shown (e.g. "15.31") and press Enter
Type "skip" to skip an image
Type "dash" for ---- display
Type "quit" to stop
"""
import cv2
import json
import sys
import argparse
from pathlib import Path
def main():
parser = argparse.ArgumentParser(description='Label captured images')
parser.add_argument('--dir', default='captures',
help='Directory with images (default: captures)')
parser.add_argument('--labels', default='labels_new.json',
help='Labels output file (default: labels_new.json)')
args = parser.parse_args()
img_dir = Path(args.dir)
labels_file = Path(args.labels)
# Load existing labels
labels = {}
if labels_file.exists():
with open(labels_file) as f:
labels = json.load(f)
# Find unlabeled images
images = sorted(img_dir.glob('*.jpeg')) + sorted(img_dir.glob('*.jpg')) + sorted(img_dir.glob('*.png'))
unlabeled = [img for img in images if img.name not in labels]
print(f"Images: {len(images)} total, {len(labels)} labeled, {len(unlabeled)} remaining")
print("Type the reading, 'skip', 'dash', or 'quit'")
print("-" * 40)
for img_path in unlabeled:
img = cv2.imread(str(img_path))
if img is None:
continue
cv2.imshow('Label This Image', img)
cv2.waitKey(100)
value = input(f" {img_path.name}: ").strip()
if value.lower() == 'quit':
break
elif value.lower() == 'skip':
continue
elif value.lower() == 'dash':
labels[img_path.name] = '----'
elif value:
labels[img_path.name] = value
# Save after each label
with open(labels_file, 'w') as f:
json.dump(labels, f, indent=2)
cv2.destroyAllWindows()
print(f"\nLabeled: {len(labels)} images saved to {labels_file}")
if __name__ == '__main__':
main()