def net(stage): detectors = [None, None, None] if stage in ['pnet', 'rnet', 'onet']: modelPath = '/home/lxz/project/faceid/main/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)) logging.info("Use PNet model: %s" % (modelPath)) detectors[0] = FcnDetector(P_Net, modelPath) if stage in ['rnet', 'onet']: modelPath = '/home/lxz/project/faceid/main/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)) logging.info("Use RNet model: %s" % (modelPath)) detectors[1] = Detector(R_Net, 24, 1, modelPath) if stage in ['onet']: modelPath = '/home/lxz/project/faceid/main/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)) logging.info("Use ONet model: %s" % (modelPath)) detectors[2] = Detector(O_Net, 48, 1, modelPath) return detectors
def detect_single_face(self, img): """ used for detecting single face or vidio input : images output: bounding_box , landmark_box format of input : img : np.array() format of output : bounding_box : list of box [face_x1,face_x2,face_y1,face_y2] landmark_box : list of box [5*(landmark_x,landmark_y)] """ bounding_box = [] landmark_box = [] detect_begin = time.time() if (img is None): return [], [] if self.pnet_model: pt = time.time() pnet_detector = Detector(self.pnet_model, self.model_path[0], self.batch_size) score_box, bounding_box, landmark_box = self.detect_Pnet( pnet_detector, img) print("pnet-time: ", time.time() - pt) if ((bounding_box is None) or (len(bounding_box) == 0)): return [], [] if self.rnet_model: rt = time.time() batch_size = len(bounding_box) rnet_detector = Detector(self.rnet_model, self.model_path[1], batch_size) score_box, bounding_box, landmark_box = self.detect_Rnet( rnet_detector, img, bounding_box) print("rnet-time: ", time.time() - rt) if ((bounding_box is None) or (len(bounding_box) == 0)): return [], [] if self.onet_model: ot = time.time() batch_size = len(bounding_box) onet_detector = Detector(self.onet_model, self.model_path[2], batch_size) score_box, bounding_box, landmark_box = self.detect_Onet( onet_detector, img, bounding_box) print("onet-time: ", time.time() - ot) if ((bounding_box is None) or (len(bounding_box) == 0)): return [], [] print("detect-time: ", time.time() - detect_begin) return bounding_box, landmark_box
def test_lfw(img_dir, anno_file): thresh = config.thresh min_face_size = config.min_face stride = config.stride batch_size = config.batches detectors = [None, None, None] # 模型放置位置 model_path = [ 'checkpoint/pnet/pnet-30', 'checkpoint/rnet/rnet-22', 'checkpoint/onet/onet-30' ] detectors[0] = PNetDetector(p_net, model_path[0]) detectors[1] = Detector(r_net, 24, batch_size[1], model_path[1]) detectors[2] = Detector(o_net, 48, batch_size[2], model_path[2]) mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh) labelfile = open(anno_file, 'r') while True: imagepath = labelfile.readline().replace('\\', '/').strip('\n').split(' ') if not imagepath: break path = imagepath[0] bboxs = imagepath[1:5] landmarks = imagepath[5:] imagepath = os.path.join(img_dir, path) img = cv.imread(imagepath) 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])] # 画人脸框 cv.rectangle(img, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (255, 0, 0), 1) # 判别为人脸的置信度 cv.putText(img, '{:.2f}'.format(score), (corpbbox[0], corpbbox[1] - 2), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # 画关键点 for i in range(landmarks.shape[0]): for j in range(len(landmarks[i]) // 2): cv.circle(img, (int( landmarks[i][2 * j]), int(int(landmarks[i][2 * j + 1]))), 2, (0, 0, 255), -1) cv.imshow('im', img) if cv.waitKey() & 0xFF == ord('q'): break cv.destroyAllWindows()
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 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 __init__(self, model, model_path, batch_size, factor, min_face_size, threshold): self.pnet_model = model[0] self.rnet_model = model[1] self.onet_model = model[2] self.model_path = model_path self.batch_size = batch_size self.factor = factor self.min_face_size = min_face_size self.threshold = threshold self.pnet_detector = Detector(self.pnet_model, self.model_path[0], "Pnet", self.batch_size) self.rnet_detector = Detector(self.rnet_model, self.model_path[1], "Rnet", -1) self.onet_detector = Detector(self.onet_model, self.model_path[2], "Onet", -1)
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
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 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 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)
def build(base, anchor_generator_params, num_classes, features, use_depthwise=False, extras={}, predictor={}, heads={}): extra_layers = extras.get('layers', []) Features = getattr(_features, features['name']) features = Features(base, use_depthwise=use_depthwise, **features) num_scales = features.num_outputs + len(extra_layers) source_out_channels = features.get_out_channels() anchor_generator_builder = getattr( _anchor_generators, anchor_generator_params['type']).build_anchor_generators anchor_generators = anchor_generator_builder(**anchor_generator_params) assert num_scales == len(anchor_generators) num_boxes = [x.num_boxes for x in anchor_generators] extras = get_extras(source_out_channels, use_depthwise=use_depthwise, **extras) predictor = get_predictor(source_out_channels, num_boxes, num_classes, use_depthwise, predictor_args=predictor) out_channels = predictor.out_channels if predictor else source_out_channels heads = get_heads(out_channels, num_boxes, num_classes, **heads) return Detector(features, extras, predictor, heads, num_classes, anchor_generators=anchor_generators)
def build(base, anchor_generator_params, num_classes, features, use_depthwise=False, extras={}, predictor={}): extra_layers = extras.get('layers', []) # backward compatibility # ToDo: remove source_layers = features['out_layers'] Features = get_ctor(_features, features['name']) features = Features(base, use_depthwise=use_depthwise, **features).eval() num_scales = features.num_outputs + len(extra_layers) source_out_channels = features.get_out_channels() anchor_generator = getattr( detection, anchor_generator_params['type']).anchor_generator priors = anchor_generator.get_priors(**anchor_generator_params) assert num_scales == len(priors) num_boxes = [x.num_boxes for x in priors] extras = get_extras(source_out_channels, use_depthwise=use_depthwise, **extras) predictor, heads = get_predictor(source_out_channels, num_boxes, num_classes, use_depthwise, **predictor) return Detector(features, extras, predictor, heads, source_layers, num_classes=num_classes, priors=priors)
def detect_face(self, images): """ used for detecting face in both batch images and single image input : images output: face_boxes , landmark_boxes format of input : img : np.array() batch_size*single_img format of output : face_boxes : list of face_box batch_size*[face_x1,face_x2,face_y1,face_y2] landmark_boxes : list of landmark_box batch_size*[5*(landmark_x,landmark_y)] """ sign = False bounding_box = [] landmark_box = [] face_boxes = [] landmark_boxes = [] detect_begin = time.time() if (np.size(images.shape) == 3): sign = True img = np.zeros( (1, images.shape[0], images.shape[1], images.shape[2])) img[0, :, :, :] = images images = img for img in images: if (img is None): face_boxes.append([]) landmark_boxes.append([]) continue if self.pnet_model: pt = time.time() pnet_detector = Detector(self.pnet_model, self.model_path[0], self.batch_size) score_box, bounding_box, landmark_box = self.detect_Pnet( pnet_detector, img) print("pnet-time: ", time.time() - pt) if ((bounding_box is None) or (len(bounding_box) == 0)): face_boxes.append([]) landmark_boxes.append([]) continue if self.rnet_model: rt = time.time() batch_size = len(bounding_box) rnet_detector = Detector(self.rnet_model, self.model_path[1], batch_size) score_box, bounding_box, landmark_box = self.detect_Rnet( rnet_detector, img, bounding_box) print("rnet-time: ", time.time() - rt) if ((bounding_box is None) or (len(bounding_box) == 0)): face_boxes.append([]) landmark_boxes.append([]) continue if self.onet_model: ot = time.time() batch_size = len(bounding_box) onet_detector = Detector(self.onet_model, self.model_path[2], batch_size) score_box, bounding_box, landmark_box = self.detect_Onet( onet_detector, img, bounding_box) print("onet-time: ", time.time() - ot) if ((bounding_box is None) or (len(bounding_box) == 0)): face_boxes.append([]) landmark_boxes.append([]) continue face_boxes.append(bounding_box) landmark_boxes.append(landmark_box) print("detect-time: ", time.time() - detect_begin) if (sign): return face_boxes[0], landmark_boxes[0] else: return face_boxes, landmark_boxes
def evalution(data_file, anno_file, evalu): batch_size = 1 detectors = [None, None, None] model_path = [ root_dir + '/MTCNN/checkpoint/pnet/pnet-30', root_dir + '/MTCNN/checkpoint/rnet/rnet-30', root_dir + '/MTCNN/checkpoint/onet/onet-30' ] detectors[0] = PNetDetector(p_net, model_path[0]) detectors[1] = Detector(r_net, 24, batch_size, model_path[1]) detectors[2] = Detector(o_net, 48, batch_size, model_path[2]) mtcnn_detector = MtcnnDetector(detectors) # data: {'images': images, 'bboxes': bboxes, 'landmarks':landmarks} data = read_annotation(data_file, anno_file) img_data = list(zip(data["images"], data["bboxes"], data["landmarks"])) local_nme = [] for img_path, img_bbox, img_landmarks in img_data: img = cv.imread(img_path) img_bbox = np.array(img_bbox) if evalu == "onet": t1 = time() boxes_c, landmarks = mtcnn_detector.evaluate_onet( img, img_bbox) # 1.5ms time:0.001844(s) print("time:%f(s)" % (time() - t1)) else: t1 = time() boxes_c, landmarks = mtcnn_detector.detect( img) # 300ms time:0.254378(s) print("time:%f(s)" % (time() - t1)) nme = calculate_nme(img_landmarks, landmarks, img_bbox) if nme != []: local_nme.extend(nme) # 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])] # box_gt = img_bbox[i, :4] # corpbbox_gt = [int(box_gt[0]), int(box_gt[1]), int(box_gt[2]), int(box_gt[3])] # # 画人脸框 # cv.rectangle(img, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (0, 0, 255), 2) # cv.rectangle(img, (corpbbox_gt[0], corpbbox_gt[1]), (corpbbox_gt[2], corpbbox_gt[3]), (0, 225, 255), 2) # # 判别为人脸的置信度 # cv.putText(img, '{:.2f}'.format(score), (corpbbox[0], corpbbox[1] - 2), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # # 画关键点 # for i in range(landmarks.shape[0]): # for j in range(len(landmarks[i]) // 2): # cv.circle(img, (int(landmarks[i][2 * j]), int(int(landmarks[i][2 * j + 1]))), 3, (0, 0, 255), -1) # cv.circle(img, (int(img_landmarks[i][2 * j]), int(int(img_landmarks[i][2 * j + 1]))), 3, (0, 255, 255), -1) # cv.imshow('show image', img) # if cv.waitKey(0) & 0xFF == ord('q'): # exit() # cv.destroyAllWindows() each_landmark = np.array(local_nme).T each_landmark_mean = np.mean(each_landmark, axis=1) print('each_landmark_mean: %s' % each_landmark_mean) global_mean_nme = np.mean(each_landmark_mean) print('mean nme: %s' % global_mean_nme)
#三个网络的阀值 thresh = config.thresh #设定最小脸的大小 min_face_size = config.min_face #pnet对图像的缩小倍数 stride = config.stride detectors = [None, None, None] # 模型放置位置 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': #设定测试图片的路径
def detect(self): """ Performs object detection and publishes coordinates. """ # Initialize detector self.send_message(Color.GREEN, "[INFO] Initializing TinyYOLOv3 detector.") det = Detector("/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3.cfg", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3_68000.weights", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/obj.names") (H, W) = (None, None) # Load model self.send_message(Color.GREEN, "[INFO] Loading network model.") net = det.load_model() print(net) # Initilialize Video Stream self.send_message(Color.GREEN, "[INFO] Starting video stream.") counter = 0 dets = 0 nondets = 0 detect = True fps = FPS().start() boxes, confidences, indices, cls_ids, colors, ids, distances = [], [], [], [], [], [], [] zed_cam_size = 1280 ret = True while not rospy.is_shutdown(): # Grab next frame #ret, frame = video.read() frame = self.image #cap = cv2.VideoCapture("/home/nvidia/opencv_install/pajarito/bird.jpg") #hasFrame, frame = cap.read() color = "" diststring = "" ##AQUI SE MODIFICA EL VIDEO #frame = add_brightness(frame) #frame = add_darkness(frame) if cv2.waitKey(1) & 0xFF == ord ('q'): self.send_message(Color.RED, "[DONE] Quitting program.") break frame = imutils.resize(frame, width=1000) (H, W) = frame.shape[:2] if det.get_w() is None or det.get_h() is None: det.set_h(H) det.set_w(W) # Perform detection detect = True dets += 1 # Get bounding boxes, condifences, indices and class IDs boxes, confidences, indices, cls_ids = det.get_detections(net, frame) # Publish detections # If there were any previous detections, draw them colors = [] distances = [] obj_list = ObjDetectedList() len_list = 0 for ix in indices: i = ix[0] box = boxes[i] x, y, w, h = box x, y, w, h = int(x), int(y), int(w), int(h) if detect == True: color = self.calculate_color(frame,x,y,h,w) depth = self.img_depth[y:y+h,x:x+w] d_list = [] for j in depth: if str(j[1]) != 'nan' and str(j[1]) != 'inf': d_list.append(j[1]) if len(d_list) != 0: dist = np.mean(d_list) else: dist = 'nan' if (dist < .30): diststring = "OUT OF RANGE" else: diststring = str(dist) + " m" color = str(color.color) colors.append(color) distances.append(dist) if str(dist) != 'nan': obj = ObjDetected() #print(p1,p2) obj.x = x obj.y = y obj.h = h obj.w = w obj.X = dist obj.Y = 0 obj.color = color obj.clase = 'bouy' if cls_ids[i] == 0 else 'marker' len_list += 1 obj_list.objects.append(obj) det.draw_prediction(frame, cls_ids[i], confidences[i], color,diststring, x, y, x+w, y+h) det_str = "Det: {}, BBoxes {}, Colors {}, Distance {}".format(dets, boxes, colors, distances) self.send_message(Color.BLUE, det_str) fps.update() obj_list.len = len_list self.detector_pub.publish(obj_list) cv2.line(frame, (500,560), (500,0), (255,0,0)) fps.stop() info = [ ("Detects: ", dets), ("No detects: ", nondets), ("FPS", "{:.2F}".format(fps.fps())), ] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, det.get_h() - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # Show current frame #cv2.imshow("Frame", frame) #print(self.depth) #cv2.waitKey(3) rate.sleep()
cfg.DISPLAY_BOUNDING_BOXES, cfg.DISPLAY_MASKS, cfg.DISPLAY_CAPTION, cfg.DISPLAY_CONTOURS }: logger.error( 'Nothing shows: show_boxes=%s, show_masks=%s, show_caption=%s, show_contours=%s', cfg.DISPLAY_BOUNDING_BOXES, cfg.DISPLAY_MASKS, cfg.DISPLAY_CAPTION, cfg.DISPLAY_CONTOURS) raise IOError logger.info('Config params:{}'.format(cfg.__dict__)) logger.info('Using device %s', device) logger.info('Set up model') model = models.get_model_mask_rcnn() model.eval() model.to(device) detector = Detector(model, device) if args.images: detector.detect_on_images(args.images, args.outdir, cfg.DISPLAY_MASKS, cfg.DISPLAY_BOUNDING_BOXES, cfg.DISPLAY_CAPTION, cfg.DISPLAY_CONTOURS) elif args.video: detector.detect_on_video(args.video, args.outdir, cfg.DISPLAY_MASKS, cfg.DISPLAY_BOUNDING_BOXES, cfg.DISPLAY_CAPTION, cfg.DISPLAY_CONTOURS, flip=args.flip)
def detect(): """ Performs object detection and publishes coordinates. """ #global image and depth frames from ZED camera global image global points_list points_list = [[0, 0, 0]] * 921600 image = np.zeros((560, 1000, 3), np.uint8) # Initialize detector send_message(Color.GREEN, "[INFO] Initializing TinyYOLOv3 detector.") det = Detector( "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3.cfg", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3_68000.weights", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/obj.names") (H, W) = (None, None) # Load model send_message(Color.GREEN, "[INFO] Loading network model.") net = det.load_model() # Initilialize Video Stream send_message(Color.GREEN, "[INFO] Starting video stream.") counter = 0 dets = 0 nondets = 0 detect = True fps = FPS().start() boxes, confidences, indices, cls_ids, colors, ids, distances = [], [], [], [], [], [], [] ret = True while not rospy.is_shutdown(): # Grab next frame #ret, frame = video.read() frame = image color = "" diststring = "" ##AQUI SE MODIFICA EL VIDEO #frame = add_brightness(frame) #frame = add_darkness(frame) if cv2.waitKey(1) & 0xFF == ord('q'): send_message(Color.RED, "[DONE] Quitting program.") break print(frame.shape) frame = imutils.resize(frame, width=1000) (H, W) = frame.shape[:2] if det.get_w() is None or det.get_h() is None: det.set_h(H) det.set_w(W) # Perform detection detect = True dets += 1 # Get bounding boxes, condifences, indices and class IDs boxes, confidences, indices, cls_ids = det.get_detections(net, frame) # Publish detections det_str = "Det: {}, BBoxes {}, Colors {}, Distance {}".format( dets, boxes, colors, distances) send_message(Color.BLUE, det_str) # If there were any previous detections, draw them colors = [] distances = [] obj_list = ObjDetectedList() len_list = 0 for ix in indices: i = ix[0] box = boxes[i] x, y, w, h = box x, y, w, h = int(x), int(y), int(w), int(h) if detect == True: color = calculate_color(frame, x, y, h, w) p1 = int((x + w / 2) * 1.28) p2 = int((y + h / 2) * 1.28) dist = points_list[(p1 + p2 * 1280)][0] if (dist < .30): diststring = "OUT OF RANGE" else: diststring = str(dist) + " m" color = str(color.color) colors.append(color) distances.append(dist) obj = ObjDetected() print(p1, p2) obj.x = x obj.y = y obj.h = h obj.w = w obj.X = points_list[(p1 + p2 * 1280)][0] obj.Y = points_list[(p1 + p2 * 1280)][1] obj.color = color obj.clase = 'bouy' if cls_ids[i] == 0 else 'marker' len_list += 1 obj_list.objects.append(obj) det.draw_prediction(frame, cls_ids[i], confidences[i], color, diststring, x, y, x + w, y + h) fps.update() obj_list.len = len_list detector_pub.publish(obj_list) cv2.line(frame, (500, 560), (500, 0), (255, 0, 0)) fps.stop() info = [ ("Detects: ", dets), ("No detects: ", nondets), ("FPS", "{:.2F}".format(fps.fps())), ] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, det.get_h() - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # Show current frame #cv2.imshow("depth", depth_frame) cv2.imshow("Frame", frame) #while cv2.waitKey(0) and 0xFF == ord('q'): rate.sleep()
thresh = [0.9, 0.6, 0.7] min_face_size = 24 stride = 2 slide_window = False shuffle = 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 = 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)
thresh = [0.6, 0.7, 0.8] min_face_size = 24 stride = 2 detectors = [None, None, None] # 模型放置位置 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): """
def detect(config, weights, classes, video): """ Performs object detection and publishes a data structure that contains, instance coordinates, instance class and sets a counter that will be used by the tracker. """ # Initialize detector send_message(Color.GREEN, "[INFO] Initializing TinyYOLOv3 detector.", "info") det = Detector(config, weights, classes) (H, W) = (None, None) # Initialize object tracks tracks = Tracks() # Initilialize Video Stream send_message(Color.GREEN, "[INFO] Starting video stream.", "info") if video == "0": video = cv2.VideoCapture(0) else: video = cv2.VideoCapture(args.video) counter, N = 0, 12 detect = True fps = FPS().start() while not rospy.is_shutdown() or video.isOpened(): # Grab next frame ret, frame = video.read() if not ret: send_message(Color.RED, "[DONE] Finished processing.", "info") cv2.waitKey(2000) break elif cv2.waitKey(1) & 0xFF == ord('q'): send_message(Color.RED, "[DONE] Quitting program.", "info") break frame = imutils.resize(frame, width=1000) (H, W) = frame.shape[:2] if det.get_width() is None or det.get_height() is None: det.set_height(H) det.set_width(W) # Modify frame brightness if necessary # frame = change_brightness(frame, 0.8) # Decrease # frame = change_brightness(frame, 1.5) # Increase # Every N frames perform detection if detect: # boxes, indices, cls_ids = [], [], [] boxes, indices, cls_ids = det.get_detections(det.net, frame) print(len(boxes)) objects = [] # Create objects and update tracks for i in range(len(cls_ids)): x, y, w, h = boxes[i] # Create object with attributes: class, color, bounding box obj = Object(cls_ids[i], get_color(frame, x, y, w, h), boxes[i], cv2.TrackerKCF_create()) obj.tracker.init(frame, (x, y, w, h)) objects.append(obj) tracks.update(objects) detect = False # While counter < N, update through KCF else: if counter == N: counter = 0 detect = True for o in tracks.objects: obj = tracks.get_object(o) obj.print_object() (succ, bbox) = obj.update_tracker(frame) if not succ: obj.reduce_lives() if obj.get_lives() == 0: tracks.delete_object(o) else: obj.update_object_bbox(bbox) obj.reset_lives() tracks.set_object(obj, o) # This is for debugging purposes. Just to check that bounding boxes are updating. if DEBUG: for o in tracks.objects: obj = tracks.get_object(o) x, y, w, h = obj.get_object_bbox() det.draw_prediction(frame, obj.get_class(), obj.get_color(), obj.get_id(), int(x), int(y), int(x + w), int(y + h)) # Publish detections counter += 1 det_str = "Detections {}: {}".format(counter, tracks.objects) send_message(Color.BLUE, det_str) fps.update() fps.stop() info = [("FPS", "{:.2F}".format(fps.fps())), ("OUT", "class, color, id")] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, det.get_height() - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # Show current frame cv2.imshow("Frame", frame) rate.sleep()
def camera(test_mode, input_mode, test_dir, out_path): thresh = config.thresh min_face_size = config.min_face stride = config.stride batch_size = config.batches detectors = [None, None, None] # 模型放置位置 model_path = ['checkpoint/pnet/pnet-30', 'checkpoint/rnet/rnet-30', 'checkpoint/onet/onet-30'] pnet = PNetDetector(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) if input_mode == '1': # 选用图片 for item in os.listdir(test_dir): img_path = os.path.join(test_dir, item) img = cv.imread(img_path) # img = cv.imread("picture/2007_000346.jpg") 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])] # 画人脸框 cv.rectangle(img, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (255, 0, 0), 1) # 判别为人脸的置信度 cv.putText(img, '{:.2f}'.format(score), (corpbbox[0], corpbbox[1] - 2), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # 画关键点 for i in range(landmarks.shape[0]): for j in range(len(landmarks[i]) // 2): cv.circle(img, (int(landmarks[i][2*j]), int(int(landmarks[i][2*j+1]))), 3, (0, 0, 255), -1) cv.imshow('im', img) k = cv.waitKey(0) & 0xFF if k == 27: cv.imwrite(out_path + item, img) cv.destroyAllWindows() if input_mode == '2': cap = cv.VideoCapture(0) fourcc = cv.VideoWriter_fourcc(*'XVID') out = cv.VideoWriter(out_path + '/out.mp4', fourcc, 10, (640, 480)) while True: t1 = cv.getTickCount() ret, frame = cap.read() if ret: boxes_c, landmarks = mtcnn_detector.detect(frame) t2 = cv.getTickCount() t = (t2 - t1) / cv.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])] # 画人脸框 cv.rectangle(frame, (corpbbox[0], corpbbox[1]), (corpbbox[2], corpbbox[3]), (255, 0, 0), 1) # 画置信度 w, h = bbox[2] - bbox[0], bbox[3] - bbox[1] cv.putText(frame, 'score:{:.2f} w:{:} h:{}'.format(score, int(w), int(h)), (corpbbox[0], corpbbox[1] - 2), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # 画fps值 cv.putText(frame, 't:{:.4f}(s)'.format(t) + " " + 'fps:{:.3f}'.format(fps), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 2) # 画关键点 for i in range(landmarks.shape[0]): for j in range(len(landmarks[i])//2): cv.circle(frame, (int(landmarks[i][2 * j]), int(int(landmarks[i][2 * j + 1]))), 3, (0, 0, 255), -1) a = out.write(frame) cv.imshow("result", frame) if cv.waitKey(1) & 0xFF == ord('q'): break else: break cap.release() out.release() cv.destroyAllWindows()
def gen_hard_example(img_dir, save_dir, input_size): '''通过pnet或rnet生成下一个网络的输入''' size = input_size batch_size = config.batches min_face_size = config.min_face stride = config.stride thresh = config.thresh # 模型地址 model_path = [ root_dir + 'MTCNN/checkpoint/pnet/pnet-30', root_dir + 'MTCNN/checkpoint/rnet/rnet-30', root_dir + 'MTCNN/checkpoint/onet/onet-30' ] net, save_size = None, None if input_size == '12': net = 'pnet' save_size = 24 elif input_size == '24': net = 'rnet' save_size = 48 assert net is not None and size is not None # 图像数据地址 wider_img_dir = os.path.join(img_dir, 'WIDER_train') # 处理后的图像存放地址 data_dir = os.path.join(save_dir, str(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') if not os.path.exists(data_dir): os.mkdir(data_dir) if not os.path.exists(neg_dir): os.mkdir(neg_dir) if not os.path.exists(pos_dir): os.mkdir(pos_dir) if not os.path.exists(part_dir): os.mkdir(part_dir) detectors = [None, None, None] pnet = PNetDetector(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 mtcnn_detector = MtcnnDetector(detectors, min_face_size, stride, thresh) anno_file = os.path.join(img_dir, 'wider_face_train_bbx_gt.txt') # 读取wider face文件的image和bbox data = read_annotation(wider_img_dir, anno_file) # data: {'images': images, 'bboxes': bboxes} # bboxes: [[[,,,], ..., [,,,]], [[,,,], ..., [,,,]]] # 将data制作成迭代器, input image path, output image data print('载入数据') test_data = TestLoader(data['images']) detectors, _ = mtcnn_detector.detect_face(test_data) print('完成识别') save_file = os.path.join(data_dir, 'detections.pkl') # save to save_file 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, data_dir)
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
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()
from detection.detector import Detector from file_management.file_provider import FileProvider from detection_file import DetectionFile from messaging.email_provider import EmailProvider from xml_file_services.settings_writer import SettingsWriter from xml_file_services.settings_reader import SettingsReader writer = SettingsWriter() writer.write_file() reader = SettingsReader() detector = Detector() file_provider = FileProvider() path = reader.get_value(reader.path_node_key) files = file_provider.get_files(path) files_with_detected_people = [] for file_name in files: file = DetectionFile(path, file_name) print(file.get_full_name()) detector.run_detection(file) if file.hasDetected: files_with_detected_people.append(file.file_name) message = "" for file in files_with_detected_people: message = "\n" + message + file if(message != ""):