def main(matcher_path, test_path):
    m_trackers_paths = glob.glob(matcher_path + '/*')
    t_trackers_paths = glob.glob(test_path + '/*')
    tracker_manager = TrackerManager('test')
    matcher = FaissMatcher()
    preprocessor = Preprocessor()
    align_preprocessor = Preprocessor(algs=align_and_crop)
    face_rec_graph_face = FaceGraph()
    face_extractor = FacenetExtractor(face_rec_graph_face,
                                      model_path=Config.FACENET_DIR)
    detector = MTCNNDetector(face_rec_graph_face)

    # create matcher
    print('Creating matcher ...')
    for m_dir in m_trackers_paths:
        print('Processing ' + m_dir)
        face_id = m_dir.split('/')[-1]
        embs, labels = extract_embs(m_dir, preprocessor, face_extractor, None)
        face_id_labels = [face_id for i in range(len(labels))]
        matcher.update(embs, face_id_labels)

    # create tracker
    print('Creating trackers')
    for t_dir in t_trackers_paths:
        print('Processing ' + t_dir)
        embs, _ = extract_embs(t_dir, preprocessor, face_extractor, None)
        track_id = int(t_dir.split('/')[-1])

        first_emb = embs.pop()
        face_info = FaceInfo(None, first_emb, None, None, None, None)
        tracker_manager.current_trackers[track_id] = Tracker(
            track_id, face_info, None)
        for emb in embs:
            face_info = FaceInfo(None, emb, None, None, None, None)
            tracker_manager.current_trackers[track_id].update(face_info, None)
        len(tracker_manager.current_trackers)

    # test matching
    print('Test matching ...')
    for fid in tracker_manager.current_trackers:
        print('Processing: ' + str(fid))
        tops = tracker_manager.recognize_current_tracker(fid, matcher, None)
        print('Track_id {}, recognize: {}'.format(fid, tops))
def give_this_id_10_closest_ids():
    # init matcher
    with open('../data/top10querylog.txt', 'a') as f:
        f.write('TOP10 QUERY IS BEING IN PROCESS !!!\n\n')
    global querying_top10_image_ids_queue
    global mongodb_faceinfo
    global mongodb_dashinfo
    embs = []
    labels = []
    cursors = mongodb_dashinfo.find({})
    unique_labels = [cursor['represent_image_id'] for cursor in cursors]
    cursors = mongodb_faceinfo.find({'image_id': {'$in': unique_labels}})
    for cursor in cursors:
        embs.append(np.array(cursor['embedding']))
        labels.append(cursor['image_id'])
    nof_registered_image_ids = len(labels)
    matcher = FaissMatcher()
    matcher.fit(embs, labels)

    with open('../data/top10querylog.txt', 'a') as f:
        f.write('MATCHER BUILT!!!\n\n')

    while True:
        if nof_registered_image_ids < mongodb_dashinfo.find({}).count():
            nof_registered_image_ids = mongodb_dashinfo.find({}).count()
            print('[Query TOP10] Update new registered image_id ...')
            cursors = mongodb_dashinfo.find({
                'represent_image_id': {
                    '$nin': labels
                }
            })
            unique_labels = [cursor['represent_image_id'] for cursor in cursors]
            cursors = mongodb_faceinfo.find({
                'image_id': {
                    '$in': unique_labels
                }
            })
            adding_embs = []
            adding_labels = []
            for cursor in cursors:
                adding_embs.append(np.array(cursor['embedding']))
                adding_labels.append(cursor['image_id'])
            old_embs = embs
            old_labels = labels
            embs = old_embs + adding_embs
            labels = old_labels + adding_labels

            matcher.update(adding_embs, adding_labels)

        if not len(querying_top10_image_ids_queue) == 0:
            lock.acquire()
            queue_data = querying_top10_image_ids_queue.pop()
            lock.release()
            results = {}
            session_id = queue_data['sessionId']
            image_id = queue_data['imageId']
            print('[Query TOP10] image_id: ' + image_id)
            with open('../data/top10querylog.txt', 'a') as f:
                f.write('image_id: ' + image_id + '\n')

            cursors = mongodb_faceinfo.find({'image_id': image_id})
            if cursors.count() == 0:
                print('[Query TOP10] THIS QUERY IMAGE ID HAS YET TO REGISTER')
                with open('../data/top10querylog.txt', 'a') as f:
                    f.write('THIS QUERY IMAGE ID HAS YET TO REGISTER\n')
                face_id = mongodb_dashinfo.find({
                    'represent_image_id': image_id
                })[0]['face_id']
                unique_labels = [
                    cursor['represent_image_id']
                    for cursor in mongodb_dashinfo.find({
                        'face_id': face_id
                    })
                ]
                for label in unique_labels:
                    results[label] = '0'
            else:
                query_emb = cursors[0]['embedding']
                dists, inds = matcher._classifier.search(
                    np.array(query_emb).astype('float32'), k=15)
                dists = np.squeeze(dists)
                inds = np.squeeze(inds)
                top_match_ids = [labels[idx] for idx in inds]
                for i, top_match_id in enumerate(top_match_ids):
                    if i < 11 and top_match_id != image_id:
                        results[top_match_id] = str(dists[i])
            msg_results = {
                'actionType': 'getNearest',
                'sessionId': session_id,
                'data': {
                    'images': results
                }
            }
            with open('../data/top10querylog.txt', 'a') as f:
                f.write('Result: \n{}\n\n'.format(results))
            print('[Query TOP10] Result: \n{}'.format(results))
            rabbit_mq.send_with_exchange(Config.Queues.ACTION_RESULT,
                                         session_id, json.dumps(msg_results))
            # Those cmt for querying tracker from the image ids tracker
            # query_track_id = int(image_id.split('_')[0])
            # query_embs = [cursor['embedding'] for cursor in mongodb_faceinfo.find({'track_id': query_track_id})]

            # for emb in query_embs:
            #     predict_id, _, min_dist = matcher.match(emb, return_min_dist=True)
            #     if not predict_id in predicted_dict:
            #         predicted_dict[predict_id] = []
            #     predicted_dict[predict_id].append(min_dist)
            # avg_predicted_dict = {pid: sum(predicted_dict[pid])/float(len(predicted_dict[pid]))
            #                     for pid in predicted_dict}
            # sorted_predicted_ids = sorted(avg_predicted_dict.items(), key=lambda kv: kv[1])
            # with open('../data/query_top10_log.txt') as f:
            #     f.write('Query IMAGE_ID: ' + image_id + '\n')
            #     f.write('Results: {} \n\n'.format(sorted_predicted_ids))
            # str_results = []
            # for closest_id, dist in sorted_predicted_ids:
            #     str_results.append(Config.Rabbit.INTRA_SEP.join([closest_id, str(dist)]))
            # result_msg = Config.Rabbit.INTER_SEP.join(str_results)
        else:
            time.sleep(1)
Exemple #3
0
class FindSimilarFaceThread(Thread):
    '''
    Find similar faces from dashboard
    '''
    def __init__(self, **args):
        self.nrof_closest_faces = args.get('nrof_closest_faces', 10)
        self.database = args.get('database')
        self.rabbit_mq = args.get('rabbit_mq')
        self.event = threading.Event()
        self.setup_matcher()
        super(FindSimilarFaceThread, self).__init__()

    def join(self, timeout=None):
        print('Find similar joint')
        self.event.set()
        super(FindSimilarFaceThread, self).join()

    def setup_matcher(self):
        with open('../data/top10querylog.txt', 'a') as f:
            f.write('TOP10 QUERY IS BEING IN PROCESS !!!\n\n')
        '''
        self.embs = []
        self.labels = []
        cursors = self.mongo.mongodb_dashinfo.find({})
        unique_labels = [cursor['represent_image_id'] for cursor in cursors]
        cursors = self.mongo.mongodb_faceinfo.find({'image_id': {'$in': unique_labels}})
        for cursor in cursors:
            self.embs.append(np.array(cursor['embedding']))
            self.labels.append(cursor['image_id'])
        self.nof_registered_image_ids = len(self.labels)
        '''
        self.labels, self.embs = self.database.get_labels_and_embs_dashboard()
        self.matcher = FaissMatcher()
        self.matcher.fit(self.embs, self.labels)

        with open('../data/top10querylog.txt', 'a') as f:
            f.write('MATCHER BUILT!!!\n\n')

    def run(self):
        while not self.event.is_set():
            # first update check for new faces in dashboard
            '''
            if self.nof_registered_image_ids < self.mongo.mongodb_dashinfo.find({}).count():
                
                self.nof_registered_image_ids = self.mongo.mongodb_dashinfo.find({}).count()
                print('[Query TOP10] Update new registered image_id ...')
                cursors = self.mongo.mongodb_dashinfo.find({'represent_image_id': {'$nin': self.labels}})
                unique_labels = [cursor['represent_image_id'] for cursor in cursors]
                cursors = self.mongo.mongodb_faceinfo.find({'image_id': {'$in': unique_labels}})
                adding_embs = []
                adding_labels = []
                for cursor in cursors:
                    adding_embs.append(np.array(cursor['embedding']))
                    adding_labels.append(cursor['image_id'])
            '''
            adding_labels, adding_embs = self.database.get_labels_and_embs_dashboard(
                self.labels)
            if len(adding_labels) > 0:
                old_embs = self.embs
                old_labels = self.labels
                self.embs = old_embs + adding_embs
                self.labels = old_labels + adding_labels
                print('Find similar', len(adding_labels))
                self.matcher.update(adding_embs, adding_labels)

            # get new query from from queue, why not just trigger
            action_msg = self.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':
                    data = return_dict['data']

                    results = {}
                    session_id = data['sessionId']
                    image_id = data['imageId']
                    print('[Query TOP10] image_id: ' + image_id)
                    with open('../data/top10querylog.txt', 'a') as f:
                        f.write('image_id: ' + image_id + '\n')

                    cursors = self.database.mongodb_faceinfo.find(
                        {'image_id': image_id})
                    if cursors.count() == 0:
                        print(
                            '[Query TOP10] THIS QUERY IMAGE ID HAS YET TO REGISTER'
                        )
                        with open('../data/top10querylog.txt', 'a') as f:
                            f.write(
                                'THIS QUERY IMAGE ID HAS YET TO REGISTER\n')
                        face_id = self.database.mongodb_dashinfo.find(
                            {'represent_image_id': image_id})[0]['face_id']
                        unique_labels = [
                            cursor['represent_image_id']
                            for cursor in self.database.mongodb_dashinfo.find(
                                {'face_id': face_id})
                        ]
                        for label in unique_labels:
                            results[label] = '0'
                    else:
                        query_emb = cursors[0]['embedding']
                        embs = np.array(query_emb).astype('float32').reshape(
                            (-1, 128))
                        dists, inds = self.matcher._classifier.search(embs,
                                                                      k=15)
                        dists = np.squeeze(dists)
                        inds = np.squeeze(inds)
                        top_match_ids = [self.labels[idx] for idx in inds]
                        for i, top_match_id in enumerate(top_match_ids):
                            if i < 11 and top_match_id != image_id:
                                results[top_match_id] = str(dists[i])
                    msg_results = {
                        'actionType': 'getNearest',
                        'sessionId': session_id,
                        'data': {
                            'images': results
                        }
                    }
                    with open('../data/top10querylog.txt', 'a') as f:
                        f.write('Result: \n{}\n\n'.format(results))
                    print('[Query TOP10] Result: \n{}'.format(results))
                    self.rabbit_mq.send_with_exchange(
                        Config.Queues.ACTION_RESULT, session_id,
                        json.dumps(msg_results))
            else:
                time.sleep(1)