detectron2的read_image方法

在看代码的时候,看到一行注释:use PIL, to be consistent with evaluation

说是用PIL方法加载,却又看见了BGR这种表述,后面的调用也都是cv2格式:

那我就要看下这里面是怎么实现的了,找到了read_image函数,如下:

def read_image(file_name, format=None):
    """
    Read an image into the given format.
    Will apply rotation and flipping if the image has such exif information.

    Args:
        file_name (str): image file path
        format (str): one of the supported image modes in PIL, or "BGR" or "YUV-BT.601".

    Returns:
        image (np.ndarray):
            an HWC image in the given format, which is 0-255, uint8 for
            supported image modes in PIL or "BGR"; float (0-1 for Y) for YUV-BT.601.
    """
    with PathManager.open(file_name, "rb") as f:
        image = Image.open(f)

        # work around this bug: https://github.com/python-pillow/Pillow/issues/3973
        image = _apply_exif_orientation(image)
        return convert_PIL_to_numpy(image, format)

原来是用PIL打开后转成了numpy

然后还注意到image = _apply_exif_orientation(image)这句,不知道为了解决啥,幸好有个注释的issue:

# work around this bug: https://github.com/python-pillow/Pillow/issues/3973
 

看了下,就是说之前某些图片应用 exif_transpose 函数会报错,这里进行了解决。

def _apply_exif_orientation(image):
    """
    Applies the exif orientation correctly.

    This code exists per the bug:
      https://github.com/python-pillow/Pillow/issues/3973
    with the function `ImageOps.exif_transpose`. The Pillow source raises errors with
    various methods, especially `tobytes`

    Function based on:
      https://github.com/wkentaro/labelme/blob/v4.5.4/labelme/utils/image.py#L59
      https://github.com/python-pillow/Pillow/blob/7.1.2/src/PIL/ImageOps.py#L527

    Args:
        image (PIL.Image): a PIL image

    Returns:
        (PIL.Image): the PIL image with exif orientation applied, if applicable
    """
    if not hasattr(image, "getexif"):
        return image

    try:
        exif = image.getexif()
    except Exception:  # https://github.com/facebookresearch/detectron2/issues/1885
        exif = None

    if exif is None:
        return image

    orientation = exif.get(_EXIF_ORIENT)

    method = {
        2: Image.FLIP_LEFT_RIGHT,
        3: Image.ROTATE_180,
        4: Image.FLIP_TOP_BOTTOM,
        5: Image.TRANSPOSE,
        6: Image.ROTATE_270,
        7: Image.TRANSVERSE,
        8: Image.ROTATE_90,
    }.get(orientation)

    if method is not None:
        return image.transpose(method)
    return image

顺便又复习了下exif是干啥的:

在图像处理中,exif_orientation是一个EXIF(Exchangeable Image File Format)标签,它包含了关于图像方向的信息。这个标签对于正确显示图像的方向非常重要,尤其是在从相机导入照片到计算机或其他设备时。如果exif_orientation标签没有被正确处理,图像可能会以错误的方向显示,比如被旋转了90度、180度或270度。

在Python的Pillow库(一个图像处理库,也称为PIL的更新版本)中,exif_orientation的处理可能会导致图像显示不正确。这可能是由于以下原因:

  1. 错误的解析逻辑:Pillow可能没有正确解析EXIF数据中的exif_orientation标签,导致无法正确识别图像的正确方向。

  2. 兼容性问题:随着相机和设备的发展,EXIF标准可能会有新的更新,Pillow可能需要更新以支持这些新的变化。

  3. 错误处理:在处理某些特定的EXIF数据时,Pillow可能没有正确处理异常情况,导致图像方向错误。

  4. 性能问题:修复exif_orientation可能还包括优化代码,以提高处理大量图像时的性能。

  5. 用户反馈:用户在使用Pillow处理图像时遇到了方向问题,通过在GitHub上提出issue,开发者意识到需要修复这个问题以提高用户体验。

以前就出现过,读取的图像总是和原始图像的方向不一致,本质上是有旋转信息但是没应用过去,下次可以用这个来处理。

又是一个小细节,做个笔记,以上。

相关推荐

  1. detectron2下预训练权重替换特征提取网络

    2024-02-01 19:14:02       130 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 19:14:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 19:14:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 19:14:02       82 阅读
  4. Python语言-面向对象

    2024-02-01 19:14:02       91 阅读

热门阅读

  1. OpenAI Gym 中级教程----深入解析 Gym 代码和结构

    2024-02-01 19:14:02       46 阅读
  2. 一文详解docker compose

    2024-02-01 19:14:02       51 阅读
  3. centos 安装docker CE

    2024-02-01 19:14:02       65 阅读
  4. HIVE的数据类型-整型

    2024-02-01 19:14:02       67 阅读
  5. MySQL与PgSQL的优缺点对比

    2024-02-01 19:14:02       61 阅读
  6. SQL分页写法

    2024-02-01 19:14:02       58 阅读
  7. 征集各位的意见

    2024-02-01 19:14:02       48 阅读
  8. 【风水】-- 家居风水四大注意事项

    2024-02-01 19:14:02       47 阅读
  9. MySQL执行顺序

    2024-02-01 19:14:02       66 阅读
  10. C++面试题

    2024-02-01 19:14:02       56 阅读