blob: 25c4bc3d05dc7f9ff2325294a8819279e79f225b [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#!env/bin/python3
2
3# Copyright (c) 2021 Arm Limited. All rights reserved.
4# 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"""
24import datetime
Richard Burton17069622022-03-17 10:54:26 +000025from pathlib import Path
alexander3c798932021-03-26 21:42:19 +000026from argparse import ArgumentParser
Richard Burton17069622022-03-17 10:54:26 +000027
alexander3c798932021-03-26 21:42:19 +000028from jinja2 import Environment, FileSystemLoader
29
30parser = ArgumentParser()
31
32# Label file path
33parser.add_argument("--labels_file", type=str, help="Path to the label text file", required=True)
34# Output file to be generated
35parser.add_argument("--source_folder_path", type=str, help="path to source folder to be generated.", required=True)
36parser.add_argument("--header_folder_path", type=str, help="path to header folder to be generated.", required=True)
37parser.add_argument("--output_file_name", type=str, help="Required output file name", required=True)
38# Namespaces
39parser.add_argument("--namespaces", action='append', default=[])
40# License template
41parser.add_argument("--license_template", type=str, help="Header template file",
42 default="header_template.txt")
43
44args = parser.parse_args()
45
Richard Burton17069622022-03-17 10:54:26 +000046env = Environment(loader=FileSystemLoader(Path(__file__).parent / 'templates'),
alexander3c798932021-03-26 21:42:19 +000047 trim_blocks=True,
48 lstrip_blocks=True)
49
50
51def main(args):
52 # Get the labels from text file
53 with open(args.labels_file, "r") as f:
54 labels = f.read().splitlines()
55
56 # No labels?
57 if len(labels) == 0:
58 raise Exception(f"no labels found in {args.label_file}")
59
60 header_template = env.get_template(args.license_template)
Richard Burton17069622022-03-17 10:54:26 +000061 hdr = header_template.render(script_name=Path(__file__).name,
alexander3c798932021-03-26 21:42:19 +000062 gen_time=datetime.datetime.now(),
Richard Burton17069622022-03-17 10:54:26 +000063 file_name=Path(args.labels_file).name,
alexander3c798932021-03-26 21:42:19 +000064 year=datetime.datetime.now().year)
65
Richard Burton17069622022-03-17 10:54:26 +000066 hpp_filename = Path(args.header_folder_path) / (args.output_file_name + ".hpp")
alexander3c798932021-03-26 21:42:19 +000067 env.get_template('Labels.hpp.template').stream(common_template_header=hdr,
Richard Burton17069622022-03-17 10:54:26 +000068 filename=args.output_file_name.upper(),
alexander3c798932021-03-26 21:42:19 +000069 namespaces=args.namespaces) \
70 .dump(str(hpp_filename))
71
Richard Burton17069622022-03-17 10:54:26 +000072 cc_filename = Path(args.source_folder_path) / (args.output_file_name + ".cc")
alexander3c798932021-03-26 21:42:19 +000073 env.get_template('Labels.cc.template').stream(common_template_header=hdr,
74 labels=labels,
75 labelsSize=len(labels),
76 namespaces=args.namespaces) \
77 .dump(str(cc_filename))
78
79
80if __name__ == '__main__':
81 main(args)