Face_recognition: "Unsupported image type, must be 8bit gray or RGB image."

Created on 2 Aug 2017  路  11Comments  路  Source: ageitgey/face_recognition

  • face_recognition version: Latest, just installed today from pip3
  • Python version: 3.5.2
  • Operating System: Fresh install of Ubuntu

Description

I was just playing around with some of the example scripts, when I get this error:

sudo python3 main.py obama.jpg
obama.jpg
Traceback (most recent call last):
File "main.py", line 13, in
unkown_image_encoding = face_recognition.face_encodings(unknown_image)[0]
File "/usr/local/lib/python3.5/dist-packages/face_recognition/api.py", line 146, in face_encodings
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations)
File "/usr/local/lib/python3.5/dist-packages/face_recognition/api.py", line 105, in _raw_face_landmarks
face_locations = _raw_face_locations(face_image)
File "/usr/local/lib/python3.5/dist-packages/face_recognition/api.py", line 89, in _raw_face_locations
return face_detector(img, number_of_times_to_upsample)
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

What I Did

import face_recognition
import sys

unknown_image = sys.argv[1]
print(sys.argv[1])
obama_image = face_recognition.load_image_file('obama.jpg')
obama_image_encoding = face_recognition.face_encodings(obama_image)[0]

biden_image = face_recognition.load_image_file('biden.jpg')
biden_image_encoding = face_recognition.face_encodings(biden_image)[0]


unkown_image_encoding = face_recognition.face_encodings(unknown_image)[0]

knownfaces = [
obama_image_encoding,
biden_image_encoding
]

results = face_recognition.compare_faces(knownfaces, unkown_image_encoding)
print(results)

if results[0] == True and results[1] == False:
    print("It's Obama!")
elif results[1] == True and results[0] == False: 
    print("It's Biden!")
else:
    print('Unknown person')

The weird thing is that I tried running it without the sys.argv and with the same files, it works.

Most helpful comment

You need to call face_recognition.load_image_file() on the file name you pass in via argv. argv is just a string file name, not an array of image data.

All 11 comments

You need to call face_recognition.load_image_file() on the file name you pass in via argv. argv is just a string file name, not an array of image data.

Ohhh, I see. Thanks. Still a beginner, your help is greatly appreciated. :D

No problem :-)

I used face_recognition.load_image_file() to load the captured image but still it has this error:
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
IndexError: list index out of range

Also, it is very slow especially on Raspberry pi.
Do you have any idea why?

import face_recognition
import cv2
import os
import numpy as np
from PIL import Image

path='dataset2'
imagepath = [os.path.join(path,f)for f in os.listdir(path)]
print imagepath
images=[]
for i in imagepath:
    ID = int(os.path.split(i)[-1].split('.')[1])
    print ID
    faceimg = Image.open(i).convert('L')
    print faceimg
    #obama_image = face_recognition.load_image_file(faceimg)
    obama_image = face_recognition.load_image_file("dataset2/vrushang."+str(ID)+".jpg")
#face_recognition.load_image_file(i).convert('L')
    image=np.array(obama_image)
    images.append(image)
print images
obama_face_encoding = face_recognition.face_encodings(images)[0]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(small_frame)
        face_encodings = face_recognition.face_encodings(small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            match = face_recognition.compare_faces([obama_face_encoding], face_encoding)


            if match[0]:
                name = "vrushang"
        else:
        name = "unknown"


            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1)==27:
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

i am getting this output with error.

['dataset2/vrushang.1.jpg', 'dataset2/vrushang.2.jpg']
1
<PIL.Image.Image image mode=L size=358x358 at 0x7F9EB10B7650>
2
<PIL.Image.Image image mode=L size=412x412 at 0x7F9EB10B7190>
[array([[[101, 101, 101],
        [101, 101, 101],
        [101, 101, 101],
        ..., 
        [ 35,  35,  35],
        [ 36,  36,  36],
        [ 36,  36,  36]],

       [[101, 101, 101],
        [101, 101, 101],
        [100, 100, 100],
        ..., 
        [ 34,  34,  34],
        [ 35,  35,  35],
        [ 35,  35,  35]],

       [[101, 101, 101],
        [101, 101, 101],
        [101, 101, 101],
        ..., 
        [ 36,  36,  36],
        [ 36,  36,  36],
        [ 36,  36,  36]],

       ..., 
       [[ 43,  43,  43],
        [ 46,  46,  46],
        [ 48,  48,  48],
        ..., 
        [ 55,  55,  55],
        [ 55,  55,  55],
        [ 55,  55,  55]],

       [[ 42,  42,  42],
        [ 44,  44,  44],
        [ 47,  47,  47],
        ..., 
        [ 54,  54,  54],
        [ 53,  53,  53],
        [ 53,  53,  53]],

       [[ 41,  41,  41],
        [ 43,  43,  43],
        [ 46,  46,  46],
        ..., 
        [ 53,  53,  53],
        [ 52,  52,  52],
        [ 51,  51,  51]]], dtype=uint8), array([[[158, 158, 158],
        [159, 159, 159],
        [159, 159, 159],
        ..., 
        [172, 172, 172],
        [172, 172, 172],
        [172, 172, 172]],

       [[159, 159, 159],
        [159, 159, 159],
        [158, 158, 158],
        ..., 
        [173, 173, 173],
        [173, 173, 173],
        [172, 172, 172]],

       [[159, 159, 159],
        [158, 158, 158],
        [156, 156, 156],
        ..., 
        [174, 174, 174],
        [173, 173, 173],
        [172, 172, 172]],

       ..., 
       [[145, 145, 145],
        [144, 144, 144],
        [144, 144, 144],
        ..., 
        [ 90,  90,  90],
        [ 90,  90,  90],
        [ 90,  90,  90]],

       [[143, 143, 143],
        [144, 144, 144],
        [144, 144, 144],
        ..., 
        [ 89,  89,  89],
        [ 89,  89,  89],
        [ 89,  89,  89]],

       [[142, 142, 142],
        [143, 143, 143],
        [145, 145, 145],
        ..., 
        [ 88,  88,  88],
        [ 88,  88,  88],
        [ 89,  89,  89]]], dtype=uint8)]
Traceback (most recent call last):
  File "fc.py", line 27, in <module>
    obama_face_encoding = face_recognition.face_encodings(images)[0]
  File "/usr/local/lib/python2.7/dist-packages/face_recognition/api.py", line 189, in face_encodings
    raw_landmarks = _raw_face_landmarks(face_image, known_face_locations)
  File "/usr/local/lib/python2.7/dist-packages/face_recognition/api.py", line 148, in _raw_face_landmarks
    face_locations = _raw_face_locations(face_image)
  File "/usr/local/lib/python2.7/dist-packages/face_recognition/api.py", line 97, in _raw_face_locations
    return face_detector(img, number_of_times_to_upsample)
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

i am trying to access whole bunch of images by converting it to array and then put it in single encoding part .how to solve this problem?

@mberenjkoub

unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
IndexError: list index out of range

That just means no face encodings were found in that particular image. Just check the length of the array before assuming it has something in it:

face_encodings = face_recognition.face_encodings(unknown_image)

if len(face_encodings) == 0:
    print("No faces found!")
else:
    unknown_face_encoding = face_encodings[0]

@vrushang25

obama_face_encoding = face_recognition.face_encodings(images)[0]

The face_encodings() function doesn't take in an array of multiple images. It takes in a single image. You can't just pass in a list of numpy arrays to it.

Instead, just call face_encodings() once for each images in your images array.

now i solve the problem but it recognise me and my brother with same name why?
i make the array of the encoding part of images to compare but gives error many times
my code is below:-

import face_recognition
import cv2
import os
import numpy as np
from PIL import Image
import time
cap = cv2.VideoCapture(1)

count=1
path='dataset2'

img=[]
imagepath = [os.path.join(path,f)for f in os.listdir(path)]
c=len(imagepath)

for i in imagepath: i access the each images from my folder of images

while count<=c:
image = face_recognition.load_image_file("dataset2/vrushang."+str(count)+".jpg")
#now i will make list of the encoding parts to compare it runtime detected face
img.append(face_recognition.face_encodings(image)[0])
time.sleep(1)
count=count+1

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
img2 = []
img2 = img[0]
print"this is img2"
print img

while True:
ret, frame = cap.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
if process_this_frame:
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:

                 match = face_recognition.compare_faces(img, face_encoding)
                 print match

                 if match[0]==True:
                       name = "vrushang"
            elif match[1]==True:
                       name = "hitu"    
            elif match[3]==True:
                      name = "sardar patel" 
            elif match[2]==True:
                      name = "yaksh"
            else:       
              name = "unknown"
                face_names.append(name)
process_this_frame = not process_this_frame   
for (top, right, bottom, left), name in zip(face_locations, face_names):
    top *= 4
    right *= 4
    bottom *= 4
    left *= 4
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)      
    cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)  
cv2.imshow('Video', frame)   
if cv2.waitKey(1)==27:
    break

Release handle to the webcam

cap.release()
cv2.destroyAllWindows()

many of my relatives are wrongly recognized with this code can you please help me for this proble?

Can you please say whether face_recognition.load_image_file() function supports an image using a web URL.

No, it doesn't load images from URLs. You would have to code that yourself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cjosephson picture cjosephson  路  5Comments

Allenstin picture Allenstin  路  5Comments

xiao543348405 picture xiao543348405  路  5Comments

Sparviero-Sughero picture Sparviero-Sughero  路  4Comments

ramineniraviteja picture ramineniraviteja  路  5Comments