一、C调用Python

1.新建一个Python文件,名称为py_multipy.py:

 #import numpy as np
def multiply(a=1,b=2):
print('Function of python called!')
print('a:',a)
print('b:',b)
print('a*b:',a*b)
#print('numpy a*b:',np.multiply(a,b))

2.新建一个C调用文件,名称为call_python.c

 #include <stdio.h>
#include <stdlib.h>
#include <Python.h> int main()
{
Py_Initialize(); if(!Py_IsInitialized())
{
printf("Python init failed!\n");
return -;
} PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')"); PyObject *pDict = NULL;
PyObject *pModule = NULL;
PyObject *pName = NULL;
PyObject *pFunc = NULL;
PyObject *pArgs = NULL; pName = PyString_FromString("py_add");
pModule = PyImport_Import(pName);
if (!pModule)
{
printf("Load py_add.py failed!\n");
getchar();
return -;
} pDict = PyModule_GetDict(pModule);
if(!pDict)
{
printf("Can't find dict in py_add!\n");
return -;
} pFunc = PyDict_GetItemString(pDict,"add");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("Can't find function!\n");
getchar();
return -;
} pArgs = PyTuple_New(); PyTuple_SetItem(pArgs,,Py_BuildValue("i",));
PyTuple_SetItem(pArgs,,Py_BuildValue("i",)); PyObject_CallObject(pFunc,pArgs); if(pName)
{
Py_DECREF(pName);
} if(pArgs)
{
Py_DECREF(pArgs);
} if(pModule)
{
Py_DECREF(pModule);
} Py_Finalize();
return ; }

3.编译C文件

gcc -I/usr/include/python2.7/ call_python.c -o call_python -L/usr/lib/ -lpython2.7

在当前目录下生成可执行文件call_python

4.执行新生成的文件:./call_python

显示结果如下:

Function of python called!

('a:',111)

('b:',222)

('a*b:',333)

二、Python调用C

1.新建一个pcallc.c

 #include <stdio.h>
#include <pcallc.h> int add(int a,int b)
{
int c= ;
c = a+b;
printf("add c:",c);
return c;
}

2.新建一个pcallc.h

 #ifndef PCALLC_H
#define PCALLC_H int add(); #endif

3.生成动态库:pcallc.so

gcc -o pcallc.so -shared -fPIC pcallc.c

4.新建pcallc.py

 #-*- coding:utf-8 -*-

 import ctypes

 loadso = ctypes.cdll.LoadLibrary
lib= loadso("./pcallc.so")
lib.add(1,2) print("***finish***")

5.运行pcallc.py

python pcallc.py

add c:3

finish!

Tensorflow模型移植Arm之一:C与Python互相调用的更多相关文章

  1. 将训练好的tensorflow模型移植到android应用中

    具体步骤如下: 1.  TFLiteConverter保存模型 修改网络模型代码,将模型通过TFLiteConverter转化成为 TensorFlow Lite FlatBuffer即为.tflit ...

  2. tensorflow初次接触记录,我用python写的tensorflow第一个模型

    tensorflow初次接触记录,我用python写的tensorflow第一个模型 刚用python写的tensorflow机器学习代码,训练60000张手写文字图片,多层神经网络学习拟合17000 ...

  3. [翻译] Tensorflow模型的保存与恢复

    翻译自:http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/ ...

  4. Tensorflow模型的格式

    转载:https://cloud.tencent.com/developer/article/1009979 tensorflow模型的格式通常支持多种,主要有CheckPoint(*.ckpt).G ...

  5. 移动端目标识别(2)——使用TENSORFLOW LITE将TENSORFLOW模型部署到移动端(SSD)之TF Lite Developer Guide

    TF Lite开发人员指南 目录: 1 选择一个模型 使用一个预训练模型 使用自己的数据集重新训练inception-V3,MovileNet 训练自己的模型 2 转换模型格式 转换tf.GraphD ...

  6. 搭建 MobileNet-SSD 开发环境并使用 VOC 数据集训练 TensorFlow 模型

    原文地址:搭建 MobileNet-SSD 开发环境并使用 VOC 数据集训练 TensorFlow 模型 0x00 环境 OS: Ubuntu 1810 x64 Anaconda: 4.6.12 P ...

  7. 一份快速完整的Tensorflow模型保存和恢复教程(译)(转载)

    该文章转自https://blog.csdn.net/sinat_34474705/article/details/78995196 我在进行图像识别使用ckpt文件预测的时候,这个文章给我提供了极大 ...

  8. TensorFlow模型保存和加载方法

    TensorFlow模型保存和加载方法 模型保存 import tensorflow as tf w1 = tf.Variable(tf.constant(2.0, shape=[1]), name= ...

  9. 使用tensorflow-serving部署tensorflow模型

    使用docker部署模型的好处在于,避免了与繁琐的环境配置打交道.使用docker,不需要手动安装Python,更不需要安装numpy.tensorflow各种包,直接一个docker就包含了全部.d ...

随机推荐

  1. 2019.06.05 ABAP EXCEL 操作类代码 OLE方式(模板下载,excel上传,内表下载)

    一般使用标准的excel导入方法9999行,修改了标准的excel导入FM 整合出类:excel的 模板下载,excel上传,ALV内表下载功能. 在项目一开始可以SE24创建一个类来供整体开发使用, ...

  2. appium环境搭建步骤

    前提条件是:搭建selenium环境(侵权删) 1.安装jdk,配置环境变量: JAVA_HOME C:\Program Files\Java\jdk1.8.0_60(jdk的存放路径) CLASSP ...

  3. 【机器学习】Jackknife,Bootstraping, bagging, boosting, AdaBoosting, Rand forest 和 gradient boosting

    Jackknife,Bootstraping, bagging, boosting, AdaBoosting, Rand forest 和 gradient boosting 这些术语,我经常搞混淆, ...

  4. PTA(Advanced Level)1031.Hello World for U

    Given any string of N (≥5) characters, you are asked to form the characters into the shape of U. For ...

  5. PTA(Basic Level)1048.数字加密

    本题要求实现一种数字加密方法.首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余--这里用 J 代表 ...

  6. 【转贴】优秀的龙芯Docker虚拟机解决方案

    优秀的龙芯Docker虚拟机解决方案 http://www.loongson.cn/m/view.php?aid=836 时间:2019-05-23 15:03  来源:未知   作者:龙芯中科    ...

  7. SpringBoot 启动失败 Failed to determine a suitable driver class 问题解决方案

    Description: Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no ...

  8. 四则运算计算器的微信小程序_2 运算

    js文件: function isOperator(value) {   var operatorString = '+-*/()×÷';   return operatorString.indexO ...

  9. requests库爬取豆瓣热门国产电视剧数据并保存到本地

    首先要做的就是去豆瓣网找对应的接口,这里就不赘述了,谷歌浏览器抓包即可,然后要做的就是分析返回的json数据的结构: https://movie.douban.com/j/search_subject ...

  10. 关闭mysql查询缓存query cache(用户测试性能)

    先对query cache进行查询 mysql> show global variables like '%cache%'; 查看query_cache_size.query_cache_typ ...