0

I'm trying to modify my image so that it fits the supported image type. I've tried using cv2 and Pillow to modify the dtype however it returns the error "RuntimeError: Unsupported image type, must be 8bit gray or RGB image.".

Here is the code:

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.'''
7
  • 1
    Why are you mixing OpenCV and PIL? What happens if you pass the image converted by OpenCV directly (image_rgb)? OpenCV reads images as numpy arrays, I don't understand why you need PIL here if you end up casting the image to a numpy array anyway. You don't even have a PIL object in the first place... Commented Jul 7 at 23:33
  • Use cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    – fmw42
    Commented Jul 8 at 0:13
  • @stateMachine I tried both of them individually and neither gave me the desired result and after looking for solutions on here I couldn’t find anything so I just tried this out of desperation
    – ovoxojxy
    Commented Jul 8 at 0:23
  • @fmw42 tired that’s, unfortunately I still get the same error
    – ovoxojxy
    Commented Jul 8 at 0:27
  • 1
    @DanMašek Thank you this solved my issue!
    – ovoxojxy
    Commented Jul 9 at 19:06

1 Answer 1

0

You have absolutely nothing to do, you can use a normal image!

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.