Exemple #1
0
def test_net(gpuId):
    prefix = [
        os.path.join(mtnnDir, 'pnet'),
        os.path.join(mtnnDir, 'rnet'),
        os.path.join(mtnnDir, 'onet')
    ]
    epoch = [16, 16, 16]
    batch_size = [2048, 256, 16]
    ctx = mx.gpu(gpuId)
    thresh = [0.5, 0.5, 0.7]
    min_face_size = 40
    stride = 2

    args_p, auxs_p = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    PNet = FcnDetector(P_Net("test"), ctx, args_p, auxs_p)

    # load rnet model
    args_r, auxs_r = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args_r, auxs_r)

    # load onet model
    args_o, auxs_o = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args_o, auxs_o)
    return MtcnnDetector(
        detectors=[PNet, RNet, ONet],
        ctx=ctx,
        min_face_size=min_face_size,
        stride=stride,
        threshold=thresh,
        slide_window=False)
Exemple #2
0
def mtcnn_model(prefix, epoch, batch_size, ctx, thresh, min_face, stride,
                slide_window):
    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
    detectors[1] = RNet

    # load onet model
    args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
    detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    return mtcnn_detector
def test_net(root_path,
             dataset_path,
             image_set,
             prefix,
             epoch,
             batch_size,
             test_mode="rnet",
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False,
             shuffle=False,
             vis=False):

    detectors = [None, None, None]

    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    gt_imdb = imdb.gt_imdb()

    test_data = TestLoader(gt_imdb)
    detections = mtcnn_detector.detect_face(imdb, test_data, vis=vis)

    if test_mode == "pnet":
        net = "rnet"
    elif test_mode == "rnet":
        net = "onet"

    save_path = os.path.join(data_dir, net)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file = os.path.join(save_path, "detections.pkl")
    with open(save_file, 'wb') as f:
        cPickle.dump(detections, f, cPickle.HIGHEST_PROTOCOL)

    save_hard_example(net)
Exemple #4
0
def test_net(root_path,
             dataset_path,
             image_set,
             prefix,
             epoch,
             batch_size,
             ctx,
             test_mode="onet",
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False,
             shuffle=False,
             vis=False):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        args, auxs = load_param(prefix[1], epoch[1], convert=True, ctx=ctx)
        RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
        ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    imdb = IMDB("fddb", image_set, root_path, dataset_path, 'test')
    gt_imdb = imdb.gt_imdb()

    test_data = TestLoader(gt_imdb)
    print('1')
    # 得到的detections格式为:[[第一张图的所有预测框],[第二张图片的所有预测框],[第三张图片的所有预测框],...,[最后一张图片的所有预测框]],每个预测框为[x1,y1,x2,y2,score],score为该预测框为人脸的分数
    detections = mtcnn_detector.detect_face(imdb, test_data, vis=vis)
    save_path = "/Users/qiuxiaocong/Downloads/mtcnn1"
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file = os.path.join(save_path, "detections_onet_0009_givenPRnet.pkl")
    with open(save_file, 'wb') as f:
        pickle.dump(detections, f, pickle.HIGHEST_PROTOCOL)
    print('detections saved done!')
Exemple #5
0
def test_net(prefix,
             epoch,
             batch_size,
             ctx,
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=False, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    args, auxs = load_param(prefix[1], epoch[0], convert=False, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
    detectors[1] = RNet

    # load onet model
    args, auxs = load_param(prefix[2], epoch[2], convert=False, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
    detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    img = cv2.imread('test01.jpg')
    t1 = time.time()

    boxes, boxes_c = mtcnn_detector.detect_pnet(img)
    boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c)
    boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c)

    print 'time: ', time.time() - t1

    if boxes_c is not None:
        draw = img.copy()
        font = cv2.FONT_HERSHEY_SIMPLEX
        for b in boxes_c:
            cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),
                          (0, 255, 255), 1)
            cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4,
                        (255, 255, 255), 1)

        cv2.imshow("detection result", draw)
        cv2.waitKey(0)
Exemple #6
0
def test_net(root_path,
             dataset_path,
             prefix,
             epoch,
             batch_size,
             ctx,
             test_mode="onet",
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False,
             shuffle=False,
             vis=False):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=False, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        args, auxs = load_param(prefix[1], epoch[0], convert=False, ctx=ctx)
        RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        args, auxs = load_param(prefix[2], epoch[2], convert=False, ctx=ctx)
        ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    for i in range(1, 11):
        image_set = "fold-" + str(i).zfill(2)
        imdb = IMDB("fddb", image_set, root_path, dataset_path, 'test')
        gt_imdb = imdb.gt_imdb()

        test_data = TestLoader(gt_imdb)
        all_boxes = mtcnn_detector.detect_face(imdb, test_data, vis=vis)
        imdb.write_results(all_boxes)
Exemple #7
0
def test_net(root_path, dataset_path, image_set, prefix, epoch,
             batch_size, ctx, test_mode="rnet",
             thresh=[0.6, 0.6, 0.7], min_face_size=24,
             stride=2, slide_window=False, shuffle=False, vis=False):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
        RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
        ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size,
                                   stride=stride, threshold=thresh, slide_window=slide_window)


    imdb = IMDB("wider", image_set, root_path, dataset_path, 'test')
    gt_imdb = imdb.gt_imdb()

    test_data = TestLoader(gt_imdb)
    detections = mtcnn_detector.detect_face(imdb, test_data, vis=vis)

    if test_mode == "pnet":
        net = "rnet"
    elif test_mode == "rnet":
        net = "onet"

    save_path = "./prepare_data/%s"%net
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file = os.path.join(save_path, "detections.pkl")
    with open(save_file, 'wb') as f:
        cPickle.dump(detections, f, cPickle.HIGHEST_PROTOCOL)

    save_hard_example(net)
Exemple #8
0
def test_net(root_path,
             dataset_path,
             prefix,
             epoch,
             batch_size,
             test_mode="onet",
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False,
             shuffle=False,
             vis=False):

    detectors = [None, None, None]

    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    for i in range(1, 11):
        image_set = "fold-" + str(i).zfill(2)
        imdb = IMDB("fddb", image_set, root_path, dataset_path, 'test')
        gt_imdb = imdb.gt_imdb()
        test_data = TestLoader(gt_imdb)
        all_boxes = mtcnn_detector.detect_face(imdb, test_data, vis=vis)
        imdb.write_results(all_boxes)
def test(prefix,
         epoch,
         batch_size,
         test_mode="onet",
         thresh=[0.6, 0.6, 0.7],
         min_face_size=24,
         stride=2,
         slide_window=False,
         shuffle=False,
         vis=False):
    #img = cv2.imread("./1111.jpg")
    detectors = [None, None, None]
    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path)
    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    for name in os.listdir("C:\\Users\\JINNIU\\Desktop\\liuzhen\\qinghua"):
        img = cv2.imread(
            os.path.join("C:\\Users\\JINNIU\\Desktop\\liuzhen\\qinghua", name))
        boxes, boxes_c = mtcnn_detector.detect(img)
        visssss(img, boxes_c, name, thresh=0.998)
Exemple #10
0
    def loadModel(self):
        self.model = face_embedding.FaceModel(self.args)
        detectors = [None, None, None]
        ctx = mx.gpu(0)
        prefix = ['mtcnnmodel/pnet', 'mtcnnmodel/rnet', 'mtcnnmodel/onet']
        epoch = [16, 16, 16]
        batch_size = [2048, 256, 16]
        thresh = [0.6, 0.6, 0.7]
        min_face_size = 24
        stride = 2
        slide_window = False
        # load pnet model
        args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
        if slide_window:
            PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
        else:
            PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
        detectors[0] = PNet

        # load rnet model
        args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
        RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
        detectors[1] = RNet

        # load onet model
        args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
        ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
        detectors[2] = ONet

        self.mtcnn_detector = MtcnnDetector(detectors=detectors,
                                            ctx=ctx,
                                            min_face_size=min_face_size,
                                            stride=stride,
                                            threshold=thresh,
                                            slide_window=slide_window)
        #print (self.model)
        self.id_dataset, self.idnums = self.get_id_data(self.args.id_dir)
Exemple #11
0
def test_net(prefix,
             epoch,
             batch_size,
             ctx,
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    if slide_window:  # 使用滑动窗口(MTCNN的P_NET不使用了滑动窗口,而是全卷积网络)
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    args, auxs = load_param(prefix[1], epoch[1], convert=True, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
    detectors[1] = RNet

    # load onet model
    args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
    detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    # img = cv2.imread('test01.jpg')  # 读取图片
    # img = cv2.imread('000007.jpg')  # 读取图片
    # img = cv2.imread('crow.jpg')  # 读取图片
    img = cv2.imread('physics.jpg')  # 读取图片

    t1 = time.time()  # 开始计时

    boxes, boxes_c = mtcnn_detector.detect_pnet(img)
    boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c)
    boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c)

    print(boxes_c)

    print('time: ', time.time() - t1)

    if boxes_c is not None:
        draw = img.copy()
        font = cv2.FONT_HERSHEY_SIMPLEX  # Python 一种字体
        for b in boxes_c:  # nms和bbr之后的结果
            # for b in boxes:       # nms和bbr之前的结果
            # 在draw上绘制矩形框(左上角坐标+右下角坐标)
            cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),
                          (0, 255, 255), 1)
            # 在draw上添加文字
            cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4,
                        (255, 255, 255), 1)

        cv2.imshow("detection result", draw)
        cv2.waitKey(0)
Exemple #12
0
def test_net(prefix=['model/pnet', 'model/rnet', 'model/onet'],
             epoch=[16, 16, 16],
             batch_size=[2048, 256, 16],
             ctx=mx.cpu(0),
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             camera_path='0'):

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    PNet = FcnDetector(P_Net("test"), ctx, args, auxs)

    # load rnet model
    args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)

    # load onet model
    args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)

    mtcnn_detector = MtcnnDetector(detectors=[PNet, RNet, ONet],
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=False)

    try:
        capture = cv2.VideoCapture(int(camera_path))
    except ValueError as e:
        capture = cv2.VideoCapture(camera_path)

    try:
        first_loop = True
        while (capture.isOpened()):
            ret, img = capture.read()
            if img is None:
                continue

            # Initialize video writing
            if (first_loop):
                first_loop = False
                fourcc = cv2.VideoWriter_fourcc(*'H264')
                h, w = img.shape[:2]
                writer = cv2.VideoWriter('test.mkv', fourcc, 10, (w, h), True)

            t1 = time.time()

            boxes, boxes_c = mtcnn_detector.detect_pnet(img)
            boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c)
            boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c)

            print('shape: ', img.shape, '--', 'time: ', time.time() - t1)

            draw = img.copy()
            if boxes_c is not None:
                font = cv2.FONT_HERSHEY_SIMPLEX
                for b in boxes_c:
                    cv2.rectangle(draw, (int(b[0]), int(b[1])),
                                  (int(b[2]), int(b[3])), (0, 255, 255), 1)
                    cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])),
                                font, 0.4, (255, 255, 255), 1)

            writer.write(draw)

    except KeyboardInterrupt as e:
        print("KeyboardInterrupt")
        writer.release()
def detectOneImg(prefix,
                 epoch,
                 batch_size,
                 model,
                 imgPath,
                 test_mode="onet",
                 thresh=[0.6, 0.6, 0.7],
                 min_face_size=24,
                 stride=2,
                 slide_window=False,
                 shuffle=False,
                 vis=False):
    detectors = [None, None, None]
    #load densenet for classfication
    model.load_model()

    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    print(model_path)
    # load pnet model
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet

    # load rnet model
    if test_mode in ["rnet", "onet"]:
        RNet = Detector(R_Net, 24, batch_size[1], model_path[1])
        detectors[1] = RNet

    # load onet model
    if test_mode == "onet":
        ONet = Detector(O_Net, 48, batch_size[2], model_path[2])
        detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    images_res = []
    img = cv2.imread(os.path.join(imgPath))
    boxes, boxes_c = mtcnn_detector.detect(img)
    #print(boxes_c.shape)
    #visssss(img, boxes_c, 'plain13134.jpg', thresh=0.998)
    for i in range(boxes_c.shape[0]):
        bbox = boxes_c[i, :4].astype('int32')
        if bbox[1] < 0:
            bbox[1] = 0
        if bbox[0] < 0:
            bbox[0] = 0
        if bbox[2] > 2048:
            bbox[2] = 2048
        if bbox[3] > 2048:
            bbox[3] = 2048
        crop = img[bbox[1]:bbox[3], bbox[0]:bbox[2], :]
        #print(boxes_c[i, :4])
        #print('crop:',crop.shape)
        crop = cv2.resize(crop, (48, 48))
        #cv2.imwrite("C:\\Users\\JINNIU\\Desktop\\liuzhen\\qinghua\\temp\\"+str(i)+'.jpg',crop)
        images_res.append(crop)
    images_res = np.array(images_res).astype(np.float32)
    images_res = normalize_images(images_res)
    pred = model.test(images_res)
    bg_box = np.where(
        pred == 45
    )  #if the class is 45, the image is the background and not the traffic sign. we omit these in the next step
    #print(pred)
    #print(len(boxes_cc))
    for ii in bg_box[0]:
        boxes_c[ii, :] = 0
    #print(boxes_c)
    #del boxes_c[np.where(pred==45),:]
    visssss(img, boxes_c, imgPath, pred, thresh=0.998)
Exemple #14
0
def test_net(prefix,
             epoch,
             batch_size,
             ctx,
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False,
             camera_path='0'):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
    detectors[1] = RNet

    # load onet model
    args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
    detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    try:
        capture = cv2.VideoCapture(int(camera_path))
    except ValueError as e:
        capture = cv2.VideoCapture(camera_path)

    while (capture.isOpened()):
        ret, img = capture.read()
        if img is None:
            continue
        # img = cv2.imread('test01.jpg')
        t1 = time.time()

        boxes, boxes_c = mtcnn_detector.detect_pnet(img)
        boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c)
        boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c)

        print('shape: ', img.shape, '--', 'time: ', time.time() - t1)

        if boxes_c is not None:
            draw = img.copy()
            font = cv2.FONT_HERSHEY_SIMPLEX
            for b in boxes_c:
                cv2.rectangle(draw, (int(b[0]), int(b[1])),
                              (int(b[2]), int(b[3])), (0, 255, 255), 1)
                cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font,
                            0.4, (255, 255, 255), 1)

            cv2.imshow("detection result", draw)
        else:
            cv2.imshow("detection result", img)

        k = cv2.waitKey(1)
        if k == 27 or k == 113:  # Esc or q key to stop
            break
Exemple #15
0
def test_net(dataset_path,
             prefix,
             faceRecognize_model,
             epoch,
             batch_size,
             test_mode="onet",
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             margin=44,
             stride=2,
             slide_window=False):

    detectors = [None, None, None]

    model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)]
    if slide_window:
        PNet = Detector(P_Net, 12, batch_size[0], model_path[0])
    else:
        PNet = FcnDetector(P_Net, model_path[0])
    detectors[0] = PNet
    #load rnet model
    if test_mode in ["rnet", "onet"]:
        detectors[1] = Detector(R_Net, 24, batch_size[1], model_path[1])
    # load onet model
    if test_mode == "onet":
        detectors[2] = Detector(O_Net, 48, batch_size[2], model_path[2])

    # load faceRecognize model
    face_rec = FcnRecognize(faceRecognize_model, data_size=67, batch_size=16)
    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)
    ######load data
    input_dir0 = '/mllib/ALG/facenet-tensorflow/jz_80val'
    classes1 = os.listdir(input_dir0)
    message_path = []
    for cls1 in classes1:
        classes2_path = os.path.join(input_dir0, cls1)
        try:
            classes2 = os.listdir(classes2_path)
        except Exception as e:
            print e
            continue
        key = cv2.waitKey(1)
        if key == 'q':  ### when keycode is q
            print('====------======\n')
        img_list_tmp = []
        num_id = 0
        image_message = {}
        message = []
        meg_name = [
            'exit_id', 'img_read', 'img_detect', 'face_num', 'face_roi',
            'exit_person', 'score', 'person_name'
        ]
        ##########0 exit_id :Is there an id   ; 1 ,0
        ##########1 img_read :Whether to read successfully image  ; 1 ,0
        ##########2 img_detect :Whether to detect successfully image ; 1 ,0
        ##########3 face_num :The face amount  ; 0, 1, 2, 3, ...
        ##########4 face_roi :The face of a coordinate  ; (x1,y1,w1,h1),(x2,y2,w2,h2) ...
        ##########5 exit_person :The person amount ; 0, 1, 2, 3, ...
        ##########6 score :The person score ; 0, 1, 2, 3, ...
        ##########7 score :The person name ; name0, name1, name2, name3, ...

        for cls2 in classes2:
            classes3_path = os.path.join(classes2_path, cls2)
            print(classes3_path)
            if 1:  #classes2_path == "emb_check" :
                image_message[meg_name[0]] = 1
            else:
                num_id = 0
                image_message[meg_name[0]] = 0
                continue
            try:
                img = cv2.imread(classes3_path)
            except Exception as e:
                print e
                image_message[meg_name[1]] = 0
                continue
            #img = cv2.imread(image_name)#('test01.jpg')
            #img = cv2.imread(input_test+'.jpg')
            #cv2.imshow("img", img)
            #cv2.waitKey(0)
            image_message[meg_name[0]] = 1
            t1 = time.time()
            try:
                boxes, boxes_c = mtcnn_detector.detect_pnet(img)
            except Exception as e:
                image_message[meg_name[2]] = 0
                print e
                print(classes3_path)
                continue
            image_message[meg_name[2]] = 1
            #message.append("img_available")
            if boxes_c is None:
                image_message[meg_name[3]] = 0
                continue
            boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c)
            if boxes_c is None:
                image_message[meg_name[3]] = 0
                continue
            boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c)
            if boxes_c is None:
                image_message[meg_name[3]] = 0
                continue
            image_message[meg_name[3]] = len(boxes_c)
            print('box_num:%d', len(boxes_c))
            print 'time: ', time.time() - t1
            message.append("have_face")
            num_box = []
            if boxes_c is not None:
                img0 = misc.imread(classes3_path)
                img_size = np.asarray(img0.shape)[0:2]

                nrof_samples = len(boxes_c)

                for det in boxes_c:
                    bb = np.zeros(4, dtype=np.int32)
                    margin_tmp = ((det[3] - det[1]) - (det[2] - det[0])) / 2
                    if margin_tmp > 0:
                        size_tmp = (det[3] - det[1]) * 0
                        bb[0] = np.maximum(det[0] - margin_tmp - size_tmp, 0)
                        bb[1] = np.maximum(det[1] - size_tmp, 0)
                        bb[2] = np.minimum(det[2] + margin_tmp + size_tmp,
                                           img_size[1])
                        bb[3] = np.minimum(det[3] + size_tmp, img_size[0])
                    else:
                        size_tmp = (det[2] - det[0]) * 0
                        bb[0] = np.maximum(det[0] - size_tmp, 0)
                        bb[1] = np.maximum(det[1] + margin_tmp - size_tmp, 0)
                        bb[2] = np.minimum(det[2] + size_tmp, img_size[1])
                        bb[3] = np.minimum(det[3] - margin_tmp + size_tmp,
                                           img_size[0])
                    cropped = img0[int(bb[1]):int(bb[3]),
                                   int(bb[0]):int(bb[2]), :]
                    num_box.append(
                        "%d,%d,%d,%d" %
                        (int(bb[1]), int(bb[3]), int(bb[0]), int(bb[2])))
                    #misc.imsave('hebei/%d.png'%i, cropped)
                    #cropped=misc.imread('/home/ssd/fb_data/casic_cluster_china67/Jin Jong-oh/Jin Jong-oh_8.png')
                    #cv2.imshow("cropped", cropped)
                    #cv2.waitKey(500) ###
                    aligned = misc.imresize(cropped, (67, 67),
                                            interp='bilinear')
                    #cv2.imshow("aligned", aligned)
                    #cv2.waitKey(500) ###
                    #misc.imsave('hebei/%d.png'%i, aligned)
                    prewhitened = faceRecognize.prewhiten(aligned)
                    img_list_tmp.append(prewhitened)
                    key = cv2.waitKey(1)
            image_message[meg_name[4]] = num_box
            print(image_message)

        if len(img_list_tmp) < 2:  #1 :
            continue
        img_list = [None] * len(img_list_tmp)
        for i in range(len(img_list_tmp)):
            img_list[i] = img_list_tmp[i]
        images = np.stack(img_list)
        t_emb = time.time()
        emb = face_rec.recognize(images)

        np.save("./emb_check.txt", emb)
        print 'emb_1_time: ', time.time() - t_emb

        for i in range(len(img_list_tmp)):
            print('%1d ' % i)
            score_tmp = 65
            score2person = -1
            for j in range(len(img_list_tmp)):
                dist = np.sqrt(
                    np.sum(np.square(np.subtract(emb[i, :], emb[j, :]))))
                print('  %1.4f  ' % dict2score(dist))
                print('')
                if dict2score(dist) > score_tmp:
                    score_tmp = dict2score(dist)
                    score2person = j

            if score_tmp > 65:
                image_message[meg_name[6]] = score_tmp
                image_message[meg_name[7]] = 'num_0'

            else:
                image_message[meg_name[6]] = 0
                image_message[meg_name[7]] = 'num_0'
        print('-----====end compare !!===-----')
        print(image_message)
def test_net(prefix,
             epoch,
             batch_size,
             ctx,
             thresh=[0.6, 0.6, 0.7],
             min_face_size=24,
             stride=2,
             slide_window=False):

    detectors = [None, None, None]

    # load pnet model
    args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx)
    if slide_window:
        PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs)
    else:
        PNet = FcnDetector(P_Net("test"), ctx, args, auxs)
    detectors[0] = PNet

    # load rnet model
    args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx)
    RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs)
    detectors[1] = RNet

    # load onet model
    args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx)
    ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs)
    detectors[2] = ONet

    mtcnn_detector = MtcnnDetector(detectors=detectors,
                                   ctx=ctx,
                                   min_face_size=min_face_size,
                                   stride=stride,
                                   threshold=thresh,
                                   slide_window=slide_window)

    video_capture = cv2.VideoCapture(0)
    boxes = []
    boxes_c = []
    while True:
        #img = cv2.imread('/home/zzg/Opensource/mtcnn-master/data/custom/02.jpg')
        _, img = video_capture.read()
        t1 = time.time()

        boxes, boxes_c = mtcnn_detector.detect_pnet(img)
        if boxes_c is None:
            continue
        boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c)
        if boxes_c is None:
            continue
        boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c)

        print 'time: ', time.time() - t1

        if boxes_c is not None:
            draw = img.copy()
            font = cv2.FONT_HERSHEY_SIMPLEX
            for b in boxes_c:
                cv2.rectangle(draw, (int(b[0]), int(b[1])),
                              (int(b[2]), int(b[3])), (0, 255, 255), 1)
                #cv2.putText(draw, '%.3f'%b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1)

            cv2.imshow("detection result", draw)
            #cv2.imwrite("o12.jpg",draw)
            cv2.waitKey(10)