def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file

    if not weight_file:
        weight_file = get_file("weights.18-4.06.hdf5", pretrained_model, cache_subdir="pretrained_models",
                               file_hash=modhash, cache_dir=os.path.dirname(os.path.abspath(__file__)))

    # for face detection
    detector = dlib.get_frontal_face_detector()

    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    for img in yield_images():
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)

        # detect faces using dlib detector
        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))

        if len(detected) > 0:
            for i, d in enumerate(detected):
                x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
                xw1 = max(int(x1 - 0.4 * w), 0)
                yw1 = max(int(y1 - 0.4 * h), 0)
                xw2 = min(int(x2 + 0.4 * w), img_w - 1)
                yw2 = min(int(y2 + 0.4 * h), img_h - 1)
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
                faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

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

            # draw results
            for i, d in enumerate(detected):
                label = "{}, {}".format(int(predicted_ages[i]),
                                        "F" if predicted_genders[i][0] > 0.5 else "M")
                draw_label(img, (d.left(), d.top()), label)

        cv2.imshow("result", img)
        key = cv2.waitKey(30)

        if key == 27:
            break
def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file

    if not weight_file:
        weight_file = get_file("weights.18-4.06.hdf5", pretrained_model, cache_subdir="pretrained_models",
                               file_hash=modhash, cache_dir=os.path.dirname(os.path.abspath(__file__)))

    # load model and weights
    img_size = 64
    batch_size = 32
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)
    dataset_root = Path(__file__).parent.joinpath("appa-real", "appa-real-release")
    validation_image_dir = dataset_root.joinpath("valid")
    gt_valid_path = dataset_root.joinpath("gt_avg_valid.csv")
    image_paths = list(validation_image_dir.glob("*_face.jpg"))

    faces = np.empty((batch_size, img_size, img_size, 3))
    ages = []
    image_names = []

    for i, image_path in tqdm(enumerate(image_paths)):
        faces[i % batch_size] = cv2.resize(cv2.imread(str(image_path), 1), (img_size, img_size))
        image_names.append(image_path.name[:-9])

        if (i + 1) % batch_size == 0 or i == len(image_paths) - 1:
            results = model.predict(faces)
            ages_out = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages_out).flatten()
            ages += list(predicted_ages)
            # len(ages) can be larger than len(image_names) due to the last batch, but it's ok.

    name2age = {image_names[i]: ages[i] for i in range(len(image_names))}
    df = pd.read_csv(str(gt_valid_path))
    appa_abs_error = 0.0
    real_abs_error = 0.0

    for i, row in df.iterrows():
        appa_abs_error += abs(name2age[row.file_name] - row.apparent_age_avg)
        real_abs_error += abs(name2age[row.file_name] - row.real_age)

    print("MAE Apparent: {}".format(appa_abs_error / len(image_names)))
    print("MAE Real: {}".format(real_abs_error / len(image_names)))
Example #3
0
class FaceCV(object):
    """
    Singleton class for face recongnition task
    """
    # print(os.getcwd())
    CASE_PATH = os.path.join(os.getcwd(), 'pretrained_models', 'haarcascade_frontalface_alt.xml')
    WRN_WEIGHTS_PATH = "https://github.com/Tony607/Keras_age_gender/releases/download/V1.0/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(), "pretrained_models").replace("//", "\\")
        fpath = get_file('weights.18-4.06.hdf5',
                         self.WRN_WEIGHTS_PATH,
                         cache_subdir=model_dir)
        self.model.load_weights(fpath)

    @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):
        face_cascade = cv2.CascadeClassifier(self.CASE_PATH)

        # 0 means the default video capture device in OS
        video_capture = cv2.VideoCapture(0)
        # infinite loop, break by key ESC
        while True:
            if not video_capture.isOpened():
                sleep(5)
            # Capture frame-by-frame
            ret, frame = video_capture.read()
            # cv2.imshow('video', frame)
            # cv2.waitKey(0)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)            
            faces = face_cascade.detectMultiScale(
                gray,
                scaleFactor=1.2,
                minNeighbors=10,
                minSize=(self.face_size, self.face_size)
            )
            # 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]),
                                        "F" if predicted_genders[i][0] > 0.5 else "M")
                self.draw_label(frame, (face[0], face[1]), label)

            cv2.imshow('Keras Faces', frame)
            if cv2.waitKey(5) == 27:  # ESC key press
                break
        # When everything is done, release the capture
        video_capture.release()
        cv2.destroyAllWindows()
Example #4
0
def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file
    margin = args.margin
    image_dir = args.image_dir

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

    # for face detection
    detector = dlib.get_frontal_face_detector()

    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    image_generator = yield_images_from_dir(image_dir) if image_dir else yield_images()
    ages_pred = ""
    for img in image_generator:
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)

        # detect faces using dlib detector
        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))

        if len(detected) > 0:
            for i, d in enumerate(detected):
                x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
                xw1 = max(int(x1 - margin * w), 0)
                yw1 = max(int(y1 - margin * h), 0)
                xw2 = min(int(x2 + margin * w), img_w - 1)
                yw2 = min(int(y2 + margin * h), img_h - 1)
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
                faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

            # predict ages and genders of the detected faces
            results = model.predict(faces)
            predicted_genders = results[0]
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()
            
            ages_pred += str(predicted_ages) + " "

            ## draw results
            #for i, d in enumerate(detected):
            #    label = "{}, {}".format(int(predicted_ages[i]),
            #                            "F" if predicted_genders[i][0] < 0.5 else "M")
            #    draw_label(img, (d.left(), d.top()), label)

        ##cv2.imshow("result", img)
        key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)

        if key == 27:  # ESC
            break
    preds = open("predictions.txt", "w")
    preds.write(ages_pred)
    preds.close()
    def __init__(self, depth=16, width=8, face_size=64):
        self.face_size = face_size
        self.model = WideResNet(face_size, depth=depth, k=width)()

        _vAR_fpath = self._vAR_WRN_WEIGHTS_PATH
        self.model.load_weights(_vAR_fpath)
Example #6
0
def main():
    args = get_args()
    depth = 16
    k = args.width
    weight_file = args.weight_file
    margin = args.margin
    image_dir = args.image_dir
    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
    detector = dlib.get_frontal_face_detector()

    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    #image_generator = yield_images_from_dir(image_dir) if image_dir else yield_images()

    #for img in image_generator:
    input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_h, img_w, _ = np.shape(input_img)
    cv2.imshow("hey", img)
    #cv2.waitkey()
    # detect faces using dlib detector
    #detected = detector(input_img, 1)
    #h,w = img.shape[0:2]
    #blob = cv2.dnn.blobFromImage((image, 1,  (300*w//h, 300)), (144, 177, 123), False)
    #model.setInput(blob)
    #detected = model.forward
    detected_face = cv2.CascadeClassifier(img)
    print(detected)
    faces = np.empty((len(detected), img_size, img_size, 3))

    if len(detected) > 0:
        for i, d in enumerate(detected):
            x1, y1, x2, y2, w, h = d.left(), d.top(
            ), d.right() + 1, d.bottom() + 1, d.width(), d.height()
            xw1 = max(int(x1 - margin * w), 0)
            yw1 = max(int(y1 - margin * h), 0)
            xw2 = min(int(x2 + margin * w), img_w - 1)
            yw2 = min(int(y2 + margin * h), img_h - 1)
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
            # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
            faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :],
                                           (img_size, img_size))

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

        # draw results
        for i, d in enumerate(detected):
            label = "{}, {}".format(
                int(predicted_ages[i]),
                "M" if predicted_genders[i][0] < 0.5 else "F")
            draw_label(img, (d.left(), d.top()), label)

        cv2.imwrite(os.path.join(IMAGE_DIR, "result.png"), img)
        #out.write(img)
        print("1 frame written")
        key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)
Example #7
0
 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(), "pretrained_models").replace("//", "\\")
     fpath = get_file('weights.hdf5',self.WRN_WEIGHTS_PATH,cache_subdir=model_dir)
     self.model.load_weights(fpath)
Example #8
0
def main():
    args = get_args()
    input_path = args.input
    batch_size = args.batch_size
    nb_epochs = args.nb_epochs
    depth = args.depth
    k = args.width
    validation_split = args.validation_split
    use_augmentation = args.aug

    logging.debug("Loading data...")
    image, gender, age, _, image_size, _ = load_data(input_path)
    X_data = image
    y_data_g = np_utils.to_categorical(gender, 2)
    y_data_a = np_utils.to_categorical(age, 101)

    model = WideResNet(image_size, depth=depth, k=k)()
    sgd = SGD(lr=0.1, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss=["categorical_crossentropy", "categorical_crossentropy"],
                  metrics=['accuracy'])

    logging.debug("Model summary...")
    model.count_params()
    model.summary()

    logging.debug("Saving model...")
    mk_dir("models")
    with open(os.path.join("models", "WRN_{}_{}.json".format(depth, k)), "w") as f:
        f.write(model.to_json())

    mk_dir("checkpoints")
    callbacks = [LearningRateScheduler(schedule=Schedule(nb_epochs)),
                 ModelCheckpoint("checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5",
                                 monitor="val_loss",
                                 verbose=1,
                                 save_best_only=True,
                                 mode="auto")
                 ]

    logging.debug("Running training...")

    data_num = len(X_data)
    indexes = np.arange(data_num)
    np.random.shuffle(indexes)
    X_data = X_data[indexes]
    y_data_g = y_data_g[indexes]
    y_data_a = y_data_a[indexes]
    train_num = int(data_num * (1 - validation_split))
    X_train = X_data[:train_num]
    X_test = X_data[train_num:]
    y_train_g = y_data_g[:train_num]
    y_test_g = y_data_g[train_num:]
    y_train_a = y_data_a[:train_num]
    y_test_a = y_data_a[train_num:]

    if use_augmentation:
        datagen = ImageDataGenerator(
            width_shift_range=0.1,
            height_shift_range=0.1,
            horizontal_flip=True,
            preprocessing_function=get_random_eraser(v_l=0, v_h=255))
        training_generator = MixupGenerator(X_train, [y_train_g, y_train_a], batch_size=batch_size, alpha=0.2,
                                            datagen=datagen)()
        hist = model.fit_generator(generator=training_generator,
                                   steps_per_epoch=train_num // batch_size,
                                   validation_data=(X_test, [y_test_g, y_test_a]),
                                   epochs=nb_epochs, verbose=1,
                                   callbacks=callbacks)
    else:
        hist = model.fit(X_train, [y_train_g, y_train_a], batch_size=batch_size, epochs=nb_epochs, callbacks=callbacks,
                         validation_data=(X_test, [y_test_g, y_test_a]))

    logging.debug("Saving weights...")
    model.save_weights(os.path.join("models", "WRN_{}_{}.h5".format(depth, k)), overwrite=True)
    pd.DataFrame(hist.history).to_hdf(os.path.join("models", "history_{}_{}.h5".format(depth, k)), "history")
Example #9
0
def main():
    args = get_args()
    input_path = args.input
    batch_size = args.batch_size
    nb_epochs = args.nb_epochs
    max_age = args.max_age + 1
    depth = args.depth
    k = args.width
    transfer_learning = args.transfer_learning
    validation_split = args.validation_split
    use_augmentation = args.aug
    initial_weights = '/home/paula/THINKSMARTER_/Model/demographics-model-prediction/pretrained_models/weights.18-4.06.hdf5'
    # weight_file = '/home/paula/THINKSMARTER_/Model/age-gender-estimation-adapted/checkpoints/weights.09-4.32.hdf5'

    _weight_decay = 0.0005
    _use_bias = False
    _weight_init = "he_normal"

    logging.debug("Loading data...")
    image, gender, age, _, image_size, _ = load_data(input_path)
    X_data = image
    y_data_g = np_utils.to_categorical(gender, 2)
    y_data_a = np_utils.to_categorical(age, max_age)

    if transfer_learning:

        model = WideResNet(image_size, depth=depth, k=k, units_age=101)()
        model.load_weights(initial_weights)

        inputs = model.input
        flatten = model.layers[-3].output  # capa flatten
        dense1 = Dense(units=2,
                       kernel_initializer=_weight_init,
                       use_bias=_use_bias,
                       kernel_regularizer=l2(_weight_decay),
                       activation="softmax")(flatten)
        dense2 = Dense(units=117,
                       kernel_initializer=_weight_init,
                       use_bias=_use_bias,
                       kernel_regularizer=l2(_weight_decay),
                       activation="softmax")(flatten)
        model = Model(inputs=inputs, outputs=[dense1, dense2])

        # ---------------------------------
        # IDEA: fine tuning (nomes entreno les dos ultimes capes)
        # for layer in model.layers[:-2]:
        #     layer.trainable = False

    else:
        model = WideResNet(image_size, depth=depth, k=k, units_age=max_age)()

    sgd = SGD(lr=0.1, momentum=0.9, nesterov=True)
    model.compile(
        optimizer=sgd,
        loss=["categorical_crossentropy", "categorical_crossentropy"],
        metrics=['accuracy'])

    logging.debug("Model summary...")
    model.count_params()
    model.summary()

    if args.plot_model:
        plot_model(model,
                   to_file='experiments_pictures/model_plot.png',
                   show_shapes=True,
                   show_layer_names=True)

    logging.debug("Saving model...")
    mk_dir("models")
    with open(os.path.join("models", "WRN_{}_{}.json".format(depth, k)),
              "w") as f:
        f.write(model.to_json())

    mk_dir("checkpoints")
    # tensorBoard = TensorBoard(log_dir='events', histogram_freq=0, batch_size=32, write_graph=True, write_grads=False, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None)

    callbacks = [
        LearningRateScheduler(schedule=Schedule(nb_epochs)),
        ModelCheckpoint("checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5",
                        monitor="val_loss",
                        verbose=1,
                        save_best_only=True,
                        mode="auto")
    ]

    logging.debug("Running training...")

    data_num = len(X_data)
    indexes = np.arange(data_num)
    np.random.shuffle(indexes)
    X_data = X_data[indexes]
    y_data_g = y_data_g[indexes]
    y_data_a = y_data_a[indexes]
    train_num = int(data_num * (1 - validation_split))
    X_train = X_data[:train_num]
    X_test = X_data[train_num:]
    y_train_g = y_data_g[:train_num]
    y_test_g = y_data_g[train_num:]
    y_train_a = y_data_a[:train_num]
    y_test_a = y_data_a[train_num:]

    if use_augmentation:
        datagen = ImageDataGenerator(width_shift_range=0.1,
                                     height_shift_range=0.1,
                                     horizontal_flip=True,
                                     preprocessing_function=get_random_eraser(
                                         v_l=0, v_h=255))
        training_generator = MixupGenerator(X_train, [y_train_g, y_train_a],
                                            batch_size=batch_size,
                                            alpha=0.2,
                                            datagen=datagen)()

        hist = model.fit_generator(generator=training_generator,
                                   steps_per_epoch=train_num // batch_size,
                                   validation_data=(X_test,
                                                    [y_test_g, y_test_a]),
                                   epochs=nb_epochs,
                                   verbose=1,
                                   callbacks=callbacks)
    else:
        hist = model.fit(X_train, [y_train_g, y_train_a],
                         batch_size=batch_size,
                         epochs=nb_epochs,
                         callbacks=callbacks,
                         validation_data=(X_test, [y_test_g, y_test_a]))

    logging.debug("Saving weights...")
    model.save_weights(os.path.join("models", "WRN_{}_{}.h5".format(depth, k)),
                       overwrite=True)
    pd.DataFrame(hist.history).to_hdf(
        os.path.join("models", "history_{}_{}.h5".format(depth, k)), "history")

    with open('history_tmp.txt', 'w') as f:
        for key in hist.history:
            print(key, file=f)
        f.write('\n')
        json.dump(hist.history, f)
Example #10
0
from wide_resnet import WideResNet
from utils import *

import numpy as np
import dlib
import cv2

import asyncio
import os

WEIGHTS_FILE = 'models/weights.hdf5'

# тут мы создаем детектор лиц и описываем модель
detector = dlib.get_frontal_face_detector()

model = WideResNet(64, depth=16, k=8)()
# модель уже предобучена, поэтому мы просто загружаем веса из файла
model.load_weights(WEIGHTS_FILE)


async def main():
    config = Config('config.json')

    bot = TelegramClient(f'sessions/bot_{config.NAME}',
                         config.api_id,
                         config.api_hash,
                         connection=ConnectionTcpMTProxyRandomizedIntermediate,
                         proxy=config.proxy)

    @bot.on(events.NewMessage(incoming=True, pattern='/start'))
    async def start_handler(event: events.NewMessage.Event):
class FaceImage(object):
    """
    Singleton class for face recognition task
    """
    CASE_PATH = "./models/haarcascade_frontalface_alt.xml"
    WRN_WEIGHTS_PATH = "https://github.com/Tony607/Keras_age_gender/releases/download/V1.0/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(FaceImage, cls).__new__(cls)
        return cls.instance

    def __init__(self, depth=16, width=8, face_size=64):
        self.face_size = face_size
        print("Loading WideResNet model...")
        self.model = WideResNet(face_size, depth=depth, k=width)()
        model_dir = os.path.join(os.getcwd(), "models").replace("//", "\\")
        fpath = get_file('weights.18-4.06.hdf5',
                         self.WRN_WEIGHTS_PATH,
                         cache_subdir=model_dir)
        self.model.load_weights(fpath)
        print("Loaded WideResNet model")
        
        # Load emotion models
       # print("Loading emotion model...")
        #self.emotion_model = emotion.load_model_dir("models")
        #print("Loaded emotion model")
        
        
        if RECOGNIZE_FACES:
            print("Loading face recognizer...")
            self.face_recognizer = FaceRecognizer()
            print("Loaded face recognizer")
            
          
    @classmethod
    def draw_label_top(cls, image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
                   font_scale=1, thickness=2, alpha=OVERLAY_ALPHA):
        size = cv2.getTextSize(label, font, font_scale, thickness)[0]
        x, y = point
        overlay = image.copy()
        cv2.rectangle(overlay, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
        cv2.putText(overlay, label, point, font, font_scale, (255, 255, 255), thickness)
        cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)

    @classmethod
    def draw_label_bottom(cls, image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
                   font_scale=0.5, thickness=1, row_index=0, alpha=OVERLAY_ALPHA):
        size = cv2.getTextSize(label, font, font_scale, thickness)[0]
        point = (point[0], point[1] + (row_index * size[1]))
        x, y = point
        overlay = image.copy()
        cv2.rectangle(overlay, (x, y), (x + size[0], y + size[1]), (255, 0, 0), cv2.FILLED)
        point = x, y+size[1]
        cv2.putText(overlay, label, point, font, font_scale, (255, 255, 255), thickness)
        cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
        
    def get_regular_face(self, img, bb):
        return img[bb.top():bb.bottom()+1, bb.left():bb.right()+1, :]

    def get_expanded_face(self, img, bb):
        img_h, img_w, _ = np.shape(img)
        x1, y1, x2, y2, w, h = bb.left(), bb.top(), bb.right() + 1, bb.bottom() + 1, bb.width(), bb.height()
        xw1 = max(int(x1 - 0.4 * w), 0)
        yw1 = max(int(y1 - 0.4 * h), 0)
        xw2 = min(int(x2 + 0.4 * w), img_w - 1)
        yw2 = min(int(y2 + 0.4 * h), img_h - 1)
        return cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (self.face_size, self.face_size))

    def detect_face(self, img):
        # workaround for CV2 bug
        img = copy.deepcopy(img)
        
        # for face detection
        detector = dlib.get_frontal_face_detector()
            
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img_h, img_w, _ = np.shape(input_img)


        # detect faces using dlib detector
        if RECOGNIZE_FACES == True:
            face_bbs, identities = self.face_recognizer.identify_image_faces(img)
        else:
            face_bbs = detector(input_img, 1)
        expanded_face_imgs = np.empty((len(face_bbs), self.face_size, self.face_size, 3))
        emotion2_results = []
  
        # Get face images      
        for i, bb in enumerate(face_bbs):
            x1, y1, x2, y2, w, h = bb.left(), bb.top(), bb.right() + 1, bb.bottom() + 1, bb.width(), bb.height()
            expanded_face_imgs[i, :, :, :] = self.get_expanded_face(img, bb)
            reg_face = self.get_regular_face(img, bb)
            gray_face = gray_image[y1:y2, x1:x2]
            
            try:
                gray_face = cv2.resize(gray_face, (emotion_target_size))
            except:
               continue
            #reg_face = copy.deepcopy(reg_face)
            gray_face = preprocess_input(gray_face, True)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_prediction = emotion_classifier.predict(gray_face)
            emotion_probability = np.max(emotion_prediction)
            emotion_label_arg = np.argmax(emotion_prediction)
            emotion_text = emotion_labels[emotion_label_arg]
            emotion2_results.append(emotion_text)
          #  emotion2_results.append(emotion.emotionof(self.emotion_model, reg_face)[0])

        
        if len(expanded_face_imgs) > 0:
            # predict ages and genders of the detected faces
            results = self.model.predict(expanded_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, bb in enumerate(face_bbs):
            
            if RECOGNIZE_FACES == True:
                # Display name

                label12 = "{}".format(identities[i])
                label1="{}, {}".format(int(predicted_ages[i]),
                                                 "F" if predicted_genders[i][0] > 0.5 else "M")

                self.draw_label_bottom(img, (bb.left(), bb.bottom()), label1,row_index=2)
                self.draw_label_bottom(img, (bb.left(), bb.bottom() + 1), label12, row_index=0)
                ## Display emotion
                if identities[i] == "Unknown" or "customer" in identities[i]:
                    label2 = "{}".format(emotion2_results[i])
                else:
                    try:
                        label2 = "{}".format(emotion2_results[i])
                    except:
                        label2 = "{}".format('normal')
                self.draw_label_bottom(img, (bb.left(), bb.bottom()+1), label2, row_index=1)
            else:
                ## Display age, gender and emotion
                label2 = "{}".format(emotion2_results[i])
                self.draw_label_bottom(img, (bb.left(), bb.bottom()), label2, row_index=0)

        # draw face rectangles
        for i, bb in enumerate(face_bbs):
            x1, y1, x2, y2, w, h = bb.left(), bb.top(), bb.right() + 1, bb.bottom() + 1, bb.width(), bb.height()             
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)

        return img
    def detect_face_info(self, file_path):
        
        img = cv2.imread(file_path)
        print(img)
        # workaround for CV2 bug
        img = copy.deepcopy(img)
        
        # for face detection
        detector = dlib.get_frontal_face_detector()
            
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img_h, img_w, _ = np.shape(input_img)


        # detect faces using dlib detector
        if RECOGNIZE_FACES == True:
            face_bbs, identities = self.face_recognizer.identify_image_faces(img)
        else:
            face_bbs = detector(input_img, 1)
        expanded_face_imgs = np.empty((len(face_bbs), self.face_size, self.face_size, 3))
        emotion2_results = []
  
        # Get face images      
        for i, bb in enumerate(face_bbs):
            x1, y1, x2, y2, w, h = bb.left(), bb.top(), bb.right() + 1, bb.bottom() + 1, bb.width(), bb.height()
            expanded_face_imgs[i, :, :, :] = self.get_expanded_face(img, bb)
            reg_face = self.get_regular_face(img, bb)
            gray_face = gray_image[y1:y2, x1:x2]
            
            try:
                gray_face = cv2.resize(gray_face, (emotion_target_size))
            except:
               continue
            #reg_face = copy.deepcopy(reg_face)
            gray_face = preprocess_input(gray_face, True)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_prediction = emotion_classifier.predict(gray_face)
            emotion_probability = np.max(emotion_prediction)
            emotion_label_arg = np.argmax(emotion_prediction)
            emotion_text = emotion_labels[emotion_label_arg]
            emotion2_results.append(emotion_text)
            
          #  emotion2_results.append(emotion.emotionof(self.emotion_model, reg_face)[0])

        
        if len(expanded_face_imgs) > 0:
            # predict ages and genders of the detected faces
            results = self.model.predict(expanded_face_imgs)
            predicted_genders = results[0]
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()
            
        all_faces_info=[]    
        # draw results
        for i, bb in enumerate(face_bbs):
                 face_info = {'Name':identities[i], 'Age':int(predicted_ages[i]),
                              'Gender':"F" if predicted_genders[i][0] > 0.5 else "M",
                              'Imotion':emotion2_results[i]}
                 all_faces_info.append(face_info)
           
  
        return all_faces_info    
                "--confidence",
                type=float,
                default=0.4,
                help="minimum probability to filter weak detections")
ap.add_argument("-s",
                "--skip-frames",
                type=int,
                default=30,
                help="# of skip frames between detections")
args = vars(ap.parse_args())

# Code for age and gender detection
# predict function 0 index position is probability of gender with female if prob > 0.5
# Age is in 1st index and a softmax output, so dot product is to be done with 0 to 100.
face_size = 64
model = WideResNet(face_size, depth=16, k=8)()
fpath = 'pretrained_models\\weights.18-4.06.hdf5'
model.load_weights(fpath)
# Model loaded into the memory


def crop_face(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:
Example #13
0
def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file

    ## cargo modelo pre-entrenado
    if not weight_file:
        weight_file = "pretrained_models/weights.18-4.06.hdf5"
    ##  weight_file ="pretrained_models/weights.29-3.80.hdf5"


# para detectar la cara
    detector = dlib.get_frontal_face_detector()

    # cargo el modelo desde WideResNet y los pesos
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    #  capturo la imagen
    for img in yield_images():
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)

        # detecta caras usando DLIB (FrontalFaceDetector)
        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))

        # si encontro caras para cada una genera un marco rectangular
        if len(detected) > 0:
            for i, d in enumerate(detected):
                x1, y1, x2, y2, w, h = d.left(), d.top(
                ), d.right() + 1, d.bottom() + 1, d.width(), d.height()
                xw1 = max(int(x1 - 0.4 * w), 0)
                yw1 = max(int(y1 - 0.4 * h), 0)
                xw2 = min(int(x2 + 0.4 * w), img_w - 1)
                yw2 = min(int(y2 + 0.4 * h), img_h - 1)
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
                faces[i, :, :, :] = cv2.resize(
                    img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

            # predice sexo y edad de las imagenes detectadas
            results = model.predict(faces)
            predicted_genders = results[0]
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()

            # pinta las etiquetas
            for i, d in enumerate(detected):
                label = "{}{}".format(
                    "M" if predicted_genders[i][0] > 0.5 else "H",
                    int(predicted_ages[i]))
                draw_label(img, (d.left(), d.top()), label)

        #muestra los resultados
        cv2.imshow("result", img)
        key = cv2.waitKey(30)

        # si apretas escape nos vamos de viaje (salis del programa)
        if key == 27:
            break
Example #14
0
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8,
                            allow_growth=False)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

# Missing this was the source of one of the most challenging an insidious bugs that I've ever encountered.
# Without explicitly linking the session the weights for the dense layer added below don't get loaded
# and so the model returns random results which vary with each model you upload because of random seeds.
K.set_session(sess)

# Use this only for export of the model.
# This must come before the instantiation of ResNet50
K._LEARNING_PHASE = tf.constant(0)
K.set_learning_phase(0)

model = WideResNet()()
# Training Not included; We're going to load pretrained weights
model.load_weights(weight_path)

# Import the libraries needed for saving models
# Note that in some other tutorials these are framed as coming from tensorflow_serving_api which is no longer correct
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import tag_constants, signature_constants

# I want the full prediction tensor out, not classification. This format: {"image": model.input} took me a while to track down
prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def(
    {"image": model.input}, {
        "prediction_g": model.output[0],
        "prediction_a": model.output[1]
    })
Example #15
0
def age_main():
    print("-------------start---------------")
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file
    margin = args.margin
    image_dir = args.image_dir

    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
    detector = dlib.get_frontal_face_detector()

    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    image_generator = yield_images_from_dir(
        image_dir) if image_dir else yield_images()
    print("-------------start for loop ---------------")
    for img in image_generator:

        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)

        # detect faces using dlib detector
        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))

        print("-------------start if---------------")
        if len(detected) > 0:
            for i, d in enumerate(detected):
                x1, y1, x2, y2, w, h = d.left(), d.top(
                ), d.right() + 1, d.bottom() + 1, d.width(), d.height()
                xw1 = max(int(x1 - margin * w), 0)
                yw1 = max(int(y1 - margin * h), 0)
                xw2 = min(int(x2 + margin * w), img_w - 1)
                yw2 = min(int(y2 + margin * h), img_h - 1)
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
                faces[i, :, :, :] = cv2.resize(
                    img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

            # predict ages and genders of the detected faces
            results = model.predict(faces)
            predicted_genders = results[0]
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()
            print(int(predicted_ages))

            print("-------------end if---------------")

        key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)

        #if key == 27:  # ESC
        #   break
    print("-------------end for loop---------------")
def predict_gen_and_age(
    current_folder_post_fix='0/',
    crop_with_mask_root_dir='/home/amanda/Documents/cropped_new_crunchbase/',
    save_result_root_dir='/home/amanda/Documents/predicted_results/crunchbase_new_data/'
):
    # face detection
    start_t = time.time()
    crop_with_mask_dir = crop_with_mask_root_dir + current_folder_post_fix

    save_result_dir = save_result_root_dir + current_folder_post_fix
    if not os.path.exists(save_result_dir):
        os.makedirs(save_result_dir)

    file_lst = [
        f for f in os.listdir(crop_with_mask_dir)
        if f[-4:] == '.jpg' or f[-4:] == '.png'
    ]

    file_num = len(file_lst)

    # load model and weights.
    img_size = 64
    faces = np.empty((file_num, img_size, img_size, 3))
    depth = 16
    k = 8
    weight_file = '/home/amanda/Documents/age_gender_model/weights.18-4.06.hdf5'

    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    img_height_lst = []
    img_width_lst = []
    for i, cur_im_name in enumerate(file_lst):
        if (i + 1) % 100 == 0:
            print('{} out of {}'.format(i + 1, len(file_lst)))
        cur_im_path = os.path.join(crop_with_mask_dir, cur_im_name)
        img = cv2.imread(cur_im_path)
        img_h, img_w, _ = np.shape(img)
        img_height_lst.append(img_h)
        img_width_lst.append(img_w)
        faces[i, :, :, :] = cv2.resize(img[:, :, :], (img_size, img_size))

    print('Start to make inferences...')

    results = model.predict(faces)
    print('Done!')

    predicted_genders = results[0]
    predicted_gender_prob = predicted_genders[:,
                                              1]  # the probability of being a male, (0, 1).
    predicted_gender_binary = [round(i) for i in predicted_gender_prob]

    ages = np.arange(0, 101).reshape(101, 1)
    predicted_ages = results[1].dot(ages).flatten()

    result_df = pd.DataFrame(columns=[
        'filename', 'age', 'gender binary', 'gender probability', 'img height',
        'img width'
    ])
    result_df['gender probability'] = predicted_gender_prob
    result_df['gender binary'] = predicted_gender_binary
    result_df['age'] = predicted_ages
    result_df['filename'] = file_lst
    result_df['img height'] = img_height_lst
    result_df['img width'] = img_width_lst

    file_name = 'gender_age_only_ind_' + current_folder_post_fix[:-1] + '.pkl'
    file_full_path = save_result_dir + file_name
    result_df.to_pickle(file_full_path)

    print('Done. Total time = {}'.format(time.time() - start_t))
class FaceCV(object):
    """

    Singleton class for face recongnition task

    """

    CASE_PATH = "haarcascade_frontalface_default.xml"

    WRN_WEIGHTS_PATH = "https://github.com/Tony607/Keras_age_gender/releases/download/V1.0/weights.18-4.06.hdf5"

    #########>>>>>
    model_ethinicity = load_model('keras_FACERACE_trained_model.h5')
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    fa = FaceAligner(predictor,
                     desiredFaceWidth=100,
                     desiredLeftEye=(0.32, 0.32))
    array = ["White", "Black", "Asian", "Indian", "Other"]
    ##########>>>>>>>>>>>>
    ####>>>>>>
    face_detection = cv2.CascadeClassifier(
        'haarcascade_frontalface_default.xml')
    emotion_classifier = load_model('models/_mini_XCEPTION.106-0.65.hdf5',
                                    compile=False)
    EMOTIONS = [
        "angry", "disgust", "scared", "happy", "sad", "surprised", "neutral"
    ]

    ###########>>
    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(),
                                 "pretrained_models").replace("//", "\\")

        fpath = get_file('weights.18-4.06.hdf5',
                         self.WRN_WEIGHTS_PATH,
                         cache_subdir=model_dir)

        self.model.load_weights(fpath)

    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):

        face_cascade = cv2.CascadeClassifier(self.CASE_PATH)

        # 0 means the default video capture device in OS

        video_capture = cv2.VideoCapture(0)

        # infinite loop, break by key ESC

        while True:

            if not video_capture.isOpened():

                sleep(5)

            # Capture frame-by-frame

            ret, frame = video_capture.read()

            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            ####>>
            rects = self.detector(gray, 2)
            ####~>>>>>>>>>>
            faces = face_cascade.detectMultiScale(gray,
                                                  scaleFactor=1.2,
                                                  minNeighbors=10,
                                                  minSize=(self.face_size,
                                                           self.face_size))

            # 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
                ########>>>>>>>>
                for rect in rects:
                    (x, y, w, h) = rect_to_bb(rect)
                    faceAligned = self.fa.align(frame, gray, rect)
                    cv2.imwrite("Img" + str(i) + ".png", faceAligned)
                    faceAligned = cv2.cvtColor(faceAligned, cv2.COLOR_BGR2RGB)
                    arr = np.reshape(faceAligned, (1, 100, 100, 3))
                    arr = arr.astype(np.float)
                #############>>>>>>>
                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()
                ##########>>>>>>>>>>>>
                pd = self.model_ethinicity.predict(arr)
                race = self.array[np.asscalar(pd.argmax(axis=1))]
                #########>>>>
                ##########>>>>>
                frontal_faces = sorted(faces,
                                       reverse=True,
                                       key=lambda x: (x[2] - x[0]) *
                                       (x[3] - x[1]))[0]
                (fX, fY, fW, fH) = frontal_faces
                roi = gray[fY:fY + fH, fX:fX + fW]
                roi = cv2.resize(roi, (48, 48))
                roi = roi.astype("float") / 255.0
                roi = img_to_array(roi)
                roi = np.expand_dims(roi, axis=0)
                np.reshape(roi, (48, 48, 1))
                preds = self.emotion_classifier.predict(roi)[0]
                emotion_probability = np.max(preds)
                emotion = self.EMOTIONS[preds.argmax()]

    ########>>>>>>>>
    # draw results

            for i, face in enumerate(faces):

                label = "{}, {} , {},{}".format(
                    emotion, race, int(predicted_ages[i]),
                    "F" if predicted_genders[i][0] > 0.5 else "M")

                self.draw_label(frame, (face[0], face[1]), label)

            cv2.imshow('Keras Faces', frame)

            if cv2.waitKey(5) == 27:  # ESC key press

                break

        # When everything is done, release the capture

        video_capture.release()

        cv2.destroyAllWindows()
Example #18
0
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

if args.dataset == 'cifar10':
        trainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
        testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
        nclasses = 10
elif args.dataset == 'cifar100':
        trainset = datasets.CIFAR100(root='./data', train=True, download=True, transform=transform_train)
        testset = datasets.CIFAR100(root='./data', train=False, download=True, transform=transform_test)
        nclasses = 100

train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True, num_workers=8)
test_loader = torch.utils.data.DataLoader(testset, batch_size=args.batch_size, shuffle=False, num_workers=8)

model = WideResNet(16, 8, .0, nclasses)

# enable sparsification for Conv and Linear layers above pthres. Initial pruning threshold is set lower than the requested. 
iter_sparsify(model, erfinv(.6 * args.starget) * math.sqrt(2), True, args.pthres)

# not adaptive, per-layer fixed, sparsity:
#iter_sparsify(model, erfinv(1-args.starget) * math.sqrt(2), False)

if args.cuda:
    model.cuda(gpu_id)

parameters, sparameters = [], []
for name, p in model.named_parameters():
    if ".r" in name:
        sparameters += [p]
    else:
Example #19
0
# -*- coding: utf-8 -*-

from wide_resnet import WideResNet
import numpy as np
import cv2
import dlib
import pickle

depth = 16
width = 8
img_size = 64
model = WideResNet(img_size, depth=depth, k=width)()
model.load_weights('weights.hdf5')

detector = dlib.get_frontal_face_detector()
cap = cv2.VideoCapture('../data/video.mp4')

pos = []
frame_id = -1

while cap.isOpened():
    ret, image_np = cap.read()
    frame_id += 1
    if len((np.array(image_np)).shape) == 0:
        break

    image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
    img_h = image_np.shape[0]
    img_w = image_np.shape[1]
    detected = detector(image_np, 1)
Example #20
0
def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file

    if not weight_file:
        weight_file = os.path.join("pretrained_models", "weights.18-4.06.hdf5")

    # for face detection
    detector = dlib.get_frontal_face_detector()

    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    # capture video
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

    while True:
        # get video frame
        ret, img = cap.read()

        if not ret:
            print("error: failed to capture image")
            return -1

        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)

        # detect faces using dlib detector
        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))

        for i, d in enumerate(detected):
            x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
            xw1 = max(int(x1 - 0.4 * w), 0)
            yw1 = max(int(y1 - 0.4 * h), 0)
            xw2 = min(int(x2 + 0.4 * w), img_w - 1)
            yw2 = min(int(y2 + 0.4 * h), img_h - 1)
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
            # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
            faces[i,:,:,:] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

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

        # draw results
        for i, d in enumerate(detected):
            label = "{}, {}".format(int(predicted_ages[i]),
                                    "F" if predicted_genders[i][0] > 0.5 else "M")
            draw_label(img, (d.left(), d.top()), label)

        cv2.imshow("result", img)
        key = cv2.waitKey(30)

        if key == 27:
            break
class FaceCV(object):

    _vAR_config = ConfigParser()
    _vAR_config.read('test.ini')
    _vAR_CASE_PATH = _vAR_config.get('section_a', '_vAR_CASE_PATH')
    _vAR_WRN_WEIGHTS_PATH = _vAR_config.get('section_a',
                                            '_vAR_WRN_WEIGHTS_PATH')

    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)()

        _vAR_fpath = self._vAR_WRN_WEIGHTS_PATH
        self.model.load_weights(_vAR_fpath)

    def draw_label(cls,
                   image,
                   point,
                   label,
                   font=cv2.FONT_HERSHEY_COMPLEX,
                   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), (0, 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):

        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):
        face_cascade = cv2.CascadeClassifier(self._vAR_CASE_PATH)

        #url = 'https://www.youtube.com/watch?v=ktKTPiOld1g'

        form = sg.FlexForm('Demo Chooser')

        layout = [[
            sg.Text('Enter youtube link or 0 for webcam'),
            sg.InputText()
        ], [sg.OK()]]

        button, value = form.Layout(layout).Read()
        print(value[0])
        if value[0] == '0':
            video_capture = cv2.VideoCapture(0)
        else:
            url = value[0]
            vPafy = pafy.new(value[0])
            play = vPafy.getbest(preftype="mp4")
            video_capture = cv2.VideoCapture(play.url)

        # infinite loop, break by key ESC
        while True:
            if not video_capture.isOpened():
                sleep(5)
            # Capture each frame
            ret, frame = video_capture.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray,
                                                  scaleFactor=1.2,
                                                  minNeighbors=10,
                                                  minSize=(self.face_size,
                                                           self.face_size))

            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, 255, 255),
                              2)
                face_imgs[i, :, :, :] = face_img
            if len(face_imgs) > 0:
                # age and gender model integration
                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()

            for i, face in enumerate(faces):
                label = "{}, {}".format(
                    int(predicted_ages[i]),
                    "F" if predicted_genders[i][0] > 0.5 else "M")
                self.draw_label(frame, (face[0], face[1]), label)

            with open('csv_output.csv', 'a', newline='') as file1:

                writer = csv.writer(file1)

                for i, face in enumerate(faces):

                    writer.writerow([
                        int(predicted_ages[i]),
                        "F" if predicted_genders[i][0] > 0.5 else "M"
                    ])

            cv2.imshow('Keras Faces', frame)
            if cv2.waitKey(5) == 27:  # ESC key press
                break

        video_capture.release()
        cv2.destroyAllWindows()
Example #22
0
class FaceCV(object):
    CASE_PATH = "./pretrained_models/haarcascade_frontalface_alt.xml"
    WRN_WEIGHTS_PATH = "https://github.com/iamshreeram/human-gender-detection/releases/download/1.0/weights.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(), "pretrained_models").replace("//", "\\")
        fpath = get_file('weights.hdf5',self.WRN_WEIGHTS_PATH,cache_subdir=model_dir)
        self.model.load_weights(fpath)

    @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), (0, 255, 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):
        face_cascade = cv2.CascadeClassifier(self.CASE_PATH)

        # 0 means the default video capture device in OS
        video_capture = cv2.VideoCapture(-1)
        # infinite loop, break by key ESC
        while True:
            if not video_capture.isOpened():
                sleep(5)
            # Capture frame-by-frame
            ret, frame = video_capture.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            print(face_cascade)
            faces = face_cascade.detectMultiScale(
                gray,
                scaleFactor=1.2,
                minNeighbors=10,
                minSize=(self.face_size, self.face_size)
            )
            # 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]),"F" if predicted_genders[i][0] > 0.5 else "M")
                # self.draw_label(frame, (face[0], face[1]), label)
                label = "{}".format("Female" if predicted_genders[i][0] > 0.5 else "Male")
                self.draw_label(frame, (face[0], face[1]), label)

            cv2.imshow('Gender Detector Engine', frame)
            if cv2.waitKey(5) == 27:  # ESC key press
                break
        # When everything is done, release the capture
        video_capture.release()
        cv2.destroyAllWindows()
Example #23
0
def main():
    nb_class = 2

    args = get_args()
    input_path = args.input
    batch_size = args.batch_size
    nb_epochs = args.nb_epochs
    lr = args.lr
    opt_name = args.opt
    depth = args.depth
    k = args.width
    validation_split = args.validation_split
    output_path = Path(__file__).resolve().parent.joinpath(args.output_path)
    output_path.mkdir(parents=True, exist_ok=True)

    logging.debug("Loading data...")
    image, gender, age, _, image_size, _ = load_data(input_path)
    X_data = image
    y_data_g = np_utils.to_categorical(gender, 2)
    y_data_a = np_utils.to_categorical(age, 101)

    #vggface = VGGFace(model='resnet50')

    #
    vgg_model = VGGFace(model='resnet50',
                        include_top=False,
                        input_shape=(224, 224, 3))
    last_layer = vgg_model.get_layer('avg_pool').output
    x = Flatten(name='flatten')(last_layer)
    out = Dense(nb_class, activation='softmax', name='classifier')(x)
    custom_vgg_model = Model(vgg_model.input, out)
    #

    model = WideResNet(image_size, depth=depth, k=k)()
    opt = get_optimizer(opt_name, lr)
    model.compile(
        optimizer=opt,
        loss=["categorical_crossentropy", "categorical_crossentropy"],
        metrics=['accuracy'])

    logging.debug("Model summary...")
    model.count_params()
    model.summary()

    callbacks = [
        LearningRateScheduler(schedule=Schedule(nb_epochs, lr)),
        ModelCheckpoint(str(output_path) +
                        "/weights.{epoch:02d}-{val_loss:.2f}.hdf5",
                        monitor="val_loss",
                        verbose=1,
                        save_best_only=False,
                        mode="auto")
    ]

    logging.debug("Running training...")

    data_num = len(X_data)
    indexes = np.arange(data_num)
    np.random.shuffle(indexes)
    X_data = X_data[indexes]
    y_data_g = y_data_g[indexes]
    y_data_a = y_data_a[indexes]
    train_num = int(data_num * (1 - validation_split))
    X_train = X_data[:train_num]
    X_test = X_data[train_num:]
    y_train_g = y_data_g[:train_num]
    y_test_g = y_data_g[train_num:]
    y_train_a = y_data_a[:train_num]
    y_test_a = y_data_a[train_num:]

    hist = model.fit(X_train, [y_train_g, y_train_a],
                     batch_size=batch_size,
                     epochs=nb_epochs,
                     callbacks=callbacks,
                     validation_data=(X_test, [y_test_g, y_test_a]))

    logging.debug("Saving history...")
    pd.DataFrame(hist.history).to_hdf(
        output_path.joinpath("history_{}_{}.h5".format(depth, k)), "history")