1

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.?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.