您是否尝试使用VNImageRequestHandler
的orientation
属性进行操作?
let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .right, options: [:])
在使用后置摄像头进行纵向模式视频输入时,我必须将其设置为.right
。
let orientation = CGImagePropertyOrientation(uiImage.imageOrientation)
let imageElement = ciImage.applyingOrientation(Int32(orientation.rawValue))
// Show the image in the UI.
originalImage.image = uiImage
同时也请查看https://github.com/gunapandianraj/iOS-Vision的代码,用于将Vision矩形转换为UIKit矩形
private static func translateVisionToNormalBoundingBox(bb: CGRect, imageFullRect: CGRect) -> CGRect
{
let renormalized = VNImageRectForNormalizedRect(bb, Int(imageFullRect.width), Int(imageFullRect.height))
// Vertically translate origin !!!
// Vertically translate origin !!!
// Vertically translate origin !!!
return CGRect(
origin: CGPoint(
x: renormalized.origin.x,
y: imageFullRect.maxY - renormalized.origin.y - renormalized.size.height
),
size: renormalized.size
)
}
对于cgImage方向,我使用了StackOverflow的扩展:
extension UIImage {
var cgImagePropertyOrientation: CGImagePropertyOrientation {
switch imageOrientation
{
case .down: return .down
case .left: return .left
case .right: return .right
case .up: return .up
case .downMirrored: return .downMirrored
case .leftMirrored: return .leftMirrored
case .rightMirrored: return .rightMirrored
case .upMirrored: return .upMirrored
// TWEAK FOR NEW CASES !!!
// TWEAK FOR NEW CASES !!!
// TWEAK FOR NEW CASES !!!
@unknown default:
return .down
}
}
}
orientation
时会得到未解决的标识符'uiImage'
的错误提示。是否需要导入特定的库才能使其正常工作? - Veejay