我正在尝试在我的项目中使用Apple 网站 上提供的inceptionV3模型,将CoreML和ARKit相结合。
我从ARKit的标准模板(Xcode 9 beta 3)开始。
我不是实例化一个新的相机会话,而是重用了由ARSCNView启动的会话。
在我的viewDelegate的末尾,我编写了以下内容:
我会把我的视图控制器扩展,以符合ARSessionDelegate协议(可选协议)。
起初我尝试了那段代码,但随后注意到Inception需要一种类型为Image、像素缓冲区为<RGB,<299,299>的像素缓冲区。
虽然不推荐这样做,但我想我只需调整我的帧大小,然后尝试从中获取预测。我正在使用此函数进行调整大小(从https://github.com/yulingtianxia/Core-ML-Sample中获取)。
很遗憾,这还不足以使其正常工作。这是捕获的错误:
我不再有错误了。但是一旦我尝试进行预测,AVCaptureSession就会停止。看起来我遇到了Enric_SA在苹果开发者论坛上遇到的相同问题。 编辑3:所以我尝试实现rickster的解决方案。对于inceptionV3效果很好。我想尝试一个特征观察(VNClassificationObservation)。此时,使用TinyYolo无法正常工作。绑定不正确。正在努力解决它。
我从ARKit的标准模板(Xcode 9 beta 3)开始。
我不是实例化一个新的相机会话,而是重用了由ARSCNView启动的会话。
在我的viewDelegate的末尾,我编写了以下内容:
sceneView.session.delegate = self
我会把我的视图控制器扩展,以符合ARSessionDelegate协议(可选协议)。
// MARK: ARSessionDelegate
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate frame: ARFrame) {
do {
let prediction = try self.model.prediction(image: frame.capturedImage)
DispatchQueue.main.async {
if let prob = prediction.classLabelProbs[prediction.classLabel] {
self.textLabel.text = "\(prediction.classLabel) \(String(describing: prob))"
}
}
}
catch let error as NSError {
print("Unexpected error ocurred: \(error.localizedDescription).")
}
}
}
起初我尝试了那段代码,但随后注意到Inception需要一种类型为Image、像素缓冲区为<RGB,<299,299>的像素缓冲区。
虽然不推荐这样做,但我想我只需调整我的帧大小,然后尝试从中获取预测。我正在使用此函数进行调整大小(从https://github.com/yulingtianxia/Core-ML-Sample中获取)。
func resize(pixelBuffer: CVPixelBuffer) -> CVPixelBuffer? {
let imageSide = 299
var ciImage = CIImage(cvPixelBuffer: pixelBuffer, options: nil)
let transform = CGAffineTransform(scaleX: CGFloat(imageSide) / CGFloat(CVPixelBufferGetWidth(pixelBuffer)), y: CGFloat(imageSide) / CGFloat(CVPixelBufferGetHeight(pixelBuffer)))
ciImage = ciImage.transformed(by: transform).cropped(to: CGRect(x: 0, y: 0, width: imageSide, height: imageSide))
let ciContext = CIContext()
var resizeBuffer: CVPixelBuffer?
CVPixelBufferCreate(kCFAllocatorDefault, imageSide, imageSide, CVPixelBufferGetPixelFormatType(pixelBuffer), nil, &resizeBuffer)
ciContext.render(ciImage, to: resizeBuffer!)
return resizeBuffer
}
很遗憾,这还不足以使其正常工作。这是捕获的错误:
Unexpected error ocurred: Input image feature image does not match model description.
2017-07-20 AR+MLPhotoDuplicatePrediction[928:298214] [core]
Error Domain=com.apple.CoreML Code=1
"Input image feature image does not match model description"
UserInfo={NSLocalizedDescription=Input image feature image does not match model description,
NSUnderlyingError=0x1c4a49fc0 {Error Domain=com.apple.CoreML Code=1
"Image is not expected type 32-BGRA or 32-ARGB, instead is Unsupported (875704422)"
UserInfo={NSLocalizedDescription=Image is not expected type 32-BGRA or 32-ARGB, instead is Unsupported (875704422)}}}
我不确定从这里开始该做什么。
如果有更好的建议将两者结合起来,我愿意听取。
编辑: 我还尝试了@dfd建议的YOLO-CoreML-MPSNNGraph中的resizePixelBuffer方法,错误完全相同。
编辑2: 因此,我将像素格式更改为kCVPixelFormatType_32BGRA(而不是传递给resizePixelBuffer的pixelBuffer的相同格式)。
let pixelFormat = kCVPixelFormatType_32BGRA // line 48
我不再有错误了。但是一旦我尝试进行预测,AVCaptureSession就会停止。看起来我遇到了Enric_SA在苹果开发者论坛上遇到的相同问题。 编辑3:所以我尝试实现rickster的解决方案。对于inceptionV3效果很好。我想尝试一个特征观察(VNClassificationObservation)。此时,使用TinyYolo无法正常工作。绑定不正确。正在努力解决它。