class face_detect(object): def __init__(self): self.load_model() def load_model(self): thresh = [0.6, 0.7, 0.7] min_face_size = 20 stride = 2 slide_window = False detectors = [None, None, None] prefix = [ './weight/PNet_landmark/PNet', './weight/RNet_landmark/RNet', './weight/ONet_landmark/ONet' ] epoch = [18, 14, 16] model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)] PNet, RNet, ONet = FcnDetector(P_Net, model_path[0]), Detector(R_Net, 24, 1, model_path[1]), \ Detector(O_Net, 48, 1, model_path[2]) detectors[0], detectors[1], detectors[2] = PNet, RNet, ONet self.mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) def detect(self, img, show_pic=False): """ :param img: BGR格式的图片 :param show_pic: 是否展示图片 :return:boxes_c type is ndarray, shape is (M, 5) """ boxes_c, landmarks = self.mtcnn_detector.detect(img) if show_pic: for i in range(boxes_c.shape[0]): bbox = boxes_c[i, :4] score = boxes_c[i, 4] corpbbox = [ int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) ] cv2.rectangle(frame, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (255, 0, 0), 2) cv2.putText(frame, '{:.3f}'.format(score), (corpbbox[0], corpbbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 3) for i in range(landmarks.shape[0]): for j in range(int(len(landmarks[i]) / 2)): cv2.circle(frame, (int(landmarks[i][2 * j]), int(int(landmarks[i][2 * j + 1]))), 2, (0, 0, 255)) cv2.putText(frame, 'handsome men', (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (127, 255, 212), 2) cv2.imshow("", frame) if cv2.waitKey(0) & 0xFF == ord('q'): cv2.destroyAllWindows() return boxes_c
mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh) #设定输出路径 out_path = config.out_path #两种输入方式,1为图片,2为摄像头 if config.input_mode == '1': #设定测试图片的路径 path = config.test_dir #print(path) for item in os.listdir(path): img_path = os.path.join(path, item) img = cv2.imread(img_path) #获知图片中的人脸框和关键点 boxes_c, landmarks = mtcnn_detector.detect(img) for i in range(boxes_c.shape[0]): bbox = boxes_c[i, :4] score = boxes_c[i, 4] corpbbox = [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])] #画人脸框 cv2.rectangle(img, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (255, 0, 0), 1) #判别为人脸的置信度 cv2.putText(img, '{:.2f}'.format(score), (corpbbox[0], corpbbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) #画关键点 for i in range(landmarks.shape[0]): for j in range(len(landmarks[i]) // 2): cv2.circle(img, (int(
start_path = '/home/sherk/Workspace/mtcnn-pytorch/' filenames = os.listdir(start_path + 'img') missing_detection = 0 false_detection = 0 all_detection = 0 all_labels = 0 for filename in tqdm(filenames): iou_threshold = 0.4 image = start_path + 'img/{}'.format(filename) img = cv2.imread(image) boxes_det, _ = mtcnn_detector.detect(img) boxes_det = boxes_det.astype(np.int) xml_file = start_path + 'anno/{}.xml'.\ format( '.'.join( filename.split('.')[:-1] ) ) boxes_lab = boxes_extract(xml_file) if boxes_lab is None: if boxes_det is not None: false_detection += len(boxes_det) all_detection += len(boxes_det) continue if boxes_det.shape[0] == 0: if boxes_lab is not None:
return True if ((xs > bbox[0]).all() & (xs < bbox[2]).all()) &\ ((ys > bbox[1]).all() & (ys < bbox[3]).all()) else False #%% generate the landmark file celeba_landmark_list = open('data/celeba_trainImageList.txt', 'w') err_idx = 0 for i in tqdm(range(sub_labels.shape[0])): src_img = join(img_dir, sub_labels.image_id.iloc[i]) src_write = join('Img/img_celeba', sub_labels.image_id.iloc[i]) # des_img = join(out_path, sub_labels.image_id.iloc[i]) img = cv2.imread(src_img) bboxes, landmarks = mtcnn_detector.detect(img) label_landmark = sub_labels.iloc[i, -10:].values if (bboxes.shape[0] > 0): if (in_box(label_landmark, bboxes[0, :])): bboxes_list = [ str(x) for x in list(bboxes[0, [0, 2, 1, 3]].astype(np.int)) ] landmarks_list = [str(x) for x in list(label_landmark)] ln = ' '.join([src_write] + bboxes_list + landmarks_list) + '\n' celeba_landmark_list.write(ln) else: err_idx += 1
min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) video_capture = cv2.VideoCapture(0) video_capture.set(3, 500) video_capture.set(4, 600) corpbbox = None while True: # fps = video_capture.get(cv2.CAP_PROP_FPS) t1 = cv2.getTickCount() ret, frame = video_capture.read() if ret: image = np.array(frame) boxes_c, landmarks = mtcnn_detector.detect(image) print(landmarks.shape) t2 = cv2.getTickCount() t = (t2 - t1) / cv2.getTickFrequency() fps = 1.0 / t for i in range(boxes_c.shape[0]): bbox = boxes_c[i, :4] score = boxes_c[i, 4] corpbbox = [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])] # if score > thresh: cv2.rectangle(frame, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (255, 0, 0), 2) cv2.putText(frame, '{:.3f}'.format(score), (corpbbox[0], corpbbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 3)