def test_identifier(): print('test identifier:') detector = Detector() aligner = Aligner() identifier = Identifier() # load image image_color_A = Image.open('data/single.jpg').convert('RGB') image_gray_A = image_color_A.convert('L') image_color_B = Image.open('data/double.jpg').convert('RGB') image_gray_B = image_color_B.convert('L') # detect face in image faces_A = detector.detect(image_gray_A) faces_B = detector.detect(image_gray_B) draw_A = ImageDraw.Draw(image_color_A) draw_B = ImageDraw.Draw(image_color_B) if len(faces_A) and len(faces_B): landmarks_A = aligner.align(image_gray_A, faces_A[0]) featA = identifier.extract_feature_with_crop(image_color_A, landmarks_A) print(len(featA)) draw_A.rectangle([(faces_A[0].left, faces_A[0].top), (faces_A[0].right, faces_A[0].bottom)], outline='green') sim_list = [] for face in faces_B: landmarks_B = aligner.align(image_gray_B, face) featB = identifier.extract_feature_with_crop( image_color_B, landmarks_B) sim = identifier.calc_similarity(featA, featB) sim_list.append(sim) print('sim: {}'.format(sim_list)) index = np.argmax(sim_list) for i, face in enumerate(faces_B): color = 'green' if i == index else 'red' draw_B.rectangle([(face.left, face.top), (face.right, face.bottom)], outline=color) image_color_A.show() image_color_B.show() identifier.release() aligner.release() detector.release()
def test_cropface(model_path, img_name): if model_path is None: print('test_aligner: Please specify the path to the model folder') return if img_name is None: print('test_aligner: Please specify the path to the image file') return assert os.path.isdir( model_path ) is True, 'test_aligner: The model file path should be a folder' assert os.path.isfile(img_name) is True, 'test_aligner: no such file' model_path_fd = model_path + '/seeta_fd_frontal_v1.0.bin' model_path_fa = model_path + '/seeta_fa_v1.1.bin' model_path_fr = model_path + '/seeta_fr_v1.0.bin' detector = Detector(model_path_fd) detector.set_min_face_size(30) aligner = Aligner(model_path_fa) identifier = Identifier(model_path_fr) image_color = Image.open(img_name).convert('RGB') image_gray = image_color.convert('L') import cv2 faces = detector.detect(image_gray) for face in faces: landmarks = aligner.align(image_gray, face) crop_face = identifier.crop_face(image_color, landmarks) Image.fromarray(crop_face).show() identifier.release() aligner.release() detector.release()
def test_identifier(): print('test identifier:') # load model detector = Detector() aligner = Aligner() identifier = Identifier() # load image image_color_A = cv2.imread('data/single.jpg', cv2.IMREAD_COLOR) image_gray_A = cv2.cvtColor(image_color_A, cv2.COLOR_BGR2GRAY) image_color_B = cv2.imread('data/double.jpg', cv2.IMREAD_COLOR) image_gray_B = cv2.cvtColor(image_color_B, cv2.COLOR_BGR2GRAY) # detect face in image faces_A = detector.detect(image_gray_A) faces_B = detector.detect(image_gray_B) if len(faces_A) and len(faces_B): landmarks_A = aligner.align(image_gray_A, faces_A[0]) featA = identifier.extract_feature_with_crop(image_color_A, landmarks_A) cv2.rectangle(image_color_A, (faces_A[0].left, faces_A[0].top), (faces_A[0].right, faces_A[0].bottom), (0, 255, 0), thickness=2) sim_list = [] for face in faces_B: landmarks_B = aligner.align(image_gray_B, face) featB = identifier.extract_feature_with_crop( image_color_B, landmarks_B) sim = identifier.calc_similarity(featA, featB) sim_list.append(sim) print('sim: {}'.format(sim_list)) index = np.argmax(sim_list) for i, face in enumerate(faces_B): color = (0, 255, 0) if i == index else (0, 0, 255) cv2.rectangle(image_color_B, (face.left, face.top), (face.right, face.bottom), color, thickness=2) cv2.imshow('single', image_color_A) cv2.imshow('double', image_color_B) cv2.waitKey(0) identifier.release() aligner.release() detector.release()
def test_recognition(img_path,db_path): if not os.path.isfile(img_path) or not os.path.isdir(db_path): print("indicated path doesn't exist!");return # load model detector = Detector(DET_MODEL_PATH) aligner = Aligner(ALI_MODEL_PATH) identifier = Identifier(REC_MODEL_PATH) # load test image image_color_A = cv2.imread(img_path, cv2.IMREAD_COLOR) image_gray_A = cv2.cvtColor(image_color_A, cv2.COLOR_BGR2GRAY) faces_A = detector.detect(image_gray_A) # load database for fn in os.listdir(db_path): fp = os.path.join(db_path,fn) if not os.path.isfile(fp): continue image_color_B = cv2.imread(fp, cv2.IMREAD_COLOR) image_gray_B = cv2.cvtColor(image_color_B, cv2.COLOR_BGR2GRAY) # detect face in image faces_B = detector.detect(image_gray_B) if len(faces_A) and len(faces_B): landmarks_A = aligner.align(image_gray_A, faces_A[0]) featA = identifier.extract_feature_with_crop(image_color_A, landmarks_A) # cv2.rectangle(image_color_A, (faces_A[0].left, faces_A[0].top), (faces_A[0].right, faces_A[0].bottom), (0,255,0), thickness=2) sim_list = [] for face in faces_B: landmarks_B = aligner.align(image_gray_B, face) featB = identifier.extract_feature_with_crop(image_color_B, landmarks_B) sim = identifier.calc_similarity(featA, featB) sim_list.append(sim) print('sim: {}'.format(sim_list)) # index = np.argmax(sim_list) for i, face in enumerate(faces_B): color = (0,255,0) if sim_list[i] > 0.5 else (0,0,255) cv2.rectangle(image_color_B, (face.left, face.top), (face.right, face.bottom), color, thickness=2) # cv2.imshow('test', resize(image_color_A)) cv2.imshow('double', resize(image_color_B)) cv2.waitKey(0) release(aligner,detector,identifier)
def test_cropface(): detector = Detector() detector.set_min_face_size(30) aligner = Aligner() identifier = Identifier() image_color = Image.open('data/chloecalmon.png').convert('RGB') image_gray = image_color.convert('L') faces = detector.detect(image_gray) for face in faces: landmarks = aligner.align(image_gray, face) crop_face = identifier.crop_face(image_color, landmarks) Image.fromarray(crop_face).show() identifier.release() aligner.release() detector.release()
def test_cropface(): detector = Detector('SeetaFaceEngine/model/seeta_fd_frontal_v1.0.bin') detector.set_min_face_size(30) aligner = Aligner('SeetaFaceEngine/model/seeta_fa_v1.1.bin') identifier = Identifier('SeetaFaceEngine/model/seeta_fr_v1.0.bin') image_color = Image.open('data/chloecalmon.png').convert('RGB') image_gray = image_color.convert('L') import cv2 faces = detector.detect(image_gray) for face in faces: landmarks = aligner.align(image_gray, face) crop_face = identifier.crop_face(image_color, landmarks) Image.fromarray(crop_face).show() identifier.release() aligner.release() detector.release()
class SeetaFaceExtractor(BaseFeatureExtractor): def __init__(self): self.detector = Detector() self.detector.set_min_face_size(30) self.aligner = Aligner() self.identifier = Identifier() def extact(self, image_path, is_one_face=False): image = cv2.imread(image_path) if image is None: print('Unable to load image: {}'.format(image_path)) return None image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if is_one_face: bb1 = self.detector.detect(image_gray) bbs = [bb1] else: bbs = self.detector.detect(image_gray) if len(bbs) > 1: print('More than two faces in {}'.format(image_path)) return None if len(bbs) == 0 or (is_one_face and bb1 is None): return None reps = [] for bb in bbs: aligned_face = self.aligner.align(image_gray, bb) if aligned_face is None: return None rep = self.identifier.extract_feature_with_crop(image, aligned_face) return rep reps.append(rep) # 因为已经明确一张图只有一个人 if is_one_face: return reps[0] else: return reps
def test_aligner(): print('test aligner:') # load model detector = Detector() detector.set_min_face_size(30) aligner = Aligner() image_color = Image.open('data/chloecalmon.png').convert('RGB') image_gray = image_color.convert('L') faces = detector.detect(image_gray) draw = ImageDraw.Draw(image_color) draw.ellipse((0, 0, 40, 80), fill=128) for face in faces: landmarks = aligner.align(image_gray, face) for point in landmarks: x1, y1 = point[0] - 2, point[1] - 2 x2, y2 = point[0] + 2, point[1] + 2 draw.ellipse((x1, y1, x2, y2), fill='red') image_color.show() aligner.release() detector.release()
def test_aligner(): print('test aligner:') # load model detector = Detector() detector.set_min_face_size(30) aligner = Aligner() image_color = cv2.imread('data/chloecalmon.png', cv2.IMREAD_COLOR) image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY) faces = detector.detect(image_gray) for face in faces: landmarks = aligner.align(image_gray, face) for point in landmarks: cv2.circle(image_color, point, 1, (0, 255, 0), 2) cv2.imshow('test aligner', image_color) cv2.waitKey(0) aligner.release() detector.release()
def test_aligner(model_path, img_name): print('test aligner:') if model_path is None: print('test_aligner: Please specify the path to the model folder') return if img_name is None: print('test_aligner: Please specify the path to the image file') return assert os.path.isdir( model_path ) is True, 'test_aligner: The model file path should be a folder' assert os.path.isfile(img_name) is True, 'test_aligner: no such file' # load model model_path_fd = model_path + '/seeta_fd_frontal_v1.0.bin' detector = Detector(model_path_fd) detector.set_min_face_size(30) model_path_fa = model_path + '/seeta_fa_v1.1.bin' aligner = Aligner(model_path_fa) image_color = cv2.imread(img_name, cv2.IMREAD_COLOR) image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY) faces = detector.detect(image_gray) for face in faces: landmarks = aligner.align(image_gray, face) for point in landmarks: cv2.circle(image_color, point, 1, (0, 255, 0), 2) cv2.imshow('test aligner', image_color) cv2.waitKey(0) aligner.release() detector.release()
def test_aligner(model_path, img_name): print('test aligner:') if model_path is None: print('test_aligner: Please specify the path to the model folder') return if img_name is None: print('test_aligner: Please specify the path to the image file') return assert os.path.isdir( model_path ) is True, 'test_aligner: The model file path should be a folder' assert os.path.isfile(img_name) is True, 'test_aligner: no such file' # load model model_path_fd = model_path + '/seeta_fd_frontal_v1.0.bin' detector = Detector(model_path_fd) detector.set_min_face_size(30) model_path_fa = model_path + '/seeta_fa_v1.1.bin' aligner = Aligner(model_path_fa) image_color = Image.open(img_name).convert('RGB') image_gray = image_color.convert('L') print(np.array(image_gray)) faces = detector.detect(image_gray) draw = ImageDraw.Draw(image_color) draw.ellipse((0, 0, 40, 80), fill=128) for face in faces: landmarks = aligner.align(image_gray, face) for point in landmarks: x1, y1 = point[0] - 2, point[1] - 2 x2, y2 = point[0] + 2, point[1] + 2 draw.ellipse((x1, y1, x2, y2), fill='red') image_color.show() aligner.release() detector.release()
class FaceFeatureExtract(): ''' 提取图片中人脸特征并存储 2018/3/30 V1.0 ''' images_name = [] isShow = None detector = None aligner = None identifier = None def __init__(self, treshhold=20, single=False, logfile=None, pics_dir='data', isShow=True): ''' single: 是否为处理单个图片的模式 logfile: 日志文件路径 pics_dir:若single为False则图片文件夹路径 若single为True则图片路径 isShow: 显示图片处理过程 ''' self.detector = Detector() self.aligner = Aligner() self.identifier = Identifier() self.detector.set_min_face_size(treshhold) if not single: all_files = os.listdir(pics_dir) for f in all_files: if not os.path.isdir(f): self.images_name.append(os.path.join(pics_dir,f)) else: self.images_name.append(pics_dir) self.images_name.sort() if logfile == None: logging.basicConfig(level=logging.INFO, format='%(levelname)s-%(lineno)d-%(asctime)s %(message)s', filename=logfile) else:#print to screen logging.basicConfig(level=logging.INFO, format='%(levelname)s-%(lineno)d-%(asctime)s [FaceFeatureExtract]: %(message)s') self.isShow = isShow def detect(self): ''' 返回一个字典数组:长度等于图片数。例如: [ { 'name':'1.jpg', 'features':[[1,23,...],[2,3,....]], 'landmarks':[[1,2,3,4],[5,6,7,8]] },... ] ''' pic_dic_list = [] for pic in self.images_name: image = Image.open(pic).convert('RGB') lands, faces, feats = self.__extract_features(image) logging.info("detecting %s: find %d faces"%(pic, len(lands))) if self.isShow: draw = ImageDraw.Draw(image) for i, face in enumerate(faces): draw.rectangle([face.left, face.top, face.right, face.bottom], outline='red') image.show() pic_dic = {} pic_dic['name'] = pic pic_dic['landmarks'] = lands pic_dic['feats'] = feats pic_dic_list.append(pic_dic) return pic_dic_list def __extract_features(self, img): # detect face in image image_gray = img.convert('L') faces = self.detector.detect(image_gray) feats = [] landmarks = [] for detect_face in faces: landmark = self.aligner.align(image_gray, detect_face) landmarks.append(landmark) feat = self.identifier.extract_feature_with_crop(img, landmark) feats.append(feat) return landmarks, faces, feats def release(self): ''' 释放资源 ''' self.detector.release() self.aligner.release() self.identifier.release()
class FaceBoxes(object): def __init__(self): self.args = argparse() gpu_id = int(self.args.gpu) if gpu_id < 0: caffe.set_mode_cpu() else: caffe.set_device(gpu_id) assert os.path.exists( self.args.deploy_det), 'file {} is not found'.format( self.args.deploy_det) assert os.path.isfile( self.args.weights_det), 'file {} is not found'.format( self.args.weights_det) self.net = caffe.Net(self.args.deploy_det, self.args.weights_det, caffe.TEST) # self.Onet_p = ONet_Points(self.args.deploy_det_land, self.args.weights_det_land) print('\t deploy_det:{} is used'.format(self.args.deploy_det)) print('\t weights_det:{} is used'.format(self.args.weights_det)) self.aligner = Aligner(self.args.seeta_land) def detect(self, img): h, w = img.shape[:2] height_new, width_new = int(self.args.image_scale * h), int( self.args.image_scale * w) # height_new = width_new = 1024 self.net.blobs['data'].reshape(1, 3, height_new, width_new) im = cv2.resize(img, (width_new, height_new)) transformed_image = 0.00390625 * (im.astype(np.float32, copy=False) - np.array([104, 117, 123])).transpose( (2, 0, 1)) self.net.blobs['data'].data[...] = transformed_image out = self.net.forward() boxes = out['detection_out'][0, 0, :, 3:7] * np.array([w, h, w, h]) # cls = out['detection_out'][0, 0, :, 1] conf = out['detection_out'][0, 0, :, 2] bboxes = np.hstack((boxes, conf[:, np.newaxis])).astype(np.float32, copy=False) bboxes = [bbox for bbox in bboxes if bbox[4] > 0] if len(bboxes) == 0: return None gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) landmarks = [] for box in bboxes: face = Face() face.left = box[0] face.top = box[1] face.right = box[2] face.bottom = box[3] face.score = box[4] landmark = self.aligner.align(gray, face) # seetaface1 landmarks landmarks.append(landmark) return np.array(bboxes), np.array(landmarks) def maxFace(self, image, boxes, points): assert image is not None if len(boxes) == 0: return None else: max_box_area = 0 idx_tmp = 0 for idx, box in enumerate(boxes): box_area = (box[2] - box[0]) * (box[3] - box[1]) if max_box_area < box_area: max_box_area = box_area idx_tmp = idx p = points[idx_tmp] maxface = get_norm2(image, boxes[idx_tmp], p, image_size=self.args.norm_size) # maxface = get_norm2(image, boxes[idx_tmp], p, image_size='118, 118') # maxface = get_norm(image, p, cropSize=(112, 112), ec_mc_y=48, ec_y=40) return maxface, idx_tmp
def recog(request): try: global avdata, name_img detector = Detector('SeetaFaceEngine/model/seeta_fd_frontal_v1.0.bin') aligner = Aligner('SeetaFaceEngine/model/seeta_fa_v1.1.bin') identifier = Identifier('SeetaFaceEngine/model/seeta_fr_v1.0.bin') detector.set_min_face_size(30) if request.method == 'POST': path = tempIMG(img=request.FILES['img'], ) path.save() image_color_A = imread(str(path.img)) image_gray_A = cv2.cvtColor(image_color_A, cv2.COLOR_BGR2GRAY) faces_A = detector.detect(image_gray_A) cv2.rectangle(image_color_A, (faces_A[0].left, faces_A[0].top), (faces_A[0].right, faces_A[0].bottom), (0, 255, 0), thickness=2) cv2.imwrite('facerecog/static/facerecog/' + 'img.jpg', image_color_A) length_list = [] if len(faces_A) or 0: landmarks_A = aligner.align(image_gray_A, faces_A[0]) feat_test = identifier.extract_feature_with_crop( image_color_A, landmarks_A) average_sim_list = [] name_list = [] sim_list = [] for cla in avdata: simlist = [] name_list.append(cla) weight = 0 for fea in avdata[cla]: sim = feat_match(feat_test, fea) simlist.append(sim) if sim > 0.5: simlist.append(sim) if sim > 0.55: simlist.append(sim) if sim > 0.6: simlist.append(sim) sim_list.append(simlist) if len(simlist) == 0: average_sim_list.append(0) else: average_sim = sum(simlist) / len(simlist) average_sim_list.append(average_sim) # print(average_sim_list) max_index = average_sim_list.index(max(average_sim_list)) sort_list = sorted(average_sim_list) result_list = [] for j in range(5): result_list.append(name_list[average_sim_list.index( sort_list[-(j + 1)])]) print(name_list[max_index]) print(average_sim_list[max_index]) identifier.release() aligner.release() detector.release() name = str(request.FILES['img']) print(name) file_name = [] file_name.append(name) print(result_list) name_img = np.load('name_img.npy').item() print(name_img[result_list[0]]) img_link = [] for name in result_list: img_link.append(name_img[name]) content = { 'result_list': result_list, 'file_name': file_name, 'img_link': img_link } # return HttpResponse(json.dumps(content,ensure_ascii=False)) return render(request, 'facerecog/match.html', content) except: return HttpResponse('請指定有人臉的檔案!')
def test_identifier(model_path, img1_name, img2_name): print('test identifier:') if model_path is None: print('test_identifier: Please specify the path to the model folder') return if img1_name is None: print('test_identifier: Please specify the path to the image file1') return if img2_name is None: print('test_identifier: Please specify the path to the image file2') return assert os.path.isdir( model_path ) is True, 'test_identifier: The model file path should be a folder' assert os.path.isfile( img1_name) is True, 'test_identifier: no such image file1' assert os.path.isfile( img2_name) is True, 'test_identifier: no such image file2' # load model model_path_fd = model_path + '/seeta_fd_frontal_v1.0.bin' model_path_fa = model_path + '/seeta_fa_v1.1.bin' model_path_fr = model_path + '/seeta_fr_v1.0.bin' detector = Detector(model_path_fd) aligner = Aligner(model_path_fa) identifier = Identifier(model_path_fr) # load image image_color_A = cv2.imread(img1_name, cv2.IMREAD_COLOR) image_gray_A = cv2.cvtColor(image_color_A, cv2.COLOR_BGR2GRAY) image_color_B = cv2.imread(img2_name, cv2.IMREAD_COLOR) image_gray_B = cv2.cvtColor(image_color_B, cv2.COLOR_BGR2GRAY) # detect face in image faces_A = detector.detect(image_gray_A) faces_B = detector.detect(image_gray_B) if len(faces_A) and len(faces_B): landmarks_A = aligner.align(image_gray_A, faces_A[0]) featA = identifier.extract_feature_with_crop(image_color_A, landmarks_A) cv2.rectangle(image_color_A, (faces_A[0].left, faces_A[0].top), (faces_A[0].right, faces_A[0].bottom), (0, 255, 0), thickness=2) sim_list = [] for face in faces_B: landmarks_B = aligner.align(image_gray_B, face) featB = identifier.extract_feature_with_crop( image_color_B, landmarks_B) sim = identifier.calc_similarity(featA, featB) sim_list.append(sim) print('sim: {}'.format(sim_list)) index = np.argmax(sim_list) for i, face in enumerate(faces_B): color = (0, 255, 0) if i == index else (0, 0, 255) cv2.rectangle(image_color_B, (face.left, face.top), (face.right, face.bottom), color, thickness=2) cv2.imshow('single', image_color_A) cv2.imshow('double', image_color_B) cv2.waitKey(0) identifier.release() aligner.release() detector.release()
def test_identifier(model_path, img1_name, img2_name): print('test identifier:') if model_path is None: print('test_identifier: Please specify the path to the model folder') return if img1_name is None: print('test_identifier: Please specify the path to the image file1') return if img2_name is None: print('test_identifier: Please specify the path to the image file2') return assert os.path.isdir( model_path ) is True, 'test_identifier: The model file path should be a folder' assert os.path.isfile( img1_name) is True, 'test_identifier: no such image file1' assert os.path.isfile( img2_name) is True, 'test_identifier: no such image file2' model_path_fd = model_path + '/seeta_fd_frontal_v1.0.bin' model_path_fa = model_path + '/seeta_fa_v1.1.bin' model_path_fr = model_path + '/seeta_fr_v1.0.bin' detector = Detector(model_path_fd) detector.set_min_face_size(30) aligner = Aligner(model_path_fa) identifier = Identifier(model_path_fr) # load image image_color_A = Image.open(img1_name).convert('RGB') image_gray_A = image_color_A.convert('L') image_color_B = Image.open(img2_name).convert('RGB') image_gray_B = image_color_B.convert('L') # detect face in image faces_A = detector.detect(image_gray_A) faces_B = detector.detect(image_gray_B) draw_A = ImageDraw.Draw(image_color_A) draw_B = ImageDraw.Draw(image_color_B) if len(faces_A) and len(faces_B): landmarks_A = aligner.align(image_gray_A, faces_A[0]) featA = identifier.extract_feature_with_crop(image_color_A, landmarks_A) draw_A.rectangle([(faces_A[0].left, faces_A[0].top), (faces_A[0].right, faces_A[0].bottom)], outline='green') sim_list = [] for face in faces_B: landmarks_B = aligner.align(image_gray_B, face) featB = identifier.extract_feature_with_crop( image_color_B, landmarks_B) sim = identifier.calc_similarity(featA, featB) sim_list.append(sim) print('sim: {}'.format(sim_list)) index = np.argmax(sim_list) for i, face in enumerate(faces_B): color = 'green' if i == index else 'red' draw_B.rectangle([(face.left, face.top), (face.right, face.bottom)], outline=color) image_color_A.show() image_color_B.show() identifier.release() aligner.release() detector.release()