def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file
    global margin
    margin = args.margin
    image_dir = args.image_dir
    global celeb_model
    celeb_model = args.model
    global classifier
    classifier = args.classifier

    if not weight_file:
        weight_file = get_file("weights.28-3.73.hdf5",
                               pretrained_model,
                               cache_subdir="pretrained_models",
                               file_hash=modhash,
                               cache_dir=str(Path(__file__).resolve().parent))

    # for face detection
    global detector
    detector = dlib.get_frontal_face_detector()

    # load model and weights
    global model
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)
    model._make_predict_function()
Exemplo n.º 2
0
class FaceCV(object):
    """
    Singleton class for face recongnition task
    """

    CASE_PATH = ".\\harcascade\\haarcascade_frontalface_alt.xml"
    WRN_WEIGHTS_PATH = ".\\model_weights\\weights.18-4.06.hdf5"

    def __new__(cls, weight_file=None, depth=16, width=8, face_size=64):
        if not hasattr(cls, 'instance'):
            cls.instance = super(FaceCV, cls).__new__(cls)
        return cls.instance

    def __init__(self, depth=16, width=8, face_size=64):
        self.face_size = face_size
        self.model = WideResNet(face_size, depth=depth, k=width)()
        model_dir = os.path.join(os.getcwd(), "model_weights").replace("//", "\\")
        fpath = get_file('weights.18-4.06.hdf5',
                         self.WRN_WEIGHTS_PATH,
                         cache_subdir=model_dir)
        self.model.load_weights(fpath)
        self.model._make_predict_function()

    @classmethod
    def draw_label(cls, image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
                   font_scale=1, thickness=2):
        size = cv2.getTextSize(label, font, font_scale, thickness)[0]
        x, y = point
        cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
        cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness)

    def crop_face(self, imgarray, section, margin=40, size=64):
        """
        :param imgarray: full image
        :param section: face detected area (x, y, w, h)
        :param margin: add some margin to the face detected area to include a full head
        :param size: the result image resolution with be (size x size)
        :return: resized image in numpy array with shape (size x size x 3)
        """
        img_h, img_w, _ = imgarray.shape
        if section is None:
            section = [0, 0, img_w, img_h]
        (x, y, w, h) = section
        margin = int(min(w, h) * margin / 100)
        x_a = x - margin
        y_a = y - margin
        x_b = x + w + margin
        y_b = y + h + margin
        if x_a < 0:
            x_b = min(x_b - x_a, img_w - 1)
            x_a = 0
        if y_a < 0:
            y_b = min(y_b - y_a, img_h - 1)
            y_a = 0
        if x_b > img_w:
            x_a = max(x_a - (x_b - img_w), 0)
            x_b = img_w
        if y_b > img_h:
            y_a = max(y_a - (y_b - img_h), 0)
            y_b = img_h
        cropped = imgarray[y_a: y_b, x_a: x_b]
        resized_img = cv2.resize(cropped, (size, size), interpolation=cv2.INTER_AREA)
        resized_img = np.array(resized_img)
        return resized_img, (x_a, y_a, x_b - x_a, y_b - y_a)


    def detect_face(self,frame):

        age = 'Detecting...'
        gender = 'Detecting...'
        face_cascade = cv2.CascadeClassifier(self.CASE_PATH)

        with session1.as_default():
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(
                gray,
                scaleFactor=1.2,
                minNeighbors=10,
                minSize=(self.face_size, self.face_size)
            )
            if faces is not():

                # placeholder for cropped faces
                face_imgs = np.empty((len(faces), self.face_size, self.face_size, 3))
                for i, face in enumerate(faces):
                    face_img, cropped = self.crop_face(frame, face, margin=40, size=self.face_size)
                    (x, y, w, h) = cropped
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 200, 0), 2)
                    face_imgs[i, :, :, :] = face_img

                if len(face_imgs) > 0:
                    # predict ages and genders of the detected faces
                    results = self.model.predict(face_imgs)
                    predicted_genders = results[0]
                    ages = np.arange(0, 101).reshape(101, 1)
                    predicted_ages = results[1].dot(ages).flatten()

                # draw results
                for i, face in enumerate(faces):
                    label = "{}, {}".format(int(predicted_ages[i]-3),
                                            "F" if predicted_genders[i][0] > 0.5 else "M")

                    self.draw_label(frame, (face[0], face[1]), label)
                    print(int(predicted_ages[i]))

                    if(predicted_genders[i][0] > 0.5):
                        gender = "Female"
                    else:
                        gender = "Male"

                    age = (int(predicted_ages[i])-3)

        return age , gender
Exemplo n.º 3
0
face_size = 64
age_gender_model = WideResNet(face_size, depth=16, k=8)()
age_gender_model_dir = os.path.join(os.getcwd(),
                                    'pretrained_models').replace('//', '\\')
image_width = 1280
image_height = 720

fpath = get_file(
    'weights.18-4.06.hdf5',
    'https://github.com/Tony607/Keras_age_gender/releases/download/V1.0/weights.18-4.06.hdf5',
    cache_dir=age_gender_model_dir)

age_gender_model.load_weights(fpath)

# Address the error: ValueError: Tensor Tensor("dense_1/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.
age_gender_model._make_predict_function()

#   Start code from facenet/src/compare.py
print('Creating networks and loading parameters')
with tf.Graph().as_default():

    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=gpu_memory_fraction)
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                            log_device_placement=False))
    with sess.as_default():
        pnet, rnet, onet = facenet.src.align.detect_face.create_mtcnn(
            sess, None)

    cap = cv2.VideoCapture(0)
    cap.set(3, image_width)