问题
I am wrote sample C++ application. Now I am going communicate with Python through Swig.
What I did
- 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
I had successfully passed byte array from Python to C++.
Now i am passing double array values into python fro C++.
I did not get double array values in python from C++.
What I need:
How to get double array values from C++ to python.?
Is any typemaps i missed for generating swig wrapper.?
来源:https://stackoverflow.com/questions/33297064/how-to-implement-array-in-python-swig