blob: 11d5040a25715291eb5207215b59225e09c18080 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#!env/bin/python3
2
Alex Tawsedaba3cf2023-09-29 15:55:38 +01003# SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00004# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""
19Utility script to convert a given text file with labels (annotations for an
20NN model output vector) into a vector list initialiser. The intention is for
21this script to be called as part of the build framework to auto-generate the
22cpp file with labels that can be used in the application without modification.
23"""
alexander3c798932021-03-26 21:42:19 +000024from argparse import ArgumentParser
Alex Tawsedaba3cf2023-09-29 15:55:38 +010025from pathlib import Path
Richard Burton17069622022-03-17 10:54:26 +000026
alexander3c798932021-03-26 21:42:19 +000027from jinja2 import Environment, FileSystemLoader
28
Alex Tawsedaba3cf2023-09-29 15:55:38 +010029from gen_utils import GenUtils
30
31# pylint: disable=duplicate-code
alexander3c798932021-03-26 21:42:19 +000032parser = ArgumentParser()
33
34# Label file path
Alex Tawsedaba3cf2023-09-29 15:55:38 +010035parser.add_argument(
36 "--labels_file",
37 type=str,
38 help="Path to the label text file",
39 required=True
40)
alexander3c798932021-03-26 21:42:19 +000041
Alex Tawsedaba3cf2023-09-29 15:55:38 +010042# Output file to be generated
43parser.add_argument(
44 "--source_folder_path",
45 type=str,
46 help="path to source folder to be generated.",
47 required=True
48)
49
50parser.add_argument(
51 "--header_folder_path",
52 type=str,
53 help="path to header folder to be generated.",
54 required=True
55)
56
57parser.add_argument(
58 "--output_file_name",
59 type=str,
60 help="Required output file name",
61 required=True
62)
63
64# Namespaces
65parser.add_argument(
66 "--namespaces",
67 action='append',
68 default=[]
69)
70
71# License template
72parser.add_argument(
73 "--license_template",
74 type=str,
75 help="Header template file",
76 default="header_template.txt"
77)
78
79parsed_args = parser.parse_args()
alexander3c798932021-03-26 21:42:19 +000080
Richard Burton17069622022-03-17 10:54:26 +000081env = Environment(loader=FileSystemLoader(Path(__file__).parent / 'templates'),
alexander3c798932021-03-26 21:42:19 +000082 trim_blocks=True,
83 lstrip_blocks=True)
84
85
Alex Tawsedaba3cf2023-09-29 15:55:38 +010086# pylint: enable=duplicate-code
alexander3c798932021-03-26 21:42:19 +000087def main(args):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010088 """
89 Generate labels .cpp
90 @param args: Parsed args
91 """
alexander3c798932021-03-26 21:42:19 +000092 # Get the labels from text file
Alex Tawsedaba3cf2023-09-29 15:55:38 +010093 with open(args.labels_file, "r", encoding="utf8") as f:
alexander3c798932021-03-26 21:42:19 +000094 labels = f.read().splitlines()
95
96 # No labels?
97 if len(labels) == 0:
Alex Tawsedaba3cf2023-09-29 15:55:38 +010098 raise ValueError(f"no labels found in {args.label_file}")
alexander3c798932021-03-26 21:42:19 +000099
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100100 hdr = GenUtils.gen_header(env, args.license_template, Path(args.labels_file).name)
alexander3c798932021-03-26 21:42:19 +0000101
Richard Burton17069622022-03-17 10:54:26 +0000102 hpp_filename = Path(args.header_folder_path) / (args.output_file_name + ".hpp")
alexander3c798932021-03-26 21:42:19 +0000103 env.get_template('Labels.hpp.template').stream(common_template_header=hdr,
Richard Burton17069622022-03-17 10:54:26 +0000104 filename=args.output_file_name.upper(),
alexander3c798932021-03-26 21:42:19 +0000105 namespaces=args.namespaces) \
106 .dump(str(hpp_filename))
107
Richard Burton17069622022-03-17 10:54:26 +0000108 cc_filename = Path(args.source_folder_path) / (args.output_file_name + ".cc")
alexander3c798932021-03-26 21:42:19 +0000109 env.get_template('Labels.cc.template').stream(common_template_header=hdr,
110 labels=labels,
111 labelsSize=len(labels),
112 namespaces=args.namespaces) \
113 .dump(str(cc_filename))
114
115
116if __name__ == '__main__':
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100117 main(parsed_args)