opencv 从BufferedImage获取Uint 8Tensor- java

2w2cym1i  于 2022-12-27  发布在  Java
关注(0)|答案(1)|浏览(142)

我终于设法为一个项目找到了一个可行的解决方案,在这个项目中,我使用重新训练的tf2图像检测模型对从OpenCV视频捕捉对象提取的帧进行推理。我很高兴分享所有没有产生效果的路线,但真正起作用的是在下面的代码中将OpenCV Mat转换为Tensor。
我开始工作的代码是把垫子变成一个BufferedImage,然后把它保存成JPEG格式到磁盘上,通过DecodeJpeg函数把JPEG从磁盘上读回Tensor,非常难看。
如何直接将BufferedImage转换为Tensor(Uint8),而不必为此问题保存和读取JPEG文件?

SavedModelBundle model = SavedModelBundle.load(modelPath,"serve");
BufferedImage drawImg = BIfromMat(Mat aMat);
ImageIO.write(drawImg,"png",new File(svImgPath1));

try (Graph g = new Graph(); Session s = new Session(g)) {
    Ops tf = Ops.create(g);
    Constant<TString> fileName = tf.constant(svImgPath1);
    ReadFile readFile = tf.io.readFile(fileName);
    Session.Runner runner = s.runner();
    DecodeJpeg.Options options = DecodeJpeg.channels(3L);
    DecodeJpeg decodeImage = tf.image.decodeJpeg(readFile.contents(),options);
        //fetch image from file
        Shape imageShape = runner.fetch(decodeImage).run().get(0).shape();
        //reshape the tensor to 4D for input to model
        Reshape<TUint8> reshape = tf.reshape(decodeImage,
            tf.array(1,
                imageShape.asArray()[0],
                imageShape.asArray()[1],
                imageShape.asArray()[2]
            )
        );
                    
        try (TUint8 reshapeTensor = (TUint8)s.runner().fetch(reshape).run().get(0)) {
            Map<String,Tensor> feedDict = new HashMap<>();
            //The given SavedModel SignatureDef input
            feedDict.put("input_tensor", reshapeTensor);
            //gets the detected objects
            Map<String, Tensor> outputTensorMap = model.function("serving_default").call(feedDict);
xzv2uavs

xzv2uavs1#

在这个问题上没有太多的活动,所以让我发布我自己设法弄清楚的东西。这个版本做灰度。

BufferedImage buffImg = ... //provide a grayscale BufferedImage
int width = buffImg.getWidth();
int height = buffImg.getHeight();
Shape theShape = Shape.of(1,height,width,1);
ByteNdArray pixMatrix = NdArrays.ofBytes(theShape);
//set the content of NdArray with the byte[] of the BufferedImage
//could do a byte at a time with pixMatrix.setByte(bA[j*width+i],0,j,i,0);
TUint8 theTens = TUint8.tensorOf(pixMatrix);

相关问题