def test(stage, testFolder): print("Start testing in %s"%(testFolder)) detectors = [None, None, None] if stage in ['pnet', 'rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/pnet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('pnet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) # auto match a max epoch model modelPath = os.path.join(modelPath, "pnet-%d"%(maxEpoch)) print("Use PNet model: %s"%(modelPath)) detectors[0] = FcnDetector(P_Net,modelPath) if stage in ['rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/rnet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('rnet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "rnet-%d"%(maxEpoch)) print("Use RNet model: %s"%(modelPath)) detectors[1] = Detector(R_Net, 24, 1, modelPath) if stage in ['onet']: modelPath = os.path.join(rootPath, 'tmp/model/onet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('onet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "onet-%d"%(maxEpoch)) print("Use ONet model: %s"%(modelPath)) detectors[2] = Detector(O_Net, 48, 1, modelPath) mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size = 24, threshold=[0.9, 0.6, 0.7]) testImages = [] for name in os.listdir(testFolder): testImages.append(os.path.join(testFolder, name)) testDatas = TestLoader(testImages) # Now to detect allBoxes, allLandmarks = mtcnnDetector.detect_face(testDatas) print("\n") # Save it for idx, imagePath in enumerate(testImages): savePath = os.path.join(rootPath, 'testing', 'results_%s'%(stage)) if not os.path.isdir(savePath): os.makedirs(savePath) image = cv2.imread(imagePath) save_bboxes(savePath,idx,image,allBoxes[idx]) for bbox in allBoxes[idx]: cv2.putText(image,str(np.round(bbox[4],2)),(int(bbox[0]),int(bbox[1])),cv2.FONT_HERSHEY_TRIPLEX,1,color=(255,0,255)) cv2.rectangle(image, (int(bbox[0]),int(bbox[1])),(int(bbox[2]),int(bbox[3])),(0,0,255)) allLandmark = allLandmarks[idx] if allLandmark is not None: # pnet and rnet will be ignore landmark for landmark in allLandmark: for i in range(len(landmark)/2): cv2.circle(image, (int(landmark[2*i]),int(int(landmark[2*i+1]))), 3, (0,0,255)) cv2.imwrite(os.path.join(savePath, "result_%d.jpg" %(idx)), image) print("Save image to %s"%(savePath))
def main(args): '''通过PNet或RNet生成下一个网络的输入''' size = args.input_size batch_size = config.batches min_face_size = config.min_face stride = config.stride thresh = config.thresh #模型地址 model_path = ['../model/PNet/', '../model/RNet/', '../model/ONet'] if size == 12: net = 'PNet' save_size = 24 elif size == 24: net = 'RNet' save_size = 48 # 图片数据地址 base_dir = 'g:/mtcnn-dataset/data/WIDER_train/' # 处理后的图片存放地址 data_dir = 'g:/mtcnn-dataset/data/%d' % (save_size) neg_dir = os.path.join(data_dir, 'negative') pos_dir = os.path.join(data_dir, 'positive') part_dir = os.path.join(data_dir, 'part') for dir_path in [neg_dir, pos_dir, part_dir]: if not os.path.exists(dir_path): os.makedirs(dir_path) detectors = [None, None, None] PNet = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet if net == 'RNet': RNet = Detector(R_Net, 24, batch_size[1], model_path[1]) detectors[1] = RNet filename = '../data/wider_face_train_bbx_gt.txt' #读取文件的image和box对应函数在utils中 data = read_annotation(base_dir, filename) mtcnn_detector = MtcnnDetector(detectors, min_face_size=min_face_size, stride=stride, threshold=thresh) save_path = data_dir save_file = os.path.join(save_path, 'detections.pkl') if not os.path.exists(save_file): #将data制作成迭代器 print('载入数据') test_data = TestLoader(data['images']) detectors, _ = mtcnn_detector.detect_face(test_data) print('完成识别') with open(save_file, 'wb') as f: pickle.dump(detectors, f, 1) print('开始生成图像') save_hard_example(save_size, data, neg_dir, pos_dir, part_dir, save_path)
def test(stage, testFolder): print("Start testing in %s"%(testFolder)) detectors = [None, None, None] if stage in ['pnet', 'rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/pnet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('pnet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) # auto match a max epoch model modelPath = os.path.join(modelPath, "pnet-%d"%(maxEpoch)) print("Use PNet model: %s"%(modelPath)) detectors[0] = FcnDetector(P_Net,modelPath) if stage in ['rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/rnet/model/middle/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('rnet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "rnet-%d"%(maxEpoch)) print("Use RNet model: %s"%(modelPath)) detectors[1] = Detector(R_Net, 24, 1, modelPath) if stage in ['onet']: modelPath = os.path.join(rootPath, 'tmp/onet/model/small/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('onet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "onet-%d"%(maxEpoch)) print("Use ONet model: %s"%(modelPath)) detectors[2] = Detector(O_Net, 48, 1, modelPath) mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size =12, threshold=[0.6, 0.6, 0.7],scale_factor=0.7) testImages = [] for name in os.listdir(testFolder): testImages.append(os.path.join(testFolder, name)) print("\n") right_num=0 miss_num=0 FN=0 # Save it for idx, imagePath in enumerate(testImages): if(idx<=6000): image = cv2.imread(imagePath) image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) print(right_num,FN,miss_num) try: allBoxes, allLandmarks = mtcnnDetector.detect_face([image]) if(allBoxes.__len__()==1): right_num+=1 else: FN+=(allBoxes.__len__()-1) except: miss_num+=1 pass else: break
def test(stage): detectors = [None, None, None] if stage in ['pnet', 'rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/pnet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('pnet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) # auto match a max epoch model modelPath = os.path.join(modelPath, "pnet-%d"%(maxEpoch)) print("Use PNet model: %s"%(modelPath)) detectors[0] = FcnDetector(P_Net,modelPath) if stage in ['rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/rnet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('rnet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "rnet-%d"%(maxEpoch)) print("Use RNet model: %s"%(modelPath)) detectors[1] = Detector(R_Net, 24, 1, modelPath) if stage in ['onet']: modelPath = os.path.join(rootPath, 'tmp/model/onet/') a = [b[5:-6] for b in os.listdir(modelPath) if b.startswith('onet-') and b.endswith('.index')] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "onet-%d"%(maxEpoch)) print("Use ONet model: %s"%(modelPath)) detectors[2] = Detector(O_Net, 48, 1, modelPath) mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size =50, threshold=[0.8, 0.8, 0.9],scale_factor=0.4) cap = cv2.VideoCapture(0) while(True): testImages = [] ret, image = cap.read() image=cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) testImages.append(image) # Now to detect starttime=time.time() allBoxes, allLandmarks = mtcnnDetector.detect_face(testImages) # print("\n") # Save it # print(time.time()-starttime) for bbox in allBoxes[0]: cv2.putText(image,str(np.round(bbox[4],2)),(int(bbox[0]),int(bbox[1])),cv2.FONT_HERSHEY_TRIPLEX,1,color=(255,0,255)) cv2.rectangle(image, (int(bbox[0]),int(bbox[1])),(int(bbox[2]),int(bbox[3])),(0,0,255)) allLandmark = allLandmarks[0] if allLandmark is not None: # pnet and rnet will be ignore landmark for landmark in allLandmark: for i in range(int(len(landmark)/2)): cv2.circle(image, (int(landmark[2*i]),int(int(landmark[2*i+1]))), 3, (0,0,255)) cv2.imshow("test", image) c = cv2.waitKey(1) & 0xFF if c == 27 or c == ord('q'): break
def test_net(batch_size, stage, thresh, min_face_size, stride): print(">>>>>> Detect bbox for %s..." % (stage)) detectors = [None, None, None] if stage in ["rnet", "onet"]: modelPath = os.path.join(rootPath, 'tmp/model/pnet/') # 第几个checkpoint a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('pnet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "pnet-%d" % (maxEpoch)) print("Use PNet model: %s" % (modelPath)) PNet = FcnDetector(P_Net, modelPath) detectors[0] = PNet if stage in ["onet"]: modelPath = os.path.join(rootPath, 'tmp/model/rnet/') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('rnet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "rnet-%d" % (maxEpoch)) print("Use RNet model: %s" % (modelPath)) RNet = Detector(R_Net, 24, batch_size, modelPath) detectors[1] = RNet # read annatation(type:dict) widerImagesPath = os.path.join(rootPath, "dataset", "WIDER_train", "images") annoTxtPath = os.path.join(rootPath, "dataset", "wider_face_train_bbx_gt.txt") # data['images'], data['bboxes'] data = read_wider_annotation(widerImagesPath, annoTxtPath) mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh) test_data = TestLoader(data['images']) # do detect detections, _ = mtcnn_detector.detect_face(test_data) # save detect result save_path = os.path.join(rootPath, "tmp/data", stage) if not os.path.exists(save_path): os.makedirs(save_path) save_file = os.path.join(save_path, "detections.pkl") with open(save_file, 'wb') as f: pickle.dump(detections, f, 1) print("\nDone! Start to do OHEM...") __save_data(stage, data, save_path)
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
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 realse(stack): print('Begin to get frame......') os.environ["CUDA_VISIBLE_DEVICES"] = '1' detectors = net('onet') mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size=24, threshold=[0.9, 0.6, 0.7]) while True: if len(stack) > 30: image = stack.pop() image = cv2.resize(image, (int(image.shape[1]), int(image.shape[0]))) image = np.array(image) boxes_c, _ = mtcnnDetector.detect_video(image) for bbox in boxes_c: x1 = int(bbox[0]) y1 = int(bbox[1]) x2 = int(bbox[2]) y2 = int(bbox[3]) cv2.putText(image, str(np.round(bbox[4], 2)), (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_TRIPLEX, 0.3, color=(0, 255, 0)) cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255)) # cut = image[y1:y2, x1:x2] # for i in range(len(boxes_c)): # cv2.imwrite(str(i) + '.jpg', cut) print('deteced face: ({},{}), ({},{})'.format(x1, y1, x2, y2)) cv2.imshow("Detected", image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
def load_mtcnn(): MODEL_PATH = config.MTCNN_MODEL_PATH MIN_FACE_SIZE = int(config.MIN_FACE_SIZE) STEPS_THRESHOLD = [float(i) for i in config.STEPS_THRESHOLD.split(",")] detectors = [None, None, None] prefix = [MODEL_PATH + "/PNet_landmark/PNet", MODEL_PATH + "/RNet_landmark/RNet", MODEL_PATH + "/ONet_landmark/ONet"] epoch = [18, 14, 16] model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)] PNet = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet RNet = Detector(R_Net, 24, 1, model_path[1]) detectors[1] = RNet ONet = Detector(O_Net, 48, 1, model_path[2]) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=MIN_FACE_SIZE, threshold=STEPS_THRESHOLD) return mtcnn_detector
model_path = ['model/PNet/', 'model/RNet/', 'model/ONet'] batch_size = config.batches PNet = PDetector(P_Net, model_path[0]) detectors[0] = PNet #判断最后测试选择的网络 if test_mode in ["RNet", "ONet"]: RNet = Detector(R_Net, 24, batch_size[1], model_path[1]) detectors[1] = RNet 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) #设定输出路径 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]
def test(stage, testFolder): print("Start testing in %s" % (testFolder)) detectors = [None, None, None] if stage in ['pnet', 'rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/origin/model/pnet') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('pnet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) # auto match a max epoch model modelPath = os.path.join(modelPath, "pnet-%d" % (maxEpoch)) print("Use PNet model: %s" % (modelPath)) detectors[0] = FcnDetector(P_Net, modelPath) if stage in ['rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/origin/model/rnet') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('rnet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "rnet-%d" % (maxEpoch)) print("Use RNet model: %s" % (modelPath)) detectors[1] = Detector(R_Net, 24, 1, modelPath) if stage in ['onet']: modelPath = os.path.join(rootPath, 'tmp/origin/model/onet') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('onet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "onet-%d" % (maxEpoch)) print("Use ONet model: %s" % (modelPath)) detectors[2] = Detector(O_Net, 48, 1, modelPath) mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size=50, threshold=[0.8, 0.9, 0.9]) fileFoldName = "faceListInt.txt" outFilename = 'F:/software/yansan/MTCNN-on-FDDB-Dataset-master/FDDB-folds/' + 'predict.txt' # fileOutName foldFilename = 'F:/software/yansan/MTCNN-on-FDDB-Dataset-master/FDDB-folds/' + fileFoldName prefixFilename = 'E:/database/FDDB_Face Detection Data Set and Benchmark/' fout = open(outFilename, 'a+') f = open(foldFilename, 'r') # FDDB-fold-00.txt, read for imgpath in tqdm(f.readlines()): testImages = [] imgpath = imgpath.split('\n')[0] # foutOnce.write(imgpath+'\n') # foutFold.write(imgpath+'\r') img = cv2.imread(prefixFilename + imgpath + '.jpg') img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) if img is None: continue testImages.append(img) boundingboxes, points = mtcnnDetector.detect_face(testImages) # boundingboxes, points = demo.detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor) text1 = str(imgpath) + '\n' + str(len(boundingboxes[0])) + '\n' fout.write(text1) # FDDB-fold-%02d-out.txt or predict.txt for bbox in boundingboxes[0]: # print(bbox,"???") text2 = str(int(bbox[0])) + ' ' + str(int(bbox[1])) + ' ' \ + str(abs(int(bbox[2] - bbox[0]))) + ' ' \ + str(abs(int(bbox[3] - bbox[1]))) + ' ' \ + str(bbox[4]) + '\n' fout.write(text2) # FDDB-fold-%02d-out.txt or predict.txt # text2 = str(int(boundingboxes[coordinate][0][0])) # fout.write(text2) # FDDB-fold-%02d-out.txt or predict.txt # print error f.close() # input the fold list, FDDB-fold-00.txt fout.close() # output the result, predict.txt
PNet = PDetector(P_Net, 12, batch_size[0], model_path[0]) else: PNet = PDetector(P_Net, model_path[0]) detectors[0] = PNet # load rnet model RNet = RODetector(R_Net, 24, batch_size[1], model_path[1]) detectors[1] = RNet # load onet model 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) gt_imdb = [] path = "xixi" for item in os.listdir(path): gt_imdb.append(os.path.join(path, item)) test_data = TestLoader(gt_imdb) all_boxes, landmarks = mtcnn_detector.detect_face(test_data) count = 0 for imagepath in gt_imdb: image = cv2.imread(imagepath)
model_path = ['model/PNet/', 'model/RNet/', 'model/ONet'] batch_size = [2048, 256, 32] PNet = FcnDetector(P_Net, model_path[0]) # detecotors for PNet detectors[0] = PNet # in and output path path = 'picture' out_path = 'output' detectors[1] = Detector(R_Net, 24, batch_size[1], model_path[1]) detectors[2] = Detector(O_Net, 48, batch_size[2], model_path[2]) # Use the three detectors to construct a mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh, scale_factor=0.909) #%% #%% def box_of_landmarks(bboxes, landmark): """ pick the box contains all the landmarks in bboxes :bboxes: all bboxes, must be shape of (_, 5), sorted descending by probility :landmarks: """ assert landmark.shape == (10, ), 'error landmark shape' assert (bboxes.shape[0] > 0) & (bboxes.shape[1] == 5), 'error bboxes shape'
# 模型放置位置 model_path = ['model/PNet/', 'model/RNet/', 'model/ONet'] batch_size = config.batches detectors[0] = FcnDetector(P_Net, model_path[0]) # detecotors for PNet if test_mode in ['RNet', 'ONet']: detectors[1] = Detector(R_Net, 24, batch_size[1], model_path[1]) if test_mode == 'ONet': detectors[2] = Detector(O_Net, 48, batch_size[2], model_path[2]) # Use the three detectors to construct a mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh, scale_factor=scale_factor) 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)
# 模型放置位置 model_path = ['model/PNet/', 'model/RNet/', 'model/ONet'] batch_size = config.batches PNet = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet if test_mode in ["RNet", "ONet"]: RNet = Detector(R_Net, 24, batch_size[1], model_path[1]) detectors[1] = RNet 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) def oneImg(path): img = cv2.imread(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),
cv2.putText(image, str(name), (left, top - 10), cv2.FONT_HERSHEY_TRIPLEX, 1, color=(0, 255, 0)) cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2) cv2.imwrite(str(img_path) + '.jpg', image) cv2.imshow('Recognition_Results', image) cv2.waitKey(500) cv2.destroyAllWindows() if __name__ == '__main__': logset() detectors = net('onet') os.environ["CUDA_VISIBLE_DEVICES"] = '1' mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size=40, threshold=[0.9, 0.6, 0.7]) for image_file in os.listdir("/home/lxz/project/faceid/datasets/test"): full_file_path = os.path.join("/home/lxz/project/faceid/datasets/test", image_file) predictions = predict( full_file_path, model_path="/home/lxz/project/faceid/LFW_classifier_model.clf") show_results( os.path.join("/home/lxz/project/faceid/datasets/test", image_file), predictions)
def recognize(stack): logging.info("[INFO]:Starting video stream...") # os.environ["CUDA_VISIBLE_DEVICES"] = '1' # config = tf.ConfigProto() # config.gpu_options.allow_growth = True # session = tf.Session(config = config) data = pickle.loads( open('/home/lxz/project/faceid/alignment.pickle', "rb").read()) detectors = net('onet') mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size=60, threshold=[0.9, 0.6, 0.7]) logging.info('MTCNN/KNN Model load sucessed !!!!') while True: if len(stack) > 20: boxes = [] frame = stack.pop() image = np.array(frame) allBoxes, _ = mtcnnDetector.detect_video(image) for box in allBoxes: x_1 = int(box[0]) y_1 = int(box[1]) x_2 = int(box[2]) y_2 = int(box[3]) boxes.append((y_1 - 10, x_2 + 12, y_2 + 10, x_1 - 12)) logging.debug(boxes) start = time.time() # num_jitters(re-sample人脸的次数)参数的设定,数值越大精度相对会高,但是速度会慢; encodings = face_recognition.face_encodings(frame, boxes, num_jitters=6) end = time.time() logging.info('[INFO]:Encoding face costed: {} s'.format(end - start)) print('encode time is {}ms'.format((end - start) * 1000)) names = [] for encoding in encodings: # distance between faces to consider it a match, optimize is 0.6 matches = face_recognition.compare_faces(data['encodings'], encoding, tolerance=0.35) name = 'Stranger' if True in matches: matchesidx = [i for (i, b) in enumerate(matches) if b] counts = {} for i in matchesidx: name = data['names'][i] counts[name] = counts.get(name, 0) + 1 name = max(counts, key=counts.get) logging.debug(name) names.append(name) # 绘制检测框 + 人脸识别结果 for ((top, right, bottom, left), name) in zip(boxes, names): # print(name) y1 = int(top) x1 = int(right) y2 = int(bottom) x2 = int(left) cv2.rectangle(frame, (x2, y1), (x1, y2), (0, 0, 255), 2) if name == 'Stranger': cv2.putText(frame, name, (x2, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) else: print(name) cv2.putText(frame, name, (x2, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow('Recognize-no-alignment', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
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 = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet RNet = Detector(R_Net, 24, 1, model_path[1]) detectors[1] = RNet ONet = Detector(O_Net, 48, 1, 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) 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)
def test(stage, profiling): print("Start Detecting") detectors = [None, None, None] if stage in ['pnet', 'rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/pnet/') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('pnet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) # auto match a max epoch model modelPath = os.path.join(modelPath, "pnet-%d" % (maxEpoch)) print("Use PNet model: %s" % (modelPath)) detectors[0] = FcnDetector(P_Net, modelPath, profiling) if stage in ['rnet', 'onet']: modelPath = os.path.join(rootPath, 'tmp/model/rnet/') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('rnet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "rnet-%d" % (maxEpoch)) print("Use RNet model: %s" % (modelPath)) detectors[1] = Detector(R_Net, 24, 1, modelPath, profiling) if stage in ['onet']: modelPath = os.path.join(rootPath, 'tmp/model/onet/') a = [ b[5:-6] for b in os.listdir(modelPath) if b.startswith('onet-') and b.endswith('.index') ] maxEpoch = max(map(int, a)) modelPath = os.path.join(modelPath, "onet-%d" % (maxEpoch)) print("Use ONet model: %s" % (modelPath)) detectors[2] = Detector(O_Net, 48, 1, modelPath, profiling) mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size=24, threshold=[0.9, 0.6, 0.7]) # Now to detect camID = 0 cap = cv2.VideoCapture(camID) while True: ret, image = cap.read() if ret == 0: break [h, w] = image.shape[:2] print(h, w) #image_data = cv2.flip(image, 1) #image_data = cv2.flip(image, 1) image_data = image start_time = time.time() testImages = [] testImages.append(image_data) allBoxes, allLandmarks = mtcnnDetector.detect_face(testImages) inf_time = time.time() - start_time print("inference time(s): {}".format(inf_time)) del testImages[0] #print("allBoxes: {}".format(allBoxes)) #print("allLandmarks: {}".format(allLandmarks)) #print("\n") # Save it if (len(allBoxes) >= 1): for idx, bbox in enumerate(allBoxes): cv2.putText(image_data, str(np.round(bbox[idx][4], 2)), (int(bbox[idx][0]), int(bbox[idx][1])), cv2.FONT_HERSHEY_TRIPLEX, 1, color=(255, 0, 255)) cv2.rectangle(image_data, (int(bbox[idx][0]), int(bbox[idx][1])), (int(bbox[idx][2]), int(bbox[idx][3])), (0, 0, 255)) allLandmark = allLandmarks[idx][0].tolist() total_landmark_pts = len(allLandmark) if allLandmark is not None and len( allLandmark ) == 10: # pnet and rnet will be ignore landmark for index, landmark in enumerate(allLandmark): for i in range(int(total_landmark_pts / 2)): cv2.circle(image_data, (int(allLandmark[2 * i]), int(int(allLandmark[2 * i + 1]))), 3, (255, 255, 255)) cv2.imshow('Face/Landmark Detection', image_data) k = cv2.waitKey(1) & 0xff if k == ord('q') or k == 27: break cap.release()
def realse(stack): # 解决GPU内存占用问题 import tensorflow as tf os.environ["CUDA_VISIBLE_DEVICES"] = '0' config = tf.ConfigProto() #config.gpu_options.per_process_gpu_memory_fraction = 0.3 config.gpu_options.allow_growth = True session = tf.Session(config=config) print('Begin to get frame......') max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=1) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) # mtcnn detectors = mtcnn('onet') mtcnnDetector = MtcnnDetector(detectors=detectors, min_face_size=24, threshold=[0.9, 0.6, 0.7]) while True: if len(stack) >= 20: frame = stack.pop() #frame = cv2.resize(frame, (int(frame.shape[1]/3), int(frame.shape[0]/3))) frame = np.array(frame) # 输出tmp信息为[x,y,w,h]; x:检测框左上角点的x坐标;y:检测框左上角y坐标;w:框宽;h:框高; # 原MTCNN输出的信息为检测框一对角点的坐标信息(左上角点、右下角点),以及检测为人脸的概率值[x1,y2,x2,y2,置信度]; tmp, _ = mtcnnDetector.detect_follow(frame) features = encoder(frame, tmp) detections = [ Detection(bbox, 1.0, feature) for bbox, feature in zip(tmp, features) ] # Run non-maxima suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression( boxes, nms_max_overlap, scores) detections = [detections[i] for i in indices] # Call the tracker tracker.predict() tracker.update(detections) for track in tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue bbox = track.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 0), 1) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 0.5, (0, 255, 0), 2) for det in detections: bbox = det.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 0, 255), 1) cv2.imshow("Detected", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()