c 调用Python脚本和函数(一)

1.py

import numpy as np
def data():
#	matrix=np.arange(1,100)
	matrix=[[0,1,2,3],[1,1,1,1]]
	print matrix

1.cpp

#include<Python.h>
int main()
{
    Py_Initialize();
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");
    PyObject * pModule = NULL; //shengmingbianliang
    PyObject * pFunc = NULL;
    pModule =PyImport_ImportModule("1");
    pFunc= PyObject_GetAttrString(pModule, "data");
    PyEval_CallObject(pFunc, NULL);
    Py_Finalize();
    return 0;
}

 

发布了63 篇原创文章 · 获赞 4 · 访问量 4万+
展开阅读全文

在c语言程序中调用python脚本

03-05

最近在研究yolo,想在yolo源码(c语言)中调用随机森林算法来判断各检测框之间的关系。 只针对行人出检测框,提取框与框之间的四个参数:交并比、中心点距离、颜色直方图差值、面积差。以此作为输入参数。 先利用python完成了模型的训练并保存。在服务器上模拟调用也是可行的。随机生成数据,通过模型判断。 部分代码如下:test.c srand((unsigned)time(NULL)); for (i=0;i<50;i++){ io = rand()/(double)(RAND_MAX); printf("iou:%f\n",io); dist = rand()/(double)(RAND_MAX/100); printf("distance:%f\n",dist); su = rand()/(double)(RAND_MAX/100); printf("sub:%f\n",su); sua = rand()%50000; printf("suba:%d\n",sua); Py_Initialize(); if(!Py_IsInitialized()) { printf("Python init failed!\n"); return; } PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('//home//wangxiangsheng//wuhao//test')"); PyObject *pName = NULL; PyObject *pModule = NULL; PyObject *pDict = NULL; PyObject *pFunc = NULL; PyObject *pArgs = NULL; PyObject *pResult = NULL; pName = PyString_FromString("RF"); pModule = PyImport_Import(pName); pDict = PyModule_GetDict(pModule); pFunc = PyDict_GetItemString(pDict,"randomF"); pArgs = Py_BuildValue("(f,f,f,i)",io,dist,su,sua); pResult = PyObject_CallObject(pFunc,pArgs); PyArg_Parse(pResult, "i", &res); printf("result is %d\n",res); ``` 调用的python脚本:RF.py ``` import sklearn as skl from sklearn.externals import joblib from sklearn.ensemble import RandomForestClassifier def randomF(iou,distance,sub,suba): test_data = [[iou,distance,sub,suba]] clf = joblib.load("//home//wangxiangsheng//wuhao//train_model.m") output = clf.predict(test_data) print output return output 但是,当我按照这样改写yolo源码之后,运行起来就会出问题, ![图片说明](https://img-ask.csdn.net/upload/201803/05/1520231552_54452.jpg) 第一帧有数据,没画面,第二帧就直接卡住了。 代码较多,放部分片断 ``` Py_Initialize(); if(!Py_IsInitialized()) { printf("Python init failed!\n"); return; } PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('//home//wangxiangsheng//darknet//src')"); PyObject *pName = NULL; PyObject *pModule = NULL; PyObject *pDict = NULL; PyObject *pFunc = NULL; PyObject *pArgs = NULL; PyObject *pResult = NULL; pName = PyString_FromString("RF"); pModule = PyImport_Import(pName); if(!pModule) { printf("Load RF.py failed!\n"); getchar(); } pDict = PyModule_GetDict(pModule); if(!pDict) { printf("Can't find dict in RF!\n"); } pFunc = PyDict_GetItemString(pDict,"randomF"); if(!pFunc || !PyCallable_Check(pFunc)) { printf("Can't find function!\n"); getchar(); } for(j=0;j<10;j++){ for(w=j+1;w<39;w++){ float media = box_iou(para[j].p, para[w].p); float iout; if (media<=0){ iout = 0.000000; } else { iout = media; } int p,suba; float res; suba = abs(para[j].barea-para[w].barea); float sub=0; for(p=0;p<64;p++){ sub = sub + pow(para[j].hist[p]-para[w].hist[p],2); } float distance; distance = sqrt(pow(para[w].p.x-para[j].p.x,2)+pow(para[w].p.y-para[j].p.y,2)); printf("%f-%f-%f-%d \n",iout,distance,sub,suba); pArgs = Py_BuildValue("(f,f,f,i)",iout,distance,sub,suba); pResult = PyObject_CallObject(pFunc,pArgs); PyArg_Parse(pResult, "f", &res); printf("result is %f\n",res); ``` 求哪位指导一下,感激不尽! 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览