使用CoreML与React Native

3

enter image description here

我遇到了错误

参数标签'(image:)'与任何可用的重载不匹配

我按照这里的教程和苹果的文档操作,但是当我尝试从React Native接受图像时,我开始收到这个错误。Swift和React Native之间的桥梁正在工作,只有在我开始尝试使用CoreML时才出现错误。

我认为这与新的Swift语法有关,但我不确定如何修复它,我也没有看到任何人在React Native中使用CoreML。

这是我的完整函数:

import Foundation
import CoreML

@objc(Printer)
class Printer: NSObject {

  @objc func imageRec(_ image:CGImage) -> CVPixelBuffer? {
let model = Inceptionv3();

  UIGraphicsBeginImageContextWithOptions(CGSize(width: 299, height: 299), true, 1.0)
//image.draw(in: CGRect(x: 0, y: 0, width: 299, height: 299))
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
var pixelBuffer : CVPixelBuffer?
let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(newImage.size.width), Int(newImage.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(newImage.size.width), height: Int(newImage.size.height), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: rgbColorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)

context?.translateBy(x: 0, y: newImage.size.height)
context?.scaleBy(x: 1.0, y: -1.0)

UIGraphicsPushContext(context!)
newImage.draw(in: CGRect(x: 0, y: 0, width: newImage.size.width, height: newImage.size.height))
UIGraphicsPopContext()
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))

guard let prediction = try? model.prediction(image: newImage) else {
}

}
}
1个回答

1
你需要初始化 modelInput 类(当你添加数据模型时,这是一个自动生成的类),然后将 modelInput 发送到预测中。
以下是可能有帮助的代码片段:
        // create indupt data model
        // in your case this must be taking image as argument
        let input = ProfitPredictorInput.init(R_D_Spend: 
          Double(rndInv)!, Administration: Double(adminInv)!, 
           Marketing_Spend: Double(marketingInv)!)

        // give input data model to your mlmodel
        let outData = try mlModel?.prediction(input: input)

你可以使用 this 进行参考。

我对R_D_Spend、Administration和Marketing_Spend是什么感到困惑?我尝试过这样做,但我在init行上得到了相同的错误。 - codegeek511
R_D_Spend、Administration、Marketing_Spend 是我机器学习模型的参数。在 Xcode 中打开您的 MLModel,它将显示您的 ml-model 的输入输出参数,您必须使用它来代替原有的参数。 - user3378829
嗯,参数是'image'。 - codegeek511

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接