我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用dlib.get_frontal_face_detector()。
def __init__(self, num_jitters=10, dnn=False, det_threshold=0.0, upsample=0): import_dlib() ppath = os.path.join(os.environ['HOME'], '.tdesc') if not dnn: self.detector = dlib.get_frontal_face_detector() else: detpath = os.path.join(ppath, 'models/dlib/mmod_human_face_detector.dat') self.detector = dlib.face_detection_model_v1(detpath) shapepath = os.path.join(ppath, 'models/dlib/shape_predictor_68_face_landmarks.dat') self.sp = dlib.shape_predictor(shapepath) facepath = os.path.join(ppath, 'models/dlib/dlib_face_recognition_resnet_model_v1.dat') self.facerec = dlib.face_recognition_model_v1(facepath) self.num_jitters = num_jitters self.dnn = dnn self.det_threshold = det_threshold self.upsample = upsample print >> sys.stderr, 'DlibFaceWorker: ready (dnn=%d | num_jitters=%d)' % (int(dnn), int(num_jitters))
def __init__(self, facePredictor): """ Instantiate an 'AlignDlib' object. :param facePredictor: The path to dlib's facial landmark detector :type facePredictor: str :param OPENCV_Detector: The path to opencv's HaarCasscade :type OPENCV_Detector: str :param HOG_Detector: The path to dlib's HGO face detection model :type HOG_Detector: str """ assert facePredictor is not None self.OPENCV_Detector = cv2.CascadeClassifier("/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_default.xml") self.HOG_Detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(facePredictor)
def __init__(self, facePredictor): """ Instantiate an 'AlignDlib' object. :param facePredictor: The path to dlib's :type facePredictor: str """ assert facePredictor is not None #pylint: disable=no-member self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(facePredictor)
def load_trained_models(): """ Helper function to load DLIB's models. """ if not os.path.isfile("data/dlib_data/shape_predictor_68_face_landmarks.dat"): return global FACE_DETECTOR_MODEL, LANDMARKS_PREDICTOR FACE_DETECTOR_MODEL = dlib.get_frontal_face_detector() LANDMARKS_PREDICTOR = dlib.shape_predictor("data/dlib_data/shape_predictor_68_face_landmarks.dat")
def load_trained_models(): if not os.path.isfile("data/dlib_data/shape_predictor_68_face_landmarks.dat"): return global FACE_DETECTOR_MODEL, LANDMARKS_PREDICTOR FACE_DETECTOR_MODEL = dlib.get_frontal_face_detector() LANDMARKS_PREDICTOR = dlib.shape_predictor("data/dlib_data/shape_predictor_68_face_landmarks.dat")
def __init__(self,heads_list=[],predictor_path="./data/shape_predictor_68_face_landmarks.dat"): ''' head_list: ????????????????????????????????????????? ????????????????????????? predictor_path: dlib????? ''' #?????? self.PREDICTOR_PATH = predictor_path self.FACE_POINTS = list(range(17, 68)) self.MOUTH_POINTS = list(range(48, 61)) self.RIGHT_BROW_POINTS = list(range(17, 22)) self.LEFT_BROW_POINTS = list(range(22, 27)) self.RIGHT_EYE_POINTS = list(range(36, 42)) self.LEFT_EYE_POINTS = list(range(42, 48)) self.NOSE_POINTS = list(range(27, 35)) self.JAW_POINTS = list(range(0, 17)) # ???????? self.ALIGN_POINTS = (self.LEFT_BROW_POINTS + self.RIGHT_EYE_POINTS + self.LEFT_EYE_POINTS + self.RIGHT_BROW_POINTS + self.NOSE_POINTS + self.MOUTH_POINTS) # ??????????????????????????????????????????? self.OVERLAY_POINTS = [self.LEFT_EYE_POINTS + self.RIGHT_EYE_POINTS + self.LEFT_BROW_POINTS + self.RIGHT_BROW_POINTS, self.NOSE_POINTS + self.MOUTH_POINTS] # ?????? self.COLOUR_CORRECT_BLUR_FRAC = 0.6 #?????????????dlib self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(self.PREDICTOR_PATH) #???? self.heads={} if heads_list: self.load_heads(heads_list)
def __init__(self, align_path, net_path): # Init align and net """ Dlib / Openface Face recognizer :param align_path: Dlib align path :param net_path: Openface neural network path """ self._align = openface.AlignDlib(os.path.expanduser(align_path)) self._net = openface.TorchNeuralNet(os.path.expanduser(net_path), imgDim=96, cuda=False) self._face_detector = dlib.get_frontal_face_detector() self._trained_faces = []
def __init__(self, facePredictor = None): """Initialize the dlib-based alignment.""" self.detector = dlib.get_frontal_face_detector() if facePredictor != None: self.predictor = dlib.shape_predictor(facePredictor) else: self.predictor = None
def debug_face_landmark(file, output=False, output_name='output'): detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(dat_face_landmark) image = cv2.imread(file) image = imutils.resize(image, width=500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) img_size = gray.shape faces = detector(gray, 1) for (i, itr_face) in enumerate(faces): shape = predictor(gray, itr_face) shape = shape_to_np(shape) # convert dlib's rectangle to a OpenCV-style bounding box # [i.e., (x, y, w, h)], then draw the face bounding box (x, y, w, h) = rect_to_bb(itr_face, img_size, file) #print "landmark: ({:d}, {:d}) ({:d}, {:d})".format(x, y, w, h) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # show the face number cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image for (x, y) in shape: cv2.circle(image, (x, y), 1, (0, 0, 255), -1) # show the output image with the face detections + facial landmarks cv2.imshow(file, image) cv2.waitKey(0) if output: cv2.imwrite("../" + str(output_name + 1) + '.jpg', image) cv2.destroyAllWindows()
def dlib_function(self,image): detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(self.shape_predictor) image = imutils.resize(image, width=500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rects = detector(image, 1) for (i, rect) in enumerate(rects): shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) for (x, y) in shape: cv2.circle(image, (x, y), 1, (0, 0, 255), -1) return image
def __init__(self, align_path, net_path, storage_folder): self._bridge = CvBridge() self._learn_srv = rospy.Service('learn', LearnFace, self._learn_face_srv) self._detect_srv = rospy.Service('detect', DetectFace, self._detect_face_srv) self._clear_srv = rospy.Service('clear', Empty, self._clear_faces_srv) # Init align and net self._align = openface.AlignDlib(align_path) self._net = openface.TorchNeuralNet(net_path, imgDim=96, cuda=False) self._face_detector = dlib.get_frontal_face_detector() self._face_dict = {} # Mapping from string to list of reps self._face_dict_filename = rospy.get_param( '~face_dict_filename', '' ) if ( self._face_dict_filename != '' ): if ( not os.path.isfile( self._face_dict_filename ) ): print '_face_dict does not exist; will save to %s' % self._face_dict_filename else: with open( self._face_dict_filename, 'rb') as f: self._face_dict = pickle.load( f ) print 'read _face_dict: %s' % self._face_dict_filename if not os.path.exists(storage_folder): os.makedirs(storage_folder) self._storage_folder = storage_folder
def __init__(self, predictor_path): self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(str(predictor_path))
def __init__(self): self.PREDICTOR_PATH = "../shape_predictor_68_face_landmarks.dat" self.MOUTH_POINTS = [list(range(48, 61))] self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(self.PREDICTOR_PATH)
def __init__(self): self.PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat" MOUTH_POINTS = list(range(48, 61)) self.OVERLAY_POINTS = [MOUTH_POINTS] self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(self.PREDICTOR_PATH)
def __init__(self): self.detector = dlib.get_frontal_face_detector()
def face_extraction(path): path_str = path[:-1] if path.endswith('/') else path output_dir = path_str + '_faces' if not os.path.isdir(output_dir): os.makedirs(output_dir) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(dat_face_landmark) face_cascade = cv2.CascadeClassifier(xml_face_classifier) undetectLst = list() numfile = get_dataInfo(path_str) not_detected = 0 itr = 0 for itr_file in os.listdir(path_str): if itr_file.endswith('.jpg'): file = "{:s}/{:s}".format(path_str, itr_file) image = cv2.imread(file) image = imutils.resize(image, width=500) bFace, faces = facial_landmark_detection(image, detector, predictor, file) if not bFace: bFace, faces = face_detect_classifier(image, face_cascade) if not bFace: print file undetectLst.append(file) not_detected += 1 image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imwrite("{:s}/{:s}".format(output_dir, itr_file), image) continue x, y, w, h = faces crop_img = image[y:y + h, x:x + w] crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) cv2.imwrite("{:s}/{:s}".format(output_dir, itr_file), crop_img) itr += 1 else: continue total = itr + not_detected print "{:s}: {:4d} of {:4d} file missed detected, detect rate {:2.2f}%"\ .format(path_str, not_detected, total, 100.0 * itr / total) return undetectLst, total
def main_func(): img_path='snap.jpg' # THE PATH OF THE IMAGE TO BE ANALYZED font=cv2.FONT_HERSHEY_DUPLEX emotions = ["anger", "happy", "sadness"] #Emotion list clahe=cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8)) # Histogram equalization object face_det=dlib.get_frontal_face_detector() land_pred=dlib.shape_predictor("data/DlibPredictor/shape_predictor_68_face_landmarks.dat") SUPPORT_VECTOR_MACHINE_clf2 = joblib.load('data/Trained_ML_Models/SVM_emo_model_7.pkl') # Loading the SVM model trained earlier in the path mentioned above. pred_data=[] pred_labels=[] a=crop_face(img_path) img=cv2.imread(a) gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) clahe_gray=clahe.apply(gray) landmarks_vec = get_landmarks(clahe_gray,face_det,land_pred) #print(len(landmarks_vec)) #print(landmarks_vec) if landmarks_vec == "error": pass else: pred_data.append(landmarks_vec) np_test_data = np.array(pred_data) a=SUPPORT_VECTOR_MACHINE_clf2.predict(pred_data) #cv2.putText(img,'DETECTED FACIAL EXPRESSION : ',(8,30),font,0.7,(0,0,255),2,cv2.LINE_AA) #l=len('Facial Expression Detected : ') #cv2.putText(img,emotions[a[0]].upper(),(150,60),font,1,(255,0,0),2,cv2.LINE_AA) #cv2.imshow('test_image',img) #print(emotions[a[0]]) cv2.waitKey(0) cv2.destroyAllWindows() return emotions[a[0]]
def averageFacesImage(df, outpath): detector = dlib.get_frontal_face_detector() numRows = 7 facesPerRow = 26 imageSize = 30 FULLIm = np.zeros((numRows * imageSize, facesPerRow * imageSize*2, 3), dtype=np.uint8) # df = df[df["numImages"] >= 2] df = df.sort(['attractiveness']) grouped = df.groupby("gender") for i, (gender, group) in enumerate(grouped): fullIm = np.zeros((numRows * imageSize, facesPerRow * imageSize, 3), dtype=np.uint8) numPeople = group.shape[0] numPeoplePerRow = int(numPeople / numRows) for j in range(numRows): rowPeople = group.iloc[numPeoplePerRow*j:numPeoplePerRow*(j+1)] # randomSampleIndexs = np.linspace(0,rowPeople.shape[0]-1,facesPerRow).astype(np.int32) randomSampleIndexs = random.sample(range(numPeoplePerRow), facesPerRow) impathss = np.array(rowPeople["impaths"].as_matrix().tolist()) attractiveness = np.array(rowPeople["attractiveness"].as_matrix().tolist()) for l,k in enumerate(randomSampleIndexs): impaths = impathss[k] randomImIndexs = random.sample(range(len(impaths)), len(impaths)) for index in randomImIndexs: im = cv2.imread(impaths[index]) rects = detector(im, 1) if len(rects) != 1: continue rect = rects[0] ryt = max(rect.top() - (rect.height()*0.2) ,0) ryb = min(rect.bottom() + (rect.height()*0.2) , im.shape[0]) rxl = max(rect.left() - (rect.width()*0.2) ,0) rxr = min(rect.right() + (rect.width()*0.2) ,im.shape[1]) faceim = im[ryt:ryb, rxl:rxr] if faceim.shape[0] < 40 or faceim.shape[1] < 40: continue faceim = ensureImageSmallestDimension(faceim, imageSize) y=j*imageSize x=l*imageSize print("%d,%d"%(x,y)) fullIm[y:y + imageSize, x:x + imageSize, :] = faceim[:imageSize, :imageSize, :] x = (l*(imageSize*2)) + (imageSize*((i+j)%2)) FULLIm[y:y + imageSize, x:x + imageSize, :] = faceim[:imageSize, :imageSize, :] cv2.imshow("faces", FULLIm) cv2.waitKey(1) break cv2.imwrite(os.path.join(outpath, "allfaces_%s.jpg"%gender),fullIm) cv2.imwrite(os.path.join(outpath, "allfaces.jpg" ), FULLIm)