How to implement Array in python Swig.?

∥☆過路亽.° 提交于 2021-02-11 06:30:25

问题


I am wrote sample C++ application. Now I am going communicate with Python through Swig.

What I did

  1. Generated interface file for my function

classify.i

%module example

%{
#include "Facdetect.h"
%}
%include "typemaps.i"
%include "cstring.i"
%include "cdata.i"
%include <std_string.i>
%apply unsigned char *{unsigned char *x};
%apply double *INOUT{double *y};
%include "Facdetect.h"

Facdetect.h

#include <iostream>
class facedetect
{
public:
void display(unsigned char  *x,double  *y);
};

sample.cpp

#include <cstdlib>
#include "Facdetect.h"


using namespace std;
void facedetect::display( unsigned char  *x,double  *y)
{

    cv::Mat in(512,512,CV_8UC3,x);
    cv::Mat res=imdecode(in,1);

    cv::cvtColor(res,res,cv::COLOR_RGB2GRAY);

       std::vector<uchar> buff;
      cv::imencode(".jpg",res,buff);
       int k=0;
   std::cout<<" buffer size "<<buff.size()<<std::endl;
   for(int j=0;j<10;j++)
   {
   y[k++]=j;

   }
   }

sample.py

import os
import io
import sys
import time
import example
from PIL import Image
import struct
from array import array

def readimage(path):
    count = os.stat(path).st_size / 2
    with open(path, "rb") as f:
        return bytearray(f.read())

bytes = readimage("sample.jpg")
print len(bytes)
c=example.facedetect()
ret = array(10)
ret=c.display(bytes,ret)
print ret
  1. I had successfully passed byte array from Python to C++.

  2. Now i am passing double array values into python fro C++.

  3. I did not get double array values in python from C++.

What I need:

  1. How to get double array values from C++ to python.?

  2. Is any typemaps i missed for generating swig wrapper.?

来源:https://stackoverflow.com/questions/33297064/how-to-implement-array-in-python-swig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!