【问题标题】:Tracking multiple objects in iOS 13在 iOS 13 中跟踪多个对象
【发布时间】:2020-02-02 20:07:42
【问题描述】:

我的应用程序使用 Apple 的示例代码使用 Vision 跟踪视频的多个对象(在我的例子中,它在举重练习期间跟踪杠铃的路径),但在更新到 iOS 13 后,视频无法正确显示。不像以前那样填满屏幕,现在视频被裁剪了,你只能看到它的一小部分。我已经与 Apple 技术支持人员进行了交谈,他们承认了这个错误,但他们的计划中没有修复。

最让我烦恼的是 a) 横向视频可以正常工作,但纵向视频不行 b) 该错误仅发生在真实设备中,而不发生在模拟器中。请参阅附件中用于根据视频比例(横向或纵向)显示视频的代码部分。

private func scaleImage(to viewSize: CGSize) -> UIImage? {
    guard self.image != nil && self.image.size != CGSize.zero else {
        return nil
    }

    self.imageAreaRect = CGRect.zero

    // There are two possible cases to fully fit self.image into the the ImageTrackingView area:
    // Option 1) image.width = view.width ==> image.height <= view.height
    // Option 2) image.height = view.height ==> image.width <= view.width
    let imageAspectRatio = self.image.size.width / self.image.size.height

    // Check if we're in Option 1) case and initialize self.imageAreaRect accordingly
    let imageSizeOption1 = CGSize(width: viewSize.width, height: floor(viewSize.width / imageAspectRatio))
    if imageSizeOption1.height <= viewSize.height {

        print("Landscape. View size: \(viewSize)")
        let imageX: CGFloat = 0
        let imageY = floor((viewSize.height - imageSizeOption1.height) / 2.0)
        self.imageAreaRect = CGRect(x: imageX,
                                    y: imageY,
                                    width: imageSizeOption1.width,
                                    height: imageSizeOption1.height)

    }

    if self.imageAreaRect == CGRect.zero {
        // Check if we're in Option 2) case if Option 1) didn't work out and initialize imageAreaRect accordingly

        print("portrait. View size: \(viewSize)")
        let imageSizeOption2 = CGSize(width: floor(viewSize.height * imageAspectRatio), height: viewSize.height)
        if imageSizeOption2.width <= viewSize.width {

            let imageX = floor((viewSize.width - imageSizeOption2.width) / 2.0)
            let imageY: CGFloat = 0
            self.imageAreaRect = CGRect(x: imageX,
                                        y: imageY,
                                        width: imageSizeOption2.width,
                                        height: imageSizeOption2.height)

        }
    }

    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(self.imageAreaRect.size, false, 0.0)
    self.image.draw(in: CGRect(x: 0.0, y: 0.0, width: self.imageAreaRect.size.width, height: self.imageAreaRect.size.height))

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 附带说明一下,我发现 iOS 13 中的对象跟踪功能几乎无法使用......它几乎无法正常工作。对于我在 iOS 12 中实现的一项功能,我不断获得 0.8-0.99 的跟踪置信度,而在 iOS 13 上,置信度从未超过 0.15。
  • 感谢您的反馈安迪。我没有遇到这个问题:我的应用程序基本上像以前一样跟踪所有内容。如果您像我一样使用 Apple 的示例代码,您是否尝试过我在自我回答中提供的解决方法?

标签: ios13 vision video-tracking


【解决方案1】:

在跟踪 Apple 技术支持数周后,他们提出了一个非常简单的解决方法。基本上,您只需要通过 CGImage 进行转换,而不是直接从 CIImage 转换为 UIImage。

这是旧代码(您可以在 Apple 文档的当前示例中看到)

 if let frame = frame {
            let ciImage = CIImage(cvPixelBuffer: frame).transformed(by: transform)
            let uiImage = UIImage(ciImage: ciImage)
            self.trackingView.image = uiImage
        }

这是更正

if let frame = frame {
            let ciImage = CIImage(cvPixelBuffer: frame).transformed(by: transform)
            guard let cgImage = CIContext.init().createCGImage(ciImage, from: ciImage.extent) else {
                return
            }
            let uiImage = UIImage(cgImage: cgImage)
            self.trackingView.image = uiImage
        }

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多