人脸识别和截取嘴唇区域的代码如下:
import numpy as np import cv2import dlibfrom PIL import Image def crop(source,pos): x1=pos[2][0] y1=pos[2][1] x2=pos[1][0] y2=pos[1][1] d=abs(x2-x1) region = source[(int)(y1-d*0.75):y2,x1:x2] # save the image cv2.imwrite("output/Mouth1.jpg", region) x1=pos[1][0] y1=pos[1][1] x2=pos[0][0] y2=pos[0][1] d=abs(x1-x2) region = source[y1-d:y2,x1:x2] # save the image cv2.imwrite("output/Mouth2.jpg", region) def detect_mouth(img,pos): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) detector = dlib.get_frontal_face_detector() #use the predictor predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for a in dets: cv2.rectangle(img,(a.left(),a.top()),(a.right(),a.bottom()),(255,0,0)) #point_list=[]#save the mouth point to point_list[]# #Extract 68 feature points of the face and crop the lip image# for index, face in enumerate(dets): print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom())) shape = predictor(gray, face) for i, pt in enumerate(shape.parts()): #print('Part {}: {}'.format(i, pt)) #print(i) pt_pos = (pt.x, pt.y) if i>=48 and i<=67: cv2.circle(img, pt_pos, 2, (255, 0, 0), 1) if i>=56 and i<=58: #print(pt_pos) pos[i-56][0]=pt.x pos[i-56][1]=pt.y #cv2.circle(img, pt_pos, 2, (255, 0, 0), 1) return img if __name__ == "__main__": img = cv2.imread("test3.png") #copy the input image for the later crop# img_clone = np.copy(img) cv2.imwrite("input/source.jpg",img_clone) #save the lip position to pos array# pos=np.zeros((3,2), dtype=int) result=detect_mouth(img,pos) cv2.imwrite("input/source2.jpg",result) #crop the lip areas# source = cv2.imread("input/source.jpg") crop(source,pos) # show the result cv2.imshow('FaceDetect',result) cv2.waitKey(0) cv2.destroyAllWindow
既然已经截取到嘴唇的小矩形图像了,接下来的工作就和前面一样了,在数据库中对比每个RGB值输出最小误差对应的口红信息,而这儿也有难到文摘菌,单纯的比对RGB分量对口红色号来说并不适用,有可能每个分量相差很小,而叠加起来的颜色和提取到的颜色并不相似,在颜色的比对上需要手动调参。
几经波折,最后输出的结果还是可以接受的,上图人像中涂的口红色号,感兴趣的读者可以查下正好是下面输出排名第一的口红信息。
误差分析对于我们测试的图片信息,文摘菌标记了嘴唇区域的特征点,我们提取到的RGB值(156,59,103)颜色如下所示: