def compare_function():
    kd_m = KdTreeMatcher()
    li_m = LinearMatcher()
    smaller_dict = PickleUtils.read_pickle(
        '../session/db/smaller_reg_dict.pkl', default={})
    if smaller_dict == {}:
        print('Missing ../session/db/smaller_reg_dict.pkl')
        return -1
    test_img_ids = list(smaller_dict.keys())
    test_embs = [
        PickleUtils.read_pickle(
            os.path.join(Config.LIVE_DIR,
                         '{}.{}'.format(image_id, Config.PKL_EXT)))[1]
        for image_id in test_img_ids
    ]
    same = 0
    not_same = 0
    for emb in test_embs:
        kd_id, _ = kd_m.match(emb)
        li_id, _ = li_m.match(emb)
        if kd_id == li_id:
            same += 1
        else:
            not_same += 1

    print('Same: {}\nNot Same: {}'.format(same, not_same))
def main(src, dst):
    # his_dic = get_history_in_send_folder()
    his_dic = PickleUtils.read_pickle('/home/manho/data/his_dic.pkl')
    NEW_TRACKING_DIR = dst
    create_if_not_exist(NEW_TRACKING_DIR)
    track_id_dirs = glob.glob(src + '/*')

    for track_id_dir in track_id_dirs:
        print('Processing ' + track_id_dir)
        splited_file_name = glob.glob(track_id_dir +
                                      '/*')[0].split('/')[-1].replace(
                                          '.jpg', '').split('_')
        face_id = splited_file_name[0]
        track_id = track_id_dir.split('/')[-1]
        print('FACEID: {}, TRACKID: {}'.format(face_id, track_id))

        face_id_dir = os.path.join(NEW_TRACKING_DIR, face_id)
        create_if_not_exist(face_id_dir)

        new_track_id_dir = os.path.join(face_id_dir, track_id)

        subprocess.call(["cp", "-r", track_id_dir, face_id_dir])
        this_mtime = -1
        if face_id in list(his_dic.keys()):
            this_mtime = his_dic[face_id].pop(0)
            if his_dic[face_id] == []:
                his_dic.pop(face_id, None)
        else:
            this_mtime = os.stat(track_id_dir).st_mtime
        modify_image_id(new_track_id_dir, track_id, time_stamp=this_mtime)
    PickleUtils.save_pickle('/home/manho/data/his_dic_remain.pkl', his_dic)
    print('Done!')
Ejemplo n.º 3
0
def extract_images(trackers, person_id):
    print('Extract Register Images ...')
    reg_dict = PickleUtils.read_pickle(Config.REG_IMAGE_FACE_DICT_FILE)
    if reg_dict is None:
        reg_dict = {}

    # Clear reg_dict and pickles in live folder
    reg_dict = clear_person_id_in_reg(reg_dict, person_id)
    print('Tracker len {}'.format(len(trackers)))
    print('Tracker ids')
    for i in trackers.keys():
        print(i)
    first_frame_id_tracked = trackers[0].elements[0].frame_id
    b_f = first_frame_id_tracked - 5
    a_f = first_frame_id_tracked + 5
    the_last_fids = [
        fid for fid in trackers.keys()
        if (b_f <= trackers[fid].elements[0].frame_id <= a_f)
    ]
    # counter_dict = {fid: len(trackers[fid].elements) for fid in trackers.keys()}
    # max_len_fid = max(counter_dict, key=counter_dict.get)
    if not os.path.exists('../data/tracking/register-{}'.format(person_id)):
        os.mkdir('../data/tracking/register-{}'.format(person_id))
    face_embs = []
    face_labels = []
    if len(the_last_fids) > 1:
        return face_embs, face_labels, 'many_faces'
    for fid in the_last_fids:
        if len(trackers[fid].elements) < 100:
            return face_embs, face_labels, 'not_good'
        with open('register_function_log.txt', 'a') as f:
            f.write('Len of elements {}\n'.format(len(trackers[fid].elements)))
        for index, element in enumerate(trackers[fid].elements):
            face_embs.append(element.embedding)

            # Write images out

            image_id = '{}_{}_{}_{}'.format(person_id, element.frame_id, index,
                                            element.time_stamp)
            live_tup = ((element.display_image), (element.embedding))
            PickleUtils.save_pickle('{}/{}.pkl'.format(Config.LIVE_DIR,
                                                       image_id),
                                    value=live_tup)
            with open('register_function_log.txt', 'a') as f:
                f.write('Save pkl {}/{}.pkl'.format(Config.LIVE_DIR, image_id))
            face_embs.append(element.embedding)
            # if not os.path.exists('../data/register/register-{}'.format(person_id)):
            #     os.mkdir('../data/register/register-{}'.format(person_id))
            # img_path = '../data/register/register-{}/{}.jpg'.format(person_id,
            #                                                         image_id)
            # misc.imsave(img_path, element.display_image)
            # Save register
            reg_dict[image_id] = person_id
    PickleUtils.save_pickle(Config.REG_IMAGE_FACE_DICT_FILE, value=reg_dict)
    face_labels = [person_id] * len(face_embs)
    return face_embs, face_labels, 'ok'
Ejemplo n.º 4
0
def regdict_to_faceinfo():
    mongodb_faceinfo.remove({})
    reg_dict = PickleUtils.read_pickle(reg_dict_path)
    reg_dict_length = len(reg_dict)
    for i, i_id in enumerate(reg_dict):
        print(reg_dict_length - i)
        splitted_image_id = i_id.split('_')
        track_id = int(splitted_image_id[0])
        image_id = i_id
        face_id = reg_dict[i_id]
        time_stamp = float(splitted_image_id[5])
        bounding_box = splitted_image_id[1:5]
        bounding_box = [int(bb_num) for bb_num in bounding_box]
        padded_bbox = splitted_image_id[-4:len(splitted_image_id)]
        padded_bbox = '_'.join(padded_bbox)

        display_img = PickleUtils.read_pickle(live_folder + '/' + i_id +
                                              '.pkl')[0]
        display_img = cv2.cvtColor(display_img, cv2.COLOR_BGR2RGB)

        cropped_img = CropperUtils.reverse_display_face(
            display_img, padded_bbox)
        # Extract feature
        preprocessed_image = preprocessor.process(cropped_img)
        emb_array, _ = face_extractor.extract_features(preprocessed_image)

        mongodb_faceinfo.insert_one({
            'track_id': track_id,
            'image_id': image_id,
            'face_id': face_id,
            'time_stamp': time_stamp,
            'bounding_box': bounding_box,
            'embedding': emb_array.tolist(),
            'points': None,
            'is_registered': True
        })
def generate_smaller_reg_dict():
    reg_image_face_dict = PickleUtils.read_pickle(
        Config.REG_IMAGE_FACE_DICT_FILE, default={})
    ids = set([reg_image_face_dict[fid] for fid in reg_image_face_dict])
    smaller_reg_dict = {}
    for single_id in ids:
        img_ids = [
            key for key, value in reg_image_face_dict.items()
            if value == single_id
        ]
        for i in range(5):
            smaller_reg_dict[img_ids[i]] = single_id
    PickleUtils.save_pickle('../session/db/smaller_reg_dict.pkl',
                            value=smaller_reg_dict)
    print('Smaller reg dict created.')
Ejemplo n.º 6
0
        labels = []
        for face_ids, face_embs in UNKNOW_EMBS_DICT.items():
            face_ids = [face_id] * len(face_embs)
            embs = embs + face_embs
            labels = labels + face_ids
        self.matcher.fit(embs, labels)

        # check that KNOWN set dist > 0
        for face_id, embs in KNOW_EMBS_DICT.items():
            for emb in embs:
                top_ids, dist = self.matcher.match(emb, 2, return_dists=True)
                self.assertNotAlmostEqual(dist[0], 0)


if __name__ == '__main__':
    reg_face_dict = PickleUtils.read_pickle(
        Config.PickleFile.REG_IMAGE_FACE_DICT_FILE, default={})
    face_ids = list(set(reg_face_dict.values()))
    known_faces = face_ids[:-5]
    unknown_faces = face_ids[-5:]
    print('Know face', known_faces)
    print('Unknow face', unknown_faces)
    KNOW_EMBS_DICT = defaultdict(list)
    KNOW_IMGID_EMBS_DICT = defaultdict(list)
    UNKNOW_EMBS_DICT = defaultdict(list)
    for image_id, face_id in reg_face_dict.items():
        emb = PickleUtils.read_pickle(
            os.path.join('/mnt/data/tch-data/tch-data/session/live/',
                         '{}.pkl'.format(image_id)))[1]
        if face_id in known_faces:
            KNOW_EMBS_DICT[face_id].append(emb)
            KNOW_IMGID_EMBS_DICT[image_id].append(emb)
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)