-
Notifications
You must be signed in to change notification settings - Fork 1
/
tao_to_lightly.py
89 lines (72 loc) · 2.36 KB
/
tao_to_lightly.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
import argparse
import json
from pathlib import Path
from tqdm import tqdm
import numpy as np
def tao_to_lightly(input_dir: Path) -> None:
task_name = "minneapple"
# Create the necessary directories.
lightly_dir = Path(".lightly/")
prediction_dir = lightly_dir / "predictions"
minneapple_dir = prediction_dir / task_name
output_dir = minneapple_dir / "raw/images/"
output_dir.mkdir(parents=True, exist_ok=True)
# Convert the predictions from TAO to Lightly
for tao_prediction_file in tqdm(input_dir.glob("*.txt")):
contents = np.genfromtxt(tao_prediction_file)
if len(contents.shape) > 1:
lightly_prediction = {
"file_name": str(
Path("raw/images/") / tao_prediction_file.with_suffix(".png").name
),
"predictions": [],
}
for (
x0,
y0,
x1,
y1,
_,
_,
_,
_,
_,
_,
_,
score,
) in contents[:, 4:]:
lightly_prediction["predictions"].append(
{
"category_id": 0, # everything is an apple
"bbox": [x0, y0, x1 - x0, y1 - y0],
"score": score,
}
)
lightly_prediction_file = (
output_dir / tao_prediction_file.with_suffix(".json").name
)
with lightly_prediction_file.open("w") as f:
json.dump(lightly_prediction, f)
# Add tasks and schema
tasks = [task_name]
task_file = prediction_dir / "tasks.json"
with task_file.open("w") as f:
json.dump(tasks, f)
schema = {
"task_type": "object-detection",
"categories": [
{
"id": 0,
"name": "Apple",
}
],
}
schema_file = minneapple_dir / "schema.json"
with schema_file.open("w") as f:
json.dump(schema, f)
print("The .lightly directory was successfully created and populated.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--input_dir", type=str)
args = parser.parse_args()
tao_to_lightly(Path(args.input_dir))