Exemplo n.º 1
0
    def setUp(self):
        if not Config.DEBUG:
            raise ('To run RabbitMQTest, DEBUG Mode must be on')

        self.rb = RabbitMQ()
        self.queue_name = self.rb.RECEIVE_WEB_CAM
        self.frame_reader = RabbitFrameReader(self.rb, self.queue_name)
Exemplo n.º 2
0
class RabbitMQTest(unittest.TestCase):

    def setUp(self):
        if not Config.DEBUG:
            raise ('To run RabbitMQTest, DEBUG Mode must be on')

        self.rb = RabbitMQ()
        self.queue_name = self.rb.RECEIVE_WEB_CAM
        self.frame_reader = RabbitFrameReader(self.rb, self.queue_name)

    def test_no_frame_avaiable(self):
        frame = self.frame_reader.next_frame()
        self.assertIsNone(frame)

    def test_read_frame_from_queue(self):
        sample = sample_image()
        self.rb.send(self.queue_name, encode_image(sample))
        time.sleep(2)
        frame = self.frame_reader.next_frame()
        self.assertIsNotNone(frame)
        self.assertEqual(frame.shape, sample.shape)
        self.assertAlmostEqual(np.sum(frame - sample), 0)

    def test_read_queue_from_non_existing_queue(self):
        queue_name = 'non_exist'
        self.assertRaises(ValueError, RabbitFrameReader, self.rb, queue_name)
Exemplo n.º 3
0
def generate_video_sample(cam_url, area):
    '''generating'''
    print('Generating... ')
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))
    # Variables for tracking faces
    frame_counter = 0

    # Variables holding the correlation trackers and the name per faceid
    frame_sample = {}

    face_rec_graph = FaceGraph()
    face_extractor = FacenetExtractor(face_rec_graph)
    detector = MTCNNDetector(face_rec_graph)
    preprocessor = Preprocessor()
    if args.cam_url is not None:
        frame_reader = URLFrameReader(args.cam_url, scale_factor=1)
    else:
        frame_reader = RabbitFrameReader(rabbit_mq)

    try:
        while True:  # frame_reader.has_next():
            frame = frame_reader.next_frame()
            frame_sample[frame_counter] = FrameSample()
            frame_sample[frame_counter].read_image = frame
            if frame is None:
                print("Waiting for the new image")
                continue

            print("Frame ID: %d" % frame_counter)

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                origin_bbs, points = detector.detect_face(frame)
                frame_sample[frame_counter].origin_bbs = origin_bbs
                frame_sample[frame_counter].points = points
                for _, origin_bb in enumerate(origin_bbs):
                    cropped_face = CropperUtils.crop_face(frame, origin_bb)

                    # Calculate embedding
                    preprocessed_image = preprocessor.process(cropped_face)
                    emb_array, coeff = face_extractor.extract_features(
                        preprocessed_image)
                    frame_sample[frame_counter].embs.append(emb_array)

            frame_counter += 1
    except KeyboardInterrupt:
        print('Keyboard Interrupt !!! Release All !!!')
        print('Saved this video sample as ../session/db/sample.pkl')
        PickleUtils.save_pickle('../session/db/sample.pkl', frame_sample)
def get_frames(frame_queue, frame_reader, re_source, cam_url, queue_reader,
               lock, is_on):
    stream_time = time.time()
    while True and is_on[0]:
        if len(frame_queue) > Config.FRAME_QUEUE_SIZE:
            lock.acquire()
            frame_queue.clear()
            lock.release()
        frame = frame_reader.next_frame()
        if frame is not None:
            lock.acquire()
            frame_queue.append(frame)
            lock.release()
            stream_time = time.time()
        else:
            if time.time() - stream_time > Config.STREAM_TIMEOUT:
                print('Trying to connect to stream again...')
                if cam_url is not None:
                    frame_reader = URLFrameReader(cam_url, scale_factor=1)
                elif queue_reader is not None:
                    frame_reader = RabbitFrameReader(rabbit_mq, queue_reader)
                else:
                    print('Empty Image Source')
                    return -1
def generic_function(cam_url, queue_reader, area, face_extractor_model,
                     re_source, multi_thread):
    '''
    This is main function
    '''
    print("Generic function")
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))

    # Variables for tracking faces
    frame_counter = 0

    # Variables holding the correlation trackers and the name per faceid
    tracking_dirs = glob.glob(Config.TRACKING_DIR + '/*')
    if tracking_dirs == []:
        number_of_existing_trackers = 0
    else:
        lof_int_trackid = [
            int(tracking_dir.split('/')[-1]) for tracking_dir in tracking_dirs
        ]
        number_of_existing_trackers = max(lof_int_trackid) + 1
    imageid_to_keyid = {}
    tracker_manager = TrackerManager(
        area,
        imageid_to_keyid=imageid_to_keyid,
        current_id=number_of_existing_trackers)

    if Config.Matcher.CLEAR_SESSION:
        clear_session_folder()

    global querying_top10_image_ids_queue
    # mongodb_faceinfo.remove({})
    # reg_dict = PickleUtils.read_pickle(Config.REG_IMAGE_FACE_DICT_FILE)
    # if reg_dict is not None:
    #     for fid in reg_dict:
    #         mongodb_faceinfo.insert_one({'image_id': fid, 'face_id': reg_dict[fid]})
    #     print('Saved regdict in mongodb as collection regdict')
    matcher = FaissMatcher()
    matcher.build(
        mongodb_faceinfo, imageid_to_keyid=imageid_to_keyid, use_image_id=True)
    svm_matcher = None
    if Config.Matcher.CLOSE_SET_SVM:
        svm_matcher = SVMMatcher()
        svm_matcher.build(mongodb_faceinfo)

    track_results = TrackerResultsDict()

    if Config.CALC_FPS:
        start_time = time.time()
    if cam_url is not None:
        frame_reader = URLFrameReader(cam_url, scale_factor=1)
    elif queue_reader is not None:
        frame_reader = RabbitFrameReader(rabbit_mq, queue_reader)
    elif args.anno_mode:
        frame_reader = URLFrameReader('./nothing.mp4', scale_factor=1)
    else:
        print('Empty Image Source')
        return -1
    if not args.anno_mode:
        video_out_fps, video_out_w, video_out_h, = frame_reader.get_info()
        print(video_out_fps, video_out_w, video_out_h)
        bbox = [
            int(Config.Frame.ROI_CROP[0] * video_out_w),
            int(Config.Frame.ROI_CROP[1] * video_out_h),
            int(Config.Frame.ROI_CROP[2] * video_out_w),
            int(Config.Frame.ROI_CROP[3] * video_out_h)
        ]
        # bbox = [0, 0, video_out_w, video_out_h]

    video_out = None
    if Config.Track.TRACKING_VIDEO_OUT:
        video_out = VideoHandle(time.time(), video_out_fps, int(video_out_w),
                                int(video_out_h))

    # Turn on querying top 10 from queue
    if Config.QUERY_TOP10_MODE:
        thread = threading.Thread(target=give_this_id_10_closest_ids)
        thread.daemon = True
        thread.start()

    frame_queue = []
    lock = threading.Lock()

    if multi_thread:
        is_on = [True]
        t = threading.Thread(target=(get_frames), args=(frame_queue, frame_reader, re_source, \
                                                        cam_url, queue_reader, lock, is_on, ))
        t.start()

    try:
        while True:
            ms_msg = rabbit_mq.receive_str(Config.Queues.MERGE)
            ms_flag = 'merge'
            if ms_msg is None:
                ms_msg = rabbit_mq.receive_str(Config.Queues.SPLIT)
                ms_flag = 'split'
            if ms_msg is not None:
                merge_anchor, merge_list = extract_info_from_json(ms_msg)
                while merge_list != []:
                    image_id1 = merge_list.pop()
                    split_merge_id(ms_flag, image_id1, merge_anchor, matcher,
                                   preprocessor, face_extractor,
                                   tracker_manager, mongodb_dashinfo,
                                   mongodb_faceinfo, mongodb_mslog)
                continue

            action_msg = rabbit_mq.receive_str(Config.Queues.ACTION)
            if action_msg is not None:
                return_dict = json.loads(action_msg)
                print('Receive: {}'.format(return_dict))
                if return_dict['actionType'] == 'getNearest':
                    lock.acquire()
                    querying_top10_image_ids_queue.append(return_dict['data'])
                    lock.release()
                    continue

            if args.anno_mode:
                print('Annotation Mode, waiting for new tasks ...')
                time.sleep(1)
                continue

            if multi_thread:
                if len(frame_queue) > 0:
                    lock.acquire()
                    frame = frame_queue.pop(0)
                    lock.release()
                else:
                    frame = None
            else:
                frame = frame_reader.next_frame()

            tracker_manager.update_trackers(frame)

            #do this before check_and_recognize tracker (sync local matcher vs mongodb)

            trackers_return_dict, recognized_results = update_recognition(
                self, matcher, svm_matcher, mongodb_faceinfo)
            for tracker, matching_result_dict in recognized_results:
                handle_results(tracker, matching_result_dict, imageid_to_keyid = imageid_to_keyid, \
                                                                                    dump=False)

            # trackers_return_dict = tracker_manager.find_and_process_end_track(mongodb_faceinfo)
            track_results.merge(trackers_return_dict)

            tracker_manager.long_term_history.check_time(
                matcher, mongodb_faceinfo)

            if frame is None:
                print("Waiting for the new image")
                # if Config.Track.RECOGNIZE_FULL_TRACK:
                #     overtime_track_ids = tracker_manager.find_overtime_current_trackers(
                #         time_last=Config.Track.CURRENT_EXTRACR_TIMER-5,
                #         find_unrecognized=True
                #     )

                #     for overtime_track_id in overtime_track_ids:
                #         checking_tracker, top_predicted_face_ids, matching_result_dict = \
                #             tracker_manager.check_and_recognize_tracker(
                #                 matcher,
                #                 overtime_track_id,
                #                 mongodb_faceinfo,
                #                 svm_matcher)
                #         handle_results(checking_tracker, matching_result_dict, imageid_to_keyid = imageid_to_keyid,\
                #                                                                                  dump=False)
                if re_source and not multi_thread:
                    print('Trying to connect the stream again ...')
                    if cam_url is not None:
                        frame_reader = URLFrameReader(cam_url, scale_factor=1)
                    elif queue_reader is not None:
                        frame_reader = RabbitFrameReader(
                            rabbit_mq, queue_reader)
                    else:
                        print('Empty Image Source')
                        return -1
                break

            print("Frame ID: %d" % frame_counter)
            if "_rotate" in video:
                # rotate cw
                rotation = int(video.split("_")[-1].split(".")[0])
                frame = rotate_image_90(frame, rotation)

            if Config.Track.TRACKING_VIDEO_OUT:
                video_out.tmp_video_out(frame)
            if Config.CALC_FPS:
                fps_counter = time.time()

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                # crop frame
                #frame = frame[bbox[1]:bbox[3], bbox[0]:bbox[2],:]
                origin_bbs, points = detector.detect_face(frame)
                if len(origin_bbs) > 0:
                    origin_bbs = [origin_bb[:4] for origin_bb in origin_bbs]
                    display_and_padded_faces = [
                        CropperUtils.crop_display_face(frame, origin_bb)
                        for origin_bb in origin_bbs
                    ]
                    #cropped_faces = [CropperUtils.crop_face(frame, origin_bb) for origin_bb in origin_bbs]
                    preprocessed_images = [
                        preprocessor.process(
                            CropperUtils.crop_face(frame, origin_bb))
                        for origin_bb in origin_bbs
                    ]
                    embeddings_array, _ = face_extractor.extract_features_all_at_once(
                        preprocessed_images)
                for i, origin_bb in enumerate(origin_bbs):

                    display_face, str_padded_bbox = display_and_padded_faces[i]
                    #cropped_face = CropperUtils.crop_face(frame, origin_bb)

                    # Calculate embedding
                    preprocessed_image = preprocessed_images[i]
                    # preprocessed_image = align_preprocessor.process(frame, points[:,i], aligner, 160)
                    emb_array = np.asarray([embeddings_array[i]])

                    face_info = FaceInfo(
                        #oigin_bb.tolist(),
                        #emb_array,
                        frame_counter,
                        origin_bb,
                        points[:, i]
                        #display_face,
                        #str_padded_bbox,
                        #points[:,i].tolist()
                    )

                    is_good_face = handle_filters(points[:, i], coeff_extractor,
                                                  face_info, preprocessed_image)

                    face_info.is_good = is_good_face
                    # TODO: refractor matching_detected_face_with_trackers
                    matched_track_id = tracker_manager.track(face_info)

                    if not face_info.is_good:
                        print('BAD FACE')
                        continue

                    # Update tracker_manager
                    tracker_manager.update(matched_track_id, frame, face_info)
                    checking_tracker = None
                    # if not Config.Track.RECOGNIZE_FULL_TRACK:
                    #     checking_tracker, top_predicted_face_ids, matching_result_dict = \
                    #         tracker_manager.check_and_recognize_tracker(
                    #             matcher,
                    #             matched_track_id,
                    #             mongodb_faceinfo,
                    #             svm_matcher)
                    #     handle_results(checking_tracker, matching_result_dict, imageid_to_keyid = imageid_to_keyid, \
                    #                                                                 dump=True)

            # if Config.Track.RECOGNIZE_FULL_TRACK:
            #     overtime_track_ids = tracker_manager.find_overtime_current_trackers(
            #         time_last=Config.Track.CURRENT_EXTRACR_TIMER-5,
            #         find_unrecognized=True
            #     )

            #     for overtime_track_id in overtime_track_ids:
            #         checking_tracker, top_predicted_face_ids, matching_result_dict = \
            #             tracker_manager.check_and_recognize_tracker(
            #                 matcher,
            #                 overtime_track_id,
            #                 mongodb_faceinfo,
            #                 svm_matcher)
            #         handle_results(checking_tracker, matching_result_dict, imageid_to_keyid = imageid_to_keyid, \
            #                                                                                             dump=False)

            frame_counter += 1
            if Config.CALC_FPS:
                print("FPS: %f" % (1 / (time.time() - fps_counter)))
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
        Config.Track.CURRENT_EXTRACR_TIMER = 0
        trackers_return_dict = tracker_manager.find_and_process_end_track(
            mongodb_faceinfo)
        Config.Track.HISTORY_CHECK_TIMER = 0
        Config.Track.HISTORY_EXTRACT_TIMER = 0
        tracker_manager.long_term_history.check_time(matcher, mongodb_faceinfo)
    except KeyboardInterrupt:
        if multi_thread:
            is_on[0] = False
            t.join()
        print('Keyboard Interrupt !!! Release All !!!')
        Config.Track.CURRENT_EXTRACR_TIMER = 0
        trackers_return_dict = tracker_manager.find_and_process_end_track(
            mongodb_faceinfo)
        Config.Track.HISTORY_CHECK_TIMER = 0
        Config.Track.HISTORY_EXTRACT_TIMER = 0
        tracker_manager.long_term_history.check_time(matcher, mongodb_faceinfo)
        if Config.CALC_FPS:
            print('Time elapsed: {}'.format(time.time() - start_time))
            print('Avg FPS: {}'.format(
                (frame_counter + 1) / (time.time() - start_time)))
        frame_reader.release()
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
            video_out.release()
Exemplo n.º 6
0
def main_function(cam_url, image_url, queue_reader, area):
    '''
    This is main function
    '''
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))
    # Variables for tracking faces
    frame_counter = 0

    # Variables holding the correlation trackers and the name per faceid
    list_of_trackers = TrackersList()

    clear_tracking_folder()

    # Model for human detection
    print('Load YOLO model ...')
    options = {
        "model": "./cfg/yolo.cfg",
        "load": "../models/yolo.weights",
        "threshold": 0.5
    }
    detector = TFNet(options)

    # Model for person re-id
    body_extractor = BodyExtractor()

    if image_url is not None:
        imgcv = cv2.imread(image_url)
        results = detector.return_predict(imgcv)
        print(results)
        imgcv = draw_results(imgcv, results)
        print('Result drawn as ../test-data/result.jpg')
        cv2.imwrite('../test-data/result.jpg', imgcv)

    track_results = TrackerResultsDict()
    predict_dict = {}
    if Config.CALC_FPS:
        start_time = time.time()
    if args.cam_url is not None:
        frame_reader = URLFrameReader(args.cam_url, scale_factor=1)
    elif queue_reader is not None:
        frame_reader = RabbitFrameReader(rabbit_mq, queue_reader)
    else:
        print('Empty Image Source')
        return -1

    video_out_fps, video_out_w, video_out_h, = frame_reader.get_info()
    print(video_out_fps, video_out_w, video_out_h)
    center = (int(video_out_w / 2), int(video_out_h / 2))
    bbox = [
        int(center[0] - Config.Frame.ROI_CROP[0] * video_out_w),
        int(center[1] - video_out_h * Config.Frame.ROI_CROP[1]),
        int(center[0] + Config.Frame.ROI_CROP[2] * video_out_w),
        int(center[1] + Config.Frame.ROI_CROP[3] * video_out_h)
    ]
    video_out = None

    if Config.Track.TRACKING_VIDEO_OUT:
        # new_w = abs(bbox[0] - bbox[2])
        # new_h = abs(bbox[1] - bbox[3])
        # print(new_w, new_h)
        video_out = VideoHandle('../data/tracking_video_out.avi',
                                video_out_fps, int(video_out_w),
                                int(video_out_h))
    dlib_c_tr_video = cv2.VideoWriter('../data/check_tracking_video.avi',
                                      cv2.VideoWriter_fourcc(*'XVID'),
                                      video_out_fps,
                                      (int(video_out_w), int(video_out_h)))

    try:
        while True:  # frame_reader.has_next():
            frame = frame_reader.next_frame()
            if frame is None:
                print("Waiting for the new image")
                trackers_return_dict, predict_trackers_dict = \
                    list_of_trackers.check_delete_trackers(None, rabbit_mq)
                track_results.update_two_dict(trackers_return_dict)
                predict_dict.update(predict_trackers_dict)
                # list_of_trackers.trackers_history.check_time(None)
                # if args.cam_url is not None:
                #     frame_reader = URLFrameReader(args.cam_url, scale_factor=1)
                # elif queue_reader is not None:
                #     frame_reader = RabbitFrameReader(rabbit_mq, queue_reader)
                # else:
                #     print('Empty Image Source')
                #     return -1

                continue

            print("Frame ID: %d" % frame_counter)

            # frame = frame[bbox[1]:bbox[3], bbox[0]:bbox[2], :]

            if Config.Track.TRACKING_VIDEO_OUT:
                video_out.tmp_video_out(frame)
            if Config.CALC_FPS:
                fps_counter = time.time()

            tmpi = list_of_trackers.update_dlib_trackers(frame)
            dlib_c_tr_video.write(tmpi)

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                start_time = time.time()
                detected_bbs = []
                results = detector.return_predict(frame)
                print('Detect time: {}'.format(1 / (time.time() - start_time)))
                for result in results:
                    if result['label'] != 'person':
                        continue
                    x1 = result['topleft']['x']
                    y1 = result['topleft']['y']
                    x2 = result['bottomright']['x']
                    y2 = result['bottomright']['y']

                    origin_bb = [x1, y1, x2, y2]
                    detected_bbs.append(origin_bb)

                    print(is_inner_bb(bbox, origin_bb))
                    if not is_inner_bb(bbox, origin_bb):
                        continue
                    if calc_bb_size(origin_bb) < 10000:
                        continue
                    bb_size = calc_bb_size(origin_bb)
                    body_image = frame[origin_bb[1]:origin_bb[3],
                                       origin_bb[0]:origin_bb[2], :]
                    # body_emb = body_extractor.extract_feature(body_image)

                    # TODO: refractor matching_detected_face_with_trackers
                    matched_fid = list_of_trackers.matching_face_with_trackers(
                        frame, frame_counter, origin_bb, None, body_image,
                        body_extractor)

                    # Update list_of_trackers
                    list_of_trackers.update_trackers_list(
                        matched_fid, time.time(), origin_bb, body_image,
                        bb_size, area, frame_counter, None, body_extractor,
                        rabbit_mq)

            # list_of_trackers.update_trackers_by_tracking(body_extractor,
            #                                              frame,
            #                                              area,
            #                                              frame_counter,
            #                                              detected_bbs,
            #                                              rabbit_mq)

            trackers_return_dict, predict_trackers_dict = \
                list_of_trackers.check_delete_trackers(None, rabbit_mq)
            track_results.update_two_dict(trackers_return_dict)
            predict_dict.update(predict_trackers_dict)

            # Check extract trackers history time (str(frame_counter) + '_' + str(i))
            # list_of_trackers.trackers_history.check_time(None)

            frame_counter += 1
            if Config.CALC_FPS:
                print("FPS: %f" % (1 / (time.time() - fps_counter)))
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
        if Config.Track.PREDICT_DICT_OUT:
            PickleUtils.save_pickle(Config.PREDICTION_DICT_FILE, predict_dict)
    except KeyboardInterrupt:
        print('Keyboard Interrupt !!! Release All !!!')
        # list_of_trackers.trackers_history.check_time(matcher)
        if Config.CALC_FPS:
            print('Time elapsed: {}'.format(time.time() - start_time))
            print('Avg FPS: {}'.format(
                (frame_counter + 1) / (time.time() - start_time)))
        frame_reader.release()
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
            video_out.release()
        if Config.Track.PREDICT_DICT_OUT:
            PickleUtils.save_pickle(Config.PREDICTION_DICT_FILE, predict_dict)
Exemplo n.º 7
0
def cam_worker_function(cam_url, area):
    '''
    Cam worker function
    '''
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))

    # Modify Config
    Config.Track.TRACKING_QUEUE_CAM_TO_CENTRAL = True

    rabbit_mq = RabbitMQ((Config.Rabbit.USERNAME, Config.Rabbit.PASSWORD),
                         (Config.Rabbit.IP_ADDRESS, Config.Rabbit.PORT))

    frame_counter = 0

    # Variables holding the correlation trackers and the name per faceid
    list_of_trackers = TrackersList()

    face_rec_graph = FaceGraph()
    face_extractor = FacenetExtractor(face_rec_graph)
    detector = MTCNNDetector(face_rec_graph)
    preprocessor = Preprocessor()
    matcher = KdTreeMatcher()
    if Config.CALC_FPS:
        start_time = time.time()
    if args.cam_url is not None:
        frame_reader = URLFrameReader(args.cam_url, scale_factor=1.5)
    else:
        frame_reader = RabbitFrameReader(rabbit_mq)

    try:
        while True:  # frame_reader.has_next():
            frame = frame_reader.next_frame()
            if frame is None:
                print("Waiting for the new image")
                list_of_trackers.check_delete_trackers(matcher,
                                                       rabbit_mq,
                                                       history_mode=False)
                continue

            print("Frame ID: %d" % frame_counter)

            if Config.CALC_FPS:
                fps_counter = time.time()

            list_of_trackers.update_dlib_trackers(frame)

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                origin_bbs, points = detector.detect_face(frame)
                for i, origin_bb in enumerate(origin_bbs):
                    display_face, _ = CropperUtils.crop_display_face(
                        frame, origin_bb)
                    print("Display face shape")
                    print(display_face.shape)
                    if 0 in display_face.shape:
                        continue
                    cropped_face = CropperUtils.crop_face(frame, origin_bb)

                    # Calculate embedding
                    preprocessed_image = preprocessor.process(cropped_face)
                    emb_array, coeff = face_extractor.extract_features(
                        preprocessed_image)

                    # Calculate angle
                    angle = FaceAngleUtils.calc_angle(points[:, i])

                    # TODO: refractor matching_detected_face_with_trackers
                    matched_fid = list_of_trackers.matching_face_with_trackers(
                        frame, origin_bb, emb_array)

                    # Update list_of_trackers
                    list_of_trackers.update_trackers_list(
                        matched_fid, origin_bb, display_face, emb_array, angle,
                        area, frame_counter, matcher, rabbit_mq)

                    if Config.Track.TRACKING_QUEUE_CAM_TO_CENTRAL:
                        track_tuple = (matched_fid, display_face, emb_array,
                                       area, time.time(), origin_bb, angle)
                        rabbit_mq.send_tracking(
                            track_tuple,
                            rabbit_mq.RECEIVE_CAM_WORKER_TRACKING_QUEUE)

            # Check detete current trackers time
            list_of_trackers.check_delete_trackers(matcher,
                                                   rabbit_mq,
                                                   history_mode=False)

            frame_counter += 1
            if Config.CALC_FPS:
                print("FPS: %f" % (1 / (time.time() - fps_counter)))

    except KeyboardInterrupt:
        print('Keyboard Interrupt !!! Release All !!!')
        if Config.CALC_FPS:
            print('Time elapsed: {}'.format(time.time() - start_time))
            print('Avg FPS: {}'.format(
                (frame_counter + 1) / (time.time() - start_time)))
        frame_reader.release()
def generic_function(cam_url, queue_reader, area, re_source, use_frame_queue):
    global rabbit_mq
    '''
    This is main function
    '''
    print("Generic function")
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))

    if Config.Matcher.CLEAR_SESSION:
        clear_session_folder()

    if Config.Mode.CALC_FPS:
        start_time = time.time()

    if cam_url is not None:
        frame_reader = URLFrameReader(cam_url,
                                      scale_factor=1,
                                      re_source=re_source)
    elif queue_reader is not None:
        frame_reader = RabbitFrameReader(rabbit_mq, queue_reader)
    else:
        print('Empty Image Source')
        return -1

    if use_frame_queue:
        frame_src = FrameQueue(frame_reader,
                               max_queue_size=Config.Frame.FRAME_QUEUE_SIZE)
        frame_src.start()
    else:
        frame_src = frame_reader

    video_out = None
    if Config.Track.TRACKING_VIDEO_OUT:
        video_out = VideoHandle(time.time(), video_out_fps, int(video_out_w),
                                int(video_out_h))

    # Variables for tracking faces
    frame_counter = 0
    # Variables holding the correlation trackers and the name per faceid
    tracking_dirs = os.listdir(Config.Dir.TRACKING_DIR)
    if tracking_dirs == []:
        current_tracker_id = 0
    else:
        list_of_trackid = [int(tracking_dir) for tracking_dir in tracking_dirs]
        current_tracker_id = max(list_of_trackid) + 1
    imageid_to_keyid = {}

    matcher = FaissMatcher()
    matcher.build(database,
                  imageid_to_keyid=imageid_to_keyid,
                  use_image_id=True)
    tracker_manager = TrackerManager(area,
                                     matcher,
                                     database.mongodb_faceinfo,
                                     imageid_to_keyid=imageid_to_keyid,
                                     current_id=current_tracker_id)

    try:
        while True:
            frame = frame_src.next_frame()
            if Config.Mode.CALC_FPS:
                fps_counter = time.time()
            if frame is None:
                print("Waiting for the new image")
                tracker_manager.update(rabbit_mq)
                time.sleep(1)
                continue

            # track by kcf
            tracker_manager.update_trackers(frame)
            tracker_manager.update(rabbit_mq)

            origin_bbs, points = TensorflowAdapter.detect_face(frame)
            if len(origin_bbs) > 0:
                origin_bbs = [origin_bb[:4] for origin_bb in origin_bbs]
                embeddings_array = [None] * len(origin_bbs)
                tracker_manager.process_new_detections(frame,
                                                       origin_bbs,
                                                       points,
                                                       embeddings_array,
                                                       frame_id=frame_counter)

            frame_counter += 1
            if Config.Mode.CALC_FPS:
                print("FPS: %f" % (1 / (time.time() - fps_counter)))

        #TODO: this line never run
        tracker_manager.update(rabbit_mq)

    except KeyboardInterrupt:
        if use_frame_queue:
            frame_src.stop()
        print('Keyboard Interrupt !!! Release All !!!')
        tracker_manager.update(rabbit_mq)

        frame_src.release()
        if Config.CALC_FPS:
            print('Time elapsed: {}'.format(time.time() - start_time))
            print('Avg FPS: {}'.format(
                (frame_counter + 1) / (time.time() - start_time)))
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
            video_out.release()

    else:
        raise Exception('Error')
def generic_function(cam_url, area):
    '''
    This is main function
    '''
    print("Generic function")
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))
    # Variables for tracking faces

    # Variables holding the correlation trackers and the name per faceid
    list_of_trackers = TrackersList()

    clear_tracking_folder()

    matcher = KdTreeMatcher()
    print("Load sample")
    frame_sample = PickleUtils.read_pickle('../session/db/sample.pkl')
    frame_counter = 0
    track_results = TrackerResultsDict()
    predict_dict = {}
    if Config.CALC_FPS:
        start_time = time.time()
    if args.cam_url is not None:
        frame_reader = URLFrameReader(args.cam_url, scale_factor=1)
    else:
        frame_reader = RabbitFrameReader(rabbit_mq)
    video_out = None
    video_out_fps = 24
    video_out_w = 1920
    video_out_h = 1080
    print(video_out_fps, video_out_w, video_out_h)
    center = (int(video_out_w / 2), int(video_out_h / 2))
    bbox = [
        int(center[0] - 0.35 * video_out_w),
        int(center[1] - video_out_h * 0.35),
        int(center[0] + 0.35 * video_out_w),
        int(center[1] + 0.35 * video_out_h)
    ]
    if Config.Track.TRACKING_VIDEO_OUT:
        video_out = VideoHandle('../data/tracking_video_out.avi', video_out_fps,
                                int(video_out_w), int(video_out_h))
    try:
        while True:  # frame_reader.has_next():
            frame = frame_sample[frame_counter].read_image
            if frame is None:
                print("Waiting for the new image")
                trackers_return_dict, predict_trackers_dict = \
                    list_of_trackers.check_delete_trackers(matcher, rabbit_mq)
                track_results.update_two_dict(trackers_return_dict)
                predict_dict.update(predict_trackers_dict)
                continue

            print("Frame ID: %d" % frame_counter)
            print('Num of ids in matcher: {}'.format(matcher._numofids))

            if Config.Track.TRACKING_VIDEO_OUT:
                video_out.tmp_video_out(frame)
            if Config.CALC_FPS:
                fps_counter = time.time()

            list_of_trackers.update_dlib_trackers(frame)

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                origin_bbs = frame_sample[frame_counter].origin_bbs
                points = frame_sample[frame_counter].points
                for i, origin_bb in enumerate(origin_bbs):
                    print(is_inner_bb(bbox, origin_bb))
                    if not is_inner_bb(bbox, origin_bb):
                        continue
                    display_face, _ = CropperUtils.crop_display_face(
                        frame, origin_bb)

                    # Calculate embedding
                    emb_array = frame_sample[frame_counter].embs[i]

                    # Calculate angle
                    angle = FaceAngleUtils.calc_angle(points[:, i])

                    # TODO: refractor matching_detected_face_with_trackers
                    matched_fid = list_of_trackers.matching_face_with_trackers(
                        frame, origin_bb, emb_array)

                    # Update list_of_trackers
                    list_of_trackers.update_trackers_list(
                        matched_fid, time.time(), origin_bb, display_face,
                        emb_array, angle, area, frame_counter, i, matcher,
                        rabbit_mq)

            trackers_return_dict, predict_trackers_dict = \
                list_of_trackers.check_delete_trackers(matcher, rabbit_mq)
            track_results.update_two_dict(trackers_return_dict)
            predict_dict.update(predict_trackers_dict)

            # Check extract trackers history time (str(frame_counter) + '_' + str(i))
            list_of_trackers.trackers_history.check_time()

            frame_counter += 1
            if Config.CALC_FPS:
                print("FPS: %f" % (1 / (time.time() - fps_counter)))
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
        if Config.Track.PREDICT_DICT_OUT:
            PickleUtils.save_pickle(Config.PREDICTION_DICT_FILE, predict_dict)
    except KeyboardInterrupt:
        print('Keyboard Interrupt !!! Release All !!!')
        if Config.CALC_FPS:
            print('Time elapsed: {}'.format(time.time() - start_time))
            print('Avg FPS: {}'.format(
                (frame_counter + 1) / (time.time() - start_time)))
        frame_reader.release()
        if Config.Track.TRACKING_VIDEO_OUT:
            print('Write track video')
            video_out.write_track_video(track_results.tracker_results_dict)
            video_out.release()
        if Config.Track.PREDICT_DICT_OUT:
            PickleUtils.save_pickle(Config.PREDICTION_DICT_FILE, predict_dict)