dlib face detection error : Unsupported image type, must be 8bit gray or RGB image
Asked Answered
L

4

6

i am trying to crop out the faces from instagram avatars by first detecting the faces and then resizing the image. i am reading all the images which have been stored in a dataframe and then creating a numpy array. Then i am running a frontal face detector which returns me an object but when i call the object it returns me the error stated. i tried giving only colored images as input but that did not work neither did try and except. Here is the code:

 df = pd.read_csv('/home/instaurls2.csv')
img_width, img_height = 139, 139
confidence = 0.8
#graph = K.get_session().graph
data1 = np.array([io.imread(row[1]) for row in df.itertuples()])
#print(data1)
detector = dlib.get_frontal_face_detector()
print (detector)
dets=detector(data1,1) # **error arrives here**
print (dets)
output=None
for i, d in enumerate(dets):
    data1 = data1[d.top():d.bottom(), d.left():d.right()]
    data1 = resize(data1, (img_width, img_height))
    output = np.expand_dims(data1, axis=0)
print (output)
Loutitia answered 13/2, 2018 at 10:25 Comment(6)
I suspect it's the format of your data1 array. Is it floats or strings when it should be ints or bytes?Madera
@Madera [[[[ 34 34 34] [ 35 35 35] [ 40 40 40] ... [ 8 8 8] [ 12 12 12] [ 12 12 12]] [[ 39 39 39] [ 30 30 30] [ 25 25 25] ... [ 11 11 11] [ 1 1 1] [ 5 5 5]] [[ 54 54 54] [ 44 44 44] [ 34 34 34] ... [ 32 32 32] [ 9 9 9] [ 0 0 0]] ... that's how my data1 looks like i guess it's int and not string or floatLoutitia
I think that might be one too many square braces []. Three channels, R, G, and B; one height; one width makes three sets of square braces.Madera
Also, you have it in "packed" form there (rgb, rgb, rgb....). I don't know if dlib expects planar (rrrr...., gggg...., bbbb....) or not. Might not be a problem, though.Madera
@Madera what could be the error and how can i rectify it/Loutitia
reshape data1 so that it's (channels, height, width), then display it. That should get rid of your square brackets and might fix your problem.Madera
L
12

This is an old thread but if anyone has recently encountered this error, this could very much be due to Numpy 2.0 major update; https://numpy.org/devdocs/release/2.0.0-notes.html

This was released couple of days ago and for the moment Dlib does not support it and throws the above error. I was setting up a new VM and spent hours investigating why the same code works in a previous one and not in the new one… hopefully Dlib will release an update supporting this new version soon, but in the meantime if you see this error - use an older Numpy version (1.X.X)

Leesa answered 18/6, 2024 at 14:23 Comment(1)
Nice, great find! I'm not sure if I ever found this before they fix it. Thanks a lot!Ruination
B
3

Opencv reads image as BGR per default.

You can read images with cv2:

import cv2
cv2.imread(image_filepath)
Bryantbryanty answered 19/8, 2019 at 20:13 Comment(0)
A
2

This error is due to numpy 2. I reinstalled numpy with the following commands:

pip uninstall -y numpy
pip install numpy==1.23.5

and it started working properly!!

Aureaaureate answered 29/7, 2024 at 14:6 Comment(0)
T
0

This worked for me:

image.astype('uint8')
Thirty answered 7/9, 2022 at 20:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.