#!/usr/bin/env bash ''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" # ''' ''''export PY_PATH=${SCRIPT_PATH}/venv/bin/python # ''' ''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@" # ''' ''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # ''' ''''exit 255 # ''' # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import subprocess import sys import utils as _utils # TODO Find better way to suppress trackback on error sys.tracebacklimit = 0 def _get_parser(): parser = argparse.ArgumentParser( description='command line tool to convert TensorFlow lite to circle') _utils._add_default_arg(parser) ## tflite2circle arguments tflite2circle_group = parser.add_argument_group('converter arguments') # input and output path. tflite2circle_group.add_argument( '-i', '--input_path', type=str, help='full filepath of the input file') tflite2circle_group.add_argument( '-o', '--output_path', type=str, help='full filepath of the output file') return parser def _verify_arg(parser, args): """verify given arguments""" # check if required arguments is given missing = [] if not _utils._is_valid_attr(args, 'input_path'): missing.append('-i/--input_path') if not _utils._is_valid_attr(args, 'output_path'): missing.append('-o/--output_path') if len(missing): parser.error('the following arguments are required: ' + ' '.join(missing)) def _parse_arg(parser): args = parser.parse_args() # print version if args.version: _utils._print_version_and_exit(__file__) return args def _convert(args): # get file path to log dir_path = os.path.dirname(os.path.realpath(__file__)) logfile_path = os.path.realpath(args.output_path) + '.log' with open(logfile_path, 'wb') as f: # make a command to convert from tflite to circle tflite2circle_path = os.path.join(dir_path, 'tflite2circle') tflite2circle_cmd = _utils._make_tflite2circle_cmd(tflite2circle_path, getattr(args, 'input_path'), getattr(args, 'output_path')) f.write((' '.join(tflite2circle_cmd) + '\n').encode()) # convert tflite to circle _utils._run(tflite2circle_cmd, err_prefix="tflite2circle", logfile=f) def main(): # parse arguments parser = _get_parser() args = _parse_arg(parser) # parse configuration file _utils._parse_cfg(args, 'one-import-tflite') # verify arguments _verify_arg(parser, args) # convert _convert(args) if __name__ == '__main__': _utils._safemain(main, __file__)