我有一个Python函数,返回一个多维的numpy数组.我想从Lua调用这个Python函数,并尽快将数据导入Lua Torch Tensor.我有一个非常缓慢的解决方案,我正在寻找一种明显更快的方式(10fps或更高的顺序).我不确定这是否可行.
考虑到Facebook支持的Torch日益普及以及Lua缺乏的Python中广泛易用的图像处理工具,我相信这对其他人会有用.
我正在使用lunatic-python的Bastibe fork来从Lua调用Python函数.在上一个问题和本文档的帮助下,我提出了一些有效的代码,但速度太慢了.我正在使用Lua 5.1和Python 2.7.6,如果需要可以更新它们.
Lua代码:"test_lua.lua"
require 'torch'
print(package.loadlib("libpython2.7.so", "*"))
require("lua-python")
getImage = python.import "test_python".getImage
pb = python.builtins()
function getImageTensor(pythonImageHandle,width,height)
imageTensor = torch.Tensor(3,height,width)
image_0 = python.asindx(pythonImageHandle(height,width))
for i=0,height-1 do
image_1 = python.asindx(image_0[i])
for j=0,width-1 do
image_2 = python.asindx(image_1[j])
for k=0,2 do
-- Tensor indices begin at 1
-- User python inbuilt to-int function to return integer
imageTensor[k+1][i+1][j+1] = pb.int(image_2[k])/255
end
end
end
return imageTensor
end
a = getImageTensor(getImage,600,400)
Run Code Online (Sandbox Code Playgroud)
Python代码:"test_python.py"
import numpy
import os,sys …Run Code Online (Sandbox Code Playgroud)