def onet_detector(image): """ :param image: 输入为48*48大小的图像 :return: 返回概率值 """ sym = O_Net('test') ctx = mx.cpu() # ctx = mx.gpu() # ctx = [mx.gpu(int(i)) for i in [0,1,2,3]] args, auxs = load_param('model/onet', 9, convert=False, ctx=ctx) data_size = 48 # landmark net 输入的图像尺寸为48*48 data_shapes = {'data': (1, 3, data_size, data_size)} # # img_resized = cv2.resize(image, (48, 48)) newimg = transform(image) args['data'] = mx.nd.array(newimg, ctx) executor = sym.simple_bind(ctx, grad_req='null', **dict(data_shapes)) executor.copy_params_from(args, auxs) executor.forward(is_train=False) # inference out_list = [[] for _ in range(len(executor.outputs))] for o_list, o_nd in zip(out_list, executor.outputs): o_list.append(o_nd.asnumpy()) out = list() for o in out_list: out.append(np.vstack(o)) cls_pro = out[0][0][1] return out
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 creat_mtcnn_detector(prefix, epoch, batch_size, test_mode, thresh, min_face_size, ctx): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) PNet = FcnDetector(P_Net20("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model if test_mode in ["onet", "hardrnet", "hardonet"]: 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 == "hardonet": args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test", False), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=4, threshold=thresh, slide_window=False) return mtcnn_detector
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)
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!')
def train_O_net(image_set, root_path, dataset_path, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, frequent, lr, resume): imdb = IMDB("mtcnn", image_set, root_path, dataset_path) gt_imdb = imdb.gt_imdb() gt_imdb = imdb.append_flipped_images(gt_imdb) sym = O_Net() train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, 48, frequent, not resume, lr)
def train_O_net(image_set, root_path, dataset_path, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, batch_size, thread_num, frequent, lr,lr_epoch, resume, with_landmark): imdb = IMDB("mtcnn", image_set, root_path, dataset_path, 'train') gt_imdb = imdb.get_annotations() sym = O_Net('train',with_landmark) train_net(sym, prefix, ctx, pretrained, epoch, begin_epoch, end_epoch, gt_imdb, batch_size, thread_num, 48, True, True, with_landmark, frequent, not resume, lr, lr_epoch)
def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=4, 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_Net20("test"), 20, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net20("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') t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet20(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)
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)
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)
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)
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 = MtcnnDetector1(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('zhang.jpeg') # 读取图片 # img = cv2.imread('curry.jpg') # 读取图片 # img = cv2.imread('physics.jpg') # 读取图片 # img = cv2.imread('000007.jpg') # 读取图片 # img = cv2.imread('test01.jpg') # 读取图片 # img = cv2.imread('NBA98.jpg') # img = cv2.imread('download.jpg') # img = cv2.imread('/Users/qiuxiaocong/Downloads/WIDER_train/images/7--Cheering/7_Cheering_Cheering_7_16.jpg') # img = cv2.imread('/Users/qiuxiaocong/Downloads/WIDER_train/images/11--Meeting/11_Meeting_Meeting_11_Meeting_Meeting_11_77.jpg') # img = cv2.imread('/Users/qiuxiaocong/Downloads/3Dfacedeblurring/dataset_test/falling1/input/00136.png') img = cv2.imread('/Users/qiuxiaocong/Downloads/facetrack_python/error.jpg') 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) # x1 y1 x2 y2 original_detect = [] crop_list = [] detect_len_list = [] nd_array = [] score_list = [] if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX # Python 一种字体 idx = 0 for b in boxes_c: # nms和bbr之后的结果 # 在draw上绘制矩形框(左上角坐标+右下角坐标) b_new0 = np.array(b[0:4]) # 添加检测框 original_detect.append(b_new0) b_new = convert_to_square(b_new0) # 添加送入到landmark net的48*48大小的框 crop_list.append(b_new) score_list.append(b[4]) # cv2.rectangle(draw, (int(b_new[0]), int(b_new[1])), (int(b_new[2]), int(b_new[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) img_draw = img[int(b_new[1]):int(b_new[3]), int(b_new[0]):int(b_new[2])] detect_len = min(img_draw.shape[0], img_draw.shape[1]) # print(img_draw.shape[0], img_draw.shape[1]) if detect_len != 0: detect_len_list.append(detect_len) img_resized = cv2.resize(img_draw, (48, 48)) # cv2.imshow("detection result", draw) # print('img_resized type is :{}'.format(type(img_resized))) nd_array.append(img_resized) # cv2.imwrite("detection_result{}.jpg".format(idx), img_resized) # cv2.waitKey(0) idx = idx + 1 return crop_list, detect_len_list, original_detect, idx, img, nd_array
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)
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 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
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)