结合 CoreML 和 ARKit
Combining CoreML and ARKit
我正在尝试使用 Apple website.
上给定的 inceptionV3 模型在我的项目中结合 CoreML 和 ARKit
我从 ARKit (Xcode 9 beta 3) 的标准模板开始
我没有实例化新的相机会话,而是重复使用了 ARSCNView 启动的会话。
在 viewDelegate 的末尾,我写了:
sceneView.session.delegate = self
然后我扩展我的 viewController 以符合 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)}}}
不确定我能从这里做什么。
如果有更好的建议将两者结合起来,我会洗耳恭听。
Edit: 我也尝试了@dfd推荐的YOLO-CoreML-MPSNNGraph中的resizePixelBuffer方法,错误一模一样。
Edit2: 所以我把像素格式改成kCVPixelFormatType_32BGRA(和resizePixelBuffer中传入的pixelBuffer格式不一样)
let pixelFormat = kCVPixelFormatType_32BGRA // line 48
我没有错误了。但是,一旦我尝试做出预测,AVCaptureSession 就会停止。似乎我 运行 遇到了同样的问题 Enric_SA 是 apple developers forum 上的 运行。
Edit3:所以我尝试实施 rickster 解决方案。与 inceptionV3 配合良好。我想尝试一个特征观察(VNClassificationObservation)。目前,它无法使用 TinyYolo。边界是错误的。想弄明白。
不要自己处理图像以将它们提供给 Core ML。使用 Vision. (No, not that one. This one.) Vision takes an ML model and any of several image types (including CVPixelBuffer
) 并自动获取适合模型评估的尺寸、宽高比和像素格式的图像,然后为您提供模型的结果。
这是您需要的代码的粗略框架:
var request: VNRequest
func setup() {
let model = try VNCoreMLModel(for: MyCoreMLGeneratedModelClass().model)
request = VNCoreMLRequest(model: model, completionHandler: myResultsMethod)
}
func classifyARFrame() {
let handler = VNImageRequestHandler(cvPixelBuffer: session.currentFrame.capturedImage,
orientation: .up) // fix based on your UI orientation
handler.perform([request])
}
func myResultsMethod(request: VNRequest, error: Error?) {
guard let results = request.results as? [VNClassificationObservation]
else { fatalError("huh") }
for classification in results {
print(classification.identifier, // the scene label
classification.confidence)
}
}
请参阅 另一个问题以获得更多指示。
我正在尝试使用 Apple website.
上给定的 inceptionV3 模型在我的项目中结合 CoreML 和 ARKit我从 ARKit (Xcode 9 beta 3) 的标准模板开始
我没有实例化新的相机会话,而是重复使用了 ARSCNView 启动的会话。
在 viewDelegate 的末尾,我写了:
sceneView.session.delegate = self
然后我扩展我的 viewController 以符合 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)}}}
不确定我能从这里做什么。
如果有更好的建议将两者结合起来,我会洗耳恭听。
Edit: 我也尝试了@dfd推荐的YOLO-CoreML-MPSNNGraph中的resizePixelBuffer方法,错误一模一样。
Edit2: 所以我把像素格式改成kCVPixelFormatType_32BGRA(和resizePixelBuffer中传入的pixelBuffer格式不一样)
let pixelFormat = kCVPixelFormatType_32BGRA // line 48
我没有错误了。但是,一旦我尝试做出预测,AVCaptureSession 就会停止。似乎我 运行 遇到了同样的问题 Enric_SA 是 apple developers forum 上的 运行。
Edit3:所以我尝试实施 rickster 解决方案。与 inceptionV3 配合良好。我想尝试一个特征观察(VNClassificationObservation)。目前,它无法使用 TinyYolo。边界是错误的。想弄明白。
不要自己处理图像以将它们提供给 Core ML。使用 Vision. (No, not that one. This one.) Vision takes an ML model and any of several image types (including CVPixelBuffer
) 并自动获取适合模型评估的尺寸、宽高比和像素格式的图像,然后为您提供模型的结果。
这是您需要的代码的粗略框架:
var request: VNRequest
func setup() {
let model = try VNCoreMLModel(for: MyCoreMLGeneratedModelClass().model)
request = VNCoreMLRequest(model: model, completionHandler: myResultsMethod)
}
func classifyARFrame() {
let handler = VNImageRequestHandler(cvPixelBuffer: session.currentFrame.capturedImage,
orientation: .up) // fix based on your UI orientation
handler.perform([request])
}
func myResultsMethod(request: VNRequest, error: Error?) {
guard let results = request.results as? [VNClassificationObservation]
else { fatalError("huh") }
for classification in results {
print(classification.identifier, // the scene label
classification.confidence)
}
}
请参阅