tensorflow js RGB to Lab from input img

时间:2018-04-20 10:01:24

标签: javascript tensorflow tensorflow.js

我一直在搜索和调试,但我发现任何对我有用的东西。我正在做一个Web应用程序,我尝试从黑白图像到颜色,为此我输入了一个输入,我加载图像并进行推理(目前使用图像到图像模型)。

事实是,我想在进入网络之前将rgb的图像转换为实验室作为预处理,因为这是我打算训练它的方式。我的代码如下:

var myInput = document.getElementById('myFileInput');

function processPic() {
    if (myInput.files && myInput.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#prev_img_id').attr('src', e.target.result);
            //Initiate the JavaScript Image object.
            var image = new Image();
           //Set the Base64 string return from FileReader as source.
           image.src = e.target.result;
           image.onload = function () {
               //alert(this.height)
               const webcamImage = tf.fromPixels(this);
               const batchedImage = webcamImage.expandDims(0);
               predict(batchedImage.toFloat().div(tf.scalar(127)).sub(tf.scalar(1)))
            }
        }

        reader.readAsDataURL(myInput.files[0]);
    }
}

myInput.addEventListener('change', processPic, false);


function predict(the_img) {
    //get predictions 
    let pred = mobilenet.predict(the_img);
    //retreive the highest probability class label 
    let cls = pred.argMax().buffer().values[0];

    alert(IMAGENET_CLASSES[cls]);
}

2 个答案:

答案 0 :(得分:0)

关于将RGB转换为LAB,存在一些资源,例如http://www.easyrgb.com/en/math.php

您也可以试试这个JS实现(https://github.com/antimatter15/rgb-lab,实际上是使用 easyrgb 网站中的方程式),在{{{{}}内调用rgb2lab()函数1}}。

要访问``所需的图像数据,您可以查看此SO线程(How do I access/change pixels in a javascript image object?),即使用中间画布。

答案 1 :(得分:0)

我编写了一些代码来使用tensorflow.js操作来执行此操作,该代码可以通过矩阵乘法进一步优化,但是它可以正常工作,如果仍然有用的话,应该把您带到正确的道路上。

RGB2LAB TFJS