对于使用 CoreML 和 Vision 的对象识别应用程序,我从 Apple 下载了一个示例应用程序 ( https://developer.apple.com/documentation/vision/recognizing_objects_in_live_capture )。不幸的是,这个应用程序似乎只能在纵向模式下工作,但我需要横向使用它。
在标准配置中,我的对象将被检测到,但相机预览层旋转 90°。旋转预览图层后,我的对象仍然可以被识别,但不如以前那么准确。这让我很烦恼,对我和我的客户来说都是 Not Acceptable 。
我尝试像这样旋转预览图层:
previewLayer.connection?.videoOrientation = .landscapeRight
并尝试像这样旋转视频输出:
videoDataOutput.connection(with: .video)?.videoOrientation = .landscapeLeft
旋转后,边界框的位置不正确。移动相机会导致盒子发生更奇怪的移动。它似乎与以下功能有关:
public func exifOrientationFromDeviceOrientation() -> CGImagePropertyOrientation {
let curDeviceOrientation = UIDevice.current.orientation
let exifOrientation: CGImagePropertyOrientation
switch curDeviceOrientation {
case UIDeviceOrientation.portraitUpsideDown: // Device oriented vertically, home button on the top
exifOrientation = .left
case UIDeviceOrientation.landscapeLeft: // Device oriented horizontally, home button on the right
exifOrientation = .upMirrored
case UIDeviceOrientation.landscapeRight: // Device oriented horizontally, home button on the left
exifOrientation = .down
case UIDeviceOrientation.portrait: // Device oriented vertically, home button on the bottom
exifOrientation = .up
default:
exifOrientation = .up
}
return exifOrientation
}
如果我改变
case UIDeviceOrientation.landscapeLeft:
exifOrientation = .upMirrored
至
case UIDeviceOrientation.landscapeLeft:
exifOrientation = .left
它将正确定位边界框,并且相机移动似乎也工作正常。但物体的识别却遇到了很大的困难。
最佳答案
我改变了方向
detectionOverlay.setAffineTransform(CGAffineTransform(rotationAngle: CGFloat(.pi / 0.5)).scaledBy(x: scale, y: -scale))
和
captureConnection?.videoOrientation = .landscapeRight
这是我使用landscapeRight 来检测对象的演示。 https://github.com/gwh111/CoreMLRecord
关于 swift 4 : Vision Camera Preview Issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198722/