红联Linux门户
Linux帮助

linux下C/C++调用luajit

发布时间:2016-10-14 09:54:25来源:linux网站作者:shakevincent
参考了很多网上的资料,但大部分都是直接调用lua的,因为lua和luajit 的API相互兼容,所以很多程序都可以直接调用。
 
先上代码:
 
这是test.c 的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "luajit.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/*
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luajit.h"
};*/
//如果使用C++编译的。不屏蔽这几条语句,但是要把上面几个相同的头文件屏蔽
//并把.C修改为.cpp
const char str1[]= "/home/lyz/opencv测试/firstopencv/1.jpg";
const char str2[]= "/home/lyz/opencv测试/firstopencv/2.jpg";
static lua_State *globalL = NULL;
lua_State* L;
int luaadd(const char str1[],const char str2[])
{
luaL_openlibs(L);
float sum;                   
lua_getglobal(L,"test");         //the function name           
lua_pushstring(L,str1);    
lua_pushstring(L,str2);          
lua_call(L, 2, 1);          //call 2 arguments, return 1 result.         
sum = (float)lua_tonumber(L, -1);//get the result.        
lua_pop(L,-1);                   //cleanup the return
return sum;
}
int main(int argc, char *argv[])
{
float table[129];                           //initialize Lua
float sum;
int i;        
L = lua_open();                    //load Lua base libraries
luaL_openlibs(L);                  //load the script
luaL_dofile(L, "test.lua");         //call the add function       
sum = luaadd(str1,str2);                    
lua_close(L);
//对数据TXT计算距离
FILE *fin;
float a[128];
//int i;
fin = fopen("image2.txt","r"); 
for (i=0;i<128;i++)
fscanf(fin,"%f", &a[i]);        
fclose(fin);                    
//for (i=0;i<128;i++) 
//printf("%f\n",a[i]);  
//printf("***********************\n");    
//printf("***********************\n"); 
FILE *fbn;
float b[128];
fbn = fopen("image1.txt","r"); 
for (i=0;i<128;i++)
fscanf(fbn,"%f", &b[i]);        
fclose(fbn);                    
//for (i=0;i<128;i++) 
//printf("%f\n",b[i]);  
double dis;
double D=0;
for (i=0;i<128;i++)
{
D=D+((a[i]-b[i])*(a[i]-b[i]));      
}
//dis = sqrt(D);
printf("********两张图片的欧式距离为:**********\n");
printf ("%f\n",D);
return 0;
}
 
这是test.lua的代码
function test(str1,str2)
require 'torch'
require 'nn'
require 'dpnn'
require 'image'
--print (str1)
--print (str2)
io.stdout:setvbuf 'no'
torch.setdefaulttensortype('torch.FloatTensor')
net = torch.load('/home/lyz/openface-master/models/openface/nn4.v1.t7')
net:evaluate()
-----------------------------------------------------------
local path = "/home/lyz/opencv测试/firstopencv/1.jpg"
local path1 = "/home/lyz/opencv测试/firstopencv/2.jpg"
local path2 = "/home/lyz/opencv测试/firstopencv/3.jpg"
local PATH = {path,path1,path2}
--获取路径
function stripfilename(filename)
return string.match(filename, "(.+)/[^/]*%.%w+$") 
end
--获取文件名
function strippath(filename)
return string.match(filename, ".+/([^/]*%.%w+)$") 
end
--  print (stripfilename(str1))
--  print (strippath(str1))
--  print (stripfilename(str2))
--  print (strippath(str2))
img = torch.Tensor(1, 3, 96, 96)      --定义一个空文件读取第一张图片
img[1] = image.load(str1, 3, byte)
img[1] = image.scale(img[1], 96, 96)
rep = net:forward(img)
data = {rep}
print('-------------THE FIRST IMAGE-----------------')
print('---------从以下路径中读取第一张图片-----------')
print (stripfilename(str1))
print('---------------图片的名称为:-----------------')
print (strippath(str1))
local distance1={}                     --转化成table型数据格式
distance1 = data[1] 
print('----------特征保存为image1.txt:--------------')                          
myFile = io.open( "image1.txt" , "w")  --保存数据
--  myFile:write("-- 数据保存文件--") 
--myFile:write(string.char (10))  
-- myFile:write(string.format("%s%s", "-- 文件创建于: ", os.date()))  
for i=1,128 do        
myFile:write(distance1[i])
myFile:write(string.char (10))      
end
io.close(myFile)
-----------------------------------------------------------
Img = torch.Tensor(1, 3, 96, 96)       --定义一个空文件读取第二张图片
Img[1] = image.load(str2, 3, byte)
Img[1] = image.scale(Img[1], 96, 96)
rep1 = net:forward(Img)
data1 = {rep1}
print('**************THE SECOND IMAGE*****************')
print('**********从以下路径中读取第一张图片************')
print (stripfilename(str2))
print('---------------图片的名称为:-----------------')
print (strippath(str2))
distance={}                         
distance = data1[1] 
print('----------特征保存为image2.txt:--------------')                             
myFile = io.open("image2.txt", "w")  --保存数据
for i=1,128 do        
myFile:write(distance[i])
myFile:write(string.char (10))  
end
io.close(myFile) 
-----------------------------------------------------------
print ('//////////////////////////////////////////////')
print ('//////////////////////////////////////////////')
return distance[1]
end 
 
简要介绍一下这个程序:
本程序是基于torch7 的luajit程序调用的。实现在C语言中输入了一个图片路径,在lua中调用图片路径,然后输入到网络中,计算得到128的数据,保存数据格式为TXT然后在C语言中调用,通过C语言计算来计算两个向量的欧式距离。
在linux下最重要的是程序的编译
//gcc -I /home/lyz/torch/install/include -L /home/lyz/torch/install/lib -lm return.c -o return /home/lyz/torch/install/lib/libluajit.so -ldl
这是一个完整的GCC编译,大家可以参考这来。主要是因为luajit是直接在torch7中依赖安装的,所以路径应该设置为luajit的安装路径。
另外我在程序中用到了math.h的sqrt()函数。所以GCC编译还需要加上
//gcc -I /home/lyz/torch/install/include -L /home/lyz/torch/install/lib -lm return.c -o return /home/lyz/torch/install/lib/libluajit.so -ldl -lm
这样才能编译通过。
 
贴上结果图
linux下C/C++调用luajit
 
本文永久更新地址:http://www.linuxdiyf.com/linux/25019.html