I 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)
In python include the following line of code to convert your image from BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)