gpt4 book ai didi

ios - 将一个 VNCoreMLFeatureValueObservation 结果(3D Double Array)转换为多个 UIImage

转载 作者:行者123 更新时间:2023-11-28 14:27:11 25 4
gpt4 key购买 nike

我有一个 coreml 模型,它在运行后返回一个 VNCoreMLFeatureValueObservation 对象,其中包含 1 个“MultiArray:Double 10 x IMG_SIZE x IMG_SIZE 数组”

我如何将其转换为 10 个 UIImages,每个具有 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 Double Array)转换为多个 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com