alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame^] | 1 | #!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 | """ |
| 19 | Utility script to convert a given text file with labels (annotations for an |
| 20 | NN model output vector) into a vector list initialiser. The intention is for |
| 21 | this script to be called as part of the build framework to auto-generate the |
| 22 | cpp file with labels that can be used in the application without modification. |
| 23 | """ |
| 24 | import datetime |
| 25 | import os |
| 26 | from argparse import ArgumentParser |
| 27 | from jinja2 import Environment, FileSystemLoader |
| 28 | |
| 29 | parser = ArgumentParser() |
| 30 | |
| 31 | # Label file path |
| 32 | parser.add_argument("--labels_file", type=str, help="Path to the label text file", required=True) |
| 33 | # Output file to be generated |
| 34 | parser.add_argument("--source_folder_path", type=str, help="path to source folder to be generated.", required=True) |
| 35 | parser.add_argument("--header_folder_path", type=str, help="path to header folder to be generated.", required=True) |
| 36 | parser.add_argument("--output_file_name", type=str, help="Required output file name", required=True) |
| 37 | # Namespaces |
| 38 | parser.add_argument("--namespaces", action='append', default=[]) |
| 39 | # License template |
| 40 | parser.add_argument("--license_template", type=str, help="Header template file", |
| 41 | default="header_template.txt") |
| 42 | |
| 43 | args = parser.parse_args() |
| 44 | |
| 45 | env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')), |
| 46 | trim_blocks=True, |
| 47 | lstrip_blocks=True) |
| 48 | |
| 49 | |
| 50 | def 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 | |
| 80 | if __name__ == '__main__': |
| 81 | main(args) |