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 |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 25 | from pathlib import Path |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | from argparse import ArgumentParser |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 27 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | from jinja2 import Environment, FileSystemLoader |
| 29 | |
| 30 | parser = ArgumentParser() |
| 31 | |
| 32 | # Label file path |
| 33 | parser.add_argument("--labels_file", type=str, help="Path to the label text file", required=True) |
| 34 | # Output file to be generated |
| 35 | parser.add_argument("--source_folder_path", type=str, help="path to source folder to be generated.", required=True) |
| 36 | parser.add_argument("--header_folder_path", type=str, help="path to header folder to be generated.", required=True) |
| 37 | parser.add_argument("--output_file_name", type=str, help="Required output file name", required=True) |
| 38 | # Namespaces |
| 39 | parser.add_argument("--namespaces", action='append', default=[]) |
| 40 | # License template |
| 41 | parser.add_argument("--license_template", type=str, help="Header template file", |
| 42 | default="header_template.txt") |
| 43 | |
| 44 | args = parser.parse_args() |
| 45 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 46 | env = Environment(loader=FileSystemLoader(Path(__file__).parent / 'templates'), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | trim_blocks=True, |
| 48 | lstrip_blocks=True) |
| 49 | |
| 50 | |
| 51 | def 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 Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 61 | hdr = header_template.render(script_name=Path(__file__).name, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | gen_time=datetime.datetime.now(), |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 63 | file_name=Path(args.labels_file).name, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | year=datetime.datetime.now().year) |
| 65 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 66 | hpp_filename = Path(args.header_folder_path) / (args.output_file_name + ".hpp") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | env.get_template('Labels.hpp.template').stream(common_template_header=hdr, |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 68 | filename=args.output_file_name.upper(), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | namespaces=args.namespaces) \ |
| 70 | .dump(str(hpp_filename)) |
| 71 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 72 | cc_filename = Path(args.source_folder_path) / (args.output_file_name + ".cc") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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) |