确保Image Data在Swift 5中的iOS App上正确定向 [英] Ensuring Image Data is correctly oriented on iOS App in Swift 5

查看:245
本文介绍了确保Image Data在Swift 5中的iOS App上正确定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase ML Kit进行面部检测,并且在文档中显示:

I am using Firebase ML Kit for Face Detection, and in the documentation it says:

如有必要,旋转图像,使其imageOrientation属性为.up. 使用正确旋转的UIImage创建VisionImage对象. 不要指定任何旋转元数据,必须使用默认值.topLeft.

If necessary, rotate the image so that its imageOrientation property is .up. Create a VisionImage object using the correctly-rotated UIImage. Do not specify any rotation metadata—the default value, .topLeft, must be used.

我遇到了一个问题,从互联网上传的照片通常可以正常工作,但是当我从相机拍摄照片时似乎出现了问题.我感觉这是由于图像的定向方式所致,我无法弄清楚应如何检查图像以确保满足上面列出的这两个要求.我尝试打印出images.imageOrientation,但是它对我没有太大帮助,由于某种原因,我无法使用UIImageOrientationUp,我在另一个stackoverflow答案中使用过UIImageOrientationUp.

I am running into the problem where photos I upload from the internet tend to work properly but when I take photos from my camera there seems to be issues. I have a feeling it is due to the way the images are oriented and I can't figure out how I should check the images to ensure these two requirements listed above are satisfied. I tried printing out the images.imageOrientation but it wasn't helping me much, and I for some reason could not use the UIImageOrientationUp which I saw used in a different stackoverflow answer.

这是我尝试打印图像方向时要打印的内容:

This is what gets printed when I try to print the images orientation:

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的新手,这是我的第一个真实"应用程序,因此,对于实现目标的更好或更简单的方法,我感到抱歉.

Anyways if someone could help me write a function that I can use to ensure the orientations of the image I am about to pass to the ML Kit are oriented correctly, I would really appreciate it. Thanks! I am an iOS novice this is my first "Real" app so I am sorry if there was a better or easier way to accomplish my goal.

***因此,我发现当我用相机拍摄照片时,它的朝向是.right,但在实际的imageView上看起来还不错.我尝试将方向更改为.up,但现在图像实际上已向右旋转,并且检测仍然失败...我想我需要将方向更改为.up,但如果可能的话,不实际旋转图像.因为当我尝试设置该值时,它说它是一个get only属性

*** So I found that when I take a picture with my camera it is oriented to the .right but it looks fine on the actual imageView. I tried changing the orientation to the .up but now the image is actually rotated to the right and the detection still failed... I think I need to change the orientation to .Up without actually rotating the image if that is possible. Because when I try to set the value it says its a get only property

推荐答案

感谢我们的帮助,我是MLKit团队的Julie,很抱歉迟到了这个话题.

Thanks for reaching out to us, I'm Julie from MLKit team, sorry to catch up this thread late.

是,从相机拍摄照片时,默认方向并非始终为.up,例如,如果以人像模式拍摄,则图像的方向为.right.

Yes, when a photo is captured from the camera, the default orientation is not always .up, e.g., if it is taken in a portrait mode, the orientation of the image.orientation is .right.

人脸检测器实际上在处理方向不是.up的图像时非常灵活,关键步骤是正确设置方向:

Face detector is actually quite flexible in handling images whose orientation is not .up, the key step is to set the orientation correctly:

以下是在我们的,请查看该应用是否可以解决您的问题.

Here is an example of detecting faces using photos captured from camera in our quickstart app, check it out to see if it will solve your problem.

基本上,您只需要正确设置imageMetadata.orientation值即可,例如

Basically you only need to set the imageMetadata.orientation value correctly like this:

    // 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

和方向之间的映射可以找到

and the mapping between the orientations can be found here:

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检测器中都具有更广泛的用途:

This statement for UIImage is for a more general purpose across all ML Kit detectors:


Create a VisionImage object using the correctly-rotated UIImage. Do not specify any rotation metadata—the default value, .topLeft, must be used.

但是对于脸部,只需正确设置方向即可以轻巧的方式进行处理.对于给您带来的混乱,我们深表歉意,并将在下一个版本中更新此声明.

but for face, it can be handled in a lightweight way by just setting the orientation correctly. We apologize for the confusion that it brought to you, and we will update this statement in the next release.

感谢您报告问题,并希望快速入门应用程序对您的开发有所帮助.

Thanks for reporting the problem, and hope the quickstart app would be helpful to your development.

干杯

朱莉

这篇关于确保Image Data在Swift 5中的iOS App上正确定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆