0

I was working on a project, where I was trying to use the DeepLabV3 coreML model to remove the background of the image. I found text documentation online on how to remove the background using this model. So when I am using their method or function it said that - Value of type 'MLMultiArray' has no member 'image'

SO now I don't understand the issue.

The Func -

func visionRequestDidComplete(request: VNRequest, error: Error?) {
            DispatchQueue.main.async {
                if let observations = request.results as? [VNCoreMLFeatureValueObservation],
                    let segmentationmap = observations.first?.featureValue.multiArrayValue {

                    let segmentationMask = segmentationmap.image(min: 0, max: 1)

                    self.outputImage = segmentationMask!.resizedImage(for: self.selectedImage.size)!

                    maskInputImage()
                }
            }
    }
jnpdx
  • 45,847
  • 6
  • 64
  • 94

1 Answers1

0

I just encountered and tracked the same issue and I believe what you are looking for is this.

The below came from "CoreMLHelpers" Swift library but specifically "MLMultiArray+Image.swift"

#if canImport(UIKit)

import UIKit

extension MLMultiArray {
  public func image(min: Double = 0,
                    max: Double = 255,
                    channel: Int? = nil,
                    axes: (Int, Int, Int)? = nil) -> UIImage? {
    let cgImg = cgImage(min: min, max: max, channel: channel, axes: axes)
    return cgImg.map { UIImage(cgImage: $0) }
  }
}

public func createUIImage(fromFloatArray features: MLMultiArray,
                          min: Float = 0,
                          max: Float = 255) -> UIImage? {
  let cgImg = createCGImage(fromFloatArray: features, min: min, max: max)
  return cgImg.map { UIImage(cgImage: $0) }
}

#endif

Unfortunately, I am attempting to utilize the same method on macOS but I'm not utilizing Catalina (UIKit is not an option for me), so I am trying to come up with an alternative.

Good luck and I hope it all turns out well for you.

MisterE
  • 95
  • 10