def test(path, imgname): scales = [640, 640] args = parse_args() print('args=', args) detector = RetinaFace(args.network, gpuid, nms=0.3) img = cv2.imread(path + imgname) print(img.shape) im_shape = img.shape target_size = scales[0] max_size = scales[1] im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) #if im_size_min>target_size or im_size_max>max_size: im_scale = float(target_size) / float(im_size_min) # prevent bigger axis from being more than max_size: if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) scales = [im_scale] flip = False all_time = 0 for c in range(count): start = time.time() faces, landmarks = detector.detect(img, thresh, scales=scales, do_flip=flip) end = time.time() if c != 0: all_time += end - start print('count{}, faces.shape={}, landmarks,shape={}'.format( c, faces.shape, landmarks.shape)) print("average time:{}".format(all_time / (count - 1))) if faces is not None: print('find', faces.shape[0], 'faces') font = cv2.FONT_HERSHEY_SIMPLEX for i in range(faces.shape[0]): #print('score', faces[i][4]) box = faces[i] ibox = box[0:4].copy().astype(np.int) cv2.rectangle(img, (ibox[0], ibox[1]), (ibox[2], ibox[3]), (255, 0, 0), 2) score = box[4] k = "%.3f" % score cv2.putText(img, k, (ibox[0] + 2, ibox[1] + 14), font, 0.6, (0, 255, 0), 2) if landmarks is not None: landmark5 = landmarks[i].astype(np.int) #print(landmark.shape) for l in range(landmark5.shape[0]): color = (0, 0, 255) if l == 0 or l == 3: color = (0, 255, 0) cv2.circle(img, (landmark5[l][0], landmark5[l][1]), 1, color, 2) filename = '640_' + imgname print('writing', filename) cv2.imwrite(filename, img)
def __init__(self, args): self.detector = RetinaFace(args.retina_model, 0, args.gpu, 'net3') self.threshold = args.threshold self.scales = args.scales self.max_face_number = args.max_face_number self.counter = 0 self.image_size = args.image_size
def _main(_argv): detector = RetinaFace(FLAGS.weights_path, use_gpu_nms = False) if not os.path.isdir(FLAGS.save_folder): os.mkdir(FLAGS.save_folder) subdirs = [x[0] for x in os.walk(FLAGS.widerface_data_dir)][1:] save_dir = FLAGS.save_folder for subdir in subdirs: print(subdir) output_dir = os.path.join(save_dir, subdir.split("/")[-1]) if not os.path.isdir(output_dir): os.mkdir(output_dir) for file in os.listdir(subdir): if os.path.isfile(os.path.join(output_dir, file.replace("jpg", "txt"))): continue img = cv2.imread(os.path.join(subdir, file)) faces, ldmks = detector.detect(img, 0.01) with open(os.path.join(output_dir, file.replace("jpg", "txt")), "w+") as f: f.write(file.split("/")[-1].split(".")[0] + "\n") f.write(str(len(faces)) + "\n") for face in faces: f.write(str(int(face[0])) + " " + str(int(face[1])) + " " + str(int(face[2]) - int(face[0])) + " " + str(int(face[3]) - int(face[1])) + " " + str(face[4]) + "\n")
def __init__(self, with_tracking=False, thresh=0.8, fpd=10): gpuid = 0 self.detector = RetinaFace('./model/R50', 0, gpuid, 'net3') self.scales = [1024, 1980] self.thresh = thresh self.with_tracking = with_tracking self.frames_per_detection = fpd print('initialized retina face model')
def __init__(self, gpu_index=-1, arcface_model="model-r100-ii/model,0", image_size='112,112', retina_model="/model/R50"): if gpu_index >= 0: retina_ctx = mx.gpu(gpu_index) else: retina_ctx = mx.cpu() self.face_detector = RetinaFace(prefix=retina_model, epoch=0, ctx_id=gpu_index) self.face_recognition = ArcfaceModel(gpu=gpu_index, model=arcface_model, image_size=image_size)
def __init__(self, min_face_size=0): """Face detector based on RetinaFace. Args: min_face_size (int): minimum size of a face to be considered a match. Compared against min(width, height) of the face bounding box. """ self.min_size = min_face_size self.model = RetinaFace(quality="normal")
def __init__(self): self.thresh = 0.8 self.scales = [1024, 1980] # 0>=でGPU利用 self.gpuid = -1 self.model_path = os.path.join(os.path.dirname(__file__), 'model/retinaface-R50/R50') print("retina model path: ", self.model_path) self.detector = RetinaFace(self.model_path, 0, self.gpuid, 'net3')
def __init__(self, arguments, mx_context): self.args = arguments self.ctx = mx_context self.model = face_model.FaceModel(args) rtpath, epoch = self.args.rt_model.split(',') self.detector = RetinaFace(rtpath, int(epoch), self.args.gpu, 'net3') self.dataset = None # Collection of features of known names self.names = {} # Names of known person self.persons = [] # List of person detected self.crop_resolution = int(self.args.image_size.split(',') [0]) # Resolution to crop person face self.pv = [float(p) for p in self.args.poses.split(',')]
def __init__(self, model_str): """Init instance. Args: model_str ([type]): string of model checkpoint by `prefix,epoch` """ prefix, epoch = model_str.split(',') epoch = int(epoch) self.nms_threshold = 0.8 self.scales = (1024, 1980) ctx_id = 0 if is_cuda_available() else -1 self._detector = RetinaFace(prefix, epoch, ctx_id, 'net3')
def process(queue_camera_info, queue_camera_img): from retinaface import RetinaFace import cv2 import numpy as np gpuid = 1 thresh = 0.7 detector = RetinaFace('./model/mnet.25', 0, gpuid, 'net3') while True: camera_info_process = queue_camera_info.get() try: # print('process is waiting') # t = time.time() with open('log.txt', 'a+') as f: f.write( time.strftime('%H:%M:%S', time.localtime()) + 'face detect ' + str(queue_camera_info.qsize()) + '\n') img = cv2.imread('/tmp/' + camera_info_process['filename']) print(img.shape) im_shape = img.shape scales = [128, 128] target_size = scales[0] max_size = scales[1] im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) scales = [im_scale] faces, landmarks = detector.detect(img, thresh, scales=scales) print(landmarks.shape) print(faces.shape) # if faces.shape[0] == 1: camera_info_process['image'] = img camera_info_process['camera_face_img'] = faces camera_info_process['landmarks'] = landmarks # else: # print(landmarks.shape) # camera_info_process['image'] = img #: print(landmarks[0]) # camera_info_process['camera_face_img'] = faces[0].reshape([1,5]) # camera_info_process['landmarks'] = landmarks[0].reshape([1,5,2]) queue_camera_img.put(camera_info_process) # print('process',time.time() - t) except: # pass print('process is error')
def main(): parser = argparse.ArgumentParser(description='convert arcface models to onnx') # general parser.add_argument('--prefix', default='./model/mnet.25', help='prefix to load model.') parser.add_argument('--epoch', default=0, type=int, help='epoch number to load model.') parser.add_argument('--gpuid', default=0, type=int, help='ctx_id in model.') parser.add_argument('--network', default='net3', type=str, help='network in model.') parser.add_argument('--input_shape', nargs='+', default=[1, 3, 640, 640], type=int, help='input shape.') args = parser.parse_args() converted_onnx_filename = args.prefix + '.onnx' model = RetinaFace(args.prefix, args.epoch, args.gpuid, args.network).model # model = RetinaFaceCoV(args.prefix, args.epoch, args.gpuid, args.network).model sym, arg_params, aux_params = model.symbol, model._arg_params, model._aux_params model = get_sym_train(sym) mx.model.save_checkpoint(args.prefix + '_transpose', args.epoch, model, arg_params, aux_params) change_plus(args.prefix + '_transpose') converted_onnx_filename = onnx_mxnet.export_model(args.prefix + '_transpose-symbol.json', f'{args.prefix}_transpose-{args.epoch:04d}.params', [args.input_shape], np.float32, converted_onnx_filename) model = onnx.load_model(converted_onnx_filename) input_shape = args.input_shape d = model.graph.input[0].type.tensor_type.shape.dim rate = (input_shape[2] / d[2].dim_value, input_shape[3] / d[3].dim_value) print("rate: ", rate) d[0].dim_value = input_shape[0] d[2].dim_value = int(d[2].dim_value * rate[0]) d[3].dim_value = int(d[3].dim_value * rate[1]) for output in model.graph.output: d = output.type.tensor_type.shape.dim d[0].dim_value = input_shape[0] onnx.save_model(model, args.prefix + '-d.onnx')
class RetinaFaceDetector: def __init__(self, min_face_size=0): """Face detector based on RetinaFace. Args: min_face_size (int): minimum size of a face to be considered a match. Compared against min(width, height) of the face bounding box. """ self.min_size = min_face_size self.model = RetinaFace(quality="normal") def detect(self, img: np.array): assert len(img.shape) == 3 and img.shape[2] == 3 bbs = self.model.predict(img, threshold=0.95) return [{ "box": [b["x1"], b["y1"], b["x2"], b["y2"]], "keypoints": { "left_eye": (int(b["left_eye"][0]), int(b["left_eye"][1])), "right_eye": (int(b["right_eye"][0]), int(b["right_eye"][1])), "nose": (int(b["nose"][0]), int(b["nose"][1])), "mouth_left": (int(b["left_lip"][0]), int(b["left_lip"][1])), "mouth_right": (int(b["right_lip"][0]), int(b["right_lip"][1])), } } for b in bbs if min(b["x2"] - b["x1"], b["y2"] - b["y1"]) >= self.min_size]
def deepFaceAnalysis(retina_model, deepface_models, image_path): age_list = {} gender_list = {} race_list = {} emotion_list = {} # Pass all images through DeepFace faces = RetinaFace.extract_faces(image_path, model=retina_model) if len(faces) > 0: if len(faces) > 1: removeBackgroundFaces(faces) for i, face in enumerate(faces, start=1): dict = "face_" + str(i) face_feature = DeepFace.analyze( face, actions=['age', 'gender', 'race', 'emotion'], models=deepface_models, enforce_detection=False) age_list[dict] = (face_feature["age"]) gender_list[dict] = (face_feature["gender"]) race_list[dict] = (face_feature["dominant_race"]) emotion_list[dict] = (face_feature["dominant_emotion"]) return len(faces), age_list, gender_list, race_list, emotion_list else: return 0, "None", "None", "None", "None"
def get_retinaface_detector(model_str, gpuid): _vec = model_str.split(',') assert len(_vec) == 3 prefix = _vec[0] epoch = int(_vec[1]) net = _vec[2] detector = RetinaFace(prefix, epoch, gpuid, net) return detector
def load_model(): pretrained_path = '/content/drive/My Drive/retinaface weights/mobilenet0.25_Final.pth' # print('Loading pretrained model from {}'.format(pretrained_path)) model = RetinaFace(cfg=cfg_mnet, phase='test') device = torch.device('cpu') pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage) if "state_dict" in pretrained_dict.keys(): pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.') else: pretrained_dict = remove_prefix(pretrained_dict, 'module.') check_keys(model, pretrained_dict) model.load_state_dict(pretrained_dict, strict=False) # print('Finished loading model!') return model
def init(self): self.detector = RetinaFace('./model/R50', 0, 0, 'net3') self.mark_detector = MarkDetector() self.pose_stabilizers = [Stabilizer( state_num=2, measure_num=1, cov_process=0.1, cov_measure=0.1) for _ in range(6)] sample_img = None #if self.color_img is None: # print("None") # time.sleep(0.2) # #height, width, _ = self.color_img.shape self.pose_estimator = PoseEstimator(img_size=(480, 640))
def detect_face(face_detector, img, align=True): from retinaface import RetinaFace from retinaface.commons import postprocess #--------------------------------- resp = [] # The BGR2RGB conversion will be done in the preprocessing step of retinaface. # img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #retinaface expects RGB but OpenCV read BGR """ face = None img_region = [0, 0, img.shape[0], img.shape[1]] #Really? faces = RetinaFace.extract_faces(img_rgb, model = face_detector, align = align) if len(faces) > 0: face = faces[0][:, :, ::-1] return face, img_region """ #-------------------------- obj = RetinaFace.detect_faces(img, model=face_detector, threshold=0.9) if type(obj) == dict: for key in obj: identity = obj[key] facial_area = identity["facial_area"] y = facial_area[1] h = facial_area[3] - y x = facial_area[0] w = facial_area[2] - x img_region = [x, y, w, h] #detected_face = img[int(y):int(y+h), int(x):int(x+w)] #opencv detected_face = img[facial_area[1]:facial_area[3], facial_area[0]:facial_area[2]] if align: landmarks = identity["landmarks"] left_eye = landmarks["left_eye"] right_eye = landmarks["right_eye"] nose = landmarks["nose"] #mouth_right = landmarks["mouth_right"] #mouth_left = landmarks["mouth_left"] detected_face = postprocess.alignment_procedure( detected_face, right_eye, left_eye, nose) resp.append((detected_face, img_region)) return resp
def load_model(self): ''' load net and weights params :return: net and weights parms ''' sep = os.sep file_abs = os.path.abspath(__file__) file_path = file_abs[0:file_abs.rfind(sep)] model_path = file_path + sep + '../../models/RetinaFace/R50/R50' detector = RetinaFace('./model/test', 0, self._gpu, 'net3') return detector
def retina_face(img_path): thresh = 0.8 scales = [250, 250] gpuid = -1 detector = RetinaFace( '/home/shenzhonghai/FaceClustering/detection/RetinaFace/model/R50', 0, gpuid, 'net3') # detector = RetinaFace('./model/R50', 0, gpuid, 'net3') img = cv2.imread(img_path) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if 'PIE' in img_path: # border 64 -> 112 # img = cv2.copyMakeBorder(img, 24, 24, 24, 24, cv2.BORDER_CONSTANT, value=[0, 0, 0]) img = cv2.copyMakeBorder(img, 24, 24, 24, 24, cv2.BORDER_REPLICATE) im_shape = img.shape target_size = scales[0] max_size = scales[1] im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) #im_scale = 1.0 #if im_size_min>target_size or im_size_max>max_size: im_scale = float(target_size) / float(im_size_min) # prevent bigger axis from being more than max_size: if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) scales = [im_scale] flip = False faces, landmarks = detector.detect_center(img, thresh, scales=scales, do_flip=flip) # img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) if faces is not None: return img, landmarks return img, []
class FaceDetector(object): """RetinaFace detect face boxes and five facial landmarks.""" def __init__(self, model_str): """Init instance. Args: model_str ([type]): string of model checkpoint by `prefix,epoch` """ prefix, epoch = model_str.split(',') epoch = int(epoch) self.nms_threshold = 0.8 self.scales = (1024, 1980) ctx_id = 0 if is_cuda_available() else -1 self._detector = RetinaFace(prefix, epoch, ctx_id, 'net3') def predict(self, img: np.ndarray) -> tuple: """predict. Args: img (np.ndarray): [description] img (np.ndarray): [description] Returns: tuple: [description] """ scales = self._cal_scales(img) faces, landmarks = self._detector.detect(img, self.nms_threshold, scales=scales, do_flip=False) if faces is not None: faces = faces.astype(np.int32) if landmarks is not None: landmarks = landmarks.astype(np.int32) return faces, landmarks def __call__(self, img: np.ndarray): return self.predict(img) def _cal_scales(self, img): im_shape = img.shape target_size = self.scales[0] max_size = self.scales[1] im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) #im_scale = 1.0 #if im_size_min>target_size or im_size_max>max_size: im_scale = float(target_size) / float(im_size_min) # prevent bigger axis from being more than max_size: if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) scales = [im_scale] return scales
def _main(_argv): detector = RetinaFace(FLAGS.weights_path, FLAGS.use_gpu_nms, FLAGS.nms_thresh) img = cv2.imread(FLAGS.sample_img) faces, landmarks = detector.detect(img, FLAGS.det_thresh) if faces is not None: print('found', faces.shape[0], 'faces') for i in range(faces.shape[0]): box = faces[i].astype(np.int) color = (0, 0, 255) cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]), color, 2) if landmarks is not None: landmark5 = landmarks[i].astype(np.int) for l in range(landmark5.shape[0]): color = (0, 0, 255) if l == 0 or l == 3: color = (0, 255, 0) cv2.circle(img, (landmark5[l][0], landmark5[l][1]), 1, color, 1) cv2.imwrite(FLAGS.save_destination, img)
class FaceDetector: def __init__(self): self.gpuid = -1; self.thresh = 0.95; self.scales = [2 / 3]; self.flip = False self.detector = RetinaFace('RetinaFace/model/mnet.25', 0, self.gpuid, 'net3') def detect(self, image, left_corner_human_box=None): start = time.time() face_boxes, landmarks = self.detector.detect(image, self.thresh, scales=self.scales, do_flip=self.flip) end = time.time() for i in range(face_boxes.shape[0]): face_boxes[i] = face_boxes[i].astype(np.int) # x_min, y_min, x_max, y_max landmarks[i] = landmarks[i].astype(np.int) # leye, reye, nose, lmouth, rmouth if left_corner_human_box is not None: x_human_min = left_corner_human_box[0] y_human_min = left_corner_human_box[1] # x, y, w, h in orginal coordinate face_boxes[i][2] -= face_boxes[i][0] # w face_boxes[i][3] -= face_boxes[i][1] # h face_boxes[i][0] += x_human_min face_boxes[i][1] += y_human_min for j in range(landmarks[i].shape[0]): landmarks[i][j][0] += x_human_min landmarks[i][j][1] += y_human_min return face_boxes[:, :4], landmarks def get_faces_from_folder(self,folderPath): fileNumber = 0 for filename in os.listdir(folderPath): path = os.path.join(folderPath, filename) image = cv2.imread(path) face_boxes, landmarks = self.detect(image) for i in range(face_boxes.shape[0]): faceImage = face_preprocess.preprocess(image, face_boxes[i], landmarks[i], image_size='112,112') if not (os.path.exists("data/output")): os.makedirs("data/output") filePath = "data/output/" + str(fileNumber) + ".jpg" cv2.imwrite(filePath, faceImage) fileNumber = fileNumber + 1 def get_face_from_image(self, image): faces = list() face_boxes, landmarks = self.detect(image) for i in range(face_boxes.shape[0]): faceImage = face_preprocess.preprocess(image, face_boxes[i], landmarks[i], image_size='112,112') faces.append(faceImage) return faces, face_boxes
def _main(_argv): detector = RetinaFace(FLAGS.weights_path, FLAGS.use_gpu_nms, FLAGS.nms_thresh) image_root = [ 'data/WIDER_test/images/', 'data/WIDER_train/images/', 'data/WIDER_val/images/' ] result_save_root = 'widerface-faces/' for i in range(0, 3): create_directory(os.path.join(result_save_root, image_root[i])) face_numbers = 0 for k in range(0, 3): for parent, dir_names, file_names in os.walk(image_root[k]): for file_name in file_names: if not file_name.lower().endswith('jpg'): continue face_numbers = 0 img = cv2.imread(os.path.join(parent, file_name), cv2.IMREAD_COLOR) faces, landmarks = detector.detect(img, FLAGS.det_thresh) print(faces.shape) if faces is not None: print('found', faces.shape[0], 'faces') for i in range(faces.shape[0]): face_numbers += 1 box = faces[i].astype(np.int) color = (0, 0, 255) crop_img = img[box[1] - 10:box[3] + 10, box[0] - 10:box[2] + 10] if (crop_img.size != 0): crop_img = image_resize(crop_img, 48, 48) crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) if not cv2.imwrite( os.path.join( result_save_root, image_root[k], file_name.replace( '.jpg', 'result_{}.jpg'.format( face_numbers))), crop_img): raise Exception("Could not write image")
def detect_face(face_detector, img, align = True): face = None img_region = [0, 0, img.shape[0], img.shape[1]] img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #retinaface expects RGB but OpenCV read BGR faces = RetinaFace.extract_faces(img_rgb, model = face_detector, align = align) if len(faces) > 0: face = faces[0][:, :, ::-1] return face, img_region
def _main(_argv): detector = RetinaFace(FLAGS.weights_path, FLAGS.use_gpu_nms, FLAGS.nms_thresh) img = cv2.imread(FLAGS.sample_img) faces, landmarks = detector.detect(img, FLAGS.det_thresh) predict = Predict('data/resnet56_fer_pretrained.h5') w = 0 if faces is not None: print('found', faces.shape[0], 'faces') for i in range(faces.shape[0]): w += 1 box = faces[i].astype(np.int) crop_img = img[box[1] - 10:box[3] + 10, box[0] - 10:box[2] + 10] if (crop_img.size != 0): crop_img = image_resize(crop_img, 48, 48) crop_img = cv2.resize(crop_img, (48, 48)) crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) crop_img = np.array( crop_img.reshape( [1, crop_img.shape[0], crop_img.shape[1], 1])) result = predict.predict_emotion(crop_img) print(result)
class RetinaFaceModel: def __init__(self): gpuid = 0 self.detector = RetinaFace('./model/R50', 0, gpuid, 'net3') self.scales = [1024, 1980] self.thresh = 0.8 print('initialized retina face model') def detect_faces(self, video, output): detect_faces(video, output, 640, 360, self.detect_faces_on_img) def detect_faces_on_img(self, image): im_shape = image.shape target_size = self.scales[0] max_size = self.scales[1] im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) # im_scale = 1.0 # if im_size_min>target_size or im_size_max>max_size: im_scale = float(target_size) / float(im_size_min) # prevent bigger axis from being more than max_size: if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) scales = [im_scale] flip = False faces, landmarks = self.detector.detect(image, self.thresh, scales=scales, do_flip=flip) if faces is not None: print('find', faces.shape[0], 'faces') for i in range(faces.shape[0]): # print('score', faces[i][4]) box = faces[i].astype(np.int) # color = (255,0,0) color = (0, 0, 255) cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), color, 2) if landmarks is not None: landmark5 = landmarks[i].astype(np.int) # print(landmark.shape) for l in range(landmark5.shape[0]): color = (0, 0, 255) if l == 0 or l == 3: color = (0, 255, 0) cv2.circle(image, (landmark5[l][0], landmark5[l][1]), 1, color, 2) print(image.shape) return image
def cropped_and_aligned(img_list): error_num = 0 # retina face initialization model = RetinaFace('model-mnet/mnet.25', 0, 0, 'net3') img_list_cropped_gray = list() img_list_cropped_color = list() img_list_160 = list() for i in range(len(img_list)): # for Gray images try: if i % 1000 == 0: print('Crop and aligned', i) detections, point = model.detect(img_list[i]) img_gray = cv2.cvtColor(img_list[i], cv2.COLOR_BGR2GRAY) img_aligned_g = align_image(img_gray, detections, point, size=100) img_cropped_g = check_size(img_aligned_g) img_list_cropped_gray.append(img_cropped_g) # for BGR images img_aligned_color = align_image(img_list[i], detections, point, size=100) img_cropped_color = check_size(img_aligned_color) img_list_cropped_color.append(img_cropped_color) # for cnn img_aligned_160 = align_image(img_list[i], detections, point, size=160) img_cropped_160 = check_size(img_aligned_160) img_list_160.append(img_cropped_160) except AttributeError: error_num += 1 print('number of error ', error_num) pass return img_list_cropped_gray, img_list_cropped_color, img_list_160
def main(): parser = argparse.ArgumentParser(description='convert arcface models to onnx') # general parser.add_argument('--prefix', default='./model/R50', help='prefix to load model.') parser.add_argument('--epoch', default=0, type=int, help='epoch number to load model.') parser.add_argument('--gpuid', default=0, type=int, help='ctx_id in model.') parser.add_argument('--network', default='net3', type=str, help='network in model.') parser.add_argument('--input_shape', nargs='+', default=[1, 3, 640, 640], type=int, help='input shape.') args = parser.parse_args() converted_onnx_filename = args.prefix + '.onnx' model = RetinaFace(args.prefix, args.epoch, args.gpuid, args.network).model # model = RetinaFaceCoV(args.prefix, args.epoch, args.gpuid, args.network).model sym, arg_params, aux_params = model.symbol, model._arg_params, model._aux_params model = get_sym_train(sym) mx.model.save_checkpoint(args.prefix + '_transpose', args.epoch, model, arg_params, aux_params) change_plus(args.prefix + '_transpose') converted_onnx_filename = onnx_mxnet.export_model(args.prefix + '_transpose-symbol.json', f'{args.prefix}_transpose-{args.epoch:04d}.params', [args.input_shape], np.float32, converted_onnx_filename)
class FaceDetector: def __init__(self): self.face_detector = RetinaFace( gpu_id=0, network='resnet50') # Backbone: resnet50 or mobilenet self.threshold = 0.8 def filter_detections(self, faces): filtered_faces = [] if faces is not None: for face in faces: bbox, landmarks, score = face if score < self.threshold: continue bbox = bbox.astype(np.int) + [-5, -5, 5, 5 ] # broadcast bboxes with 5 px filtered_faces.append(bbox) return filtered_faces def __call__(self, frame): return self.filter_detections(self.face_detector.detect(frame))
def init_model(): import tensorflow as tf import cv2 import os import cv2 from retinaface import RetinaFace from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession global decode_model global retinaFace retinaFace = RetinaFace("retinaface_tf2/configs/retinaface_res50.yaml") decode_model = tf.keras.models.load_model("./models") decode_model.summary() def encode_func(impaths: list): imgs = [ cv2.imread(path) for path in impaths ] outputs = [ retinaFace.predict_img(img) for img in imgs ] region = [ img[predict[0].y1:predict[0].y2, predict[0].x1:predict[0].x2] for predict, img in zip(outputs, imgs) ] return decode_model(np.array(region)) global encode_network encode_network = encode_func