示例#1
0
 def __init__(self, args):
     self.args = args
     ctx = mx.gpu(args.gpu)
     # _vec = args.image_size.split(',')
     # assert len(_vec)==2
     # image_size = (int(_vec[0]), int(_vec[1]))
     # self.threshold = args.threshold
     self.det_minsize = 50
     self.det_threshold = [0.6, 0.7, 0.8]
     #self.det_factor = 0.9
     self.image_size = args.image_size
     mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
     if args.det == 0:
         detector = MtcnnDetector(model_folder=mtcnn_path,
                                  ctx=ctx,
                                  num_worker=1,
                                  accurate_landmark=True,
                                  threshold=self.det_threshold)
     else:
         detector = MtcnnDetector(model_folder=mtcnn_path,
                                  ctx=ctx,
                                  num_worker=1,
                                  accurate_landmark=True,
                                  threshold=[0.0, 0.0, 0.2])
     self.detector = detector
示例#2
0
    def __init__(self, args):
        self.args = args
        ctx = mx.gpu(0)
        image_size = (112, 112)
        self.model = None
        self.ga_model = None
        if len(args.model) > 0:
            self.model = get_model(ctx, image_size, args.model, 'fc1')

        self.threshold = args.threshold
        self.det_minsize = 50
        self.det_threshold = [0.6, 0.7, 0.8]
        # self.det_factor = 0.9
        self.image_size = image_size
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
        if args.det == 0:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=self.det_threshold)
        else:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=[0.0, 0.0, 0.2])
        self.detector = detector
示例#3
0
def crop_face(args):

    img_dir = args.input
    out_dir = args.output
    size = args.size.split(',')
    size = (int(size[0]), int(size[1]))

    name_list, path_list = list_dir(img_dir)
    mtcnn_model = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
    face_detect = MtcnnDetector(model_folder=mtcnn_model,
                                ctx=mx.gpu(args.gpu),
                                num_worker=1,
                                accurate_landmark=True)
    face_dir = []
    for name, path in zip(name_list, path_list):
        img = cv2.imread(path)
        try:
            boxes, _ = face_detect.detect_face(img, det_type=0)
            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            boxes = boxes[:, 0:4].astype(int)
            boxes = boxes[0, :]
            crop_face = img[boxes[1]:boxes[3], boxes[0]:boxes[2]]
            crop_face = cv2.resize(crop_face, size)
            save_dir = os.path.join(out_dir, name)
            if not os.path.exists(save_dir):
                os.mkdir(save_dir)
            basename = os.path.basename(path)
            save_dir = os.path.join(save_dir, basename)
            save_img(save_dir, crop_face)
            print("save image in ", save_dir)
            face_dir.append(save_dir)
        except:
            pass

    return face_dir
示例#4
0
    def __init__(self, gpu, image_size, model, threshold, det):
        ctx = mx.gpu(gpu)
        _vec = image_size.split(',')
        assert len(_vec) == 2
        image_size = (int(_vec[0]), int(_vec[1]))
        self.model = None

        if len(model) > 0:
            self.model = get_model(ctx, image_size, model, 'fc1')

        self.det = det
        self.threshold = threshold
        self.det_minsize = 120
        self.det_threshold = [0.6, 0.7, 0.8]
        self.image_size = image_size
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
        if det == 0:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=self.det_threshold)
        else:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=[0.0, 0.0, 0.2])
        self.detector = detector
示例#5
0
    def __init__(self,
                 detector_dir,
                 recognize_dir,
                 mx_epoch=0,
                 image_size=[112, 112],
                 layer='stage4_unit3_bn3',
                 gpu=-1):
        os.environ['GLOG_minloglevel'] = '2'
        self.det = 0
        if gpu >= 0:
            self.ctx = mx.gpu(gpu)
        else:
            self.ctx = mx.cpu()
        self.det_threshold = [0.6, 0.7, 0.8]

        self.mt_detector = MtcnnDetector(model_folder=detector_dir,
                                         ctx=self.ctx,
                                         num_worker=1,
                                         accurate_landmark=True,
                                         threshold=self.det_threshold)
        sym, arg_params, aux_params = mx.model.load_checkpoint(
            recognize_dir, mx_epoch)
        all_layers = sym.get_internals()
        sym = all_layers[layer + '_output']

        rec_model = mx.mod.Module(symbol=sym,
                                  context=self.ctx,
                                  label_names=None)
        rec_model.bind(data_shapes=[('data', (1, 3, image_size[0],
                                              image_size[1]))])
        rec_model.set_params(arg_params, aux_params)
        self._recognition_model = rec_model
        print("loaded detection and recognition model successfully")
示例#6
0
    def __init__(self, conf):
        self.conf = conf
        if conf.gpu >= 0:
            ctx = mx.gpu(conf.gpu)
        elif conf.gpu < 0:
            ctx = mx.cpu()
        _vec = conf.image_size.split(',')
        assert len(_vec) == 2
        image_size = (int(_vec[0]), int(_vec[1]))
        self.model = None
        self.ga_model = None
        if len(conf.model_path) > 0:
            self.model = get_model(ctx, image_size, conf.model_path, 'fc1')

        self.threshold = conf.threshold
        self.det_minsize = 50  #minimun size of bb of mtcnn
        self.det_threshold = [0.6, 0.7, 0.8]  #bo fail bb
        #self.det_factor = 0.9
        self.image_size = image_size
        mtcnn_path = os.path.join('../models/mtcnn-model')
        if conf.det == 0:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=self.det_threshold)
        else:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=[0.0, 0.0, 0.2])
        self.detector = detector
示例#7
0
 def __init__(self, prefix, epoch, ctx_id=0):
     print('loading', prefix, epoch)
     if ctx_id >= 0:
         ctx = mx.gpu(ctx_id)
     else:
         ctx = mx.cpu()
     sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
     all_layers = sym.get_internals()
     sym = all_layers['heatmap_output']
     image_size = (128, 128)
     self.image_size = image_size
     model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
     #model = mx.mod.Module(symbol=sym, context=ctx)
     model.bind(for_training=False,
                data_shapes=[('data', (1, 3, image_size[0], image_size[1]))
                             ])
     model.set_params(arg_params, aux_params)
     self.model = model
     mtcnn_path = os.path.join(os.path.dirname(__file__), '..', 'deploy',
                               'mtcnn-model')
     self.det_threshold = [0.6, 0.7, 0.8]
     self.detector = MtcnnDetector(model_folder=mtcnn_path,
                                   ctx=ctx,
                                   num_worker=1,
                                   accurate_landmark=True,
                                   threshold=self.det_threshold)
示例#8
0
    def __init__(self, args):
        self.args = args
        if args.gpu >= 0:
            ctx = mx.gpu(args.gpu)
        else:
            ctx = mx.cpu()
        _vec = args.image_size.split(',')
        assert len(_vec) == 2
        image_size = (int(_vec[0]), int(_vec[1]))
        self.model = None
        if len(args.model) > 0:
            self.model = get_model(ctx, image_size, args.model, 'fc1')

        self.det_minsize = 50
        self.det_threshold = [0.6, 0.7, 0.8]
        #self.det_factor = 0.9
        self.image_size = image_size
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
        if args.det == 0:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=self.det_threshold)
        else:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=[0.0, 0.0, 0.2])
        self.detector = detector
def main():
    detector = MtcnnDetector(model_folder='./mtcnn/model',
                             ctx=mx.cpu(),
                             num_worker=4,
                             accurate_landmark=True)
    for i in os.listdir(path1):
        if (((i in os.listdir(path2)) == True)):
            continue
        os.mkdir(path2 + str(i))
        for j in os.listdir(path1 + str(i)):
            img = cv2.imread(path1 + str(i) + "/" + j, 1)
            results = detector.detect_face(img)
            if results is None:
                continue
            if results is not None:
                total_boxes = results[0]
                points = results[1]
                for id in range(len(points)):
                    point = points[id].reshape((2, 5)).T
                    nimg = preprocess(img,
                                      total_boxes[id],
                                      point,
                                      image_size='112,112')
                    cv2.imwrite(
                        path2 + str(i) + "/" + str(j) + "_" + str(id) + "jpg",
                        nimg)
示例#10
0
def detect_face(image_path):
    #time_start=time.time()
    tiny_face_path = './result/tiny_face/'
    if not os.path.exists(tiny_face_path):
        os.makedirs(tiny_face_path)
    else:
        clean(tiny_face_path)

    detector = MtcnnDetector(model_folder='model',
                             ctx=mx.cpu(0),
                             num_worker=4,
                             accurate_landmark=False)
    print('detector', detector)
    img = cv2.imread(image_path)
    #print('img:',img)
    # run detector
    results = detector.detect_face(img)
    if results is not None:
        total_boxes = results[0]
        points = results[1]
        # extract aligned face chips
        chips = detector.extract_image_chips(img, points, 160, 0.37)

        for i, chip in enumerate(chips):
            cv2.imwrite(tiny_face_path + 'chip_' + str(i) + '.jpg', chip)
示例#11
0
def get_mtccn_faces(args, ctx, image):
    # det_threshold = [0.6,0.7,0.8]
    det_threshold = [0.0, 0.1, 0.2]
    mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
    aligned_faces = []
    detector = MtcnnDetector(model_folder=mtcnn_path,
                             ctx=ctx,
                             num_worker=1,
                             accurate_landmark=True,
                             threshold=det_threshold)
    print('Input shape: {}'.format(image.shape))
    ret = detector.detect_face(image, det_type=args.det)
    if ret is None:
        return None
    bboxes, points = ret

    if bboxes.shape[0] == 0:
        return None
    for index, bbox in enumerate(bboxes):
        point = points[index]
        point = point.reshape((2, 5)).T

        nimg = face_preprocess.preprocess(image,
                                          bbox,
                                          point,
                                          image_size='112,112')
        nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
        # cv2.imshow('window', nimg)
        # cv2.waitKey(0)
        aligned = np.transpose(nimg, (2, 0, 1))
        aligned_faces.append(aligned)
    return aligned_faces, bboxes
示例#12
0
    def _preprocessBeforeConversionToNumpy(self, image):
        if isinstance(image, PIL.Image.Image):
            # NOTE: for images will multiple faces, this model will only return
            # one vector.
            # switches PIL to cv2
            npArr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

            det_threshold = [0.6, 0.7, 0.8]
            mtcnn_path = os.path.join(os.path.dirname('__file__'),
                                      'model/mtcnn-model')
            ctx = mx.cpu()
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=det_threshold)
            # Pass input images through face detector
            ret = detector.detect_face(npArr, det_type=0)
            if ret is None:
                raise Exception("No face detected in input image.")
            bbox, points = ret
            if bbox.shape[0] == 0:
                raise Exception("No face detected in input image.")
            bbox = bbox[0, 0:4]
            points = points[0, :].reshape((2, 5)).T
            # Call preprocess() to generate aligned images
            nimg = self._preprocess(npArr, bbox, points, image_size='112,112')
            nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
            aligned = np.transpose(nimg, (2, 0, 1))
            return aligned
        else:
            raise IOError("Image Type not supported for preprocessing.")
示例#13
0
    def __init__(self, loder):
        self.loder = loder
        ctx = mx.gpu(loder[1])
        _vec = loder[2].split(',')
        assert len(_vec) == 2
        image_size = (int(_vec[0]), int(_vec[1]))
        self.model = None
        # self.ga_model = None
        if len(loder[3]) > 0:
            self.model = get_model(ctx, image_size, loder[3], 'fc1')
        # if len(loder.ga_model)>0:
        #   self.ga_model = get_model(ctx, image_size, loder.ga_model, 'fc1')

        self.threshold = loder[4]
        self.det_minsize = 20
        self.det_threshold = [0.6, 0.7, 0.8]
        #self.det_factor = 0.9
        self.image_size = image_size
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')

        if loder[0] == 0:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=self.det_threshold)
        else:
            detector = MtcnnDetector(model_folder=mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=[0.0, 0.0, 0.2])
        self.detector = detector
示例#14
0
def main():
    detector = MtcnnDetector(model_folder='model', ctx=mx.cpu(0), num_worker=1, accurate_landmark=True)

    img = cv2.imread('test2.jpg')

    # run detector
    results = detector.detect_face(img)

    if results is not None:

        total_boxes = results[0]
        points = results[1]

        # extract aligned face chips
        chips = detector.extract_image_chips(img, points, 128, 0.37)
        for i, chip in enumerate(chips):
            cv2.imshow('chip_' + str(i), chip)
            cv2.imwrite('chip_'+str(i)+'.png', chip)

        draw = img.copy()
        for b in total_boxes:
            cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (255, 255, 255))

        for p in points:
            for i in range(5):
                cv2.circle(draw, (p[i], p[i + 5]), 1, (0, 0, 255), 2)

        cv2.imshow("detection result", draw)
        cv2.waitKey(0)
示例#15
0
 def __init__(self, args):
     self.args = args
     if args.use_cpu:
         ctx = mx.cpu()
     else:
         ctx = mx.gpu(args.gpu)
     self.model = None
     self.prefix = args.prefix
     self.epoch = args.epoch
     self.image_size = args.image_size
     self.model = get_model(ctx, self.image_size, self.args.model, 'fc1',
                            self.prefix, self.epoch)
     self.threshold = args.threshold
     self.det_minsize = 50
     self.det_threshold = [0.6, 0.7, 0.8]
     #self.det_factor = 0.9
     mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
     if args.det == 0:
         detector = MtcnnDetector(model_folder=mtcnn_path,
                                  ctx=ctx,
                                  num_worker=1,
                                  accurate_landmark=True,
                                  threshold=self.det_threshold)
     else:
         detector = MtcnnDetector(model_folder=mtcnn_path,
                                  ctx=ctx,
                                  num_worker=1,
                                  accurate_landmark=True,
                                  threshold=[0.0, 0.0, 0.2])
     self.detector = detector
示例#16
0
def main():
    global args
    args = parser.parse_args()
    detector = MtcnnDetector(model_folder='model',
                             ctx=mx.cpu(0),
                             num_worker=args.num_workers,
                             accurate_landmark=False)

    for root, dirs, files in os.walk(args.root_path):
        try:
            if files[0].split('.')[1] != 'jpg':
                continue
        except IndexError:
            continue

        # make dirs
        if not os.path.exists(args.rotate_path):
            os.makedirs(args.rotate_path)
        if not os.path.exists(args.crop_path):
            os.makedirs(args.crop_path)

        for image in files:
            img = cv2.imread(root + '/' + image)

            # run detector first round
            results = detector.detect_face(img)

            if results is not None:
                total_boxes = results[0]
                points = results[1]

                leftx = points[0][0]
                lefty = points[0][5]
                rightx = points[0][1]
                righty = points[0][6]

                # rotate face
                img_tmp = PIL.Image.open(root + '/' + image)
                CropFace(img_tmp,
                         eye_left=(leftx, lefty),
                         eye_right=(rightx, righty),
                         offset_pct=(0.1, 0.1),
                         dest_sz=(200,
                                  200)).save(args.rotate_path + '/' + image)

            # run detector second round
            draw = cv2.imread(args.rotate_path + '/' + image)
            results = detector.detect_face(draw)

            if results is not None:
                total_boxes = results[0]
                points = results[1]

                # extract aligned face chips
                chips = detector.extract_image_chips(draw, points, 144,
                                                     args.crop_size)
                for i, chip in enumerate(chips):
                    cv2.imwrite(args.crop_path + '/' + image, chip)
                    print(args.crop_path + '/' + image)
示例#17
0
	def __init__(self):
		threading.Thread.__init__(self)
		self.detector = MtcnnDetector(model_folder='mtcnnmodel', ctx=mx.gpu(0), num_worker=1, accurate_landmark=False)
		self.imgset=self.readFaceFeatureALL()
		self.threadhold = 0.45  # 设定阈值
		self.net = getattr(net_sphere,'sphere20a')()
		self.net.load_state_dict(torch.load('model/sphere20a_20171020.pth'))
		self.net.cuda()
		self.net.eval()
		self.net.feature = True
 def __init__(self, arguments, mx_context):
     self.args = arguments
     self.ctx = mx_context
     self.model = face_model.FaceModel(args)
     self.detector = MtcnnDetector(model_folder='mtcnn-model/',
                                   ctx=self.ctx,
                                   num_worker=4,
                                   accurate_landmark=False)
     self.names = None  # Names of the persons in the dataset
     self.dataset = None  # Collection of features of known names
示例#19
0
    def __init__(self, args):
        ctx = mx.cpu() if args.gpu == -1 else mx.gpu(args.gpu)
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')

        self.max_face_number = args.max_face_number
        self.face_counter = 0
        self.detector = MtcnnDetector(model_folder=mtcnn_path,
                                      ctx=ctx,
                                      num_worker=1,
                                      minsize=args.mtcnn_minsize,
                                      factor=args.mtcnn_factor,
                                      accurate_landmark=True,
                                      threshold=args.mtcnn_threshold)
示例#20
0
def get_mtcnn_detector(ctx, det, mtcnn_path, thresh=[0.6, 0.7, 0.8]):
    if det == 0:
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=ctx,
                                 num_worker=1,
                                 accurate_landmark=True,
                                 threshold=thresh)
    else:
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=ctx,
                                 num_worker=1,
                                 accurate_landmark=True,
                                 threshold=[0.0, 0.0, 0.2])
    return detector
示例#21
0
def model_fn(model_dir):
    """
    Load the onnx model. Called once when hosting service starts.
    :param: model_dir The directory where model files are stored.
    :return: a model
    """
    if len(mx.test_utils.list_gpus()) == 0:
        ctx = mx.cpu()
    else:
        ctx = mx.gpu(0)

    det_threshold = [0.6, 0.7, 0.8]
    detector = MtcnnDetector(
        model_folder=f"{model_dir}/mtcnn-model",
        ctx=mx.cpu(),
        num_worker=1,
        accurate_landmark=True,
        threshold=det_threshold,
    )
    image_size = (112, 112)
    sym, arg_params, aux_params = onnx_mxnet.import_model(f"{model_dir}/resnet100.onnx")
    # create module
    mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
    mod.bind(
        for_training=False, data_shapes=[("data", (1, 3, image_size[0], image_size[1]))]
    )
    mod.set_params(arg_params=arg_params, aux_params=aux_params)

    return (detector, mod)
示例#22
0
    def __init__(self, model, ga_model, flip, ctx=mx.cpu()):
        image_size = (112, 112)
        self.model = None
        self.ga_model = None

        if len(model) > 0:
            self.model = get_model(ctx, image_size, model, 'fc1')

        if len(ga_model) > 0:
            self.ga_model = get_model(ctx, image_size, ga_model, 'fc1')
        #exception model
        emotion_model_path = os.path.join(os.path.dirname(__file__),
                                          'exceptionModel',
                                          'fer2013_XCEPTION_0.66.hdf5')
        self.emotion_classifier = load_model(emotion_model_path, compile=False)

        self.flip = flip
        self.threshold = 1.24
        self.det_minsize = 50
        #self.det_factor = 0.9
        self.image_size = image_size
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'MTCNNModel')
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=ctx,
                                 num_worker=4,
                                 accurate_landmark=False)
        self.detector = detector
示例#23
0
    def __init__(self, model, gpu_id=0, feature_norm=True):

        self.det_minsize = 60
        self.det_threshold = [0.6, 0.7, 0.7]
        #self.det_factor = 0.8
        image_size = (112, 112)
        self.image_size = image_size
        ctx = mx.gpu(gpu_id)
        self.model = None
        if model is not None:
            _vec = model.split(',')
            assert len(_vec) == 2
            prefix = _vec[0]
            epoch = int(_vec[1])
            print('loading', prefix, epoch)
            sym, arg_params, aux_params = mx.model.load_checkpoint(
                prefix, epoch)
            all_layers = sym.get_internals()
            sym = all_layers['fc1_output']
            model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
            #model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))], label_shapes=[('softmax_label', (args.batch_size,))])
            model.bind(data_shapes=[('data', (1, 3, image_size[0],
                                              image_size[1]))])
            model.set_params(arg_params, aux_params)
            self.model = model
        mtcnn_path = os.path.join(os.path.dirname(__file__),
                                  './model/mtcnn-model')
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=ctx,
                                 num_worker=1,
                                 accurate_landmark=True,
                                 threshold=self.det_threshold,
                                 minsize=self.det_minsize)
        self.detector = detector
        self.feature_norm = feature_norm
示例#24
0
    def preprocess(self, batch):
        mtcnn_path = os.path.dirname(__file__)
        det_threshold = [0.6, 0.7, 0.8]
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=self.mxnet_ctx,
                                 num_worker=1,
                                 accurate_landmark=True,
                                 threshold=det_threshold)

        img_list = [
            batch[0].get("img1"),
        ]  # batch[0].get("img2")]
        string_passed = batch[0].get("s")
        face_list = []
        for img in img_list:
            if img is None:
                self.error = "Requires two image input."
                return None

            img_arr = mx.img.imdecode(img, to_rgb=0).asnumpy()
            img_arr = get_input(detector, img_arr)
            if img_arr is None:
                self.error = "No face detected."
                return None

            img_arr = mx.nd.array(img_arr)
            img_arr = img_arr.expand_dims(axis=0)
            face_list.append([img_arr])
        face_list.append(list(string_passed))
        return face_list
示例#25
0
    def __init__(self, args):
        self.args = args
        model = edict()

        # self.threshold = 1.24
        # self.det_minsize = 50
        # self.det_threshold = [0.4,0.6,0.6]
        # self.det_factor = 0.9
        _vec = args[0].split(',')
        assert len(_vec) == 2
        image_size = (int(_vec[0]), int(_vec[1]))
        self.image_size = image_size
        _vec = args[1].split(',')
        assert len(_vec) == 2
        prefix = _vec[0]
        epoch = int(_vec[1])
        print('loading', prefix, epoch)
        # ctx = mx.gpu(args.gpu)
        ctx = mx.cpu(0)
        sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
        all_layers = sym.get_internals()
        sym = all_layers['fc1_output']
        model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
        model.bind(data_shapes=[('data', (1, 3, image_size[0],
                                          image_size[1]))])
        model.set_params(arg_params, aux_params)
        self.model = model
        mtcnn_path = os.path.join(os.path.dirname(__file__),
                                  'mxnet-mtcnn-model')
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=ctx,
                                 num_worker=4,
                                 accurate_landmark=False)
        self.detector = detector
示例#26
0
    def __init__(self, args):
        self.args = args
        model = edict()

        self.threshold = args.threshold
        self.det_minsize = 50
        self.det_threshold = [0.4, 0.6, 0.6]
        self.det_factor = 0.9
        image_size = (args.image_size[0], args.image_size[1])
        self.image_size = image_size
        _vec = args.model.split(',')
        assert len(_vec) == 2
        prefix = _vec[0]
        epoch = int(_vec[1])
        print('loading', prefix, epoch)
        ctx = mx.gpu(args.gpu)
        sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
        all_layers = sym.get_internals()
        sym = all_layers['fc1_output']
        model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
        #model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))], label_shapes=[('softmax_label', (args.batch_size,))])
        model.bind(data_shapes=[('data', (1, 3, image_size[0],
                                          image_size[1]))])
        model.set_params(arg_params, aux_params)
        self.model = model
        mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
        detector = MtcnnDetector(model_folder=mtcnn_path,
                                 ctx=ctx,
                                 num_worker=1,
                                 accurate_landmark=True,
                                 threshold=[0.0, 0.0, 0.2])
        self.detector = detector
示例#27
0
    def __init__(self,
                 age_model_str,
                 gender_model_str,
                 detection_model_path,
                 image_size=(64, 64),
                 det=0):
        self.age_model_str = age_model_str
        self.gender_model_str = gender_model_str
        self.image_size = image_size
        self.det = det
        self.mtcnn_path = detection_model_path

        if mx.context.num_gpus() > 0:
            ctx = mx.gpu(0)
        else:
            ctx = mx.cpu()

        self.age_model = None
        self.gender_model = None

        if len(self.age_model_str) > 0:
            self.age_model = load_model(self.age_model_str,
                                        self.image_size,
                                        layer='_mulscalar16',
                                        ctx=ctx)
        if len(self.gender_model_str) > 0:
            self.gender_model = load_model(self.gender_model_str,
                                           self.image_size,
                                           layer='_mulscalar16',
                                           ctx=ctx)

        self.det_minsize = 50
        self.det_threshold = [0.6, 0.7, 0.8]

        if self.det == 0:
            detector = MtcnnDetector(model_folder=self.mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=self.det_threshold)
        else:
            detector = MtcnnDetector(model_folder=self.mtcnn_path,
                                     ctx=ctx,
                                     num_worker=1,
                                     accurate_landmark=True,
                                     threshold=[0.0, 0.0, 0.2])
        self.detector = detector
class MyVideoCapture:
    def __init__(self, video_source):
        # Open the video source
        self.detector = MtcnnDetector(model_folder='model', ctx=mx.cpu(0), num_worker = 4 , accurate_landmark = False)
        self.vid = cv2.VideoCapture(video_source)
        if not self.vid.isOpened():
            raise ValueError("Unable to open video source", video_source)
 
        
 
    def get_frame(self):
        if self.vid.isOpened():
            ret, frame = self.vid.read()
            img = cv2.resize(frame, (650,500))
            (h, w) = img.shape[:2]


            results = self.detector.detect_face(img)
    
            
            
            if results != None:
              total_boxes = results[0]
              points = results[1]
              global count
              count = 0

      
              draw = img.copy()
              for b in total_boxes:
                cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (255, 255, 255))
                x1 = int(b[0])
                y1 = int(b[1])
                x2 = int(b[2])
                y2 = int(b[3])

                centroid = (int((x1+x2)/2),int((y1+y2)/2))
              
                if centroid[0]>=60 and centroid[0]<=w-60:
                  
                
                  count += 1
                  cv2.circle(draw, centroid, 4, (0, 255, 0), -1)
                  print("no_face:",count)
                  cv2.putText(draw, "count = "+str(count), (0, 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                  img = draw
                
            if ret:
                # Return a boolean success flag and the current frame converted to BGR
                return (ret, cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
            else:
                return (ret, None)
        else:
            return (ret, None)
 
    # Release the video source when the object is destroyed
    def __del__(self):
        if self.vid.isOpened():
            self.vid.release()
 def __init__(self,det=0,image_size=(64,64)):
     '''
     人脸对齐工具
     :param det:0全部检测,1关键点检测和对齐,用于已经crop的人脸图像
     :param image_size:
     '''
     self.det=det
     self.image_size=image_size
     self.ctx=mx.gpu(0)
     det_threshold = [0.6, 0.7, 0.8]
     mtcnn_path = os.path.join(os.path.dirname(__file__), 'mtcnn-model')
     if det == 0:
         self.detector = MtcnnDetector(model_folder=mtcnn_path, ctx=self.ctx, num_worker=1, accurate_landmark=True,
                                  threshold=det_threshold)
     else:
         self.detector = MtcnnDetector(model_folder=mtcnn_path, ctx=self.ctx, num_worker=1, accurate_landmark=True,
                                  threshold=[0.0, 0.0, 0.2])
示例#30
0
def detect_face(img,img_size):
    detector = MtcnnDetector(model_folder='./model', ctx=mx.cpu(0), num_worker = 1 , accurate_landmark = False)
    results = detector.detect_face(img)
    if results is None:
        return 0
    total_boxes = results[0]
    points = results[1]
    face_crops = detector.extract_image_chips(img, points, img_size, 0.37)
    '''
    draw = img.copy()
    for b in total_boxes:
        cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (255, 255, 255))
    for p in points:
        for i in range(5):
            cv2.circle(draw, (p[i], p[i + 5]), 1, (255, 0, 0), 2)
    '''
    return face_crops, total_boxes
示例#31
0
def main():
    args = get_args()
    src_file = args.src
    if not os.path.exists(src_file):
        raise ValueError("src dir not exist {}".format(src_file))

    split_ratio = args.split

    dst_dir = os.path.abspath(args.dst)

    num_gpus = args.ngpus
    if num_gpus == -1:
        num_gpus = len(mx.test_utils.list_gpus())
    if num_gpus == 0:
        ctx = mx.cpu(0)
    else:
        ctx = [mx.gpu(i) for i in range(num_gpus)]

    print("src={} dst dir={} gpu={}".format(src_file, dst_dir, num_gpus))
    s = read_clean_list(args.cleanlist)
    detector = MtcnnDetector(model_folder='model', ctx=ctx, num_worker=args.workers, accurate_landmark=True)

    file_count = 0
    with open(src_file, "r", encoding="utf-8") as f:
        last_m_id = "x"
        for line in f:
            m_id, image_search_rank, image_url, page_url, face_id, face_rectangle, face_data = line.split("\t")
            # rect = struct.unpack("ffff", base64.b64decode(face_rectangle))
            if "{}/{}".format(m_id, image_search_rank) in s:
                data = np.frombuffer(base64.b64decode(face_data), dtype=np.uint8)
                img = cv2.imdecode(data, cv2.IMREAD_COLOR)
                h, w, _ = img.shape
                if h > 128 and w > 128:
                    try:
                        # run detector
                        results = detector.detect_face(img)

                        if results is not None:
                            total_boxes = results[0]
                            points = results[1]

                            bigbox_idx = np.argmax([(b[2] - b[0]) * (b[3] - b[1]) for b in total_boxes])

                            # extract aligned face chips
                            chips = detector.extract_image_chips(img, points[bigbox_idx:bigbox_idx + 1], args.size,
                                                                 args.padding)
                            for i, chip in enumerate(chips):

                                if last_m_id != m_id:
                                    ab = "train"
                                    # let validation set has same class label as training set
                                    # see source code of pytorch's DatasetFolder
                                else:
                                    ab = "val" if random.random() > split_ratio else "train"
                                dd = os.path.join(dst_dir, ab, m_id)
                                os.makedirs(dd, exist_ok=True)
                                cv2.imwrite(os.path.join(dd, "{}.png".format(image_search_rank)),
                                            chip)
                                last_m_id = m_id

                    except Exception as e:
                        print(m_id, image_search_rank, e)

                    file_count = file_count + 1
                    if file_count % 1000 == 0:
                        print(file_count)
# coding: utf-8
import mxnet as mx
from mtcnn_detector import MtcnnDetector
import cv2
import os
import time

detector = MtcnnDetector(model_folder='model', ctx=mx.cpu(0), num_worker = 4 , accurate_landmark = False)


img = cv2.imread('test2.jpg')

# run detector
results = detector.detect_face(img)

if results is not None:

    total_boxes = results[0]
    points = results[1]
    
    # extract aligned face chips
    chips = detector.extract_image_chips(img, points, 144, 0.37)
    for i, chip in enumerate(chips):
        cv2.imshow('chip_'+str(i), chip)
        cv2.imwrite('chip_'+str(i)+'.png', chip)

    draw = img.copy()
    for b in total_boxes:
        cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (255, 255, 255))

    for p in points:
示例#33
0
def main():
    args = get_args()
    src_dir = args.src
    if not os.path.exists(src_dir):
        raise ValueError("src dir not exist {}".format(src_dir))

    split_ratio = args.split

    dst_dir = os.path.abspath(args.dst)
    err_dir = os.path.abspath(args.err)

    num_gpus = args.ngpus
    if num_gpus == -1:
        num_gpus = len(mx.test_utils.list_gpus())
    if num_gpus == 0:
        ctx = mx.cpu(0)
    else:
        ctx = [mx.gpu(i) for i in range(num_gpus)]

    print("src dir={} dst dir={} err_dir={} gpu={}".format(src_dir, dst_dir, err_dir, num_gpus))
    detector = MtcnnDetector(model_folder='model', ctx=ctx, num_worker=args.workers, accurate_landmark=False)

    file_count = 0
    for root, dirs, files in os.walk(src_dir):
        relpath = os.path.relpath(root, src_dir)
        # dd = os.path.join(dst_dir, relpath)
        ed = os.path.join(err_dir, relpath)
        class_data_written = False  # training
        for filename in files:
            if filename.lower().endswith(('.jpg', '.jpeg', '.gif', '.png')):
                absfile = os.path.join(root, filename)
                success = False
                try:
                    # warning cv2.imread does not handle file names with unicode characters.
                    img = cv2.imread(absfile)

                    # run detector
                    results = detector.detect_face(img)

                    if results is not None:

                        total_boxes = results[0]
                        points = results[1]

                        bigbox_idx = np.argmax([(b[2] - b[0]) * (b[3] - b[1]) for b in total_boxes])

                        # extract aligned face chips
                        chips = detector.extract_image_chips(img, points[bigbox_idx:bigbox_idx + 1], args.size,
                                                             args.padding)
                        for i, chip in enumerate(chips):
                            if split_ratio > 0:
                                if not class_data_written:
                                    ab = "train"
                                    class_data_written = True
                                    # let validation set has same class label as training set
                                    # see source code of pytorch's DatasetFolder
                                    os.makedirs(os.path.join(dst_dir, "val", relpath), exist_ok=True)
                                else:
                                    ab = "val" if random.random() > split_ratio else "train"
                                dd = os.path.join(dst_dir, ab, relpath)
                                os.makedirs(dd, exist_ok=True)
                                cv2.imwrite(os.path.join(dd, os.path.splitext(filename)[0] + ".png"),
                                            chip)
                                class_data_written = True
                            else:
                                dd = os.path.join(dst_dir, relpath)
                                os.makedirs(dd, exist_ok=True)
                                cv2.imwrite(os.path.join(dd, os.path.splitext(filename)[0] + ".png"),
                                            chip)
                            success = True

                except Exception as e:
                    print(relpath, filename, e)
                    pass

                if not success:
                    os.makedirs(ed, exist_ok=True)
                    shutil.copyfile(absfile, os.path.join(ed, filename))

                file_count = file_count + 1
                if file_count % 1000 == 0:
                    print(file_count)