我正在使用Firebase ML Kit来进行人脸检测,在文档中说。
如果有必要,旋转图像,使其imageOrientation属性为.up. 使用正确旋转的UIImage创建一个VisionImage对象。 不要指定任何旋转元数据--必须使用默认值,.topLeft。
我遇到的问题是,我从互联网上传的照片往往能正常工作,但当我从相机拍摄照片时,似乎有问题。我感觉这是由于图像的方向,我不知道应该如何检查图像以确保满足上述两个要求。我试着打印出images.imageOrientation,但它对我帮助不大,而且由于某些原因,我无法使用图像的 UIImageOrientationUp
我在另一个stackoverflow的答案中看到过。
这就是我试图打印图像方向时被打印出来的东西。
int:0x2809f9a40 'UISV-alignment' UIImageView:0x13de4d4b0.bottom == UILabel:0x13dec1630'orient's Profile'.bottom (active)>",
"<NSLayoutConstraint:0x2809f9a90 'UISV-alignment' UIImageView:0x13de4d4b0.top == UILabel:0x13dec1630'orient's Profile'.top (active)>",
无论如何,如果有人能帮我写一个函数,我可以用它来确保我将要传递给ML Kit的图像的方向是正确的,我将非常感激。谢谢 我是一个iOS新手,这是我的第一个 "真正的 "应用程序,所以如果有更好或更简单的方法来实现我的目标,我很抱歉。
*** 我发现,当我用相机拍照时,它的方向是.right,但在实际的imageView上看起来很好。我试着把方向改成.up,但现在图像实际上是向右旋转的,而且检测仍然失败... 我想如果可以的话,我需要在不实际旋转图像的情况下将方向改为.Up。因为当我尝试设置值时,它说它是一个只获取的属性。
谢谢你主动联系我们,我是MLKit团队的Julie,很抱歉晚了才赶上这个话题。
是的,当一张照片从相机中拍摄出来时,默认的方向并不总是 "向上"。.up
例如,如果它是在纵向模式下拍摄的,则image.orientation的方向是 .right
.
实际上,人脸检测器在处理图像时非常灵活,其方向不是 .up
,关键步骤是正确设置方向。
下面是一个使用我们的相机拍摄的照片来检测人脸的例子。快速启动程序,看看能不能解决你的问题。
基本上你只需要设置 imageMetadata.orientation
正确值 这个:
// Define the metadata for the image.
let imageMetadata = VisionImageMetadata()
imageMetadata.orientation = UIUtilities.visionImageOrientation(from: image.imageOrientation)
// Initialize a VisionImage object with the given UIImage.
let visionImage = VisionImage(image: image)
visionImage.metadata = imageMetadata
以及各方向之间的映射可以发现 此处:
public static func visionImageOrientation(
from imageOrientation: UIImage.Orientation
) -> VisionDetectorImageOrientation {
switch imageOrientation {
case .up:
return .topLeft
case .down:
return .bottomRight
case .left:
return .leftBottom
case .right:
return .rightTop
case .upMirrored:
return .topRight
case .downMirrored:
return .bottomLeft
case .leftMirrored:
return .leftTop
case .rightMirrored:
return .rightBottom
}
}
对于UIImage的这一声明是为了在所有ML Kit检测器中实现更普遍的目的。
Create a VisionImage object using the correctly-rotated UIImage. Do not specify any rotation metadata—the default value, .topLeft, must be used.
但对于人脸来说,只要正确设置方向,就可以用轻量级的方式处理。我们很抱歉给您带来的困惑,我们将在下一个版本中更新这个声明。
感谢你报告这个问题,希望快速入门应用能对你的开发有所帮助。
干杯。
茱莉