-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslothRemoveDeleted.py
More file actions
51 lines (43 loc) · 1.56 KB
/
slothRemoveDeleted.py
File metadata and controls
51 lines (43 loc) · 1.56 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
"""
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
import config
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(os.path.join(config.datasetFolder, d["filename"])):
filename = d["filename"]
className = d["class"]
annotations = []
for a in d["annotations"]:
if a["class"] not in ignoreClasses:
annotations.append(a)
# If we still have some annotations...
if annotations:
newData.append({
"annotations": annotations,
"class": className,
"filename": filename
})
else:
# Removed all annotations due to the skipped ones, so we don't
# really need this file. Alert user.
print("rm", d["filename"], file=sys.stderr)
return newData
def outputJson(data):
print(json.dumps(data, indent=4))
if __name__ == "__main__":
f = os.path.join(config.datasetFolder, "sloth.json")
data = getJson(f)
data = removeDeleted(data, ignoreClasses=["human"])
outputJson(data)