python - Dlib 裁剪后的图像是蓝色的

标签 python opencv dlib dlib-python

enter image description here我正在使用 D-lib 提取面部的某些区域。我正在使用 opencv 裁剪使用 dlib 地标点检测器检测到的区域。 但是,裁剪后的图像是蓝色的。知道为什么要改变吗? 而且我发现一些图像跳过了这段代码。因此,例如,如果我的源文件夹中有 20 个图像,在通过 dlib 检测器运行它们之后,我应该在目标文件夹中获得 40 个结果图像,因为我从每个输入中提取了两个图像。但事实并非如此。我只得到 15-20 张图片。但是它们是在程序中运行的,并不是我程序中添加的那些异常。

请在下面找到我的代码:- 并找到附件中的图片。

import sys
import os
import dlib
import glob
from skimage import io
import cv2



predictor_path = "/home[![enter image description here][1]][1]/PycharmProjects/Face_recognition/shape_predictor_68_face_landmarks.dat"
faces_folder_path = "/media/External_HDD/My_files/Datasets"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
a=[]
number=1
scanned=1
for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
    print("Processing file: {}".format(f))

    img = io.imread(f)
    name=f[-14:-4]
    print("Number of images scanned is :",scanned)
    scanned=scanned+1
    win.clear_overlay()
    win.set_image(img)

    # Ask the detector to find the bounding boxes of each face. The 1 in the
    # second argument indicates that we should upsample the image 1 time. This
    # will make everything bigger and allow us to detect more faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    if len(dets)>1:
        print ("The file has an anomaly")
        a.append(name)
        print("The number of anomalies detected: {}".format(len(a)))
        continue
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
                                                  shape.part(1)))
        print ("Part 27: {}, Part 19: {}, Part 0: {}, Part 28: {}".format(shape.part(27),shape.part(19),shape.part(0),shape.part(28)))
        left_corner= shape.part(17)
        left_x= left_corner.x
        left_y=left_corner.y
        left_y=left_y-200

        center=shape.part(29)
        center_x=center.x
        center_y=center.y
        print (left_x,left_y)
        print (center_x,center_y)

        right_crop_center_x=center_x
        right_crop_center_y=center_y-700
        right=shape.part(15)
        right_x=right.x
        right_x=right_x-200
        right_y=right.y
        os.chdir("/home/PycharmProjects/cropped")

        win.add_overlay(shape)
        crop_left= img[left_y:center_y,left_x:center_x]
        # cv2.imshow("cropped_left", crop_left)
        cv2.imwrite(name + "_crop_left" +".png" ,crop_left)

        crop_right=img[right_crop_center_y:right_y,right_crop_center_x:right_x]
        # cv2.imshow("cropped_right", crop_right)
        cv2.imwrite(name + "_crop_right" +".png",crop_right)
        print("Number of images completed is :{}".format(number))
        number = number + 1
        cv2.waitKey(2)
        print len(a)

最佳答案

在 python 中包含以下代码行以将图像从 BGR 转换为 RGB

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

关于python - Dlib 裁剪后的图像是蓝色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40267064/

相关文章:

python - 在 os x 上编译 dlib 时出错

c# - 如何在 C# 中使用 dlib 库

python - 如何在 python 中绘制低对比度对象的轮廓?

c++ - 如何在C++中使用DLIB的find_min_single_variable

Python Azure图: Access Token missing or malformed

python - 全局安装 Poetry 项目的标准方式

python - OpenCV houghLinesP 参数

python - opencv python 将掩码区域(黑色或白色像素)复制到 BGR 图像区域

python - 在 Python 2.7 中根据正则表达式匹配重新排序列表?

python - 创建自定义 JSONEncoder