baidu_38392386
TLedTS
采纳率50%
2018-03-05 06:41 阅读 3.6k

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

10

最近在研究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源码之后,运行起来就会出问题,
图片说明
第一帧有数据,没画面,第二帧就直接卡住了。

代码较多,放部分片断

     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);

求哪位指导一下,感激不尽!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐