如何在Python中修改图像以适应face_recognition参数(8位灰度或RGB图像)

问题描述 投票:0回答:1

我正在尝试修改我的图像,使其适合支持的图像类型。 我尝试使用 cv2 和 Pillow 来修改 dtype,但它返回错误“RuntimeError:不支持的图像类型,必须是 8 位灰度或 RGB 图像。”。

这是代码:

image = cv2.imread("myimage.jpeg")
print(f"Original dtype : {image.dtype}, shape: {image.shape}")
'''Output: Original dtype : uint8, shape: (720, 1080, 3)'''

image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print(f"Converted dtype: {image_rgb.dtype}, shape: {image_rgb.shape}")
'''Output: Converted dtype: uint8, shape: (720, 1080, 3)'''

pil_image = Image.fromarray(image_rgb)
image_rgb = np.array(pil_image)

image_rgb = image_rgb.astype(np.uint8)
print(f'Final dtype: {image_rgb.dtype}, shape: {image_rgb.shape}')
'''Output: Final dtype: uint8, shape: (720, 1080, 3)'''



face_locations = face_recognition.face_locations(image_rgb)
print(face_locations)
'''Output: RuntimeError: Unsupported image type, must be 8bit gray or RGB image.'''
python opencv python-imaging-library face-recognition
1个回答
0
投票

你完全没有什么可做的,你可以使用普通的图像!

import face_recognition as fr
import cv2

img = cv2.imread('img.jpg')

face_locations = fr.face_locations(img)

print(face_locations)

for (top, right, bottom, left) in face_locations:
        cv2.rectangle(img, (left-20, top-20), (right+20, bottom+20), (255, 0, 0), 2)
cv2.imshow('img',img)
cv2.waitKey(0)

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.