-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannotate_pose.py
210 lines (176 loc) · 8.15 KB
/
annotate_pose.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import argparse
import os
import cv2
import numpy as np
from src.interactive_annotator import InteractiveAnnotator
from src.json_utils import increment_idx, load_annotations, save_annotations, upload_annotations, authenticate_drive
def parse_imdir(annotations_file):
ann_filename = ".".join(os.path.basename(annotations_file).split(".")[::-1])
ann_filename = ann_filename.replace("_kpts", "")
ann_type = ann_filename.split("_")[-1]
if ann_type not in ["train2017", "val2017"]:
print(
"Could not determine image directory from annotations file name. Using 'val2017' as default."
)
ann_type = "val2017"
coco_ann_root = os.path.dirname(annotations_file)
return os.path.join(os.path.dirname(coco_ann_root), ann_type)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"coco_filepath",
type=str,
help="Filename of the coco annotations file",
)
parser.add_argument("--img-path", type=str, help="Path to the folder with images", default=None)
# Optional arguments
parser.add_argument(
"--pose-format", type=str, default="coco", help="Format of the annotated skeleton"
)
parser.add_argument("--without-hands", default=True, action=argparse.BooleanOptionalAction)
parser.add_argument("--save", default=True, action=argparse.BooleanOptionalAction)
parser.add_argument("--cloud-upload", default=False, action=argparse.BooleanOptionalAction)
parser.add_argument("--cloud-folder", type=str, help="Google Drive folder ID for uploading annotations", default='root')
args = parser.parse_args()
if not (os.path.exists(args.coco_filepath) and os.path.isfile(args.coco_filepath)):
old_fname = os.path.join(args.coco_filepath, "annotations", "person_keypoints_val2017.json")
new_fname = os.path.join(
args.coco_filepath, "annotations", "person_keypoints_val2017_kpts.json"
)
if os.path.exists(new_fname):
args.coco_filepath = new_fname
else:
args.coco_filepath = old_fname
assert os.path.exists(args.coco_filepath), "COCO annotations file ({:s}) not found".format(
args.coco_filepath
)
assert os.path.isfile(args.coco_filepath), "COCO annotations file ({:s}) is not a file".format(
args.coco_filepath
)
if args.img_path is None:
args.img_path = parse_imdir(args.coco_filepath)
args.pose_format = args.pose_format.lower()
implemented_formats = ["coco", "coco_with_thumbs"]
if args.pose_format not in implemented_formats:
raise NotImplementedError(
"Format {:s} not implemented. Use one of the following: {}".format(
args.pose_format, implemented_formats
)
)
return args
def main(args):
# Load the data
coco_data, id2name, _, _ = load_annotations(args.coco_filepath)
ann_idx = 0
new_coco_filepath = args.coco_filepath
if "_kpts.json" not in args.coco_filepath:
new_coco_filepath = args.coco_filepath.replace(".json", "_kpts.json")
if args.cloud_upload:
from pydrive.drive import GoogleDrive
gauth = authenticate_drive()
drive = GoogleDrive(gauth)
folder_id = args.cloud_folder
file_name = os.path.basename(new_coco_filepath)
annotations = coco_data["annotations"]
cv2.namedWindow("Image", cv2.WINDOW_GUI_NORMAL)
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
pose_format=args.pose_format,
)
cv2.setMouseCallback("Image", ia.mouse_callback)
while cv2.getWindowProperty("Image", cv2.WND_PROP_VISIBLE) > 0:
# The function waitKey waits for a key event infinitely (when delay<=0)
k = cv2.waitKey(100)
if k == ord("m") or k == 83: # toggle current image
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
ann_idx = increment_idx(ann_idx, len(annotations), 1)
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
pose_format=args.pose_format,
)
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
cv2.setMouseCallback("Image", ia.mouse_callback)
elif k == ord("."): # jump 10
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
ann_idx = increment_idx(ann_idx, len(annotations), 10)
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
)
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
cv2.setMouseCallback("Image", ia.mouse_callback)
elif k == ord(","): # jump -10
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
ann_idx = increment_idx(ann_idx, len(annotations), -10)
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
)
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
cv2.setMouseCallback("Image", ia.mouse_callback)
elif k == ord("n") or k == 81:
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
ann_idx = increment_idx(ann_idx, len(annotations), -1)
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
pose_format=args.pose_format,
)
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
cv2.setMouseCallback("Image", ia.mouse_callback)
elif k == ord("x"):
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
ann_idx = np.random.randint(len(annotations))
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
pose_format=args.pose_format,
)
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
cv2.setMouseCallback("Image", ia.mouse_callback)
elif k == ord("q"):
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
break
elif k == ord("u"):
annotations[ann_idx] = ia.get_annotation(json_compatible=True)
while "checked" in annotations[ann_idx].keys():
ann_idx = increment_idx(ann_idx, len(annotations), 1)
ia = InteractiveAnnotator(
annotations[ann_idx],
os.path.join(args.img_path, id2name[annotations[ann_idx]["image_id"]]),
is_start=ann_idx == 0,
pose_format=args.pose_format,
)
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
cv2.setMouseCallback("Image", ia.mouse_callback)
else:
ia.key_pressed(k)
cv2.destroyAllWindows()
coco_data["annotations"] = annotations
if args.save:
save_annotations(new_coco_filepath, coco_data, update_date=True)
if args.cloud_upload:
upload_annotations(drive, coco_data, file_name, folder_id)
if __name__ == "__main__":
args = parse_args()
main(args)