コード例 #1
0
def test_detector(basepath, path):
    print('test detector:')
    # load model
    # print(basepath)

    detector = Detector()

    detector.set_min_face_size(30)

    image_color = cv2.imread(path, cv2.IMREAD_COLOR)
    image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
    faces = detector.detect(image_gray)
    face_ls = []

    for i, face in enumerate(faces):
        tmp = image_color[face.top:face.bottom, face.left:face.right]
        cv2.imwrite(basepath + str(i) + ".png", tmp)
        face_ls.append(str(i) + ".png")

        #print('({0},{1},{2},{3}) score={4}'.format(face.left, face.top, face.right, face.bottom, face.score))
        #cv2.rectangle(image_color, (face.left, face.top), (face.right, face.bottom), (0,255,0), thickness=2)
        #cv2.putText(image_color, str(i), (face.left, face.bottom),cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), thickness=1)
    #cv2.imshow('test', image_color)
    #cv2.waitKey(0)

    detector.release()

    return face_ls
コード例 #2
0
def test_detector():
    print('test detector:')
    # load model
    detector = Detector()
    detector.set_min_face_size(30)

    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 i, face in enumerate(faces):
        print('({0},{1},{2},{3}) score={4}'.format(face.left, face.top,
                                                   face.right, face.bottom,
                                                   face.score))
        cv2.rectangle(image_color, (face.left, face.top),
                      (face.right, face.bottom), (0, 255, 0),
                      thickness=2)
        cv2.putText(image_color,
                    str(i), (face.left, face.bottom),
                    cv2.FONT_HERSHEY_COMPLEX,
                    1, (0, 255, 0),
                    thickness=1)
    cv2.imshow('test', image_color)
    cv2.waitKey(0)

    detector.release()
コード例 #3
0
def test_detector(model_path, img_name):
    print('test detector:')
    if model_path is None:
        print('test_detector: Please specify the path to the model folder')
        return
    if img_name is None:
        print('test_detector: Please specify the path to the image file')
        return
    assert os.path.isdir(
        model_path
    ) is True, 'test_detector: The model file path should be a folder'
    assert os.path.isfile(img_name) is True, 'test_detector: 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)

    image_color = Image.open(img_name).convert('RGB')
    image_gray = image_color.convert('L')
    faces = detector.detect(image_gray)
    draw = ImageDraw.Draw(image_color)
    for i, face in enumerate(faces):
        print('({0},{1},{2},{3}) score={4}'.format(face.left, face.top,
                                                   face.right, face.bottom,
                                                   face.score))
        draw.rectangle([face.left, face.top, face.right, face.bottom])
    image_color.show()
    detector.release()
コード例 #4
0
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()
コード例 #5
0
ファイル: face_detect.py プロジェクト: fangxu622/face_detect
def face_dect(video, scale, skipFrame, modelPath):

    cap = cv2.VideoCapture(video)
    detector = Detector(modelPath)
    detector.set_min_face_size(30)
    i = 0
    while (cap.isOpened()):
        ret, frame = cap.read()
        i = i + 1
        # print(ret)
        if not (i % skipFrame == 0):
            pass
        gray_1 = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        gray = cv2.resize(gray_1, None, fx=scale, fy=scale, interpolation=2)
        faces = detector.detect(gray)
        for i, face in enumerate(faces):
            # print('({0},{1},{2},{3}) score={4}'.format(face.left, face.top, face.right, face.bottom, face.score))
            cv2.rectangle(frame,
                          (int(face.left / scale), int(face.top / scale)),
                          (int(face.right / scale), int(face.bottom / scale)),
                          (0, 255, 0),
                          thickness=1)

            # cv2.putText(gray, str(i), (face.left, face.bottom), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0),
            #         thickness=1)
        cv2.imshow('frame', frame)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
コード例 #6
0
 def detect_face(self):
     detector = Detector()
     detector.set_min_face_size(self.min_face_size)
     print('Detecting and clipping faces =>>>')
     self.faces = detector.detect(self.image_grey)
     self.num_faces = len(self.faces)
     print('%d faces detected' % self.num_faces)
     detector.release()
コード例 #7
0
def test_detector():
    print('test detector:')
    # load model
    detector = Detector()
    detector.set_min_face_size(30)

    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)
    for i, face in enumerate(faces):
        print('({0},{1},{2},{3}) score={4}'.format(face.left, face.top,
                                                   face.right, face.bottom,
                                                   face.score))
        draw.rectangle([face.left, face.top, face.right, face.bottom])
    image_color.show()
    detector.release()
コード例 #8
0
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()
コード例 #9
0
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()
コード例 #10
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
コード例 #11
0
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()
コード例 #12
0
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()
コード例 #13
0
def test_detector(model_path, img_name):
    print('test detector:')
    if model_path is None:
        print('test_detector: Please specify the path to the model folder')
        return
    if img_name is None:
        print('test_detector: Please specify the path to the image file')
        return
    assert os.path.isdir(
        model_path
    ) is True, 'test_detector: The model file path should be a folder'
    assert os.path.isfile(img_name) is True, 'test_detector: 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)

    image_color = cv2.imread(img_name, cv2.IMREAD_COLOR)
    image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
    faces = detector.detect(image_gray)

    for i, face in enumerate(faces):
        print('({0},{1},{2},{3}) score={4}'.format(face.left, face.top,
                                                   face.right, face.bottom,
                                                   face.score))
        cv2.rectangle(image_color, (face.left, face.top),
                      (face.right, face.bottom), (0, 255, 0),
                      thickness=2)
        cv2.putText(image_color,
                    str(i), (face.left, face.bottom),
                    cv2.FONT_HERSHEY_COMPLEX,
                    1, (0, 255, 0),
                    thickness=1)
    cv2.imshow('test', image_color)
    cv2.waitKey(0)

    detector.release()
コード例 #14
0
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()
コード例 #15
0
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()
コード例 #16
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('請指定有人臉的檔案!')
コード例 #17
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()
コード例 #18
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()
コード例 #19
0
from pyseeta import Detector

BARCODE_URLs_FILE = sys.argv[1]

tasks = []
with open(BARCODE_URLs_FILE, 'r') as IMAGES:
    for line in IMAGES:
        key, thumb, url = line.strip().split(',')
        tasks.append((key, url))

# log file for errors
logf = open(sys.argv[2], 'w')
logf.write("Loaded %d items\n" % len(tasks))

detector = Detector()
detector.set_min_face_size(30)
# detector.release()    <-- don't do it here.  do it when all processing is done.


def process(barcode, url):

    # Construct directory structure
    save_dir = os.path.join('data', barcode[4:6], barcode[6:8], barcode)
    try:
        os.makedirs(save_dir)
    except:
        if os.path.exists(os.path.join(save_dir, 'info')):
            logf.write("%s aleardy exist\n" % save_dir)
            return
        pass
    try: