首页 文章

dlib检测器中不支持的图像类型错误

提问于
浏览
0

我的文件夹'img / datasets / neutral'中有图像,有些图像是灰色的,有些是BGR,所以当我尝试使用dlib检测面部地标时,我得到了错误 .

Error 检测=探测器(图像,1)
RuntimeError:不支持的图像类型,必须是8位灰度或RGB图像 .

我认为这个错误是由于一些图像是灰度级的,而其他图像是BGR . 我试图使用try和except但它不起作用 .

我该如何删除此错误?

python script

emotions = ['neutral', 'sad', 'happy', 'anger']

data={}
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
clf = SVC(kernel='linear', probability=True, tol=1e-3)

def get_files(emotion):
    files = glob.glob('img\\datasets\\%s\\*' %emotion)
    random.shuffle(files)
    training = files[:int(len(files)*0.8)]
    prediction = files[-int(len(files)*0.2)]
    return training, prediction

def get_landmarks(image):
detections = detector(image, 1)
for k, d in enumerate(detections):  # For all detected face instances individually
    shape = predictor(image, d)  # Draw Facial Landmarks with the predictor class
    xlist = []
    ylist = []
    for i in range(1, 68):  # Store X and Y coordinates in two lists
        xlist.append(float(shape.part(i).x))
        ylist.append(float(shape.part(i).y))

    xmean = np.mean(xlist)
    ymean = np.mean(ylist)
    xcentral = [(x - xmean) for x in xlist]
    ycentral = [(y - ymean) for y in ylist]

    landmarks_vectorised = []
    for x, y, w, z in zip(xcentral, ycentral, xlist, ylist):
        landmarks_vectorised.append(w)
        landmarks_vectorised.append(z)
        meannp = np.asarray((ymean, xmean))
        coornp = np.asarray((z, w))
        dist = np.linalg.norm(coornp - meannp)
        landmarks_vectorised.append(dist)
        landmarks_vectorised.append((math.atan2(y, x) * 360) / (2 * math.pi))

    data['landmarks_vectorised'] = landmarks_vectorised
if len(detections) < 1:
    data['landmarks_vestorised'] = "error"


def make_sets():
training_data = []
training_labels = []
prediction_data = []
prediction_labels = []

for emotion in emotions:
    print("Working on %s emotion" %emotion)
    training, prediction = get_files(emotion)

    for item in training:
        image = cv2.imread(item)
        try:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        except:
            print()
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
        clahe_image = clahe.apply(image)
        get_landmarks(clahe_image)

        if data['landmarks_vectorised'] == "error":
            print("no face detected on this one")
        else:
            training_data.append(data['landmarks_vectorised'])  # append image array to training data list
            training_labels.append(emotions.index(emotion))


    for item in prediction:
        image = cv2.imread(item)
        try:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        except:
            print()
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
        clahe_image = clahe.apply(image)
        get_landmarks(clahe_image)

        if data['landmarks_vectorised'] == "error":
            print("no face detected on this one")
        else:
            prediction_data.append(data['landmarks_vectorised'])
            prediction_labels.append(emotions.index(emotion))

return training_data, training_labels, prediction_data, prediction_labels

accur_lin = []

for i in range(0,10):
    print("Making sets %s" % i)  # Make sets by random sampling 80/20%
    training_data, training_labels, prediction_data, prediction_labels = make_sets()

    npar_train = np.array(training_data)
    npar_trainlabs = np.array(training_labels)
    print("training SVM linear %s" % i)  # train SVM
    clf.fit(npar_train, training_labels)

    print("getting accuracies %s" % i)
    npar_pred = np.array(prediction_data)
    pred_lin = clf.score(npar_pred, prediction_labels)

print("Mean value lin svm: %s" % np.mean(accur_lin))

1 回答

  • 0

    这是纠正

    def get_files(emotion):
        files = glob.glob('img\\datasets\\%s\\*' %emotion)
        random.shuffle(files)
        training = files[:int(len(files)*0.8)]
        prediction = files[-int(len(files)*0.2)]       #change this line to prediction=files[-int(len(files)*0.2):]   so that rest 20% files can be accessed
        return training, prediction
    

相关问题