【问题标题】:Unsupported image type error in dlib detectordlib 检测器中不支持的图像类型错误
【发布时间】:2018-06-14 15:58:31
【问题描述】:

我的文件夹“img/datasets/neutral”中有图像,有些图像是灰色的,有些是 BGR,所以当我尝试使用 dlib 检测面部标志时出现错误。

错误检测=检测器(图像,1)
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

我认为此错误是由于某些图像是灰度图像而其他图像是 BGR。我尝试使用 try 和 except 但它不起作用。

如何消除此错误?

python 脚本

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))

【问题讨论】:

  • 请从发布正确缩进的代码开始。 | “我认为这个错误是由于”——所以你验证了你的假设吗?我敢打赌这是不正确的,因为imread 的第二个参数的默认值为IMREAD_COLOR,这意味着“始终将图像转换为 3 通道 BGR 彩色图像”。那么,实际触发此错误的image 的值是多少?
  • 是的,你在写,我通过在中性文件夹中仅放置灰度图像来检查它。但我不知道是哪张图片导致了这个问题。所有图片都是 640x490。
  • 我正在使用 cv2.imread(item, 0),但得到同样的错误。
  • 所有图片都是.png格式
  • :) 是的,情况经常如此。您应该添加一个测试以确保cv2.imread 成功——当它失败时它返回None。像if image is None: 这样的东西,至少让它打印一些有意义的错误消息。

标签: python image opencv dlib


【解决方案1】:

这里是修正

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

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多