def get_landmarks(self, image): img = dlib.load_rgb_image(image) if self.mode == 'real': dets = self.detector(img, 1) elif self.mode == 'cartoon': dets = dlib.rectangles() faces = viola_jones_combine(img[:, :, ::-1]) if faces is not None: for i, (x, y, w, h) in enumerate(faces): rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h)) dets.append(rect) elif self.mode == 'anime': dets = dlib.rectangles() faces = viola_jones_anime(img[:, :, ::-1]) if faces is not None: for i, (x, y, w, h) in enumerate(faces): rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h)) dets.append(rect) for detection in dets: if detection.height() > 100 and detection.width() > 100: try: face_landmarks = [(item.x, item.y) for item in self.shape_predictor( img, detection).parts()] yield face_landmarks except: print("Exception in get_landmarks()!")
def test_get_bounding_box(FACE_DETECTOR): FACE_DETECTOR.side_effect = [ dlib.rectangles([dlib.rectangle(1, 1, 3, 3), dlib.rectangle(1, 1, 4, 4)]), dlib.rectangles(), ] image = np.random.randn(4, 4, 3) assert fd._get_bounding_box(image, 2) == dlib.rectangle(1, 1, 4, 4) FACE_DETECTOR.assert_called_with(image, 2) assert fd._get_bounding_box(image, 1) is None
def face_detection_alignmen(original_image): # Load the image using OpenCV bgr_img = cv2.imread(original_image) # Convert to RGB since dlib uses RGB images img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) # first traditional detector for efficient dets = detector(img, 1) num_faces = len(dets) # If there is no face detected in the image if num_faces == 0: # second use cnn detector cnn_dets = cnn_face_detector(img, 1) dets = dlib.rectangles() dets.extend([d.rect for d in cnn_dets]) num_faces = len(dets) if num_faces == 0: print('There is no face in the image') # Find the 5 face landmarks we need to do the alignment. faces = dlib.full_object_detections() for detection in dets: faces.append(sp(img, detection)) image = dlib.get_face_chip(img, faces[0], size=224, padding=0.3) cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # cv2.imshow('annotShow', cv_bgr_img) # cv2.waitKey(0) return cv_bgr_img
def face_detection(path): cnn_face_detector = dlib.cnn_face_detection_model_v1( "../../models/mmod_human_face_detector.dat") win = dlib.image_window() for file_name in os.listdir(path): name, ext = file_name.split(".") if ext == "png": file_path = path + file_name print(file_path) img = dlib.load_rgb_image(file_path) dets = cnn_face_detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): print( "Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}" .format(i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence)) rects = dlib.rectangles() rects.extend([d.rect for d in dets]) win.clear_overlay() win.set_image(img) win.add_overlay(rects) dlib.hit_enter_to_continue()
def get_faces(self, rgbImage): start_time = time.time() detected_faces = self.cnn_face_detector(rgbImage, 1) rects = dlib.rectangles() rects.extend([d.rect for d in detected_faces]) print("Face detection spend {}s".format(time.time() - start_time)) return rects #:type = <class 'dlib.rectangles'>
def cal_scores(img_path): img = io.imread(img_path) # 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))) rects = dlib.rectangles() rects.extend([d.rect for d in dets]) # Now process each face we found. for k, d in enumerate(rects): # 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 = sp(img, d) # Draw the face landmarks on the screen so we can see what face is currently being processed. # Compute the 128D vector that describes the face in img identified by # shape. In general, if two face descriptor vectors have a Euclidean # distance between them less than 0.6 then they are from the same # person, otherwise they are from different people. Here we just print # the vector to the screen. face_descriptor = facerec.compute_face_descriptor(img, shape) return list(face_descriptor)
def detect_facemarks_coords( image, bboxes, facemark_predictor_init=None, facemarks_data_pathway='../models/shape_predictor_68_face_landmarks.dat'): """ Detect face landmarks using dlib :param image: rgb image :param bboxes: bounding boxes :param facemark_predictor_init: pre initialization for speed :param facemarks_data_pathway: dlib predictor source file :return: """ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if facemark_predictor_init: facemarks_predictor = facemark_predictor_init else: facemarks_predictor = dlib.shape_predictor(facemarks_data_pathway) rectangles = dlib.rectangles() rectangles.extend([dlib.rectangle(left=bbox[0], top=bbox[1], right=bbox[2] + bbox[0], bottom=bbox[3] + bbox[1]) for bbox in bboxes]) facemarks_coords = list() for rectangle in rectangles: facemarks = facemarks_predictor(gray_image, rectangle) facemarks_coords.append(facemarks_to_coords(facemarks)) return facemarks_coords
def enlarge_drectangles(sm_dets, enlarge_ratio): if isinstance(sm_dets, dlib.rectangles): dets = dlib.rectangles() for sm_det in sm_dets: dets.append(dlib.rectangle( int(sm_det.left() * enlarge_ratio), int(sm_det.top() * enlarge_ratio), int(sm_det.right() * enlarge_ratio), int(sm_det.bottom() * enlarge_ratio), )) return dets elif isinstance(sm_dets, dlib.rectangle): det = dlib.rectangle( int(sm_det.left() * enlarge_ratio), int(sm_det.top() * enlarge_ratio), int(sm_det.right() * enlarge_ratio), int(sm_det.bottom() * enlarge_ratio)) return det elif isinstance(sm_dets, tuple) and len(sm_dets) == 4: return (sm_dets[0] * enlarge_ratio, sm_dets[1] * enlarge_ratio, sm_dets[2] * enlarge_ratio, sm_dets[3] * enlarge_ratio) else: raise TypeError( 'sm_dets needs to be type dlib.drectangles or dlib.rectangle. but is {}'.format(type(sm_dets)))
def find_faces(cv_image): global face_detector, win, last_call face_position = [None, None] faces = face_detector(cv_image, 1) print("Detections: {}".format(len(faces))) for i, d in enumerate(faces): print("face {}: Left: {}, Top: {}, Right: {}, Bottom: {}".format( i, d.left(), d.top(), d.right(), d.bottom())) if i == 0: face_position = (.5 * (d.right() + d.left()) / FRAME_WIDTH, .5 * (d.bottom() + d.top()) / FRAME_HEIGHT) rects = dlib.rectangles() rects.extend([d for d in faces]) win.clear_overlay() win.set_image(cv_image) win.add_overlay(rects) # cv2.imshow("camera_raw", cv_image) # cv.2waitKey(3) return face_position
def detect_face(self, image): dets = self.cnn_face_detector(image, 1) ''' This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects. These objects can be accessed by simply iterating over the mmod_rectangles object The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score. It is also possible to pass a list of images to the detector. - like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128) In this case it will return a mmod_rectangless object. This object behaves just like a list of lists and can be iterated over. ''' print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): print( "Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}" .format(i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence)) rects = dlib.rectangles() rects.extend([d.rect for d in dets]) if (self.debug): self.win.clear_overlay() self.win.set_image(image) self.win.add_overlay(rects) return len(dets), rects
def detectFacesOpencvCascade(detector, image, scale_factor=1.3, min_neighbors=5, height=300, verbose=True, debug=False): image_height = image.shape[0] image_width = image.shape[1] width = 0 if height != image_height: relation = float(image_width) / image_height width = int(relation * height) else: width = image_width scale_height = float(image_height) / height scale_width = float(image_width) / width image_small = cv2.resize(image, (width, height)) image_gray = cv2.cvtColor(image_small, cv2.COLOR_BGR2GRAY) faces = detector.detectMultiScale(image_gray) rects = rectangles() for (x, y, width, height) in faces: x1 = int(x * scale_width) y1 = int(y * scale_height) x2 = int((x + width) * scale_width) y2 = int((y + height) * scale_height) rects.append(rectangle(x1, y1, x2, y2)) return rects
def detectFacesDlibMmod(detector, image, height=300, verbose=True, debug=False): image_height = image.shape[0] image_width = image.shape[1] width = 0 if height != image_height: relation = float(image_width) / image_height width = int(relation * height) else: width = image_width scale_height = float(image_height) / height scale_width = float(image_width) / width image_small = cv2.resize(image, (width, height)) image_small = cv2.cvtColor(image_small, cv2.COLOR_BGR2RGB) faces_small = detector(image_small, 0) faces = rectangles() for face in faces_small: faces.append( rectangle(int(face.rect.left() * scale_width), int(face.rect.top() * scale_height), int(face.rect.right() * scale_width), int(face.rect.bottom() * scale_height))) return faces
def detectFacesOpencvDnn(detector, image, threshold=0.7, scale_factor=1.0, height=300, mean=[104, 117, 123], verbose=True, debug=False): image_height = image.shape[0] image_width = image.shape[1] width = 0 if height != image_height: relation = float(image_width) / image_height width = int(relation * height) else: width = image_width size = (width, height) blob = cv2.dnn.blobFromImage(image, scale_factor, size, mean) detector.setInput(blob) detections = detector.forward() faces = rectangles() for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence >= threshold: x1 = int(detections[0, 0, i, 3] * image_width) y1 = int(detections[0, 0, i, 4] * image_height) x2 = int(detections[0, 0, i, 5] * image_width) y2 = int(detections[0, 0, i, 6] * image_height) faces.append(rectangle(x1, y1, x2, y2)) return faces
def cnn_detect(self, image_list=None, detector_path=config_cnn_detector_path): """ 基于CNN的人脸检测函数 detector_path: 加载探测器路径 CNN模型比HOG模型更精确些,但速度更慢。有GPU才能达到HOG的速度 """ assert image_list, "image_list不能为空,请载入一个image_list" detector = dlib.cnn_face_detection_model_v1(detector_path) for i in image_list: print('正在处理文件: {}'.format(i)) img = io.imread(i) dets = detector(img, 1) ''' 检测器返回一个mmod矩形对象,这个对象包含了一个列表的mmod矩形。 只需遍历mmod_rectangles对象即可访问这些对象 mmod_rectangle对象有两个成员变量,一个dlib.rectangle对象和一个置信度分数。 也可以将图像列表传给检测器: dets = cnn_face_detector([image_list], upsample_num, batch_size = 128) 返回一个列表对象,存储一些存有图像列表的列表 ''' for k, d in enumerate(dets): show_position(k, d.rect) rects = dlib.rectangles() rects.extend([d.rect for d in dets]) show_image(img, rects)
def call_face_detector(self, image, upsample_num_times=0): detections = self.face_detector(image, upsample_num_times) if DLIB_USE_CNN: rects = dlib.rectangles() rects.extend([d.rect for d in detections]) return rects else: return detections
def test_get_faces(): """Test for get_faces """ input_image = cv2.imread("tests/data/test_payload_nface_2.jpeg") ground_truth = rectangles() ground_truth.append(rectangle(290, 290, 675, 675)) ground_truth.append(rectangle(92, 588, 315, 811)) assert ground_truth == get_faces(input_image)
def forward(self, inputs): faces, image = inputs rects = dlib.rectangles(Utils.points2rects(faces)) csses = [] for rect in rects: csses.append(_rect_to_css(rect)) result = face_recognition.face_encodings(image, csses, 10) return np.asarray(result), rects, image
def getAllFaceBoundingBoxes(rgbimg): gimg = cv2.cvtColor(rgbimg, cv2.COLOR_RGB2GRAY) faces = face_cascade.detectMultiScale(gimg, scaleFactor=1.1, minNeighbors=5) rects = dlib.rectangles() for (x,y,w,h) in faces: rects.append(dlib.rectangle(long(x),long(y),long(w-x),long(h-y))) return rects
def detect(frame, detector, do_return_dlib_rects): if type(detector) == cv2.CascadeClassifier: rects = cv_detect(frame, detector) if do_return_dlib_rects: rects = dlib.rectangles(list(map(cv_rect2dlib_rect, rects))) else: rects = dlib_detect(frame, detector) if not do_return_dlib_rects: rects = list(map(dlib_rect2cv_rect, rects)) return rects
def _face2vec(self, imageBuffer): dataFromBuffer = np.frombuffer(imageBuffer, dtype=np.uint8) image = cv2.imdecode(dataFromBuffer, cv2.IMREAD_COLOR) faceDetection = dlib.rectangles() faceDetection.append( dlib.rectangle(5, 5, image.shape[0] - 5, image.shape[1] - 5)) faceLMDetection = self.predictor(image, faceDetection[0]) vec = self.recognizer.compute_face_descriptor(image, faceLMDetection) return vec
def _use_haarcascade(self): frame = self._face_cascade_classifier.detectMultiScale(self._image_gray, 1.9, 5) result = dlib.rectangles() for (x, y, w, h) in frame: left = x top = y right = (x + w) bottom = (y + h) face = dlib.rectangle(left, top, right, bottom) result.append(face) return result return result
def _array_to_dlib_rectangles(rects_array): """ Function to convert array of rectangles (in format [[x1,y1,w1,h1],[x2,y2,w2,h2],...]) to dlib regtangles objects. Usually input array is a results of OpenCV face detection and output dlib regtangles are used for landmark detection. :param rects_array: array with results of OpenCV face detection :return: dlib rectangles object """ rects_dlib = dlib.rectangles() for (left, top, right, bottom) in rects_array: rects_dlib.append( dlib.rectangle(int(left), int(top), int(right), int(bottom))) return rects_dlib
def predict_and_encode (self, img, confidence = 0.9,ratio = 30,visua = False): predict = self.detector.detect_faces(img) #print(predict) out_boxes = [ item['box'] for item in predict if item["confidence"]> confidence and item["box"][3]>img.shape[0]/ratio] out_boxes = dlib.rectangles([dlib.rectangle(a,b,a+c,b+d) for a,b,c,d in out_boxes ]) #print(out_boxes) key = [item['keypoints'] for item in predict if item["confidence"]> confidence and item["box"][3]>img.shape[0]/ratio] img_encoded = self.face_encodings(img,out_boxes) #print(key) if visua: self.visualize(img,out_boxes,key) return out_boxes, key, img_encoded
def DetectLandmarksForPyTorch(image, boxes): # faces = detectorDlib(image) faces = dlib.rectangles() # из opencv rectangle в dlib rectangle for r in boxes: dlib.rectangles.append(faces, dlib.rectangle(left = int(r[0]), top = int(r[1]), right = int(r[2]), bottom = int(r[3]))) landmarks = [] for face in faces: # Получение координат контрольных точек и их построение на изображении landmarks.append(predictor(image, face)) return landmarks
def get_landmarks(im): # unused code for face facing calculation width, height, _unused = im.shape c_x = width / 2 c_y = height / 2 f_x = c_x / numpy.tan(60 / 2 * numpy.pi / 180) f_y = f_x camera_matrix = numpy.float32([[f_x, 0.0, c_x], [0.0, f_y, c_y], [0.0, 0.0, 1.0]]) lm_list = [] if DETECTOR_TYPE == "DNN": rects = dlib.rectangles(0) rects_list = [] num_of_face = 0 (h, w) = im.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(im, (300, 300)), 1.0, (300, 300), (103.93, 116.77, 123.68)) net.setInput(blob) detections = net.forward() # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the # prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > DNN_NOFACE_THRESH: # find the face with highest conf # compute the (x, y)-coordinates of the bounding box for the # object box = detections[0, 0, i, 3:7] * numpy.array([w, h, w, h]) [x1, y1, x2, y2] = box.astype("int") rects_list.append([[x1, y1, x2, y2], confidence]) num_of_face += 1 rects_list.sort(key=takeSecond, reverse=True) # rank the faces with confidence, highest on top for i in range(0, num_of_face): rects.append(dlib.rectangle(*(rects_list[i][0]))) # push the faces into rects container elif DETECTOR_TYPE == "DLIB": rects = detector(im, 1) # draw a box on detected face print("detected: ", len(rects)) # if len(rects) == 0: # raise NoFaces for i in range(0, len(rects)): # calculate all landmarks _t_mat = [[p.x, p.y] for p in predictor(im, rects[i]).parts()] lm_list.append(numpy.matrix(_t_mat)) print("lm_passed") return lm_list # returns a list of landmarks
def detect(image: Image): image = np.asarray(image) h, w = image.shape[:2] image = resize_by_max(image, 361) actual_h, actual_w = image.shape[:2] faces_on_small = detector(image, 1) faces = dlib.rectangles() for face in faces_on_small: faces.append( dlib.rectangle(int(face.left() / actual_w * w + 0.5), int(face.top() / actual_h * h + 0.5), int(face.right() / actual_w * w + 0.5), int(face.bottom() / actual_h * h + 0.5))) return faces
def _get_face_descriptions(self, img_rgb: numpy.ndarray) -> list: """ Extract bounding boxes surrounding each face in given image and compute its description vector. :param img_rgb: numpy.ndarray - RGB pixel matrix of image. :return: list(tuple) - Description vector and pixel coordinates of top left / bottom right corner concerning all detected faces in image. """ # Compute bounding boxes. boxes = self.face_detector(img_rgb, self.resolution) # Make sure that the data type of received # bounding boxes is actually a dlib.rectangles # instance containing a number of dlib.rectangle # objects. The reason is that the CNN-based face # detection model returns a dlib.mmod_rectangles # instance which itself contains a number of # dlib.mmod_rectangle objects. The latter is just # a wrapper of a dlib.rectangle instance with a # confidence score. See # http://dlib.net/python/index.html#dlib.mmod_rectangle # for details. if isinstance(boxes, dlib.rectangles): bounding_boxes = boxes else: bounding_boxes = dlib.rectangles() for box in boxes: bounding_boxes.append(box.rect) face_descriptions = list() for box in bounding_boxes: # Compute position of facial features # concerning currently chosen face. facial_features = self.shape_predictor(img_rgb, box) # Compute corresponding description # vector and convert it to NumPy array. vector = self.face_descriptor.compute_face_descriptor( img_rgb, facial_features) description_vector = numpy.array( vector, dtype=settings.VECTOR_ENTRY_DATA_TYPE) # Get and adjust corner coordinates of # current bounding box because in rare # cases it may be that the received # coordinates lie outside the image. top_left = image.get_top_left(box, img_rgb) bottom_right = image.get_bottom_right(box, img_rgb) # Store description vector and its # bounding box coordinates as a tuple. face_descriptions.append( (description_vector, top_left, bottom_right)) return face_descriptions
def _array_to_dlib_rectangles(rects_array): """ Function to convert array of rectangles (in format [[x1,y1,w1,h1],[x2,y2,w2,h2],...]) to dlib regtangles objects. Usually input array is a results of OpenCV face detection and output dlib regtangles are used for landmark detection. :param rects_array: array with results of OpenCV face detection :return: dlib rectangles object """ rects_dlib = dlib.rectangles() for (left, top, right, bottom) in rects_array: rects_dlib.append(dlib.rectangle( int(left), int(top), int(right), int(bottom))) return rects_dlib
def detect_facemarks_coords(self, image, bboxes): gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rectangles = dlib.rectangles() rectangles.extend([ dlib.rectangle(left=bbox[0], top=bbox[1], right=bbox[2] + bbox[0], bottom=bbox[3] + bbox[1]) for bbox in bboxes ]) facemarks_coords = list() for rectangle in rectangles: facemarks = self.facemarks_predictor(gray_image, rectangle) facemarks_coords.append(self._facemarks_to_coords(facemarks)) return facemarks_coords
def getFaceDescriptors(img, rects, jitter=0): landmarks5 = getFaceLandmarks(img, rects) #chips = dlib.get_face_chips(img, landmarks5) descriptors = [] for l5 in landmarks5: c = dlib.get_face_chip(img, l5) new_rs = dlib.rectangles() new_rs.append(dlib.rectangle(0, 0, 149, 149)) l = getFaceLandmarks(c, new_rs, points=68) descriptors.append( np.array(faceRec.compute_face_descriptor(c, l[0], jitter))) #descriptors = [faceRec.compute_face_descriptor(c, getFaceLandmarks(c, # dlib.rectangle(0,0,149,149), points=68), jitter)for c in chips] # descriptors= [np.array(faceRec.compute_face_descriptor(img, l, jitter)) for l in landmarks] #directions = [vl.angleEstimator(img, face_utils.shape_to_np(l)) for l in landmarks] return descriptors
def __init__(self, rgb_image, detection, n_init, max_disappeared, max_age, use_correlation_tracker=True, feature=None): self.uuid = str(uuid.uuid4()) self.bbox = dlib.rectangle(int(detection.bbox.left()), int(detection.bbox.top()), int(detection.bbox.right()), int(detection.bbox.bottom())) self.class_label = detection.class_label self.k_past = dlib.rectangles() if use_correlation_tracker is True: self.tracker = dlib.correlation_tracker() self.tracker.start_track(rgb_image, self.bbox) else: self.tracker = None self.state = TrackState.TENTATIVE self.n_init = n_init self.max_disappeared = max_disappeared self.max_age = max_age self.translation = None self.rotation = None if feature is not None: self.feature = np.asarray(feature, dtype=np.float32) else: self.feature = None self.missed = 0 self.age = 1 self.hits = 1 self.since_update = 1
for f in sys.argv[2:]: print("Processing file: {}".format(f)) img = io.imread(f) # 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 = cnn_face_detector(img, 1) ''' This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects. These objects can be accessed by simply iterating over the mmod_rectangles object The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score. It is also possible to pass a list of images to the detector. - like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128) In this case it will return a mmod_rectangless object. This object behaves just like a list of lists and can be iterated over. ''' print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format( i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence)) rects = dlib.rectangles() rects.extend([d.rect for d in dets]) win.clear_overlay() win.set_image(img) win.add_overlay(rects) dlib.hit_enter_to_continue()