Example #1
0
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()
Example #2
0
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()
Example #3
0
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)
Example #4
0
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
Example #5
0
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()
Example #6
0
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('請指定有人臉的檔案!')
Example #7
0
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()
Example #8
0
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()