<commit_before>#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created by BigFlower at 16/7/29

from __future__ import print_function, with_statement
import facepp
import os
from .models import Person, Photo
from utility import UPLOAD_DIR
from api_exception import api_error_type, NameExistError, NoFaceDetectedError
from config import API_KEY, API_SECRET


class FaceRecognitionSys:
    """
    Face recognition system using facepp api

    1. When instantiated, the `Group` name of the instance is determined
    2. When `initialize` is called,
        1. `Group` is created at the server
        2. `Person` are created according to images at `cls.db_img_dir`
    3. After 1.2. are carried out, call `identify_new_face` to identify a new face
    """

    def __init__(self, group_name):
        self.api = facepp.API(key=API_KEY, secret=API_SECRET)
        self.group_name = group_name

    def initialize(self, reconstruct=True):
        """
        For each face image in directory `db_img_dir`,
        carry out face recognition.
        return unless training at facepp server finished.

        :param reconstruct:
            if `True`:
                if 'Group' or 'Person' with same name already exists at the server,
                 they will be deleted and reconstruct
            if `False`:
                Nothing will happened, which means:
                1. 'Person' already in the group will not be deleted
                2. faces of 'Person' with same name will not be updated
        """
        print("reading database...")
        faces = self._get_db_faces()
        print(str(faces))
        print("{} faces read".format(sum(len(f) for f in faces.values())))

        print("Creating group..")
        self._create_group(self.group_name, reconstruct)

        print("Creating person...")
        for name, face_ids in faces.items():
            if len(face_ids) > 0:
                self._create_person(name, reconstruct, face_id=face_ids, group_name=self.group_name)
<commit_msg>Add timeout<commit_after>#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created by BigFlower at 16/7/29

from __future__ import print_function, with_statement
import facepp
import os
from .models import Person, Photo
from utility import UPLOAD_DIR
from api_exception import api_error_type, NameExistError, NoFaceDetectedError
from config import API_KEY, API_SECRET


class FaceRecognitionSys:
    """
    Face recognition system using facepp api

    1. When instantiated, the `Group` name of the instance is determined
    2. When `initialize` is called,
        1. `Group` is created at the server
        2. `Person` are created according to images at `cls.db_img_dir`
    3. After 1.2. are carried out, call `identify_new_face` to identify a new face
    """

    def __init__(self, group_name):
        self.api = facepp.API(key=API_KEY, secret=API_SECRET, timeout=3000)
        self.group_name = group_name

    def initialize(self, reconstruct=True):
        """
        For each face image in directory `db_img_dir`,
        carry out face recognition.
        return unless training at facepp server finished.

        :param reconstruct:
            if `True`:
                if 'Group' or 'Person' with same name already exists at the server,
                 they will be deleted and reconstruct
            if `False`:
                Nothing will happened, which means:
                1. 'Person' already in the group will not be deleted
                2. faces of 'Person' with same name will not be updated
        """
        print("reading database...")
        faces = self._get_db_faces()
        print(str(faces))
        print("{} faces read".format(sum(len(f) for f in faces.values())))

        print("Creating group..")
        self._create_group(self.group_name, reconstruct)

        print("Creating person...")
        for name, face_ids in faces.items():
            if len(face_ids) > 0:
                self._create_person(name, reconstruct, face_id=face_ids, group_name=self.group_name)

{'fraction_non_alphanumeric': 0.08680555555555555, 'fraction_numerical': 0.008928571428571428, 'mean_word_length': 2.522270742358079, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7632095', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1103, 'n_words': 400}