服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - c++ 调用python传输图片实例

c++ 调用python传输图片实例

2021-08-08 17:11ShellCollector C/C++

今天小编就为大家分享一篇c++ 调用python传输图片实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <Python.h>
 
#include <arrayobject.h>
 
 
 
#include "opencv2/imgcodecs.hpp"
 
#include "opencv2/imgproc.hpp"
 
#include "opencv2/videoio.hpp"
 
#include <opencv2/highgui.hpp>
 
#include <opencv2/video.hpp>
 
#include "opencv2/video/background_segm.hpp"
 
//using namespace std;
 
int init_numpy() {
 
 import_array();
 
}

初始化:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Py_SetPythonHome(L"D:\\Users\\Lenovo\\Anaconda3\\envs\\python35");
 
Py_Initialize();
 
init_numpy();
 
PyRun_SimpleString("import sys");
 
PyRun_SimpleString("sys.path.append('./')");
 
pModule = NULL;
 
pFunc = NULL;
 
pModule =PyImport_ImportModule("demo");
 
pFunc =PyObject_GetAttrString(pModule, "load_model");
 
PyEval_CallObject(pFunc,NULL);

传输代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
cv::Mat img =cv::imread("d:\\1.jpg", CV_LOAD_IMAGE_COLOR);
 
int m, n;
 
n = img.cols *3;
 
m = img.rows;
 
unsigned char *data = (unsigned char*)malloc(sizeof(unsignedchar) * m * n);
 
int p = 0;
 
for (int i = 0; i < m;i++)
 
{
 
 for (int j = 0; j < n; j++)
 
 {
 
  data[p]= img.at<unsignedchar>(i, j);
 
  p++;
 
 }
 
}
 
npy_intp Dims[2]= { m, n }; //给定维度信息
 
PyObject*PyArray = PyArray_SimpleNewFromData(2, Dims, NPY_UBYTE, data);
 
PyObject*ArgArray = PyTuple_New(1);
 
PyTuple_SetItem(ArgArray,0, PyArray);
 
PyObject *pDict= nullptr;
 
pDict =PyModule_GetDict(pModule);
 
PyObject*pFuncFive = PyDict_GetItemString(pDict, "load_image");
 
//PyObject_CallObject(pFuncFive, ArgArray);
 
 
 
PyObject*pReturn = PyObject_CallObject(pFuncFive, ArgArray);
 
int result;
 
PyArg_Parse(pReturn,"i", &result);
 
CString strtemp;
 
strtemp.Format(_T("%d"), result);
 
MessageBox(strtemp);

Python部分:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
importcv2
import numpyas np
 
w=227
h=227
c=3
sess = None
def arrayreset(array):
 # for i inrange(array.shape[1]/3):
 #  pass
 a = array[:,0:len( array[0] -2 ):3]
 b = array[:, 1:len( array[0] - 2 ):3]
 c = array[:, 2:len( array[0] - 2 ):3]
 a = a[:, :, None]
 b = b[:, :, None]
 c = c[:, :, None]
 m = np.concatenate((a,b,c),axis=2)
 return m
def load_model():
 global sess
 sess = tf.Session()
 saver = tf.train.import_meta_graph( './model/model.ckpt.meta')
 saver.restore( sess, tf.train.latest_checkpoint('./model/') )
 
def load_image(image):
 img = arrayreset(image)

其实还可以用imencode来解决:本文尚未完善

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Mat image = imread("d:\\11.jpeg", CV_LOAD_IMAGE_COLOR);
 
 
IplImage iplimage = image;
 
 
vector<uchar> buff;//buffer for coding
 
 
vector<int> param = vector<int>(2);
 
 
param[0] = CV_IMWRITE_JPEG_QUALITY;
 
 
param[1] = 95;//default(95) 0-100
 
imencode(".jpg", image, buff, param);
std::string str_encode(buff.begin(), buff.end());

以上这篇c++ 调用python传输图片实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/jacke121/article/details/78574476

延伸 · 阅读

精彩推荐