swift 4 : Vision Camera Preview Issues

标签 swift coreml apple-vision

对于使用 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/

相关文章:

swift - Swift 中的嵌套类型——好的做法是什么?

machine-learning - 从 3D 模型制作经过训练的模型(机器学习)

swift - OpenFace CoreML 模型。没有人脸检测

swift - 用于对象识别的 ARKit 和 Vision 框架

ios - 如何使用 ARKit 或 Apple Vision 测量 3d 对象的尺寸?

ios - 使用 UILabel 作为函数参数时出现“NSUnknownKeyException”

ios - SwiftUI List ForEach View 中的按钮即使不是 "tapped"也会触发?

swift - UITableViewController : Scrolling to bottom with dynamic row height starts animation at wrong position

swift - 苹果视觉框架: LCD/LED digit recognition

ios - 演示过程中出现错误后问题