def doFaceTask(self, _task): # start = time.time() # TODO: Get timer data = _task.depackage() bbs, pts, frame, frame_info = data['bbs'], data['pts'], data[ 'frame'], data['frame_info'] nrof_faces = len(bbs) # timer.preprocess_start() faces = [] preprocessed_images = [] for i in range(nrof_faces): display_face, padded_bb_str = CropperUtils.crop_display_face( frame, bbs[i][:-1]) face = detection.FaceInfo(bbs[i][:-1], bbs[i][-1], frame_info, display_face, padded_bb_str, pts[:, i]) cropped_face = CropperUtils.crop_face(frame, bbs[i][:-1]) preprocessed = self.preprocessor.process(cropped_face) preprocessed_images.append(preprocessed) faces.append(face) preprocessed_images = np.array(preprocessed_images) # timer.preprocess_done() _task = task.Task(task.Task.Face) _task.package(faces=faces, images=preprocessed_images, frame=frame, frame_info=frame_info) self.putResult(_task)
def register_function(detector, preprocessor, face_extractor, rabbit_mq): ''' dia dia ''' # Doc queue msg_list = rabbit_mq.receive_once('{}-register'.format(Config.DEMO_FOR)) if msg_list is None: return False person_id = msg_list[0] # .lower() file_url_msg = msg_list[1] list_of_trackers = TrackersList() frame_counter = 0 saved_frames = save_frames(file_url_msg) if saved_frames == []: return False print('Detecting Faces and Extracting Features ...') saved_frames.reverse() for frame in saved_frames: list_of_trackers.update_dlib_trackers(frame) origin_bbs, points = detector.detect_face(frame) if origin_bbs is None: break for i, origin_bb in enumerate(origin_bbs): display_face, padded_bbox = CropperUtils.crop_display_face( frame, origin_bb) cropped_face = CropperUtils.crop_face(frame, origin_bb) # Calculate embedding preprocessed_image = preprocessor.process(cropped_face) emb_array = face_extractor.extract_features(preprocessed_image) matched_fid = list_of_trackers.matching_face_with_trackers( frame, frame_counter, origin_bb, emb_array) # Update list_of_trackers list_of_trackers.update_trackers_list(matched_fid, time.time(), origin_bb, display_face, emb_array, -1, 'register', frame_counter, padded_bbox, None, rabbit_mq, mode='register') frame_counter += 1 if frame_counter > 601: break if list_of_trackers.trackers == {}: return False # extract images extract_images(list_of_trackers.trackers, person_id) return True
def doFaceTask(self, _task): data = _task.depackage() task_name = data['type'] if task_name != Config.Worker.TASK_TRACKER: return tracker = data['tracker'] if tracker.face_id == Config.Track.INIT_FACE_ID or \ tracker.face_id == Config.Track.BAD_TRACK: tracker.assign_face_id('Anno') for tracker_element in tracker.elements: face_image = tracker_element.face_image cropped_image = CropperUtils.reverse_display_face( tracker_element.face_image, tracker_element.str_padded_bbox) bbox_confident = tracker_element.bbox_confident landmark = tracker_element.landmarks image_id = '{}_{}'.format(tracker.track_id, tracker_element.str_info()) image_name = image_id + '.jpg' image_pkl = image_id + '.pkl' dataset_pkl_path = os.path.join(self.dataset_dir, image_pkl) annotation_image_path = os.path.join(self.annotation_dir, image_name) PickleUtils.save_pickle(dataset_pkl_path, value=(cropped_image, tracker_element.embedding, bbox_confident, landmark)) misc.imsave(annotation_image_path, face_image) logger.debug('Save image: %s' % image_id)
def doFaceTask(self, _task): # detect all at once, no cuda memory may occur data = _task.depackage() bbs, scores, frame, frame_info = data['bbs'], data['scores'], data[ 'frame'], data['frame_info'] nrof_faces = len(bbs) # timer.preprocess_start() faces = [] for i in range(nrof_faces): display_face, padded_bb_str = CropperUtils.crop_display_face( frame, np.asarray(bbs[i])) face = detection.PedestrianInfo(np.asarray(bbs[i]), frame_info, display_face, padded_bb_str, scores[i]) faces.append(face) # logger.info("Extract: %s, #Faces: %s" % (frame_info, len(faces))) # timer.extract_start() embs = self.encoder(frame, bbs_to_tlwhs(faces)) face_infos = [] for i, face in enumerate(faces): face.embedding = embs[i] face_infos.append(face) _task = task.Task(task.Task.Face) _task.package(faces=face_infos) self.putResult(_task)
def doFaceTask(self, _task): start = time.time() data = _task.depackage() task_name = data['type'] if task_name != Config.Worker.TASK_EXTRACTION: return tracker = data['tracker'] nrof_elements = len(tracker.elements) # here we do the batch embs extracting _interval = Config.Track.NUM_IMAGE_PER_EXTRACT for i in range(0, nrof_elements, _interval): _interval = min(nrof_elements - i, _interval) preprocessed_images = [] for j in range(i, i + _interval): face_image = CropperUtils.reverse_display_face( tracker.elements[j].face_image, tracker.elements[j].str_padded_bbox) preprocessed_image = self.preprocessor.process(face_image) preprocessed_images.append(preprocessed_image) embeddings_array, _ = self.extractor.extract_features_all_at_once( preprocessed_images) tracker.update_embeddings(embeddings_array, i, _interval) _task = task.Task(task.Task.Face) _task.package(tracker=tracker) self.putResult(_task) print(self.name, time.time() - start)
def doFaceTask(self, _task): # start = time.time() # TODO: Get timer data = _task.depackage() bbs, pts, frame, frame_info = data['bbs'], data['pts'], data[ 'frame'], data['frame_info'] nrof_faces = len(bbs) # timer.preprocess_start() faces = [] preprocessed_face_images = [] preprocessed_coeff_images = [] for i in range(nrof_faces): display_face, padded_bb_str = CropperUtils.crop_display_face( frame, bbs[i][:-1]) face = detection.FaceInfo(bbs[i][:-1], bbs[i][-1], frame_info, display_face, padded_bb_str, pts[:, i]) preprocessed_face = self.face_preprocessor.process( frame, pts[:, i], self.aligner, Config.Align.IMAGE_SIZE, self.prewhitening) preprocessed_face_images.append(preprocessed_face) faces.append(face) if self.use_coeff_filter: cropped_face = CropperUtils.crop_face(frame, bbs[i][:-1]) preprocessed_coeff = self.coeff_preprocessor.process( cropped_face) preprocessed_coeff_images.append(preprocessed_coeff) preprocessed_face_images = np.array(preprocessed_face_images) preprocessed_coeff_images = np.array(preprocessed_coeff_images) # timer.preprocess_done() _task = task.Task(task.Task.Face) _task.package(faces=faces, images=preprocessed_face_images, coeff_images=preprocessed_coeff_images, frame=frame, frame_info=frame_info) self.putResult(_task)
def doFaceTask(self, _task): data = _task.depackage() bbs, pts, frame, frame_info = data['bbs'], data['pts'], data[ 'frame'], data['frame_info'] bbox = self.get_biggest_face(bbs) if bbox.any(): cropped_face = CropperUtils.crop_face(frame, bbox[:-1]) preprocessed = self.preprocessor.process(cropped_face) _task = task.Task(task.Task.Face) _task.package(images=preprocessed, frame_info=frame_info) self.putResult(_task)
def threading_register_function(detector, face_extractor, register_queue, matcher): ''' dia dia ''' # Doc queue preprocessor = Preprocessor() while True: try: register_msg = register_queue.get(False) except multiprocessing.queues.Empty: register_msg = None if register_msg: person_id = register_msg[0] # .lower() file_url_msg = register_msg[1] list_of_trackers = TrackersList() frame_counter = 0 saved_frames = save_frames(file_url_msg) if saved_frames == []: continue print('Detecting Faces and Extracting Features ...') saved_frames.reverse() for frame in saved_frames: list_of_trackers.update_dlib_trackers(frame) origin_bbs, points = detector.detect_face(frame) if origin_bbs is None: break for i, origin_bb in enumerate(origin_bbs): display_face, padded_bbox = CropperUtils.crop_display_face( frame, origin_bb) cropped_face = CropperUtils.crop_face(frame, origin_bb) # Calculate embedding preprocessed_image = preprocessor.process(cropped_face) emb_array = face_extractor.extract_features( preprocessed_image) matched_fid = list_of_trackers.matching_face_with_trackers( frame, frame_counter, origin_bb, emb_array) # Update list_of_trackers list_of_trackers.update_trackers_list(matched_fid, time.time(), origin_bb, display_face, emb_array, -1, 'register', frame_counter, padded_bbox, None, rabbit_mq, mode='register') frame_counter += 1 if frame_counter > 601: break if list_of_trackers.trackers == {}: return False # extract images extract_images(list_of_trackers.trackers, person_id) matcher.update(force=True) with open('register_log.txt', 'a') as f: f.write(str(len(list_of_trackers.trackers)) + person_id + '\n')