예제 #1
0
def main(config: str = "config.yml"):

    with open(config) as f:
        cfg = yaml.safe_load(f)

    enter_folder = cfg["ent_folder"]
    out_folder = cfg["out_folder"]
    mode = cfg["mode"]
    make_dir_safe(out_folder)

    detector = RetinaFace(cfg["name_det"], cfg["epoch_det"], cfg["gpu"])
    align = Alinger(cfg["size"])

    for race in tqdm(os.listdir(enter_folder)):
        ent_race = os.path.join(enter_folder, race)
        out_race = os.path.join(out_folder, race)
        make_dir_safe(out_race)
        for gender in os.listdir(ent_race):
            ent_gender = os.path.join(ent_race, gender)
            out_gender = os.path.join(out_race, gender)
            make_dir_safe(out_gender)
            faces = os.listdir(ent_gender)
            faces.sort()
            k = 0
            out_faces = []
            imgs = []
            for face in tqdm(faces):
                k += 1
                ent_face = os.path.join(ent_gender, face)
                out_face = os.path.join(out_gender, face)
                img = cv2.imread(ent_face)
                try:
                    img = cv2.resize(img, (500, 500))
                    det_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                    _, dets = detector.detect(det_img)
                except cv2.error:
                    dets = np.empty((0, 1))
                    print(ent_face)
                if dets.shape[0] != 1:
                    if mode == "unknown":
                        continue
                    imgs.append(0)
                else:
                    lands = rework_landmarks(dets[0])
                    rew_imgs = align([img], [lands])
                    if mode == "unknown":
                        cv2.imwrite(out_face, rew_imgs[0][0][0])
                        continue
                    imgs.append(rew_imgs[0][0][0])
                out_faces.append(out_face)
                if k == 2 and mode == "known":
                    if not isinstance(imgs[0], int) and not isinstance(imgs[1], int):
                        for img, path in zip(imgs, out_faces):
                            cv2.imwrite(path, img)
                    imgs = []
                    out_faces = []
                    k = 0
예제 #2
0
class Detector:
    def __init__(self, model_path):
        self.model = RetinaFace(model_path, 0, ctx_id=0)

    def get_face_patch(self, img):
        bboxes, points = self.model.detect(img,
                                           0.7,
                                           scales=[1.0],
                                           do_flip=False)
        if isinstance(img, str):
            img = cv2.imread(img)
        faces_ = []
        key_points_ = []
        bboxes_ = []
        for face, point in zip(bboxes, points):
            #import pdb; pdb.set_trace()
            bbox = face[0:4].astype(np.int)
            to_add_face = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
            to_add_face = cv2.cvtColor(to_add_face, cv2.COLOR_BGR2RGB)
            faces_.append(to_add_face)
            key_points_.append((points.astype(np.int), face[4]))
            bboxes_.append(bbox)
            #print(to_add_face.shape)

        return faces_, np.array(key_points_), np.array(bboxes_)
예제 #3
0
    def __init__(self, args):
        self.args = args
        ctx = mx.gpu(args.gpu)
        _vec = args.image_size.split(',')
        assert len(_vec) == 2
        image_size = (int(_vec[0]), int(_vec[1]))
        self.model = None
        self.ga_model = None
        if len(args.model) > 0:
            self.model = get_model(ctx, image_size, args.model, 'fc1')
        if len(args.ga_model) > 0:
            self.ga_model = get_model(ctx, image_size, args.ga_model, 'fc1')

        self.threshold = args.threshold
        self.det_minsize = 50
        self.det_threshold = args.det_threshold
        #self.det_factor = 0.9
        self.image_size = image_size
        retina_path = args.detector_path
        if args.det == 0:
            detector = RetinaFace(retina_path, 0, ctx_id=0)
        else:
            raise ('Give detector path')
        self.detector = detector
예제 #4
0
import cv2
import sys
import numpy as np
import datetime
import time
import os
import glob

sys.path.append('../../insightface')
from RetinaFace.retinaface import RetinaFace

thresh = 0.8
gpuid = 0
scales = [360, 640]
# detector = RetinaFace('./model/retinaface-R50/R50', 0, gpuid, 'net3')
detector = RetinaFace('./model/mnet.25/mnet.25', 0, gpuid, 'net3')


def detect_video(video_path):
    cap = cv2.VideoCapture(video_path)
    success, frame = cap.read()
    im_shape = frame.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:
예제 #5
0
parser.add_argument('-o',
                    '--output_folder',
                    default="./data/BIWI_prepared/",
                    help='Output file')

args = parser.parse_args()

# Input image file list
filenames = utils.get_list_from_filenames(
    os.path.join(args.data_dir, "filename_list.txt"))
filenames = sorted(filenames)
random.shuffle(filenames)

# Init face detector
gpuid = 0
face_detector = RetinaFace('./RetinaFace/retinaface-R50', 0, gpuid, 'net3')

idx = -1
n_prrocessed = 0
examples = []
while True:

    idx += 1
    print("Processed: {}/{}".format(n_prrocessed, idx))

    if idx < 0:
        idx = 0
    if idx >= len(filenames):
        break

    example = {}
예제 #6
0
import cv2
import numpy as np
from RetinaFace.retinaface import RetinaFace

gpuid = 0
# Road Face Detection Model
detector = RetinaFace('./model/R50', 0, gpuid, 'net3')

thresh = 0.8

sum_3 = 0
sum_4 = 0
sum_34 = 0

cap = cv2.VideoCapture(0)
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

# Read Cam
while (True):
    ret, img = cap.read()
    #print(np.shape(img))
    faces, landmarks = detector.detect(img,
                                       thresh,
                                       scales=[1.0, 1.0],
                                       do_flip=True)

    if True:
        for i in range(faces.shape[0]):
            box = faces[i].astype(np.int)
            color = (0, 0, 255)
예제 #7
0
 def __init__(self, model_path):
     self.model = RetinaFace(model_path, 0, ctx_id=0)
예제 #8
0
import cv2
import sys
import numpy as np
import datetime
import os
import glob
from RetinaFace.retinaface import RetinaFace

thresh = 0.8
scales = [1024, 1980]

count = 1

gpuid = 0
detector = RetinaFace(
    '/home/yeluyue/dl/Datasets/ICCV_challenge/insightface-master/models/retinaface-R50/R50',
    0, gpuid, 'net3')

img = cv2.imread(
    '/home/yeluyue/yeluyue/DataSet/lfw/all/lfw_sor/val/Aaron_Eckhart/Aaron_Eckhart_0001.jpg'
)
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])
#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:
예제 #9
0
                help='ver dist threshold')

args = ap.parse_args()

# Load embeddings and labels
data = pickle.loads(open(args.embeddings, "rb").read())
le = pickle.loads(open(args.le, "rb").read())

embeddings = np.array(data['embeddings'])
labels = le.fit_transform(data['names'])

# Initialize detector
thresh = 0.8
gpuid = 0
scales = [360, 640]
detector = RetinaFace('../insightface/RetinaFace/model/mnet.25/mnet.25', 0,
                      gpuid, 'net3')
cap = cv2.VideoCapture(args.video_in)
success, frame = cap.read()
im_shape = frame.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)

# Initialize faces embedding model
예제 #10
0
class FaceDetector():
    def __init__(self, model_retina_path, gpu_id):
        self.model = RetinaFace(model_retina_path,
                                0,
                                ctx_id=gpu_id,
                                network='net3')

    def detect(self, img, scale_ratio=1.0):
        ret = self.model.detect(img, 0.5, scales=[scale_ratio], do_flip=False)
        if ret is None:
            return [], []
        bboxes, points = ret
        if len(bboxes) == 0:
            return [], []
        return np.asarray(bboxes), points

    def align(self, img, bbox=None, landmark=None, **kwargs):
        M = None
        image_size = []
        str_image_size = kwargs.get('image_size', '')
        if len(str_image_size) > 0:
            image_size = [int(x) for x in str_image_size.split(',')]
            if len(image_size) == 1:
                image_size = [image_size[0], image_size[0]]
                assert len(image_size) == 2
            assert image_size[0] == 112
            assert image_size[0] == 112 or image_size[1] == 96
        if landmark is not None:
            assert len(image_size) == 2
            src = np.array(
                [[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366],
                 [33.5493, 92.3655], [62.7299, 92.2041]],
                dtype=np.float32)
            if image_size[1] == 112:
                src[:, 0] += 8.0
            dst = landmark.astype(np.float32)

            tform = trans.SimilarityTransform()
            tform.estimate(dst, src)
            M = tform.params[0:2, :]
            #M = cv2.estimateRigidTransform( dst.reshape(1,5,2), src.reshape(1,5,2), False)

        if M is None:
            if bbox is None:  #use center crop
                det = np.zeros(4, dtype=np.int32)
                det[0] = int(img.shape[1] * 0.0625)
                det[1] = int(img.shape[0] * 0.0625)
                det[2] = img.shape[1] - det[0]
                det[3] = img.shape[0] - det[1]
            else:
                det = bbox
            margin = kwargs.get('margin', 44)
            bb = np.zeros(4, dtype=np.int32)
            bb[0] = np.maximum(det[0] - margin / 2, 0)
            bb[1] = np.maximum(det[1] - margin / 2, 0)
            bb[2] = np.minimum(det[2] + margin / 2, img.shape[1])
            bb[3] = np.minimum(det[3] + margin / 2, img.shape[0])
            ret = img[bb[1]:bb[3], bb[0]:bb[2], :]
            if len(image_size) > 0:
                ret = cv2.resize(ret, (image_size[1], image_size[0]))
            return ret
        else:  #do align using landmark
            assert len(image_size) == 2
            warped = cv2.warpAffine(img,
                                    M, (image_size[1], image_size[0]),
                                    borderValue=0.0)
            return warped
예제 #11
0
 def __init__(self, model_retina_path, gpu_id):
     self.model = RetinaFace(model_retina_path,
                             0,
                             ctx_id=gpu_id,
                             network='net3')
예제 #12
0
 def __init__(self,retina_path,request_add,det_threshold=0.8):
     self.detector = RetinaFace(retina_path,0, ctx_id=0)
     self.request_add = request_add
     self.det_threshold = det_threshold
예제 #13
0
class Model:
    def __init__(self,retina_path,request_add,det_threshold=0.8):
        self.detector = RetinaFace(retina_path,0, ctx_id=0)
        self.request_add = request_add
        self.det_threshold = det_threshold
        
    def check_mask(self,encoded_image):
        r = requests.post(self.request_add,data=encoded_image)
        result = r.json()['result']
        return result
    
    
    def get_face_patch(self,img):
        bboxes,points = self.detector.detect(img, self.det_threshold,scales=[1.0],do_flip=False)
        faces_=[]
        key_points_=[]
        bboxes_=[]
        for face,point in zip(bboxes,points):
            #import pdb; pdb.set_trace()
            bbox = face[0:4].astype(np.int)
            to_add_face=img[bbox[1]:bbox[3],bbox[0]:bbox[2]]
            to_add_face = get_padded_image(to_add_face)[...,::-1]/255.0
           # print(to_add_face.shape)
            faces_.append(to_add_face)
            key_points_.append((points.astype(np.int),face[4]))
            bboxes_.append(bbox)
            #print(to_add_face.shape)

        return np.array(faces_),np.array(key_points_),np.array(bboxes_)
    
    def generate_output(self,read_path,write_path,type_='video'):
        if type_ == 'video':
            self.generate_video(read_path,write_path)
        else:
            self.generate_image(read_path,write_path)
        
        
    def generate_video(self,read_path,write_path):
        cap = cv2.VideoCapture(read_path)
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        fps = cap.get(cv2.CAP_PROP_FPS)
        ret , fr = cap.read()
        org_h,org_w = fr.shape[:2]
        out = cv2.VideoWriter(f'{write_path}', fourcc, fps, (org_w,org_h))
        counter = 0
        while ret:
            if counter%2 == 0:
                counter = 0
                fr = self._infer_on_frame(fr)
                out.write(fr)
                ret,fr = cap.read()
            counter += 1
        out.release()
        
    def generate_image(self,read_path,write_path):
        fr = cv2.imread(read_path)
        if fr is None:
            print('Invalid Image')
            return
        fr = self._infer_on_frame(fr)
        plt.imsave(write_path,fr[:,:,::-1])
        
    def _infer_on_frame(self,fr):
#         org_h,org_w = fr.shape[:2]
#         fr = imutils.resize(fr,width=720)
        faces , keypoints , bboxes = self.get_face_patch(fr)
        if not len(faces)==0:
            encoded_arr = pickle.dumps(faces)
            output = self.check_mask(encoded_arr)
            
            for out,bbox in zip(output,bboxes):
#             for bbox in bboxes:
                x1,y1,x2,y2 = bbox
                if out == 'mask':
                    color = (0,255,0)
                else:
                    color = (0,0,255)
                #color = (0,255,0)
                fr = cv2.rectangle(fr.copy(),(x1,y1),(x2,y2),color,2)
#         fr = cv2.resize(fr.copy(),(org_w,org_h))
        return fr
예제 #14
0
# Build model
net = models.HeadPoseNet(config["model"]["im_width"],
                         config["model"]["im_height"],
                         nb_bins=config["model"]["nb_bins"],
                         learning_rate=config["train"]["learning_rate"],
                         backbond=config["model"]["backbond"])
# Load model
net.load_weights(config["test"]["model_file"])

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("Unable to connect to camera.")
    exit(-1)

face_detector = RetinaFace('./RetinaFace/retinaface-R50', 0, 0, 'net3')

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        faces, landmarks = face_detector.detect(frame,
                                                0.8,
                                                scales=[1.0],
                                                do_flip=False)

        if len(faces) > 0:

            face_crops = []
            face_boxes = []
            for i in range(len(faces)):
                bbox = faces[i]
예제 #15
0
import cv2
import sys
import numpy as np
import datetime
import os
import glob
from RetinaFace.retinaface import RetinaFace

thresh = 0.8
scales = [1024, 1980]

count = 1

gpuid = 0
#detector = RetinaFace('./model/resnet-50', 0, gpuid, 'net3')
detector = RetinaFace('./mnet.25/mnet.25', 0, gpuid, 'net3')

img = cv2.imread('t1.jpg')
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])
#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)
예제 #16
0
def test(args):
    print('test with', args)
    global detector
    output_folder = args.output
    if not os.path.exists(output_folder):
        os.mkdir(output_folder)
    detector = RetinaFace(args.prefix,
                          args.epoch,
                          args.gpu,
                          network=args.network,
                          nocrop=args.nocrop,
                          vote=args.bbox_vote)
    imdb = eval(args.dataset)(args.image_set, args.root_path,
                              args.dataset_path)
    roidb = imdb.gt_roidb()
    gt_overlaps = np.zeros(0)
    overall = [0.0, 0.0]
    gt_max = np.array((0.0, 0.0))
    num_pos = 0
    print('roidb size', len(roidb))

    for i in range(len(roidb)):
        if i % args.parts != args.part:
            continue
        #if i%10==0:
        #  print('processing', i, file=sys.stderr)
        roi = roidb[i]
        boxes = get_boxes(roi, args.pyramid)
        if 'boxes' in roi:
            gt_boxes = roi['boxes'].copy()
            gt_areas = (gt_boxes[:, 2] - gt_boxes[:, 0] +
                        1) * (gt_boxes[:, 3] - gt_boxes[:, 1] + 1)
            num_pos += gt_boxes.shape[0]

            overlaps = bbox_overlaps(boxes.astype(np.float),
                                     gt_boxes.astype(np.float))
            #print(im_info, gt_boxes.shape, boxes.shape, overlaps.shape, file=sys.stderr)

            _gt_overlaps = np.zeros((gt_boxes.shape[0]))

            if boxes.shape[0] > 0:
                _gt_overlaps = overlaps.max(axis=0)
                #print('max_overlaps', _gt_overlaps, file=sys.stderr)
                for j in range(len(_gt_overlaps)):
                    if _gt_overlaps[j] > 0.5:
                        continue
                    #print(j, 'failed', gt_boxes[j],  'max_overlap:', _gt_overlaps[j], file=sys.stderr)

                # append recorded IoU coverage level
                found = (_gt_overlaps > 0.5).sum()
                recall = found / float(gt_boxes.shape[0])
                #print('recall', _recall, gt_boxes.shape[0], boxes.shape[0], gt_areas, 'num:', i, file=sys.stderr)
                overall[0] += found
                overall[1] += gt_boxes.shape[0]
                #gt_overlaps = np.hstack((gt_overlaps, _gt_overlaps))
                #_recall = (gt_overlaps >= threshold).sum() / float(num_pos)
                recall_all = float(overall[0]) / overall[1]
                #print('recall_all', _recall, file=sys.stderr)
                print('[%d]' % i,
                      'recall',
                      recall, (gt_boxes.shape[0], boxes.shape[0]),
                      'all:',
                      recall_all,
                      file=sys.stderr)
        else:
            print('[%d]' % i, 'detect %d faces' % boxes.shape[0])

        _vec = roidb[i]['image'].split('/')
        out_dir = os.path.join(output_folder, _vec[-2])
        if not os.path.exists(out_dir):
            os.mkdir(out_dir)
        out_file = os.path.join(out_dir, _vec[-1].replace('jpg', 'txt'))
        with open(out_file, 'w') as f:
            name = '/'.join(roidb[i]['image'].split('/')[-2:])
            f.write("%s\n" % (name))
            f.write("%d\n" % (boxes.shape[0]))
            for b in range(boxes.shape[0]):
                box = boxes[b]
                f.write(
                    "%d %d %d %d %g \n" %
                    (box[0], box[1], box[2] - box[0], box[3] - box[1], box[4]))