def __init__(self, model_name: str = 'arcface_r100_v1', epoch_num: int = 0, use_flip: bool = False, ctx: mx.Context = mx.cpu()): self.use_flip = use_flip self.embeddings = dict() if model_name == 'arcface_r100_v1': model = model_zoo.get_model(model_name) if ctx.device_type.startswith('cpu'): ctx_id = -1 else: ctx_id = ctx.device_id model.prepare(ctx_id=ctx_id) self.model = model.model else: sym, arg_params, aux_params = mx.model.load_checkpoint( model_name, epoch_num) sym = sym.get_internals()['fc1_output'] model = mx.mod.Module(symbol=sym, context=ctx, label_names=None) data_shape = (2 if use_flip else 1, 3, 112, 112) model.bind(data_shapes=[('data', data_shape)], for_training=False) model.set_params(arg_params, aux_params) #warmup data = mx.nd.zeros(shape=data_shape) db = mx.io.DataBatch(data=(data, )) model.forward(db, is_train=False) embedding = model.get_outputs()[0].asnumpy() self.model = model self.metric = metrics.cosine
def detect(img): model = get_model('retinaface_r50_v1') model.prepare(ctx_id=-1, nms=0.4) bbox, feature_pnts = model.detect(img, threshold=0.5, scale=1.0) if bbox.shape[0] == 0: return None bbox = bbox[0, 0:4] feature_pnts = feature_pnts[0, :].reshape((2, 5)).T nimg = preprocess(img, bbox, feature_pnts, image_size='112,112') nimg = cvtColor(nimg, COLOR_BGR2RGB) aligned = transpose(nimg, (2, 0, 1)) return aligned
def prepare_images(root_dir, output_dir): whitelist_dir = 'MID' detector = model_zoo.get_model('retinaface_r50_v1') detector.prepare(ctx_id=-1, nms=0.4) for family_path in tqdm(root_dir.iterdir()): for person_path in family_path.iterdir(): if not person_path.is_dir() or not person_path.name.startswith(whitelist_dir): continue output_person = ensure_path(output_dir / person_path.relative_to(root_dir)) for img_path in person_path.iterdir(): img = cv2.imread(str(img_path)) bbox, landmarks = detector.detect(img, threshold=0.5, scale=1.0) output_path = output_person / img_path.name if len(landmarks) < 1: print(f'smth wrong with {img_path}') continue warped_img = norm_crop(img, landmarks[0]) cv2.imwrite(str(output_path), warped_img)
def get_detector_with_backup() -> Detector: detector = get_retina_resnet50(resolution=512, ctx=mx.gpu(0), batch_size=8, num_workers=6) backup = model_zoo.get_model('retinaface_r50_v1') backup.prepare(ctx_id=0, nms=0.4) def detect(img_paths: Sequence[Path]) -> Generator[Detections, None, None]: for img_path, (scores, bboxes, landmarks) in zip(img_paths, detector(img_paths)): if len(landmarks) < 1: img = cv2.imread(str(img_path)) if img is None: logging.warning(f'cannot load an image: {img_path}') yield [], [], [] continue dets, landmarks = backup.detect(img) scores = dets[:, -1] bboxes = dets[:, :4] yield scores, bboxes, landmarks return detect
return normed_embedding config = Configs() model_name = 'arcface_r100_v1' engine = config.build_model_paths(model_name, 'plan')[1] model_onnx = config.build_model_paths(model_name, 'onnx')[1] model = InsightTRT(rec_name=engine) #model = InsightTriton(rec_name=engine) #model = InsightORT(rec_name=model_onnx) model.prepare() model_orig = model_zoo.get_model(model_name, root=config.mxnet_models_dir) model_orig.prepare(-1) im = cv2.imread('test_images/crop.jpg', cv2.IMREAD_COLOR) iters = 100 t0 = time.time() for i in range(iters): emb = model.get_embedding(im) t1 = time.time() print(f'Took {t1 - t0} s. ({iters/(t1 - t0)} faces/sec)') # emb1 = model.get_embedding(im)[0] emb2 = model_orig.get_embedding(im)[0]
import numpy as np from insightface.model_zoo import get_model from server.face_processing import face_align model = get_model('arcface_r100_v1') # init model get_emmberding_face ctx_id = -1 model.prepare(ctx_id=ctx_id) from mtcnn.mtcnn import MTCNN detector = MTCNN() def get_face(Img): faces = detector.detect_faces(Img) if len(faces) != 1: return None else: box = faces[0]['box'] keypoints = faces[0]['keypoints'] left_eye = keypoints['left_eye'] right_eye = keypoints['right_eye'] nose = keypoints['nose'] mouth_left = keypoints['mouth_left'] mouth_right = keypoints['mouth_right'] points = np.asarray( [left_eye, right_eye, nose, mouth_left, mouth_right]) face_extracted = face_align.preprocess(Img, box, points,
def __init__(self, model_name='retinaface_r50_v1', gpu=False): from insightface.model_zoo import get_model self._model = get_model(model_name) _ctx_id = 1 if gpu else -1 self._model.prepare(ctx_id=_ctx_id, nms=0.4) super(RetinaFaceDetector, self).__init__()
def __init__(self): self.model = get_model('arcface_r100_v1') self.model.prepare(ctx_id=-1)