Error in Face Recognition Algorithm [closed]

asked 2017-06-22 04:06:48 -0600

beta gravatar image

updated 2017-06-22 04:18:23 -0600

Hi,

I'm using the following code for face recognition. It's working fine for most of the image datasets that I've. But when I'm using this dataset (15.8 MB one), I'm getting the error:

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

My code is the following:

import numpy as np
import dlib
import cv2
import os
import PIL.Image
import math
import operator

predictor_path = "C:/Users/userpath/shape_predictor_68_face_landmarks.dat" 
rootdir = 'C:/Users/userpath/celebrities'
font = cv2.FONT_HERSHEY_SIMPLEX
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))

recognizer = cv2.face.createEigenFaceRecognizer()

images = []
labels = []
labels_index = []

def getFaces(frame):
    dets = detector(frame, 1)
    return dets

def highlightFaces(frame, faces):
    for face in faces:
        cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (255,255,255), 1, 0)
    return frame

def markFace(frame, shape):
    cv2.circle(frame, (shape.parts()[0].x, shape.parts()[0].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[8].x, shape.parts()[8].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[16].x, shape.parts()[16].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[18].x, shape.parts()[18].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[24].x, shape.parts()[24].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[30].x, shape.parts()[30].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[36].x, shape.parts()[36].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[39].x, shape.parts()[39].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[42].x, shape.parts()[42].y), 2, (255,255,255), -1)
    cv2.circle(frame, (shape.parts()[45].x, shape.parts()[45].y), 2, (255,255,255), -1)
    return frame

def cropFace(frame, shape):
    #    points = shape.parts()
#    y1 = points[18].y if points[18].y < points[24].y else points[24].y
#    y2 = points[8].x
#    x1 = points[0].x
#    x2 = points[16].x
#
#    face = frame[y1:y2, x1:x2]
    face = frame[0:200, 0:200]
    face = cv2.resize(face, (200, 200))
    #cv2.imshow("face", face)
    return face

def centerPoint(p1, p2):
    x1, y1 = p1.x, p1.y
    x2, y2 = p2.x, p2.y
    return (x1 + (x2 - x1)* .5, y1 + (y2 - y1) * .5)

# returns the center position of right eye as tuple
def rightEye(shape):
    p1 = shape.parts()[36]
    p2 = shape.parts()[39]
    return centerPoint(p1, p2)

# returns the center position of right eye as tuple
def leftEye(shape):
    p1 = shape.parts()[42]
    p2 = shape.parts()[46]
    return centerPoint(p1, p2)

# Find angle
def faceAngle(shape):
    p1 = shape.parts()[0]
    p2 = shape.parts()[16]
    x1, y1 = p1.x, p1.y
    x2, y2 = p2.x, p2.y
    dx,dy = x2-x1,y2-y1

    rads = -math.atan2(dy, dx)
    return math.degrees(rads)

#rotate image around a point
def rotateImage(image, point, angle):
    rows,cols = image.shape 
    M = cv2.getRotationMatrix2D(point,angle * -1,1 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2017-06-22 05:46:51.222657

Comments

we won't download your dataset to inspect it, so - what kind of images are there ? what is the directory structure ?

the error does not seem to come from opencv.

berak gravatar imageberak ( 2017-06-22 04:09:38 -0600 )edit

@berak: They are jpg images of size around 10KB. The folder structure celebrities\adam_brody,celebrities\al_gore, celebrities\liv_tyler, etc. I'm using the same structure with my own dataset downloaded from internet, and it's working fine. But with celebrities dataset it's not. Can't figure out why it should be. It's a very standard dataset.

beta gravatar imagebeta ( 2017-06-22 04:16:30 -0600 )edit
1

please add a check after imread() , there might just be non-images (e.g. a readme, or something hidden, like a thumbs.db) on the path

berak gravatar imageberak ( 2017-06-22 04:24:00 -0600 )edit
1

@berak: There's a folder which has some corrupted jpg files. When I tried to follow your instruction, I found for a particular folder the code was failing. Once removed that folder, it's working perfectly. Thanks a lot for your suggestion!

beta gravatar imagebeta ( 2017-06-22 04:43:22 -0600 )edit

lol, guess, how I learned that ... ;)

berak gravatar imageberak ( 2017-06-22 04:55:28 -0600 )edit
1

I learned how to debug in these circumstances! Thank you!

beta gravatar imagebeta ( 2017-06-22 05:20:32 -0600 )edit