def match_two_images(self, thresh, score_rec, image_base64_1, image_base64_2): """ Match face in two images. Once a face matched, it will return True. :param image_base64_1: image encoded in base64 :param image_base64_2: image encoded in base64 :param score_rec: float, the score of detected faces should be larger than score_rec :param thresh: distance between face and matched face should be smaller than thresh :return:bool: True means face matched, False means face not matched. """ image_1 = resize(base64_to_image(image_base64_1)) image_2 = resize(base64_to_image(image_base64_2)) faces_1, scores_1, idx_1 = self.detector.run(image_1, 1, score_rec) faces_2, scores_2, idx_2 = self.detector.run(image_2, 1, score_rec) if len(faces_1) != 1 or len(faces_2) != 1: raise FaceNum shape_1 = self.sp(image_1, faces_1[0]) face_chip_1 = dlib.get_face_chip(image_1, shape_1) face_descriptor_1 = np.array(self.facerec.compute_face_descriptor(face_chip_1)) shape_2 = self.sp(image_2, faces_2[0]) face_chip_2 = dlib.get_face_chip(image_2, shape_2) face_descriptor_2 = np.array(self.facerec.compute_face_descriptor(face_chip_2)) distance = calculate_distance(face_descriptor_1, face_descriptor_2) if distance < thresh: return True # True means face matched return False # False means face not matched
def car_plate_match(self, image_base64): """ 现场监控图像提取车牌信息,在证件信息提取的结果中进行比对。 :param image_base64: image encoded in base64 :return: bool, True表示匹配,False表示不匹配 """ image = base64_to_image(image_base64) ocr_result = self.get_all(image) plate = [] for line in ocr_result: info = re.search('[\u4E00-\u9FA5][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]', line[-1][0]) if info: plate.append(info.group(0)) for key, value in self.tmp_result.items(): if value: if "车牌" in value.keys(): if value["车牌"] in plate: return True return False
def match_identity(self, feature_vector, thresh, score_rec, image_base64): """ Match the input feature vector and vector for each face in one image. Once a face matched, it will return True. :param score_rec: float, the score of detected faces should be larger than score_rec :param feature_vector: list, the feature vector to be matched :param thresh: distance between face and matched face should be smaller than thresh :param image_base64: image encoded in base64 :return: bool, True means face matched, False means face not matched. """ image = resize(base64_to_image(image_base64)) faces, scores, idx = self.detector.run(image, 1, score_rec) for face in faces: shape = self.sp(image, face) face_chip = dlib.get_face_chip(image, shape) face_descriptor = np.array(self.facerec.compute_face_descriptor(face_chip)) distance = calculate_distance(face_descriptor, np.array(feature_vector)) # print(distance) if distance < thresh: return True # True means face matched return False # False means face not matched
def face_register(self, image_base64, score_reg): """ Registers only one face in one picture. :param score_reg: float, the score of the face to be registered should be larger than score_reg :param image_base64: image encoded in base64 :return: list: the feature vector of the face in input image """ image = base64_to_image(image_base64) # print(image.shape[0], image.shape[1]) if image.shape[0] > 1000 or image.shape[1] > 1000: if image.shape[0] > image.shape[1]: proportion = 1000 / image.shape[0] else: proportion = 1000 / image.shape[1] image = cv2.resize(image, None, fx=proportion, fy=proportion, interpolation=cv2.INTER_AREA) faces, scores, idx = self.detector.run(image, 1, score_reg) if len(faces) != 1: raise FaceNum shape = self.sp(image, faces[0]) face_chip = dlib.get_face_chip(image, shape) feature_vector = list(self.facerec.compute_face_descriptor(face_chip)) return feature_vector
def recog_one(self, image_base64): """ Recognize one image, and to update tmp_result dict :param image_base64: image encoded in base64 :return: tmp_result: e.x. {"特种设备使用登记证": {"发证日期": "2018年8月1日"}, "中华人民共和国道路运输证": {"有效期": None, "经营范围": "危险化学品", "车牌": "鲁FBR932"}, "危险货物运输押运人员证": None, "中华人民共和国机动车驾驶证": None, "中华人民共和国机动车行驶证": None, "道路危险货物运输驾驶员证": None} :return: pos: int, to tell client witch certificate the image is. """ image = base64_to_image(image_base64) ocr_result = self.get_all(image) name = self.name_recog(ocr_result) if not name: raise NameRecFail pos = self.pos_dict[name] result = {} if name in self.config["人脸"]: feature = face.face_register(image_base64, 0.5) result["人脸"] = feature self.face_feature.append(feature) for key in self.config["证件"][name]: if key == "证件名": continue method = self.config["证件"][name][key] if isinstance(method, list): loc = get_location(method[0], ocr_result) result[key] = get_info_by_location(ocr_result, method[2], loc, method[1], 20) elif isinstance(method, str): res = [] for line in ocr_result: info = re.search(method, line[-1][0]) if info: res.append(info.group(0)) result[key] = res filter_one(result) self.tmp_result[name] = result return self.tmp_result, pos