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!')
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))
Example #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'
Example #4
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 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.')
def get_history_in_send_folder():
    his_dic = {}
    send_rbmq_dir = '/home/manho/source-code/iq_facial_recognition/data/send_rbmq'
    img_dirs = glob.glob(send_rbmq_dir + '/*.jpg')
    for img_dir in img_dirs:
        print("Processing " + img_dir)
        img_mtime = os.stat(img_dir).st_mtime
        file_name = img_dir.split('/')[-1].split('.')[0]
        splited_file_name = file_name.split('_')
        face_id = splited_file_name[0]
        if not face_id in list(his_dic.keys()):
            his_dic[face_id] = []
        his_dic[face_id].append(img_mtime)
        his_dic[face_id] = sorted(his_dic[face_id])
    print("Saved history dictionary pickle!")
    PickleUtils.save_pickle('/home/manho/data/his_dic.pkl', his_dic)
Example #7
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
        })
Example #8
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)
Example #9
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)