I have a Python function that returns a multi-dimensional numpy array. I want to call this Python function from Lua and get the data into a Lua Torch Tensor as quickly as possible. I have a solution that works quite slowly and am looking for a way that is significantly faster (order of 10fps or more). I'm not sure if this is possible.
I believe this will be of use to others considering the growing popularity of Facebook backed Torch and the extensive easy-to-use image-processing tools available in Python of which Lua lacks.
I am using the Bastibe fork of lunatic-python in order to call a Python function from Lua. With aid from this previous question and this documentation, I have come up with some code that works, but is far too slow. I am using Lua 5.1 and Python 2.7.6 and can update these if necessary.
Lua Code: "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)
Python Code: "test_python.py"
import numpy
import os,sys
import Image
def getImage(width, height):
return numpy.asarray(Image.open("image.jpg"))