class Detect: def __init__(self): self.detector = MtcnnDetector(model_folder=model, ctx=mx.cpu(0), num_worker=4, accurate_landmark=False) def detect_face(self,image): img = cv2.imread(image) if img is not None: print('detect face: read img ok,') else: print('detect face: read img fail') results =self.detector.detect_face(img) boxes=[] key_points = [] if results is not None: #box框 boxes=results[0] #人脸5个关键点 points = results[1] for i in results[0]: faceKeyPoint = [] for p in points: for i in range(5): faceKeyPoint.append([p[i], p[i + 5]]) key_points.append(faceKeyPoint) else: print('detect face: result is None') return {"boxes":boxes,"face_key_point":key_points}
def __init__(self): self.detector = MtcnnDetector(model_folder=model, ctx=mx.cpu(0), num_worker=4, accurate_landmark=False)
def main(args): is_test = True if args.is_test == '1' else False _t = Timer() detector_cast = MtcnnDetector(model_folder='./mtcnn/model', minsize=20, threshold=[0.1, 0.5, 0.9], factor=0.709, ctx=mx.gpu(args.gpu), num_worker=4, accurate_landmark=False) detector_candi = MtcnnDetector(model_folder='./mtcnn/model', minsize=20, threshold=[0.5, 0.5, 0.9], factor=0.709, ctx=mx.gpu(args.gpu), num_worker=4, accurate_landmark=False) embedding = FaceModel(model='./arcface/model/model-r50-am-lfw', ctx=mx.gpu(args.gpu)) if is_test: this_dir, json_path, save_name = osp.join(test_root, 'test'), osp.join( test_root, 'test.json'), 'face_em_test.pkl' else: this_dir, json_path, save_name = osp.join( trainval_root, 'val'), osp.join(trainval_root, 'val.json'), 'face_em_val.pkl' data_raw = load_json(json_path) movie_num, movie_cnt = len(data_raw.keys()), 0 face_dict = {} # det/extract val face feat for movie, info in data_raw.items(): face_dict.update({movie: {'cast': [], 'candidates': []}}) movie_cnt += 1 casts, casts_num = info['cast'], len(info['cast']) candidates, candidates_num = info['candidates'], len( info['candidates']) for i, cast in enumerate(casts): img_path = osp.join(this_dir, cast['img']) img = cv2.imread(img_path) cast_id = cast['id'] _t.tic() fbbox, landmark = face_det_cast(img, detector_cast) assert fbbox is not None, 'Cast: No face detected !' ffeat = face_exfeat(img, fbbox, landmark, embedding) _t.toc() print('%s %d/%d ... %s %d/%d ... time: %.3f s average: %.3f s' % (movie, movie_cnt, movie_num, cast_id, i + 1, casts_num, _t.diff, _t.average_time)) face_dict[movie]['cast'].append({ 'id': cast_id, 'fbbox': fbbox, 'ffeat': ffeat }) for i, candidate in enumerate(candidates): img_path = osp.join(this_dir, candidate['img']) img = cv2.imread(img_path) candidate_id = candidate['id'] rect = candidate['bbox'] _t.tic() crop, fbbox, landmark = face_det_candi(img, rect, detector_candi) if fbbox is None: _t.toc() print( '%s %d/%d ... %s %d/%d ... time: %.3f s average: %.3f s' % (movie, movie_cnt, movie_num, candidate_id, i + 1, candidates_num, _t.diff, _t.average_time)) face_dict[movie]['candidates'].append({ 'id': candidate_id, 'fbbox': None, 'ffeat': None }) continue ffeat = face_exfeat(crop, fbbox, landmark, embedding) _t.toc() print('%s %d/%d ... %s %d/%d ... time: %.3f s average: %.3f s' % (movie, movie_cnt, movie_num, candidate_id, i + 1, candidates_num, _t.diff, _t.average_time)) face_dict[movie]['candidates'].append({ 'id': candidate_id, 'fbbox': fbbox, 'ffeat': ffeat }) my_pickle(face_dict, osp.join('./features', save_name))