class FaceRecognition: def __init__(self) -> None: self.app = FaceAnalysis() # ctx id, <0 means using cpu self.app.prepare(ctx_id=0, det_size=(640, 640)) def get_embeddings(self, image_file): """ Generates face embedding of the image_file given """ # img = ins_get_image('myPic') img = cv2.imread(image_file) faces = self.app.get(img) rimg = self.app.draw_on(img, faces) cv2.imwrite("./output.jpg", rimg) # print(faces) # print(type(faces)) print(f"BBOX{faces[0]['bbox']}") # print(f"kps{faces[0]['kps']}") # # print(f"Landmark_3d{faces[0]['landmark_3d_68']}") # # print(f"Landmark_2d{faces[0]['landmark_2d_106']}") # print(f"Gender{faces[0]['gender']}") # print(f"Age{faces[0]['age']}") print(f"embedding{faces[0]['embedding']}") return faces
class InsightFace(FaceScanner): ID = 'InsightFace' DETECTION_MODEL_NAME = 'retinaface_r50_v1' CALCULATION_MODEL_NAME = 'arcface_r100_v1' IMG_LENGTH_LIMIT = ENV.IMG_LENGTH_LIMIT def __init__(self): super().__init__() self._detection_model = FaceAnalysis( det_name=self.DETECTION_MODEL_NAME, rec_name=None, ga_name=None) self._calculation_model = model_zoo.get_model( self.CALCULATION_MODEL_NAME) self._CTX_ID_CPU = -1 self._NMS = 0.4 self._detection_model.prepare(ctx_id=self._CTX_ID_CPU, nms=self._NMS) self._calculation_model.prepare(ctx_id=self._CTX_ID_CPU) self.det_prob_threshold = 0.8 def scan(self, img: Array3D, det_prob_threshold: float = None) -> List[ScannedFace]: scanned_faces = [] for bounding_box in self.find_faces(img, det_prob_threshold): norm_cropped_img = face_align.norm_crop( img, landmark=bounding_box.landmark) embedding = self._calculation_model.get_embedding( norm_cropped_img).flatten() scanned_faces.append( ScannedFace(box=bounding_box, embedding=embedding, img=img)) return scanned_faces def find_faces( self, img: Array3D, det_prob_threshold: float = None) -> List[InsightFaceBoundingBox]: if det_prob_threshold is None: det_prob_threshold = self.det_prob_threshold assert 0 <= det_prob_threshold <= 1 scaler = ImgScaler(self.IMG_LENGTH_LIMIT) img = scaler.downscale_img(img) results = self._detection_model.get(img, det_thresh=det_prob_threshold) boxes = [] for result in results: downscaled_box_array = result.bbox.astype(np.int).flatten() downscaled_box = InsightFaceBoundingBox( x_min=downscaled_box_array[0], y_min=downscaled_box_array[1], x_max=downscaled_box_array[2], y_max=downscaled_box_array[3], probability=result.det_score, landmark=result.landmark) box = downscaled_box.scaled(scaler.upscale_coefficient) if box.probability <= det_prob_threshold: logger.debug( f'Box Filtered out because below threshold ({det_prob_threshold}: {box})' ) continue logger.debug(f"Found: {box.dto}") boxes.append(box) return boxes
def __init__(self): super().__init__() self._detection_model = FaceAnalysis( det_name=self.DETECTION_MODEL_NAME, rec_name=None, ga_name=None) self._calculation_model = model_zoo.get_model( self.CALCULATION_MODEL_NAME) self._CTX_ID_CPU = -1 self._NMS = 0.4 self._detection_model.prepare(ctx_id=self._CTX_ID_CPU, nms=self._NMS) self._calculation_model.prepare(ctx_id=self._CTX_ID_CPU) self.det_prob_threshold = 0.8
def __init__(self, options): ''' Constructor ''' #import insightface from insightface.app import FaceAnalysis os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT'] = '0' # kwargs = {'root':os.path.join(options.storage_dir,'models')} #load Retina face model if options.gpuid == -1: ctx_id = -1 else: ctx_id = int(options.gpuid) #self.app = FaceAnalysis(allowed_modules=['detection','recognition']) self.app = FaceAnalysis() self.app.prepare(ctx_id=0, det_size=(640, 640)) print("InsightFaces Models Loaded.")
import cv2 import numpy as np import os import insightface from insightface.app import FaceAnalysis from insightface.data import get_image as ins_get_image if __name__ == '__main__': app = FaceAnalysis(allowed_modules=['detection', 'landmark_2d_106']) app.prepare(ctx_id=0, det_size=(640, 640)) img = ins_get_image('t1') faces = app.get(img) #assert len(faces)==6 tim = img.copy() color = (200, 160, 75) for face in faces: lmk = face.landmark_2d_106 lmk = np.round(lmk).astype(np.int) for i in range(lmk.shape[0]): p = tuple(lmk[i]) cv2.circle(tim, p, 1, color, 1, cv2.LINE_AA) cv2.imwrite('./test_out.jpg', tim)
# pip install onnxruntime import argparse import cv2 import sys import numpy as np import insightface from insightface.app import FaceAnalysis assert insightface.__version__>='0.2' parser = argparse.ArgumentParser(description='insightface test') # general parser.add_argument('--ctx', default=0, type=int, help='ctx id, <0 means using cpu') args = parser.parse_args() app = FaceAnalysis(name='antelope') app.prepare(ctx_id=args.ctx, det_size=(640,640)) img = cv2.imread('katya1.jpg') faces = app.get(img) assert len(faces)==1 #6 rimg = app.draw_on(img, faces) cv2.imwrite("./katya1_out.jpg", rimg) print(len(faces)) for face in faces: print(face.bbox) print(face.kps) print(face.embedding.shape)
def __init__(self) -> None: self.app = FaceAnalysis() # ctx id, <0 means using cpu self.app.prepare(ctx_id=0, det_size=(640, 640))
import argparse import cv2 import sys import numpy as np import insightface from insightface.app import FaceAnalysis from insightface.data import get_image as ins_get_image parser = argparse.ArgumentParser(description='insightface gender-age test') # general parser.add_argument('--ctx', default=0, type=int, help='ctx id, <0 means using cpu') args = parser.parse_args() app = FaceAnalysis(allowed_modules=['detection', 'genderage']) app.prepare(ctx_id=args.ctx, det_size=(640,640)) img = ins_get_image('t1') faces = app.get(img) assert len(faces)==6 for face in faces: print(face.bbox) print(face.sex, face.age)
import sys import glob import torch import pickle import os import numpy as np import cv2 import os.path as osp import insightface from insightface.app import FaceAnalysis from insightface.utils import face_align app = FaceAnalysis() app.prepare(ctx_id=0, det_size=(224, 224)) output_size = 384 input_dir = '/root/codebase/FaceSynthetics' output_dir = 'data/synthetics' if not osp.exists(output_dir): os.makedirs(output_dir) X = [] Y = [] for i in range(0, 100000): if i % 1000 == 0: print('loading', i) x = "%06d.png" % i img_path = osp.join(input_dir, x) img = cv2.imread(img_path)
import cv2 import sys import numpy as np import insightface from insightface.app import FaceAnalysis from insightface.data import get_image as ins_get_image assert insightface.__version__>='0.3' parser = argparse.ArgumentParser(description='insightface app test') # general parser.add_argument('--ctx', default=0, type=int, help='ctx id, <0 means using cpu') parser.add_argument('--det-size', default=640, type=int, help='detection size') args = parser.parse_args() app = FaceAnalysis() app.prepare(ctx_id=args.ctx, det_size=(args.det_size,args.det_size)) img = ins_get_image('t1') faces = app.get(img) assert len(faces)==6 rimg = app.draw_on(img, faces) cv2.imwrite("./t1_output.jpg", rimg) # then print all-to-all face similarity feats = [] for face in faces: feats.append(face.normed_embedding) feats = np.array(feats, dtype=np.float32) sims = np.dot(feats, feats.T) print(sims)
class InsightFaceWorker(faro.FaceWorker): ''' classdocs ''' def __init__(self, options): ''' Constructor ''' #import insightface from insightface.app import FaceAnalysis os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT'] = '0' # kwargs = {'root':os.path.join(options.storage_dir,'models')} #load Retina face model if options.gpuid == -1: ctx_id = -1 else: ctx_id = int(options.gpuid) #self.app = FaceAnalysis(allowed_modules=['detection','recognition']) self.app = FaceAnalysis() self.app.prepare(ctx_id=0, det_size=(640, 640)) print("InsightFaces Models Loaded.") def detect(self, img, face_records, options): '''Run a face detector and return rectangles.''' #print('Running Face Detector For ArchFace') img = img[:, :, :: -1] #convert from rgb to bgr . There is a reordering from bgr to RGB internally in the detector code. faces = self.app.get(img) #print('Number of detections ', dets.shape[0]) # Now process each face we found and add a face to the records list. idx = -1 for face in faces: idx += 1 face_record = face_records.face_records.add() face_record.detection.score = face.det_score ulx, uly, lrx, lry = face.bbox #create_square_bbox = np.amax(abs(lrx-ulx) , abs(lry-uly)) face_record.detection.location.CopyFrom( pt.rect_val2proto(ulx, uly, abs(lrx - ulx), abs(lry - uly))) face_record.detection.detection_id = idx face_record.detection.detection_class = "FACE" #lmark = face_record.landmarks.add() for ldx in range(0, face.kps.shape[0]): lmark = face_record.landmarks.add() lmark.landmark_id = "point_%02d" % ldx lmark.location.x = face.kps[ldx][0] lmark.location.y = face.kps[ldx][1] normed_embedding = face.normed_embedding face_record.template.data.CopyFrom( pt.vector_np2proto(normed_embedding)) if hasattr(face, 'landmark_2d_106'): attribute = face_record.attributes.add() attribute.key = 'landmark_2d_106' attribute.matrix.CopyFrom( pt.matrix_np2proto(face.landmark_2d_106)) if hasattr(face, 'landmark_3d_68'): attribute = face_record.attributes.add() attribute.key = 'landmark_3d_68' attribute.matrix.CopyFrom( pt.matrix_np2proto(face.landmark_3d_68)) if hasattr(face, 'sex'): attribute = face_record.attributes.add() attribute.key = 'sex' attribute.text = face.sex if hasattr(face, 'age'): attribute = face_record.attributes.add() attribute.key = 'age' attribute.fvalue = face.age if options.best: face_records.face_records.sort(key=lambda x: -x.detection.score) while len(face_records.face_records) > 1: del face_records.face_records[-1] #print('Done Running Face Detector For ArchFace') def locate(self, img, face_records, options): '''Locate facial features.''' pass #the 5 landmarks points that retina face detects are stored during detection def align(self, image, face_records): '''Align the images to a standard size and orientation to allow recognition.''' pass # Not needed for this algorithm. def extract(self, img, face_records): '''Extract a template that allows the face to be matched.''' # Compute the 512D vector that describes the face in img identified by #shape. #print(type(img),img.shape) def scoreType(self): '''Return the method used to create a score from the template. By default server computation is required. SCORE_L1, SCORE_L2, SCORE_DOT, SCORE_SERVER ''' return fsd.NEG_DOT def status(self): '''Return a simple status message.''' status_message = fsd.FaceServiceInfo() status_message.status = fsd.READY status_message.detection_support = True status_message.extract_support = True status_message.score_support = True status_message.score_type = self.scoreType() status_message.detection_threshold = self.recommendedDetectionThreshold( ) status_message.match_threshold = self.recommendedScoreThreshold() status_message.algorithm = "InsightFace antelopev2" return status_message def recommendedDetectionThreshold(self): return 0.5 def recommendedScoreThreshold(self, far=-1): ''' Arcface does not provide a match threshold ''' return -0.42838144