1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 %include "std_string.i" 17 18 19 %{ 20 #define SWIG_FILE_WITH_INIT 21 #include "tensorflow/lite/interpreter.h" 22 #include "tensorflow/lite/model.h" 23 #include "tensorflow/lite/python/interpreter_wrapper/interpreter_wrapper.h" 24 #include "tensorflow/lite/python/interpreter_wrapper/python_error_reporter.h" 25 %} 26 27 28 %include "tensorflow/lite/python/interpreter_wrapper/interpreter_wrapper.h" 29 30 namespace tflite { 31 namespace interpreter_wrapper { 32 %extend InterpreterWrapper { 33 34 // Version of the constructor that handles producing Python exceptions 35 // that propagate strings. CreateWrapperCPPFromFile(const char * model_path)36 static PyObject* CreateWrapperCPPFromFile(const char* model_path) { 37 std::string error; 38 if(tflite::interpreter_wrapper::InterpreterWrapper* ptr = 39 tflite::interpreter_wrapper::InterpreterWrapper 40 ::CreateWrapperCPPFromFile( 41 model_path, &error)) { 42 return SWIG_NewPointerObj( 43 ptr, SWIGTYPE_p_tflite__interpreter_wrapper__InterpreterWrapper, 1); 44 } else { 45 PyErr_SetString(PyExc_ValueError, error.c_str()); 46 return nullptr; 47 } 48 } 49 50 // Version of the constructor that handles producing Python exceptions 51 // that propagate strings. CreateWrapperCPPFromBuffer(PyObject * data)52 static PyObject* CreateWrapperCPPFromBuffer( 53 PyObject* data) { 54 std::string error; 55 if(tflite::interpreter_wrapper::InterpreterWrapper* ptr = 56 tflite::interpreter_wrapper::InterpreterWrapper 57 ::CreateWrapperCPPFromBuffer( 58 data, &error)) { 59 return SWIG_NewPointerObj( 60 ptr, SWIGTYPE_p_tflite__interpreter_wrapper__InterpreterWrapper, 1); 61 } else { 62 PyErr_SetString(PyExc_ValueError, error.c_str()); 63 return nullptr; 64 } 65 } 66 } 67 68 } // namespace interpreter_wrapper 69 } // namespace tflite 70