Exemplo n.º 1
0
    def test_delete_tracker_history_True(self, mock_dir):
        mock_dir.side_effect = fake_os('123')
        matcher = FakeMatcher()
        embs = np.eye(12)
        labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        matcher.build(embs, labels)
        # total 12 tracker
        zero = np.zeros(12)
        trackers = {}
        for i in range(12):
            vec = zero.copy()
            vec[i] = 1
            tracker = create_tracker([vec] * 6)
            tracker.area = "0"
            trackers[i] = tracker

        vec = np.zeros(12)
        vec[1] = 1
        vec[2] = 1
        vec[3] = 2
        vec[4] = 3
        new_tracker = create_tracker([vec] * 6)
        new_tracker.area = '0'
        tracker_list = TrackersList()
        tracker_list.trackers = {1: new_tracker}
        tracker_list.check_delete_trackers(matcher, Mock(), history_mode=True)
Exemplo n.º 2
0
def central_function():
    rabbit_mq = RabbitMQ((Config.Rabbit.USERNAME, Config.Rabbit.PASSWORD),
                         (Config.Rabbit.IP_ADDRESS, Config.Rabbit.PORT))
    cen_list = TrackersList()
    matcher = KdTreeMatcher()
    matcher._match_case = 'TCH'

    while True:
        fid, image, emb, area, _, origin_bb, angle = \
                    rabbit_mq.receive_tracking(rabbit_mq.RECEIVE_CAM_WORKER_TRACKING_QUEUE)
        if fid is not None:
            print("-- Received a face-track message from cam server")
            generated_fid = str(fid) + '-' + area
            cen_list.update_trackers_list(generated_fid, origin_bb, image, emb,
                                          angle, area, 0, matcher, rabbit_mq)

        # check time
        cen_list.check_delete_trackers(matcher, rabbit_mq)

        # Check extract trackers history time
        # cen_list.trackers_history.check_time()

        if fid is None:
            print('Waiting for new job ...')
            time.sleep(5)
Exemplo n.º 3
0
    def test_delete_tracker_history_false(self):
        matcher = FakeMatcher()
        embs = np.eye(12)
        labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        matcher.build(embs, labels)
        # total 12 tracker
        zero = np.zeros(12)
        trackers = {}
        for i in range(12):
            vec = zero.copy()
            vec[i] = 1
            tracker = create_tracker([vec] * 6)
            trackers[i] = tracker

        vec = np.zeros(12)
        vec[1] = 1
        vec[2] = 1
        vec[3] = 2
        vec[4] = 3
        new_tracker = create_tracker([vec] * 6)
        tracker_list = TrackersList()
        tracker_list.trackers = {1: new_tracker}
        tracker_list.check_delete_trackers(matcher, Mock(), history_mode=False)
Exemplo n.º 4
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()
Exemplo n.º 5
0
def simulate_tracking(root_folder):
    Config.Track.FACE_TRACK_IMAGES_OUT = True
    Config.Track.SEND_FIRST_STEP_RECOG_API = False
    Config.Track.MIN_MATCH_DISTACE_OUT = True
    Config.Track.CURRENT_EXTRACR_TIMER = 5

    # Load Face Extractor
    face_rec_graph = FaceGraph()
    face_rec_graph_coeff = FaceGraph()
    face_extractor = FacenetExtractor(
        face_rec_graph, model_path=Config.FACENET_DIR)
    coeff_extractor = FacenetExtractor(
        face_rec_graph_coeff, model_path=Config.COEFF_DIR)

    # Create empty KDTreeMatcher
    matcher = FaissMatcher()
    matcher._match_case = 'TCH'

    # Preprocessor
    preprocessor = Preprocessor()

    # Fake rabbit mq

    rabbit_mq = FakeMQ()

    # Clean up for
    clear_tracking_folder()
    if Config.Matcher.CLEAR_SESSION:
        clear_session_folder()

    # Setup result list
    list_of_trackers = TrackersList()
    track_results = TrackerResultsDict()
    predict_dict = {}
    confirmed_ids_dict = {}

    sim_detections = gen_images_with_time(root_folder)
    for detection in sim_detections:
        frame = create_fake_frame(detection)
        sleep(0.05)
        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)
        confirmed_ids_dict = list_of_trackers.trackers_history.confirm_id(
            confirmed_ids_dict)
        list_of_trackers.trackers_history.check_time(matcher)
        list_of_trackers.update_dlib_trackers(frame)
        facial_quality = 1
        # Crop face for features extraction

        origin_bb = detection[2]
        display_face, padded_bbox = CropperUtils.crop_display_face(
            frame, origin_bb)
        cropped_face = CropperUtils.crop_face(frame, origin_bb)

        bbox_str = '_'.join(np.array(origin_bb, dtype=np.unicode).tolist())
        # Calculate embedding
        preprocessed_image = preprocessor.process(cropped_face)
        emb_array, _ = face_extractor.extract_features(preprocessed_image)
        _, coeff = coeff_extractor.extract_features(preprocessed_image)
        if coeff < 0.15:
            img_path = '../data/notenoughcoeff/{}_{}_{}.jpg'.format(
                detection, bbox_str, coeff)
            cv2.imwrite(img_path, cv2.cvtColor(display_face, cv2.COLOR_BGR2RGB))
            facial_quality = -1
        else:
            with open('../data/coeff_log.txt', 'a') as f:
                f.write('{}_{}_{}, coeff: {}\n'.format(bbox_str, detection[0],
                                                       padded_bbox, coeff))

        matched_fid = list_of_trackers.matching_face_with_trackers(
            frame, detection[0], origin_bb, emb_array, facial_quality)
        if facial_quality == -1 or coeff < 0.15:
            continue

        list_of_trackers.update_trackers_list(
            matched_fid, time.time(), origin_bb, display_face, emb_array, 0,
            'VVT', 1, detection[0], padded_bbox, matcher, rabbit_mq)

        list_of_trackers.check_recognize_tracker(matcher, rabbit_mq,
                                                 matched_fid)

    sleep(6)
    list_of_trackers.check_delete_trackers(matcher, rabbit_mq)
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)