3

enter image description hereI am using D-lib to extract certain areas of a face. I am using opencv to crop the areas detected using dlib landmarking points detector. However, the cropped images are blue in colour. Any idea on why the change? And also I am finding some of the images are skipping this code. So, for example if I have 20 images in my source folder, after running them through the dlib detector, I should be getting 40 resultant images int he destination folder as I am extracting two images from every input. But that is not the case. I am getting only 15-20 images. But they are running in the program and they are not those exceptions added in my program.

Please find my code below:- and also find the images attached.

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)
Praveen
  • 174
  • 15
  • 2
    add sample images... probably you've mixed BGR and RGB – Micka Oct 26 '16 at 16:03
  • @Micka: Added an image. Please let me know if you need more information. – Praveen Oct 26 '16 at 16:13
  • can you please try cvtColor from RGB to BGR? Sorry I don't know the python api syntax to do so... – Micka Oct 26 '16 at 16:30
  • 1
    As said by @Micka Blue and Red channel seemed to be mixed in this case. Try: `correctImage = cv2.cvtColor(inImage,cv2.COLOR_BGR2RGB)` – masad Oct 26 '16 at 17:19
  • @masad: I get this error correctImage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.error: /root/opencv/modules/imgproc/src/color.cpp:7349: error: (-215) scn == 3 || scn == 4 in function cvtColor. – Praveen Oct 27 '16 at 04:10
  • try `correctImage = cv2.cvtColor(inImage,cv2.COLOR_BGRA2RGBA)` – Micka Oct 27 '16 at 07:38
  • @Micka Please find the error below correctImage = cv2.cvtColor(image,cv2.COLOR_BGRA2RGBA) cv2.error: /root/opencv/modules/imgproc/src/color.cpp:7349: error: (-215) scn == 3 || scn == 4 in function cvtColor – Praveen Oct 27 '16 at 21:22
  • @masad: Please find the error – Praveen Oct 27 '16 at 21:23
  • make sure that you are loading the image correctly. Check this for guidance on the above error: http://stackoverflow.com/questions/30506126/open-cv-error-215-scn-3-scn-4-in-function-cvtcolor I do not have python setup at my current workstation, however using C++ api the image above can be successfully converted using RGB to BGR conversion. This function is simply swapping the first and third channel with each other. – masad Oct 27 '16 at 21:50

2 Answers2

4

In python include the following line of code to convert your image from BGR to RGB

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
Trevor Clarke
  • 1,482
  • 1
  • 11
  • 20
xTomasM
  • 332
  • 2
  • 11
1

As mentioned in the comments, the problem you are facing is that the Red and Blue channels in your images have been swapped. To correct this you need to use cvtColor function. Below is the C++ code that corrects this (I do not currently have access to OpenCV in Python):

#include <cv.h>
#include <highgui.h>

int main()
{
    cv::Mat bgrImage = cv::imread("inImage.png");

    cv::Mat rgbImage;

    cv::cvtColor(bgrImage, rgbImage, CV_BGR2RGB);

    cv::imshow("BGR Output", bgrImage);
    cv::imshow("RGB Processed", rgbImage);

    cv::waitKey(0);
    return 1;
}

Here is the output image: enter image description here

masad
  • 1,547
  • 1
  • 18
  • 40
  • Thanks a lot. can you give me the python code please? I could visualize that my syntax is right, but can you assume the reason for the error? – Praveen Oct 28 '16 at 23:34
  • when using opencv, it's ok using the color convert. but when I run the native example(cnn_face_recognition_ex.cpp) from the dlib, the faces are blue(it's not using opencv), can you give me an idea where to look into? – Chan Kim Oct 13 '20 at 08:29