def extract_region(self, image): DIMS = image.shape blob = cv2.dnn.blobFromImage( image, 1.0, (300, 300) # , [104, 117, 123], False, False ) self.net.setInput(blob) detections = self.net.forward()[0, 0, :, :] regions = [] for i in range(detections.shape[0]): if detections[i, 2] >= self.threshold: reg = region.RectangleRegion( detections[i, 3] * DIMS[1] - self.margin / 2, detections[i, 4] * DIMS[0] - self.margin / 2, (detections[i, 5] - detections[i, 3]) * DIMS[1] + self.margin, (detections[i, 6] - detections[i, 4]) * DIMS[0] + self.margin, detections[i, 2], ) logging.debug("Face {} detected: {}".format(i, str(reg))) regions.append(reg) return regions, detections
def extract_region( self, image, threshold=0.9, # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) ): bbs, points = mtcnn_tensorflow.detect_face( image, self.min_size, self.pnet, self.rnet, self.onet, self.threshold, self.factor, ) regions = [ region.RectangleRegion( bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1], bb[4] ) for bb in bbs if bb[4] >= threshold ] return regions, (bbs, points)
def predict(self, X: np.ndarray, threshold=0.95, extract_image=False) -> datum.Datum: ret = self.detector.detect_face(X) if ret is None: return [] bbox, points = ret if bbox.shape[0] == 0: return [] data = [] for bb, point in zip(*ret): if bb[4] >= threshold: dat = datum.Datum( region=region.RectangleRegion(bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1], bb[4]), keypoints=point, ) if extract_image: point = point.reshape((2, 5)).T aligned = align_mtcnn.align(X, bb, point, image_size="112,112") dat.image = aligned data.append(dat) return data
def extract_region(self, image): raw_detection = self.face_det(image, 0) logger.info("Number of faces detected: {}".format(len(raw_detection))) regions = [ region.RectangleRegion( i.rect.left(), i.rect.top(), i.rect.right() - i.rect.left(), i.rect.bottom() - i.rect.top(), ) for i in raw_detection ] return regions, raw_detection
def update(self, image): res = [] rem_list = [] for i, tr in enumerate(self.tracklets): retval, bbox = tr.tracker.update(image) if retval: reg = region.RectangleRegion( bbox[0], bbox[1], bbox[2], bbox[3] ) res.append(reg) else: # DONE Destroy tracker condition logger.debug("Tracklet face retrieval faillure.") tr.ttd -= 1 if tr.ttd == 0: rem_list.append(i) # Remove all tracklets where ttd == 0 for i in rem_list: self.tracklets.pop(i) return res
def parse_region_line(line, type=2): contents = line[:-1].split() if type == 1: # Rectangle dat = region.RectangleRegion( float(contents[0]), float(contents[1]), float(contents[2]), float(contents[3]), float(contents[4]), ) elif type == 2: # Ellipse dat = region.EllipseRegion( float(contents[0]), float(contents[1]), float(contents[2]), float(contents[3]), float(contents[4]), float(contents[5]), ) else: raise ValueError() return dat
def openpose_face_detector(posePtr, threshold): point_top_left = np.zeros(2) face_size = 0.0 score = 0.0 points_used = set() neckScoreAbove = posePtr[1, 2] > threshold headNoseScoreAbove = posePtr[0, 2] > threshold lEarScoreAbove = posePtr[16, 2] > threshold rEarScoreAbove = posePtr[17, 2] > threshold lEyeScoreAbove = posePtr[14, 2] > threshold rEyeScoreAbove = posePtr[15, 2] > threshold counter = 0.0 if neckScoreAbove and headNoseScoreAbove: if (lEyeScoreAbove == lEarScoreAbove and rEyeScoreAbove == rEarScoreAbove and lEyeScoreAbove != rEyeScoreAbove): if lEyeScoreAbove: point_top_left += (posePtr[14, 0:2] + posePtr[16, 0:2] + posePtr[0, 0:2]) / 3.0 face_size += 0.85 * (dist(posePtr[14, 0:2], posePtr[16, 0:2]) + dist(posePtr[0, 0:2], posePtr[16, 0:2]) + dist(posePtr[14, 0:2], posePtr[0, 0:2])) points_used = points_used.union([0, 14, 16]) else: point_top_left += (posePtr[15, 0:2] + posePtr[17, 0:2] + posePtr[0, 0:2]) / 3.0 face_size += 0.85 * (dist(posePtr[15, 0:2], posePtr[17, 0:2]) + dist(posePtr[0, 0:2], posePtr[17, 0:2]) + dist(posePtr[15, 0:2], posePtr[0, 0:2])) points_used = points_used.union([0, 15, 17]) else: point_top_left += (posePtr[1, 0:2] + posePtr[0, 0:2]) / 2.0 face_size += 2.0 * dist(posePtr[1, 0:2], posePtr[0, 0:2]) points_used = points_used.union([0, 1]) counter += 1.0 if lEyeScoreAbove and rEyeScoreAbove: point_top_left += (posePtr[14, 0:2] + posePtr[15, 0:2]) / 2.0 face_size += 3.0 * dist(posePtr[14, 0:2], posePtr[15, 0:2]) counter += 1.0 points_used = points_used.union([14, 15]) if lEarScoreAbove and rEarScoreAbove: point_top_left += (posePtr[16, 0:2] + posePtr[17, 0:2]) / 2.0 face_size += 2.0 * dist(posePtr[16, 0:2], posePtr[17, 0:2]) counter += 1.0 points_used = points_used.union([16, 17]) if counter > 0: point_top_left /= counter face_size /= counter score = np.mean(posePtr[list(points_used), 2]) return region.RectangleRegion( point_top_left[0] - face_size / 2, point_top_left[1] - face_size / 2, face_size, face_size, score, )
def openpose_face_detector2(posePtr, threshold): point_top_left = np.zeros(2) face_size = [0.0, 0.0] # (width, height) score = np.mean(posePtr[[0, 1, 14, 15, 16, 17], 2]) l_true = None r_true = None neckScoreAbove = posePtr[1, 2] > threshold headNoseScoreAbove = posePtr[0, 2] > threshold lEarScoreAbove = posePtr[16, 2] > threshold rEarScoreAbove = posePtr[17, 2] > threshold lEyeScoreAbove = posePtr[14, 2] > threshold rEyeScoreAbove = posePtr[15, 2] > threshold counter = 0.2 # Leftmost point if lEarScoreAbove: l_true = posePtr[16, 0:2] # Rightmost point if rEarScoreAbove: r_true = posePtr[17, 0:2] counter = 0.0 if headNoseScoreAbove: if (lEyeScoreAbove == lEarScoreAbove and rEyeScoreAbove == rEarScoreAbove and lEyeScoreAbove != rEyeScoreAbove): if lEyeScoreAbove: point_top_left += (posePtr[14, 0:2] + posePtr[16, 0:2] + posePtr[0, 0:2]) / 3.0 face_size[1] += 0.85 * ( dist(posePtr[14, 0:2], posePtr[16, 0:2]) + dist(posePtr[0, 0:2], posePtr[16, 0:2]) + dist(posePtr[14, 0:2], posePtr[0, 0:2])) face_size[0] += 0.85 * ( dist(posePtr[14, 0:2], posePtr[16, 0:2]) + dist(posePtr[0, 0:2], posePtr[14, 0:2])) else: point_top_left += (posePtr[15, 0:2] + posePtr[17, 0:2] + posePtr[0, 0:2]) / 3.0 face_size[1] += 0.85 * ( dist(posePtr[15, 0:2], posePtr[17, 0:2]) + dist(posePtr[0, 0:2], posePtr[17, 0:2]) + dist(posePtr[15, 0:2], posePtr[0, 0:2])) face_size[0] += 0.85 * ( dist(posePtr[15, 0:2], posePtr[17, 0:2]) + dist(posePtr[0, 0:2], posePtr[15, 0:2])) counter += 1.0 elif neckScoreAbove: point_top_left += posePtr[0, 0:2] face_size[1] += 1.0 * dist(posePtr[1, 0:2], posePtr[0, 0:2]) face_size[0] += 0.0 * dist(posePtr[1, 0:2], posePtr[0, 0:2]) counter += 1.0 if lEyeScoreAbove and rEyeScoreAbove: point_top_left += (posePtr[14, 0:2] + posePtr[15, 0:2]) / 2.0 face_size[1] += 4.0 * dist(posePtr[14, 0:2], posePtr[15, 0:2]) face_size[0] += 3.0 * dist(posePtr[14, 0:2], posePtr[15, 0:2]) counter += 1.0 if lEarScoreAbove and rEarScoreAbove: point_top_left += (posePtr[16, 0:2] + posePtr[17, 0:2]) / 2.0 face_size[1] += 1.2 * dist(posePtr[16, 0:2], posePtr[17, 0:2]) face_size[0] += dist(posePtr[16, 0:2], posePtr[17, 0:2]) counter += 1.0 if counter > 0: point_top_left /= counter face_size[0] /= counter face_size[1] /= counter if l_true is not None: ll = l_true[0] face_size[0] = face_size[0] - (ll - (point_top_left[0] - face_size[0] / 2)) else: ll = point_top_left[0] - face_size[0] / 2 # if r_true is not None: rr = r_true[0] face_size[0] = rr - ll return region.RectangleRegion( ll, point_top_left[1] - face_size[1] / 2, face_size[0], face_size[1], score, )