【问题标题】:Use Create ML object detection model in swift在 swift 中使用 Create ML object detection model
【发布时间】:2021-10-09 15:55:12
【问题描述】:

您好,我在 create ML 中创建了一个对象检测模型并将其导入我的 swift 项目,但我不知道如何使用它。基本上我只是想给模型一个输入,然后接收一个输出。我打开了 Ml 模型预测选项卡并找到了输入和输出变量,但我不知道如何在代码方面实现它。我在互联网上搜索了答案,发现了多个代码 sn-ps 用于运行 ml 模型,但我无法让它们工作。

这是机器学习模型: ML Model predictions

这是我尝试过的代码:

let model = TestObjectModel()

guard let modelOutput = try? model.prediction(imagePath: "images_(2)" as! CVPixelBuffer, iouThreshold: 0.5, confidenceThreshold: 0.5) else {
    fatalError("Unexpected runtime error.")
}

print(modelOutput)

运行代码时出现此错误:

error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x106c345c0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

【问题讨论】:

    标签: swift object-detection createml


    【解决方案1】:

    好吧,首先你必须决定你声明了哪种类型的输入。当你在项目导航器中单击你的模型时,你可以看到它。

    例如:

    let mlArray = try? MLMultiArray(shape: [1024],dataType: MLMultiArrayDataType.float32)
    

    mlArray![index] = x --> 给你的数组一些数据

    let input = TestObjectModel(input: mlArray!)
           do {
    
                      let options = MLPredictionOptions.init()
                      options.usesCPUOnly = true
                      let prediction = try? self. TestObjectModel.prediction(input: input, options: options)
    

    --> 现在你可以使用预测,这是你的输出

                       } catch let err {
                           fatalError(err.localizedDescription) // Error computing NN outputs error
                       }
    

    图像作为模型输入的另一个示例:

    do {
        if let resizedImage = resize(image: image, newSize: CGSize(width: 416, height: 416)), let pixelBuffer = resizedImage.toCVPixelBuffer() {
            let prediction = try model.prediction(image: pixelBuffer)
            let value = prediction.output[0].intValue
            print(value)
        }
    } catch {
        print("Error while doing predictions: \(error)")
    }
    
    
    func resize(image: UIImage, newSize: CGSize) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
    extension UIImage {
        func toCVPixelBuffer() -> CVPixelBuffer? {
            let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
            var pixelBuffer : CVPixelBuffer?
            let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(self.size.width), Int(self.size.height), kCVPixelFormatType_32ARGB, attrs, &pixelBuffer)
            guard (status == kCVReturnSuccess) else {
                return nil
            }
    
            CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
            let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)
    
            let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
            let context = CGContext(data: pixelData, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)
    
            context?.translateBy(x: 0, y: self.size.height)
            context?.scaleBy(x: 1.0, y: -1.0)
    
            UIGraphicsPushContext(context!)
            self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
            UIGraphicsPopContext()
            CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
    
            return pixelBuffer
        }
    }
    

    【讨论】:

    • 运行这行代码时:“let input = TestObjectModel(input: mlArray!)”我收到错误“参数传递给不带参数的调用”。你知道为什么会这样吗?
    • 是的,这就是为什么我说您必须检查您的 ML 对象并检查您设置的输入类型。我只是给你一个例子。您可以编辑您的问题并向我们展示模型吗?
    • 我现在添加了一张ml模型输入输出的图片
    猜你喜欢
    • 2019-11-12
    • 2019-09-06
    • 2019-07-18
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    相关资源
    最近更新 更多