async def get(self, img, extract_embedding: bool = True, extract_ga: bool = True, return_face_data: bool = True, max_size: List[int] = None, threshold: float = 0.6): ts = time.time() t0 = time.time() # If detector has input_shape attribute, use it instead of provided value try: max_size = self.det_model.retina.input_shape[2:][::-1] except: pass img = ImageData(img, max_size=max_size) img.resize_image(mode='pad') t1 = time.time() logging.debug(f'Preparing image took: {t1 - t0}') t0 = time.time() boxes, probs, landmarks, mask_probs = self.det_model.detect(img.transformed_image, threshold=threshold) t1 = time.time() logging.debug(f'Detection took: {t1 - t0}') faces = [] await asyncio.sleep(0) if not isinstance(boxes, type(None)): t0 = time.time() for i in range(len(boxes)): # Translate points to original image size bbox = self.reproject_points(boxes[i], img.scale_factor) landmark = self.reproject_points(landmarks[i], img.scale_factor) det_score = probs[i] if not isinstance(mask_probs, type(None)): mask_prob = mask_probs[i] else: mask_prob = None # Crop faces from original image instead of resized to improve quality _crop = face_align.norm_crop(img.orig_image, landmark=landmark) face = Face(bbox=bbox, landmark=landmark, det_score=det_score, num_det=i, scale=img.scale_factor, mask_prob=mask_prob, facedata=_crop) faces.append(face) t1 = time.time() logging.debug(f'Cropping {len(boxes)} faces took: {t1 - t0}') # Process detected faces faces = [e for e in self.process_faces(faces, extract_embedding=extract_embedding, extract_ga=extract_ga, return_face_data=return_face_data)] tf = time.time() logging.debug(f'Full processing took: {tf - ts}') return faces
def detect(self, image): image = ImageData(image, self.input_shape) image.resize_image(mode='pad') dets, lmks = self.detector.detect(image.transformed_image, threshold=self.fd_conf) DETS, LMKS = [], [] for i in range(len(dets)): face = self.faceCrop(image, dets[i]) if min(face[2], face[3]) > 0: DETS.append(face) LMKS.append(lmks[i]) return np.array(DETS), np.array(LMKS)
def face_align(src_img, fd, args): src_image = ImageData(src_img, fd.input_shape) src_image.resize_image(mode='pad') dets, lmks = fd.detector.detect(src_image.transformed_image, threshold=args.fd_conf) if dets.shape[0] > 0: bindex = fd.get_max_face(dets) landmark = lmks[bindex] bbox = dets[bindex, :4].astype(np.int) image_size = '%d,%d' % (args.fr_input_size[1], args.fr_input_size[0]) warped = preprocess(src_image.transformed_image, bbox, landmark, image_size=image_size) return warped else: return None
async def get(self, img, extract_embedding: bool = True, extract_ga: bool = True, return_face_data: bool = True, max_size: List[int] = None, threshold: float = 0.6): ts = time.time() t0 = time.time() # If detector has input_shape attribute, use it instead of provided value try: max_size = self.det_model.retina.input_shape[2:][::-1] except: pass img = ImageData(img, max_size=max_size) img.resize_image(mode='pad') t1 = time.time() logging.debug(f'Preparing image took: {t1 - t0}') t0 = time.time() boxes, probs, landmarks = self.det_model.detect(img.transformed_image, threshold=threshold) t1 = time.time() logging.debug(f'Detection took: {t1 - t0}') await asyncio.sleep(0) ret = [] if not isinstance(boxes, type(None)): t0 = time.time() for i in range(len(boxes)): # Translate points to original image size bbox = self.reproject_points(boxes[i], img.scale_factor) landmark = self.reproject_points(landmarks[i], img.scale_factor) det_score = probs[i] # Crop faces from original image instead of resized to improve quality _crop = face_align.norm_crop(img.orig_image, landmark=landmark) facedata = None embedding = None embedding_norm = None normed_embedding = None gender = None age = None if return_face_data: facedata = _crop if extract_embedding and self.rec_model: embedding = self.rec_model.get_embedding(_crop).flatten() embedding_norm = norm(embedding) normed_embedding = embedding / embedding_norm if self.ga_model: if extract_ga: gender, age = self.ga_model.get(_crop) gender = int(gender) face = Face(bbox=bbox, landmark=landmark, det_score=det_score, embedding=embedding, gender=gender, age=age, normed_embedding=normed_embedding, embedding_norm=embedding_norm, facedata=facedata, num_det=i, scale=img.scale_factor) ret.append(face) t1 = time.time() logging.debug(f'Embedding {len(boxes)} faces took: {t1 - t0}') await asyncio.sleep(0) tf = time.time() logging.debug(f'Full processing took: {tf - ts}') return ret
backend = 'trt' im_size = [640, 480] detector = get_model(model_name, backend, im_size=im_size, root_dir='/models') detector.prepare(nms=0.35) # Since dynamic shapes are not yet implemented, images during inference # must be resized to exact shape which was used for TRT building. # To ensure this we read expected input shape from engine itself. input_shape = detector.input_shape[2:][::-1] logging.info(f"Images will be resized to WxH : {input_shape}") tt0 = time.time() image = cv2.imread('test_images2/china.jpg', cv2.IMREAD_COLOR) image = ImageData(image, input_shape) image.resize_image(mode='pad') tt1 = time.time() print(f"Preparing image took: {tt1 - tt0}") # Since we are using Numba JIT compilation, first run will cause # compilation on NMS module logging.info('Warming up NMS jit...') detections = detector.detect(image.transformed_image, threshold=0.1) logging.info('Warming up complete!') det_count = 0 t0 = time.time()
def get(self, img, extract_embedding: bool = True, extract_ga: bool = True, return_face_data: bool = True, max_size: List[int] = None, threshold: float = 0.6): ts = time.time() t0 = time.time() # If detector has input_shape attribute, use it instead of provided value try: max_size = self.det_model.retina.input_shape[2:][::-1] except: pass img = ImageData(img, max_size=max_size) img.resize_image(mode='pad') t1 = time.time() logging.debug(f'Preparing image took: {t1 - t0}') t0 = time.time() boxes, probs, landmarks, mask_probs = self.det_model.detect(img.transformed_image, threshold=threshold) t1 = time.time() logging.debug(f'Detection took: {t1 - t0}') ret = [] if not isinstance(boxes, type(None)): t0 = time.time() for i in range(len(boxes)): # Translate points to original image size bbox = self.reproject_points(boxes[i], img.scale_factor) landmark = self.reproject_points(landmarks[i], img.scale_factor) det_score = probs[i] if not isinstance(mask_probs, type(None)): mask_prob = mask_probs[i] else: mask_prob = None # Crop faces from original image instead of resized to improve quality _crop = face_align.norm_crop(img.orig_image, landmark=landmark) # _crop = cv2.resize(_crop, (48, 48)) # _crop = cv2.resize(_crop, (112, 112)) embedding = self.rec_model.get_embedding(_crop) embedding_norm = norm(embedding) normed_embedding = embedding / embedding_norm # print(normed_embedding.shape) # print(_crop.shape) facedata = None gender = None age = None if return_face_data: facedata = _crop # if self.ga_model: # if extract_ga: # gender, age = self.ga_model.get(_crop) # gender = int(gender) face = Face(bbox=bbox, landmark=landmark, det_score=det_score, facedata=facedata, num_det=i, scale=img.scale_factor, mask_prob=mask_prob, normed_embedding=normed_embedding) ret.append(face) t1 = time.time() logging.debug(f'Cropping {len(boxes)} faces took: {t1 - t0}') # Embedding detected faces # if extract_embedding and self.rec_model: # t0 = time.time() # ret = [e for e in self.embed_faces(ret)] # t1 = time.time() # logging.debug(f'Embedding {len(boxes)} faces took: {t1 - t0}') tf = time.time() logging.debug(f'Full processing took: {tf - ts}') return ret