ARTS 打卡 -05

发布于: 2020 年 07 月 05 日
ARTS打卡-05

Algorithm

Question: Reverse String

class Solution {
public:
void reverseString(vector<char>& s) {
int len = s.size();
int i = 0;
int j = len - 1;
while(i < j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
};

Performance: runtime beats 66.27%, memory beats 62.28%.

Review

Simplified Wrapper and Interface Generator (SWIG) is an interface compiler that connects programs written in C and C++ with scripting languages such as Perl, Python, Ruby, and Tcl. It works by taking the declarations found in C/C++ header files and using them to generate the wrapper code that scripting languages need to access the underlying C/C++ code. In addition, SWIG provides a variety of customization features that let you tailor the wrapping process to suit your application.

John Ousterhout (creator of Tcl) has written a paper that describes the benefits of scripting languages. SWIG makes it fairly easy to connect scripting languages with C/C++ code.

http://www.swig.org/exec.html

Tips

Some useful commands when debugging the building errors:

Share

How to use SWIG to call OpenCV c++ code in python

  1. c++ code: postprocess.cpp, postprocess.hpp

  2. SWIG file: postprocess.i

%module postprocess
%{
#include "postprocess.hpp"
%}
void test();
  1. setup.py

from distutils.core import setup, Extension
setup(name='postprocess_ext', version='1.0',
ext_modules=[
Extension('_postprocess', ['postprocess.cpp', 'postprocess.i'],
swig_opts=['-c++'],
depends=["postprocess.hpp"],
include_dirs=['/opt/intel/openvino_2020.2.120/opencv/include'],
library_dirs=['/opt/intel/openvino_2020.2.120/opencv/lib'],
libraries=['opencv_core', 'opencv_imgproc']
)
],
py_modules=[
"postprocess"
]
)
  1. Command:

-python -c++ postprocess.i && python3 setup.py build_ext --inplace

So far, folder looks like this:

Now you can use postprocess like below:

from human_pose_estimation_demo.postprocess import postprocess
postprocess.test()

用户头像

Geek_yansheng25

关注

还未添加个人签名 2020.05.21 加入

还未添加个人简介

评论

发布
暂无评论
ARTS打卡-05