关于相机拍摄图片带旋转的问题
最近在用paddleDetection这个框架 然后在读数据的时候有点问题 报warning
2021-03-01 02:39:12,601-WARNING: The actual image width: 3024 is not equal to the width: 4032.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:23,710-WARNING: The actual image height: 3264 is not equal to the height: 2448.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:23,710-WARNING: The actual image width: 2448 is not equal to the width: 3264.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:28,401-INFO: iter: 20, lr: 0.000005, 'loss': '1659.900146', eta: 23 days, 17:20:08, batch_cost: 4.09938 sec, ips: 1.95151 images/sec
2021-03-01 02:39:28,683-WARNING: The actual image height: 4032 is not equal to the height: 3024.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:28,683-WARNING: The actual image width: 3024 is not equal to the width: 4032.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:34,422-WARNING: The actual image height: 3264 is not equal to the height: 2448.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:34,422-WARNING: The actual image width: 2448 is not equal to the width: 3264.0 in annotation, and update sample['w'] by actual image width.
2021-03-01 02:39:38,848-WARNING: The actual image height: 4032 is not equal to the height: 3024.0 in annotation, and update sample['h'] by actual image height.
2021-03-01 02:39:38,848-WARNING: The actual image width: 3024 is not equal to the width: 4032.0 in annotation, and update sample['w'] by actual image width.
/paddle/PaddleDetection/ppdet/data/transform/operators.py:131: DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead
查了paddleDetection github 的issue 大概意思是 某图片 标签信息里的w/h与实际图片的w/h 不相等 然后自动帮你给更新了。
基于PIL读取图像的默认方式(忽略旋转信息)
基于CV2读取图像的默认方式(不忽略旋转信息)
标签是用labelimg(基于PIL)打的 其实肉眼看都没啥问题 问题在于图片中的exif里有个旋转信息 出问题的图片都是自带旋转了 。然后labelimg是PIL封装来的 读的是原图 不管它exif信息的 ,直接cv2.imread是会考虑这个exif信息的。所以读的时候参数要带个cv2.IMREAD_COLOR+cv2.IMREAD_IGNORE_ORIENTATION 可参考这篇:关于使用opencv读取经过labelImg工具进行标注后的图像出现的框与图像旋转方向不一致的问题 [author: linkrain]
然后paddleDetection读图片是这样的 。。
if 'image' not in sample:
with open(sample['im_file'], 'rb') as f:
sample['image'] = f.read()
im = sample['image']
data = np.frombuffer(im, dtype='uint8')
im = cv2.imdecode(data, 1) # BGR mode, but need RGB mode
只要把最后一句改成
im = cv2.imdecode(data,cv2.IMREAD_COLOR+cv2.IMREAD_IGNORE_ORIENTATION )
然后还有一点 我在paddle里还看到一个 如果PIL也想按exif的旋转信息来读 就用ImageOps.exif_transpose来进行变换。
PIL默认读图片是忽略exif的旋转信息的
origin_image = Image.open(sample['im_file']).convert('RGB')
origin_image = ImageOps.exif_transpose(origin_image)
最近发现,cv2.imread(path,flag)中的flag<0时 似乎也是忽略旋转信息的。