-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsloth_remove_deleted.py
executable file
·45 lines (37 loc) · 1.18 KB
/
sloth_remove_deleted.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
#!/usr/bin/env python3
"""
Creating a new dataset from part of the old dataset, I only copied some of the
images, and I'd rather not re-label them all. This removes all the deleted
images from the Sloth JSON file. (Or, rather, just doesn't print them out.)
It can also remove a set of classes if desired.
"""
import os
import sys
import json
from sloth_common import getJson
def removeDeleted(data, ignoreClasses=[]):
"""
If the image doesn't exist, then remove it from the JSON file
"""
newData = []
for d in data:
if os.path.exists(d["filename"]):
filename = d["filename"]
className = d["class"]
annotations = []
for a in d["annotations"]:
if a["class"] not in ignoreClasses:
annotations.append(a)
newData.append({
"annotations": annotations,
"class": className,
"filename": filename
})
return newData
def outputJson(data):
print(json.dumps(data, indent=4))
if __name__ == "__main__":
f = os.path.join("sloth.json")
data = getJson(f)
data = removeDeleted(data)
outputJson(data)