我有一个coreml模型,该模型运行后返回带有1个“MultiArray:Double 10 x IMG_SIZE x IMG_SIZE数组”的VNCoreMLFeatureValueObservation对象。

如何将其转换为10个UIImage,每个UIImage具有IMG_SIZE x IMG_SIZE尺寸,并且它们的值是灰度的?

最佳答案

窥探了一下之后,我发现我必须添加以下助手功能:

https://github.com/hollance/CoreMLHelpers到我的Xcode项目。
从MultiArray初始化问题开始:https://stackoverflow.com/a/44462908/403403

然后,我整理了以下解决方案:

 let request = VNCoreMLRequest(model: model) { (request, error) in
            guard let results = request.results as? [VNCoreMLFeatureValueObservation] else {
                fatalError("Model failed to process image")
            }

            let obs : VNCoreMLFeatureValueObservation = (results.first)!
            let m: MLMultiArray = obs.featureValue.multiArrayValue!
            var mArrays = [MLMultiArray]()


            for i in 0..<10 {
                let start = i*(IMG_SIZE*IMG_SIZE)
                guard let tmp : MLMultiArray = try? MLMultiArray(shape:[768,768], dataType:MLMultiArrayDataType.double) else {
                    fatalError("Unexpected runtime error. MLMultiArray")
                }
                for n in 0..<(IMG_SIZE*IMG_SIZE) {
                    tmp[n] = m[start+n]
                }
                mArrays.append(tmp)
            }



            self.imagePred0.image = mArrays[0].image(offset: 0, scale: 255)!
            self.imagePred1.image = mArrays[1].image(offset: 0, scale: 255)!
            self.imagePred2.image = mArrays[2].image(offset: 0, scale: 255)!
            self.imagePred3.image = mArrays[3].image(offset: 0, scale: 255)!
            self.imagePred4.image = mArrays[4].image(offset: 0, scale: 255)!
            self.imagePred5.image = mArrays[5].image(offset: 0, scale: 255)!
            self.imagePred6.image = mArrays[6].image(offset: 0, scale: 255)!
            self.imagePred7.image = mArrays[7].image(offset: 0, scale: 255)!
            self.imagePred8.image = mArrays[8].image(offset: 0, scale: 255)!
            self.imagePred9.image = mArrays[9].image(offset: 0, scale: 255)!



        }

希望有一种更清洁的方法,但目前可以使用

关于ios - 将VNCoreMLFeatureValueObservation结果(3D双数组)转换为多个UIImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51470008/

10-10 05:03