Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dlib裁剪的图像为蓝色_Python_Opencv_Dlib_Dlib Python - Fatal编程技术网

Python Dlib裁剪的图像为蓝色

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

我正在使用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)

正如评论中提到的,您面临的问题是图像中的红色和蓝色通道已被交换。要更正此问题,您需要使用
cvtColor
功能。下面是纠正此代码的C++代码(我目前没有在Python中访问OpenCV):

#包括
#包括
int main()
{
cv::Mat bgrImage=cv::imread(“inImage.png”);
cv::Mat rgbImage;
cv::CVT颜色(bgrImage、rgbImage、cv_BGR2RGB);
cv::imshow(“BGR输出”,bgrImage);
cv::imshow(“RGB处理”,rgbImage);
cv::waitKey(0);
返回1;
}
以下是输出图像:

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

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

添加示例图像。。。可能你把BGR和RGB@Micka:添加了一个图像。如果您需要更多信息,请告诉我。您可以尝试从RGB到BGR的cvtColor吗?很抱歉,我不知道python api语法可以这么做……正如@Mika Blue和Red channel所说的,在本例中,它们似乎是混合的。尝试:
correctImage=cv2.cvtColor(inImage,cv2.COLOR\u BGR2RGB)
@masad:我得到这个错误correctImage=cv2.cvtColor(image,cv2.COLOR\u BGR2RGB)cv2.error:/root/opencv/modules/imgproc/src/COLOR.cpp:7349:错误:(-215)scn==3 | scn==4在函数cvtColor中。非常感谢。你能给我python代码吗?我可以想象我的语法是正确的,但是你能假设错误的原因吗?当使用opencv时,使用颜色转换是可以的。但是,当我从dlib运行本机示例(cnn_face_recognition_ex.cpp)时,这些人脸是蓝色的(它没有使用opencv),您能告诉我在哪里进行研究吗?
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)