blob: 1be9c637e780650b09934c314fbd8e9f94a566fb [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
25import os
26from argparse import ArgumentParser
27from jinja2 import Environment, FileSystemLoader
28
29parser = ArgumentParser()
30
31# Label file path
32parser.add_argument("--labels_file", type=str, help="Path to the label text file", required=True)
33# Output file to be generated
34parser.add_argument("--source_folder_path", type=str, help="path to source folder to be generated.", required=True)
35parser.add_argument("--header_folder_path", type=str, help="path to header folder to be generated.", required=True)
36parser.add_argument("--output_file_name", type=str, help="Required output file name", required=True)
37# Namespaces
38parser.add_argument("--namespaces", action='append', default=[])
39# License template
40parser.add_argument("--license_template", type=str, help="Header template file",
41 default="header_template.txt")
42
43args = parser.parse_args()
44
45env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')),
46 trim_blocks=True,
47 lstrip_blocks=True)
48
49
50def main(args):
51 # Get the labels from text file
52 with open(args.labels_file, "r") as f:
53 labels = f.read().splitlines()
54
55 # No labels?
56 if len(labels) == 0:
57 raise Exception(f"no labels found in {args.label_file}")
58
59 header_template = env.get_template(args.license_template)
60 hdr = header_template.render(script_name=os.path.basename(__file__),
61 gen_time=datetime.datetime.now(),
62 file_name=os.path.basename(args.labels_file),
63 year=datetime.datetime.now().year)
64
65 hpp_filename = os.path.join(args.header_folder_path, args.output_file_name + ".hpp")
66 env.get_template('Labels.hpp.template').stream(common_template_header=hdr,
67 filename=(args.output_file_name).upper(),
68 namespaces=args.namespaces) \
69 .dump(str(hpp_filename))
70
71
72 cc_filename = os.path.join(args.source_folder_path, args.output_file_name + ".cc")
73 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)