def display(cmd_args, in_que, log_que, log_level, kill_proc, kill_app): logger = get_logger(log_que, log_level) try: logger.info('Display process starting') while kill_app.value == 0 and kill_proc.value == 0: try: obj = in_que.get(block=True, timeout=1) except Empty as emp: logger.debug('Input que to display is empty.') continue if obj.get('done', False): break if not draw(obj, logger): kill_proc.value = 1 kill_app.value = 1 break except Exception as ex: logger.error(traceback.format_exc()) kill_app.value = 1 finally: logger.info('Exiting from Display process')
def video_writer(cmd_args, in_que, log_que, log_level, kill_proc, kill_app): logger = get_logger(log_que, log_level) video = cv2.VideoWriter(cmd_args.video_out, cv2.VideoWriter_fourcc(*'mp4v'), cmd_args.fps, (cmd_args.img_width, cmd_args.img_height)) try: while kill_app.value == 0 and kill_proc.value == 0: try: obj = in_que.get(block=True, timeout=1) except Empty as emp: logger.debug('Input que to display is empty.') continue if obj.get('done', False): break frame = draw_on_frame(obj) video.write(frame) except Exception as ex: logger.error(traceback.format_exc()) kill_app.value = 1 finally: logger.info('Exiting from VideoWriter process')
def face_detector(cmd_args, in_que, out_que, log_que, log_level, kill_proc, kill_app): logger = get_logger(log_que, log_level) try: logger.debug('FaceDetector process starting') detection_threshold = cmd_args.detection_threshold min_face_height = cmd_args.min_face_height logger.info("Loading Face detector model...") mtcnn = MTCNN() while kill_proc.value == 0 and kill_app.value == 0: try: obj = in_que.get(block=True, timeout=1) except Empty as emp: logger.debug('Input que to face detector is empty.') continue if obj.get('done', False): out_que.put(obj) break obj = detect_face(obj,mtcnn,detection_threshold,min_face_height, logger) out_que.put(obj) except Exception as ex: logger.error(traceback.format_exc()) kill_proc.value = 1 kill_app.value = 1 finally: logger.info('Exiting from Face Detector')
def __init__(self, cmd_args, in_que, log_que, log_level, kill_app, is_sub_proc=False): self.is_sub_proc = is_sub_proc self.logger = get_logger(log_que, log_level) super(ImageWriter, self).__init__(None, cmd_args=cmd_args, in_que=in_que, log_que=log_que, log_level=log_level, kill_app=kill_app)
def __init__(self, cmd_args, in_que, log_que, log_level, kill_app, is_sub_proc=False): self.is_sub_proc = is_sub_proc self.logger = get_logger(log_que, log_level) super(Display, self).__init__(display, cmd_args=cmd_args, in_que=in_que, log_que=log_que, log_level=log_level, kill_app=kill_app)
def __init__(self, cmd_args, in_que, out_que, log_que, log_level, kill_app, is_sub_proc=False): self.is_sub_proc = is_sub_proc if is_sub_proc is False: self.logger = get_logger(log_que, log_level) self.mtcnn = MTCNN() self.graph = tf.get_default_graph() self.session = tf.Session(graph=self.graph) super(FaceDetector, self).__init__(face_detector, cmd_args=cmd_args, in_que=in_que, out_que=out_que, log_que=log_que, log_level=log_level, kill_app=kill_app)
def embedding_generator(cmd_args, in_que, log_que, log_level, kill_proc, kill_app): logger = get_logger(log_que, log_level) try: logger.info("Generating embeddings...") face_group_manager = FaceGroupLocalManager(cmd_args) face_group_manager.create_face_group(cmd_args.face_group or 'default') while kill_proc.value == 0 and kill_app.value == 0: try: obj = in_que.get(block=True, timeout=1) except Empty as emp: logger.debug('Input que to face detector is empty.') continue if obj.get('done', False): kill_app.value = 1 break if len(obj.get('faces', [])) == 1: face_group_manager.append_to_face_group( cmd_args.face_group or 'default', obj['embeddings'][0], obj['label']) logger.info("Face {} added.".format(obj['label'])) else: logger.error("0 or more than 1 face detected for {}".format( obj['label'])) logger.info('Completed!') except Exception as ex: logger.error(traceback.format_exc()) kill_app.value = 1 finally: logger.info('Exiting from Embedding Generator')
def face_recogniser(cmd_args, in_que, out_que, log_que, log_level, kill_proc, kill_app): logger = get_logger(log_que, log_level) try: logger.debug("Loading Face recognition model...") fg_manager = FaceGroupLocalManager(cmd_args) fg = fg_manager.get_face_group(cmd_args.face_group) known_embeddings = fg.embeddings known_labels = fg.labels known_face_ids = fg.faceIds while kill_proc.value == 0 and kill_app.value == 0: try: obj = in_que.get(block=True, timeout=1) except Empty as emp: logger.debug('Input que to face encoder is empty.') continue if obj.get('done', False): out_que.put(obj) break obj = recognise(obj, known_embeddings, known_labels, known_face_ids, cmd_args.recognition_threshold) out_que.put(obj) except Exception as ex: logger.error(traceback.format_exc()) kill_app.value = 1 finally: logger.info('Exiting from Face Recogniser')
def capture(cmd_args, out_queue, log_que, log_level, kill_proc, kill_app, draw_frames): logger = get_logger(log_que, log_level) device = cmd_args.camera_url if cmd_args.camera_url != '' else cmd_args.camera_index REQUIRED_FPS = cmd_args.fps CAMERA_WAIT = cmd_args.camera_wait img_dim = (cmd_args.img_width, cmd_args.img_height) NUM_FRAME_TO_SKIP = 2 logger.info('Acquiring camera. Please wait...') sleep(CAMERA_WAIT) factor = cmd_args.img_red_factor cap = cv2.VideoCapture(device) logger.info('Camera acquired') cap.set(cv2.CAP_PROP_FRAME_WIDTH, img_dim[0]) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, img_dim[1]) logger.info("Capturing Images with dimension: {}".format(img_dim)) def exit_gracefully(signum, frame): kill_app.value = 1 logger.warning('Terminating camera process due to kill signal') cap.release() cv2.destroyAllWindows() utils.clean_queue(out_queue) signal.signal(signal.SIGINT, exit_gracefully) signal.signal(signal.SIGTERM, exit_gracefully) skip_count = 0 tik = time() count = 0 if cap.isOpened(): logger.info('Camera opened') else: logger.error('Unable to open camera') while cap.isOpened(): if kill_proc.value > 0 or kill_app.value > 0: logger.warning('Breaking camera process loop') break ret, frame = cap.read() tok = time() count += 1 if (tok - tik) > 2.0: tik = time() camera_fps = math.ceil(count / 2) NUM_FRAME_TO_SKIP = math.ceil( (camera_fps - REQUIRED_FPS) / REQUIRED_FPS) count = 0 if ret is True: cv2.flip(frame, 1, frame) if skip_count >= NUM_FRAME_TO_SKIP: frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) red_frame_rgb = cv2.resize(frame_rgb, (int( frame.shape[1] * factor), int(frame.shape[0] * factor))) out_queue.put( { 'cap_time': time(), 'raw_frame': frame, 'small_rgb_frame': red_frame_rgb, 'factor': factor }, block=True, timeout=5) skip_count = 0 if draw_frames: logger.info( "Required frame size {}. Captured size {}".format( img_dim, frame.shape)) cv2.imshow('CameraFeed', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break else: skip_count += 1 else: break kill_app.value = 1 cap.release() utils.clean_queue(out_queue) cv2.destroyAllWindows() logger.info('Exiting from Camera Process')