-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
68 lines (55 loc) · 1.59 KB
/
utils.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
from dataclasses import dataclass
import json
import os
import pathlib
import keras
import numpy as np
from typing import TypedDict
@dataclass
class ModelData:
model: keras.Model
preprocess_model: keras.Model | None
target_class: int
class Predictions(TypedDict):
""" Represent boxes and classes for a batch of images, thus having first dimension of length BATCH_SIZE. """
# (img, box, 4)
boxes: np.ndarray
# (img, class)
classes: np.ndarray
class Labels(TypedDict):
""" Represent boxes and classes for all images in a tf.data.Dataset. """
# (img, box, 4)
boxes: np.ndarray
# (img, class)
classes: np.ndarray
def setup() -> None:
os.chdir(pathlib.Path(__file__).parent.resolve())
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
def predictions_to_list(prediction_data: Predictions | list[Predictions]) -> list:
predictions_list = []
if not isinstance(prediction_data, list):
prediction_data = [prediction_data]
for predictions in prediction_data:
predictions_item = {
"boxes": predictions["boxes"].tolist(),
"classes": predictions["classes"].tolist(),
}
predictions_list.append(predictions_item)
return predictions_list
def main():
predictions = {
"boxes": np.array([
[
[20, 40, 10, 5]
]
]),
"classes": np.array([
[
1
]
])
}
predictions_list = predictions_to_list(predictions)
print(json.dumps(predictions_list, indent=4))
if __name__ == "__main__":
main()