예제 #1
0
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)))
예제 #3
0
def main():
    # args = get_args()
    depth = 16
    k = 8
    margin = 0.4
    image_dir = "images/estimation_test/"

    weight_file = get_file("weights.28-3.73.hdf5",
                           pretrained_model,
                           cache_subdir="estimation_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)

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

        # if face is detected
        if len(detected) == 1:
            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)
                y1_save = max(y1, 0)
                y2_save = min(y2, img_h - 1)
                x1_save = max(x1, 0)
                x2_save = min(x2, img_w - 1)
                croppedImage = img[y1_save:y2_save, x1_save:x2_save]
                cv2.imwrite("images/test/test_1_cropped.png", croppedImage)
                file = open('imageScale.txt', 'w')
                file.write(
                    str(x1_save) + '\n' + str(x2_save) + '\n' + str(y1_save) +
                    '\n' + str(y2_save))
                file.close()
                img_original = img.copy()
                cv2.imwrite('images/estimation_test/test_1.png', img_original)
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                faces[i, :, :, :] = cv2.resize(
                    img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

            # predict ages of the detected faces
            results = model.predict(faces)
            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]))
                draw_label(img, (d.left(), d.top()), label)

            return img, predicted_ages[0], len(detected)
        elif len(detected) > 1:
            return img, -1, len(detected)
        else:
            return img, -1, 0
예제 #4
0
class AgeGender(object):
    def __init__(self, depth=16, width=8, face_size=64):

        self.model = WideResNet(face_size, depth=depth, k=width)()
        model_path = os.path.join(os.getcwd(),
                                  "pretrained_models").replace("//", "\\")
        weight_file_path = get_file('weights.18-4.06.hdf5',
                                    WEIGHTS_PATH,
                                    cache_subdir=model_path)
        self.model.load_weights(weight_file_path)

    def detect_face(self, video, outpath_path):

        video_capture = cv2.VideoCapture(os.path.join("video_files", video))
        frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
        print("\nFRAME COUNT: ", str(frame_count) + "\n")

        writer = None
        print("EXTRACTING FRAMES AND PERFORMING PREDICTION.\n")
        while True:
            grab, frame = video_capture.read()

            if grab is True:
                detected_faces = preprocess(frame)
            else:
                break

            if detected_faces is not ():

                face_images = np.empty((len(detected_faces), 64, 64, 3))
                for i, face in enumerate(detected_faces):
                    face_image, cropped_img = cropping(frame,
                                                       face,
                                                       margin=50,
                                                       size=64)
                    (x, y, w, h) = cropped_img
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 200, 0),
                                  2)
                    face_images[i, :, :, :] = face_image

                    results = self.model.predict(face_images)
                    genders = results[0]
                    ages = np.arange(0, 101).reshape(101, 1)
                    ages = results[1].dot(ages).flatten()

                    label = "{}, {}".format(
                        int(ages[i]), "F" if genders[i][0] > 0.5 else "M")
                    labeling(frame, (face[0], face[1]), label)

            if writer is None:
                fourcc = cv2.VideoWriter_fourcc(*"mp4v")
                writer = cv2.VideoWriter(outpath_path, fourcc, 30,
                                         ((frame.shape[1], frame.shape[0])),
                                         True)

            writer.write(frame)
            cv2.waitKey(1)

        video_capture.release()
        writer.release()
        cv2.destroyAllWindows()
def test(mtcnn, id_data, args, sess, embeddings, images_placeholder,
         phase_train_placeholder, count):
    WRN_WEIGHTS_PATH = ".\\pretrained_models\\weights.18-4.06.hdf5"
    face_size = 64
    model = WideResNet(64, depth=16, k=8)()
    model_dir = os.path.join(os.getcwd(),
                             "pretrained_models").replace("//", "\\")
    fpath = get_file('weights.18-4.06.hdf5',
                     WRN_WEIGHTS_PATH,
                     cache_subdir=model_dir)
    model.load_weights(fpath)
    cap = cv2.VideoCapture(0)
    frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    #count=0
    show_landmarks = False
    show_bb = False
    show_id = True
    show_fps = False
    #show_train = False
    #real=0
    while True:
        start = time.time()
        _, frame = cap.read()

        #Locate faces and landmarks in frame
        face_patches, padded_bounding_boxes, landmarks, bounding_boxes = detect_and_align.detect_faces(
            frame, mtcnn)
        face_imgs = np.empty((len(face_patches), face_size, face_size, 3))
        for i, bb in enumerate(padded_bounding_boxes):
            face_img, cropped = crop_face(frame, (bb[0], bb[1], 180, 180),
                                          margin=40,
                                          size=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:
            results = 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(padded_bounding_boxes):
                label = "{}, {}".format(
                    int(predicted_ages[i]),
                    "F" if predicted_genders[i][0] > 0.5 else "M")

                font = cv2.FONT_HERSHEY_SIMPLEX
                font_scale = 1
                thickness = 2

                size = cv2.getTextSize(label, font, font_scale, thickness)[0]
                x, y = (face[0], face[1])
                cv2.rectangle(frame, (x, y - size[1]), (x + size[0], y),
                              (255, 0, 0), cv2.FILLED)
                cv2.putText(frame, label, (face[0], face[1]), font, font_scale,
                            (255, 255, 255), thickness)
                #IdData.draw_label(frame, (face[0], face[1]), label)
            face_patches = np.stack(face_patches)
            feed_dict = {
                images_placeholder: face_patches,
                phase_train_placeholder: False
            }
            embs = sess.run(embeddings, feed_dict=feed_dict)

            print("Matches in frame:")
            matching_ids, matching_distances = id_data.find_matching_ids(embs)

            for bb, landmark, matching_id, dist in zip(padded_bounding_boxes,
                                                       landmarks, matching_ids,
                                                       matching_distances):
                if matching_id is None:
                    matching_id = "Unknown"
                    print("Unknown! Couldn't fint match.")

                else:
                    print("Hi %s! Distance: %1.4f" % (matching_id, dist))

                if show_id:
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    #count +=1
                    cv2.putText(frame, matching_id, (bb[0], bb[3]), font, 1,
                                (0, 225, 0), 1, cv2.LINE_AA)
                    #cv2.putText(frame, real , (100,100), font, 1, (255, 255, 255), 1, cv2.LINE_AA)

                    cv2.rectangle(frame, (bb[0], bb[1]), (bb[2], bb[3]),
                                  (255, 0, 0), 2)
                if show_landmarks:
                    for j in range(5):
                        size = 1
                        top_left = (int(landmark[j]) - size,
                                    int(landmark[j + 5]) - size)
                        bottom_right = (int(landmark[j]) + size,
                                        int(landmark[j + 5]) + size)
                        cv2.rectangle(frame, top_left, bottom_right,
                                      (255, 0, 255), 2)
                #if show_train:

        else:
            print("Couldn't find a face")

        end = time.time()

        seconds = end - start
        fps = round(1 / seconds, 2)

        if show_fps:
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(frame, str(fps), (0, int(frame_height) - 5), font, 1,
                        (255, 255, 255), 1, cv2.LINE_AA)

        cv2.imshow("frame", frame)

        key = cv2.waitKey(1)
        if key == ord("q"):
            break
        elif key == ord("l"):
            show_landmarks = not show_landmarks
        elif key == ord("b"):
            show_bb = not show_bb
        elif key == ord("i"):
            show_id = not show_id
        elif key == ord("f"):
            show_fps = not show_fps
        elif key == ord("s"):
            count = count + 1
            if count > 10:
                continue
            gin = dataSetGenerator.gv()
            key = input('Loaded images,press any key to continue')
            if key == ord('y'):

                test(mtcnn, id_data, args, sess, embeddings,
                     images_placeholder, phase_train_placeholder, count)
            else:
                test(mtcnn, id_data, args, sess, embeddings,
                     images_placeholder, phase_train_placeholder, count)
        elif key == ord('t'):
            cap.release()
            cv2.destroyAllWindows()
            main(args)

    cap.release()
    cv2.destroyAllWindows()
예제 #6
0
def detector(video_input, video_output):

    # parameters for loading data and images
    detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml'
    emotion_model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
    emotion_labels = get_labels('fer2013')
    gender_model_path = '../trained_models/gender_models/simple_CNN.81-0.96.hdf5'
    gender_labels = get_labels('imdb')
    font = cv2.FONT_HERSHEY_SIMPLEX

    gender_to_cnt = {}
    emotion_to_cnt = {}
    age_to_cnt = {}

    # hyper-parameters for bounding boxes shape
    frame_window = 10
    emotion_offsets = (20, 40)
    gender_offsets = (10, 10)

    # loading models
    face_detection = load_detection_model(detection_model_path)
    emotion_classifier = load_model(emotion_model_path, compile=False)
    # getting input model shapes for inference
    emotion_target_size = emotion_classifier.input_shape[1:3]

    gender_classifier = load_model(gender_model_path, compile=False)
    gender_target_size = gender_classifier.input_shape[1:3]

    depth = 16
    k = 8
    weight_file = "weights.18-4.06.hdf5"

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

    cnn_face_detector = dlib.cnn_face_detection_model_v1(
        'mmod_human_face_detector.dat')

    def pipeline(bgr_image):

        bgr_image = cv2.resize(bgr_image, (640, 360))
        faces = cnn_face_detector(bgr_image, 1)
        global total_faces

        total_faces = total_faces + len(faces)
        gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
        rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)

        for face in faces:
            x1, y1, x2, y2, w, h = face.rect.left(), face.rect.top(
            ), face.rect.right() + 1, face.rect.bottom() + 1, face.rect.width(
            ), face.rect.height()
            xw1 = max(int(x1 - 0.4 * w), 0)
            yw1 = max(int(y1 - 0.4 * h), 0)
            xw2 = min(int(x2 + 0.4 * w), bgr_image.shape[1] - 1)
            yw2 = min(int(y2 + 0.4 * h), bgr_image.shape[0] - 1)
            gray_face = gray_image[yw1:yw2 + 1, xw1:xw2 + 1]

            try:
                gray_face = cv2.resize(gray_face, (emotion_target_size))
            except:
                continue

            gray_face = preprocess_input(gray_face, False)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_label_arg = np.argmax(
                emotion_classifier.predict(gray_face))
            emotion_text = emotion_labels[emotion_label_arg]

            if emotion_text not in emotion_to_cnt:
                emotion_to_cnt[emotion_text] = 0
            emotion_to_cnt[emotion_text] = emotion_to_cnt[emotion_text] + 1

            color = (255, 255, 255)

            cv2.putText(rgb_image, emotion_text,
                        (face.rect.left(), face.rect.top() - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, color, 1, cv2.LINE_AA)

        face_list = np.empty((len(faces), gender_age_prediction_img_size,
                              gender_age_prediction_img_size, 3))

        for i in range(0, len(faces)):
            face = faces[i]
            x1, y1, x2, y2, w, h = face.rect.left(), face.rect.top(
            ), face.rect.right() + 1, face.rect.bottom() + 1, face.rect.width(
            ), face.rect.height()
            xw1 = max(int(x1 - 0.4 * w), 0)
            yw1 = max(int(y1 - 0.4 * h), 0)
            xw2 = min(int(x2 + 0.4 * w), bgr_image.shape[1] - 1)
            yw2 = min(int(y2 + 0.4 * h), bgr_image.shape[0] - 1)
            rgb_face = rgb_image[yw1:yw2 + 1, xw1:xw2 + 1, :]

            try:
                face_list[i, :, :, :] = cv2.resize(
                    rgb_face, (gender_age_prediction_img_size,
                               gender_age_prediction_img_size))
            except:
                continue

        gender_age_prediction = model.predict(face_list)
        for i in range(0, len(faces)):
            face = faces[i]
            predicted_genders = gender_age_prediction[0]
            gender_text = "FEMALE" if predicted_genders[i][0] > 0.5 else "MALE"

            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = gender_age_prediction[1].dot(ages).flatten()
            age_text = str(predicted_ages[i])

            if gender_text not in gender_to_cnt:
                gender_to_cnt[gender_text] = 0
            gender_to_cnt[gender_text] = gender_to_cnt[gender_text] + 1

            if age_text not in age_to_cnt:
                age_to_cnt[age_text] = 0
            age_to_cnt[age_text] = age_to_cnt[age_text] + 1

            gender_color = (255, 0, 0) if gender_text == "MALE" else (0, 0,
                                                                      255)
            cv2.rectangle(rgb_image, (face.rect.left(), face.rect.top()),
                          (face.rect.right(), face.rect.bottom()),
                          gender_color, 1)

            color = (255, 255, 255)
            cv2.putText(rgb_image, gender_text,
                        (face.rect.left(), face.rect.top() - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, gender_color, 1,
                        cv2.LINE_AA)
            cv2.putText(rgb_image, age_text,
                        (face.rect.left(), face.rect.top() - 35),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, color, 1, cv2.LINE_AA)

        bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)

        return bgr_image

    clip2 = VideoFileClip(video_input)
    white_clip = clip2.fl_image(
        pipeline)  # NOTE: this function expects color images!!
    white_clip.write_videofile(video_output, audio=False)
예제 #7
0
# Define our model parameters
depth = 16
k = 8
weight_file = None
margin = 0.4
image_dir = None

# Get our weight file 
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(sys.argv[0]).resolve().parent)
# load model and weights
img_size = 64
model = WideResNet(img_size, depth=depth, k=k)()
model.load_weights(weight_file)

detector = dlib.get_frontal_face_detector()

image_names = [f for f in listdir(image_path) if isfile(join(image_path, f))]

for image_name in image_names:
    frame = cv2.imread("./images/" + image_name)
    preprocessed_faces_emo = []           
 
    input_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    img_h, img_w, _ = np.shape(input_img)
    detected = detector(frame, 1)
    faces = np.empty((len(detected), img_size, img_size, 3))
    
    preprocessed_faces_emo = []
예제 #8
0
class scanning_face():
    def __init__(self, flag, bgr_image):
        print(os.path.abspath(''))
        self.flag = flag
        self.bgr_image = bgr_image
        self.frq = 0
        self.icon_dict, self.words_dict = load_emotion_icon()
        ###########
        self.args = get_args()
        self.depth = self.args.depth
        self.k = self.args.width
        self.weight_file = self.args.weight_file
        self.margin = self.args.margin
        ###########

        if not self.weight_file:
            self.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
        self.detector = dlib.get_frontal_face_detector()

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

    def show_face_information(self):
        # bgr_image = img
        gray_image = cv2.cvtColor(self.bgr_image, cv2.COLOR_BGR2GRAY)
        rgb_image = cv2.cvtColor(self.bgr_image, cv2.COLOR_BGR2RGB)
        faces = detect_faces(face_detection, gray_image)

        img_h, img_w, _ = np.shape(rgb_image)

        for face_coordinates in faces:
            x1, x2, y1, y2 = apply_offsets(face_coordinates, gender_offsets)
            rgb_face = rgb_image[y1:y2, x1:x2]

            x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
            gray_face = gray_image[y1:y2, x1:x2]
            try:
                rgb_face = cv2.resize(rgb_face, (gender_target_size))
                gray_face = cv2.resize(gray_face, (emotion_target_size))
            except:
                continue
            # run_thread(bgr_image)

            gray_face = preprocess_input(gray_face, False)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_label_arg = np.argmax(
                emotion_classifier.predict(gray_face))
            emotion_text = emotion_labels[emotion_label_arg]

            emotion_window.append(emotion_text)
            # emotion_window.append(English_2_chinese_emotion(emotion_text))

            rgb_face = np.expand_dims(rgb_face, 0)
            rgb_face = preprocess_input(rgb_face, False)
            gender_prediction = gender_classifier.predict(rgb_face)
            gender_label_arg = np.argmax(gender_prediction)
            gender_text = gender_labels[gender_label_arg]
            # gender_window.append(English_2_chinese_gender(gender_text))

            set_icon = emotion_text + "_" + gender_text
            print(set_icon)
            icon_img = self.icon_dict[set_icon]
            words_img = self.words_dict[set_icon]

            # if len(gender_window) > frame_window:
            #     emotion_window.pop(0)
            #     gender_window.pop(0)
            # try:
            #     emotion_mode = mode(emotion_window)
            #     gender_mode = mode(gender_window)
            # except:
            #     continue

            if gender_text == gender_labels[0]:
                color = (0, 0, 255)
            else:
                color = (255, 0, 0)

            ###################
            if (self.frq % 60 == 0):

                # detect faces using dlib detector
                detected = self.detector(rgb_image, 1)
                print(detected)
                faces_age = 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 - self.margin * w), 0)
                        yw1 = max(int(y1 - self.margin * h), 0)
                        xw2 = min(int(x2 + self.margin * w), img_w - 1)
                        yw2 = min(int(y2 + self.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_age[i, :, :, :] = cv2.resize(
                            img[yw1:yw2 + 1, xw1:xw2 + 1, :],
                            (img_size, img_size))

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

            self.frq += 1

            if ((face_coordinates[0] - face_coordinates[2]) > 50
                    and (face_coordinates[0] - face_coordinates[2]) < 180
                    and (face_coordinates[1] - 80) > 20):
                solid_box = draw_solid_box(face_coordinates, rgb_image)
                draw_bounding_box(face_coordinates, rgb_image, color)
                solid_box = Addemotion(face_coordinates, solid_box, icon_img)
                solid_box = Addemotion_word(face_coordinates, solid_box,
                                            words_img)
                draw_text(face_coordinates, rgb_image,
                          str(int(predicted_ages)), (255, 255, 255), 0, -20, 1,
                          1)

            return rgb_image
예제 #9
0
# Size of the images
if train_model == "Inception":
    img_width, img_height = 139, 139
elif train_model == "ResNet":
    img_width, img_height = 197, 197

emotions = ['Anger', 'Disgust', 'Fear', 'Happiness', 'Sadness', 'Surprise', 'Neutral']

# Reinstantiate the fine-tuned model (Also compiling the model using the saved training configuration (unless the model was never compiled))
emo_model = load_model('./trained_models/ResNet-50.h5')
# age and gender
weight_file = None
margin = 0.4
img_size = 64
model = WideResNet(img_size, depth=16, k=8)()
model.load_weights('./trained_models/weights.28-3.73.hdf5')


def preprocess_input(image):
    image = cv.resize(image, (img_width, img_height))
    ret = np.empty((img_height, img_width, 3))
    ret[:, :, 0] = image
    ret[:, :, 1] = image
    ret[:, :, 2] = image
    x = np.expand_dims(ret, axis=0)  # (1, XXX, XXX, 3)

    if train_model == "Inception":
        x /= 127.5
        x -= 1.
        return x
    elif train_model == "ResNet":
예제 #10
0
depth = 16
width = 8
alpha = 0.8
# model=None
if 0:
    CASE_PATH = "/home/merly/Documents/gender/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"
    face_size = 64
    depth = 16
    width = 8
    alpha = 0.8
    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',WRN_WEIGHTS_PATH,cache_subdir=model_dir)
    print('**********************',fpath)
    model.load_weights(fpath)

    emotion_model_path = 'models/emotion_model.hdf5'
    emotion_labels = get_labels('fer2013')

    # hyper-parameters for bounding boxes shape
    frame_window = 10
    emotion_offsets = (20, 40)

    # loading models
    face_cascade = cv2.CascadeClassifier('models/haarcascade_frontalface_default.xml')
    emotion_classifier = load_model(emotion_model_path)

# getting input model shapes for inference
    emotion_target_size = emotion_classifier.input_shape[1:3]
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)
        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)
            #reg_face = copy.deepcopy(reg_face)
            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 
                label1 = "{}".format(identities[i])
                self.draw_label_bottom(img, (bb.left(), bb.bottom()), label1)
            
                ## Display age, gender and emotion
                if identities[i] == "Unknown" or "customer" in identities[i]:
                    label2 = "{}, {}, {}".format(int(predicted_ages[i]),
                                                 "F" if predicted_genders[i][0] > 0.5 else "M",
                                                 emotion2_results[i])
                else:
                    label2 = "{}".format(emotion2_results[i])
                self.draw_label_bottom(img, (bb.left(), bb.bottom()+1), label2, row_index=1)
            else:
                ## Display age, gender and emotion 
                label2 = "{}, {}, {}".format(int(predicted_ages[i]),
                                             "F" if predicted_genders[i][0] > 0.5 else "M",
                                             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
예제 #12
0
def main():
    args = get_args()
    depth = args.depth
    k = args.width
    weight_file = args.weight_file
    margin = args.margin

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

    folder_images = os.path.expanduser(args.input_folder)
    images = []
    images = glob.glob(os.path.join(folder_images, "*.png"))
    images.extend(glob.glob(os.path.join(folder_images, "*.jpg")))
    images.extend(glob.glob(os.path.join(folder_images, "*.jpeg")))

    # 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 path in tqdm.tqdm(images):
        img_name = os.path.splitext(
            os.path.basename(path))[0]  # without file suffix
        img = cv2.imread(path)
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)

        segment_img = cv2.imread(
            os.path.join(args.output_folder, "segmented/", img_name + ".png"))

        # if not img_name == "0377":
        #     continue

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

        # predict ages and genders of the detected faces
        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)
                faces[i, :, :, :] = cv2.resize(
                    img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

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

            # process results
            # remove all female detected segments
            for i, d in enumerate(detected):
                gender = "M" if predicted_genders[i][0] < 0.5 else "F"
                label = "{}, {}".format(int(predicted_ages[i]), gender)
                print(path, label)
                draw_label(img, (d.left(), d.top()), label)
                if gender == "F":
                    centerX = int(d.left() + d.width() / 2)
                    centerY = int(d.top() + d.height() / 2)
                    segment_color = segment_img[centerY, centerX]
                    segment_pixels_indices = np.where(
                        np.all(segment_img == segment_color, axis=-1))
                    segment_img[segment_pixels_indices] = [0, 0, 0]

        # make all remaining segements white
        non_black_pixels_indices = np.any(segment_img != [0, 0, 0], axis=-1)
        segment_img[non_black_pixels_indices] = [255, 255, 255]

        # save new binary mask
        out_filename = os.path.join(args.output_folder,
                                    "age_gender_masked_binary/",
                                    img_name + ".png")
        os.makedirs(os.path.dirname(out_filename), exist_ok=True)
        cv2.imwrite(out_filename, segment_img)

        # save debug image
        out_filename = os.path.join(args.output_folder, "age_gender/",
                                    img_name + ".png")
        os.makedirs(os.path.dirname(out_filename), exist_ok=True)
        cv2.imwrite(out_filename, img)
예제 #13
0
if __name__ == "__main__":

    os.environ['CUDA_VISIBLE_DEVICES'] = '3'

    model_path = 'model'
    if not os.path.exists(model_path):
        os.mkdir(model_path)

    image_size = 128
    batch_size = 64
    nb_epochs = 30

    # depth of network (should be 10, 16, 22, 28, ...)
    # width of network (should be 2, 4, 6, 8, 10, ...)
    model = WideResNet(image_size, depth=16, k=4)()
    model.load_weights('model/weights.24-5.84.hdf5', by_name=True)
    sgd = SGD(lr=0.002, momentum=0.9, nesterov=True)
    model.compile(
        optimizer=sgd,
        loss=["categorical_crossentropy", "categorical_crossentropy"])

    gen = Generator(train_full_path="images/imdb_detect/imdb.csv", \
                    valid_full_path="images/wiki_detect/wiki.csv", \
                    image_size=image_size, \
                    batch_size=batch_size)

    callbacks = [
        LearningRateScheduler(schedule=Schedule(nb_epochs)),
        ModelCheckpoint("model/weights.{epoch:02d}-{val_loss:.2f}.hdf5",
                        verbose=1,
                        save_weights_only=True)
class FaceCV(object):
    """
    Singleton class for face recongnition task
    """
    CASE_PATH = "./pretrained_models/haarcascade_frontalface_alt.xml"
    WRN_WEIGHTS_PATH = "---IMBD || WIKI weights path (Alredy given with Aswin Sir)"

    #Establishing SuperClass
    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
    #Variables intilization
    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)

        """ I have commented out this medthod since we dont need to see the out put in real time once on the Jetson"""
#     @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)
예제 #15
0
def main():
    global s3
    # s3 = boto3.client('s3')
    session = boto3.Session(aws_access_key_id=AWS_SERVER_PUBLIC_KEY,
                            aws_secret_access_key=AWS_SERVER_SECRET_KEY)
    s3 = session.resource('s3')

    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('OK. Ready!')

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

            # draw results
            for i, d in enumerate(detected):
                label = "{}, {}".format(
                    int(predicted_ages[i] - 5),
                    "M" if predicted_genders[i][0] < 0.5 else "F")
                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

        if key == 32:  # SPACE
            frame_id = int(time.time())

            # _,embeddings = get_face_embeddings_from_image(img)
            # embedded = len(embeddings)==len(detected)
            # if not embedded:
            #     embeddings =  [None]*len(detected)
            #     print('Oops. Please try again!')
            #     return None

            response = [
                [
                    str(frame_id) + str(i + 1).zfill(2),
                    str(pd.to_datetime('now')),
                    str(frame_id),
                    str(i + 1),
                    str(int(predicted_ages[i]) - 5),
                    "M" if predicted_genders[i][0] < 0.5 else "F"
                    # ,str(embeddings[i])
                ] for i, d in enumerate(detected)
            ]

            j_response = [
                {
                    'key':
                    int(str(frame_id) + str(i + 1).zfill(2)),
                    'datetime':
                    str(pd.to_datetime('now')),
                    'frame_id':
                    frame_id,
                    'frame_face_id':
                    i + 1,
                    'predicted_ages':
                    str(int(predicted_ages[i]) - 5),
                    'predicted_genders':
                    "M" if predicted_genders[i][0] < 0.5 else "F"
                    # ,'embeddings':list(embeddings[i])
                } for i, d in enumerate(detected)
            ]

            output = '\n'.join(['|'.join(i) for i in response])
            j_output = '\n'.join([str(j) for j in j_response])
            print(j_output)
            try:
                upload(j_output)
            except:
                print('Connection to S3 failed.')

        if key == 115:  # s
            print(
                'Please enter email address to send result to (Consent required).'
            )
            print('Sending to ' + input(), end='...\n')
            output = '\n'.join(['\t'.join(i) for i in response])
            print(output)
예제 #16
0
def classify_process():
    print("* Loading model...")
    img_size = 64
    model = WideResNet(img_size, depth=16, k=8)()
    model.load_weights("checkpoints168/weights.48-3.62.hdf5")
    print("* Model loaded")

    while True:
        body = db.lpop(settings.IMAGE_QUEUE)
        if body is not None:
            body = json.loads(body.decode("utf-8"))
            image = helpers.base64_decode_image(body["image"], body["height"])

            bounding_boxes = demo_mtcnn.detect_face(image)
            margin = 0.4
            faces = np.empty((len(bounding_boxes), 64, 64, 3))
            for i, bounding_box in enumerate(bounding_boxes):
                x = int(bounding_box[0])
                y = int(bounding_box[1])
                w = int(bounding_box[2])
                h = int(bounding_box[3])
                img_h, img_w, _ = np.shape(image)
                _x = max(int(x - margin * w), 0)
                _y = max(int(y - margin * h), 0)
                _x2 = min(int(x + w + margin * w), img_w - 1)
                _y2 = min(int(y + h + margin * h), img_h - 1)
                # cv2.rectangle(image, (x, y), (x+w, y+h), (0, 155, 255), 2)
                faces[i, :, :, :] = cv2.resize(
                    image[_y:_y2 + 1, _x:_x2 + 1, :], (64, 64))

                # cv2.rectangle(image, (_x, _y), (_x2, _y2), (0, 155, 255), 2)
                # cv2.imshow("",faces[0])

                # key  = cv2.waitKey(-1)
                # if key==27:
                #     break
            if len(faces) > 0:

                results = model.predict(faces)
                # results = imagenet_utils.decode_predictions(preds)
                predicted_genders = results[0]
                ages = np.arange(0, 101).reshape(101, 1)
                predicted_ages = results[1].dot(ages).flatten()
                predicted_ages = predicted_ages.tolist()
                predicted_genders = [
                    gender.tolist() for gender in predicted_genders
                ]
                print(type(predicted_genders[0]))
                print(type(predicted_ages))

                # for i, bounding_box in enumerate(bounding_boxes):
                # 	label = "{}, {}".format(int(predicted_ages[i]),"M" if predicted_genders[i][0] < 0.5 else "F")
                # 	demo_mtcnn.draw_label(image, (int(bounding_box[0]), int(bounding_box[1])), label)
                # cv2.imshow("",image)
                # cv2.waitKey(1000)
                print("done")

            # image_out = helpers.base64_encode_image(image).decode('utf-8')

            # db.set(body["id"], json.dumps(image_out))
            data = {
                "boudingboxes": bounding_boxes.tolist(),
                "ages": predicted_ages,
                "genders": predicted_genders
            }
            db.set(body["id"], json.dumps(data))
        else:
            print("No data")
        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
예제 #17
0
import cv2
import os
from time import sleep
import numpy as np
import argparse
from wide_resnet import WideResNet

depth = 16
width = 8
face_size = 64
model = WideResNet(face_size, depth=depth, k=width)()
model.load_weights("weights.18-4.06.hdf5")


def draw_label(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(imgarray, section, margin=40, size=64):
    img_h, img_w, _ = imgarray.shape
    if section is None:
예제 #18
0
class FaceCV(object):
    FACE_DETECTOR = settings.MODEL_PARA['detect_faces_model_path']
    # WRN_WEIGHTS_PATH = "https://github.com/Tony607/Keras_age_gender/releases/download/V1.0/weights.18-4.06.hdf5"
    AGE_GENDER_PREDICTOR = settings.MODEL_PARA['age_gender_prediction_path']
    """
        Singleton class for face recongnition task
    """
    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)()
        self.model.load_weights(self.AGE_GENDER_PREDICTOR)

    @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=settings.MARGIN,
                  size=settings.FACE_SIZE):
        """
        :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_one_image(self, frame):
        out_boxes = []
        out_ages = []
        out_genders = []

        # load the face detector model
        face_cascade = cv2.CascadeClassifier(self.FACE_DETECTOR)
        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))
        print('len(faces) :{}'.format(len(faces)))

        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)
            out_boxes.append(cropped)
            face_imgs[i, :, :, :] = face_img
            print(face_imgs.shape)
        print("out_boxes : {}".format(out_boxes))

        if len(face_imgs) > 0:
            # predict ages and genders of the detected faces
            results = self.model.predict(face_imgs)
            print("----doing 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):
            out_ages.append(int(predicted_ages[i]))
            out_genders.append("F" if predicted_genders[i][0] > 0.5 else "M")
            # 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)
        print("out_boxes : {}".format(out_boxes))
        return out_boxes, out_ages, out_genders
예제 #19
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.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
    cascade_path = "face_cascades/haarcascade_profileface.xml"
    face_cascade = cv2.CascadeClassifier(cascade_path)


    # 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("people.mp4")
    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()
	print img.shape

        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.compute_face_descriptor(input_img, 1)
	detected = face_cascade.detectMultiScale(input_img, 1.1, 2, 0, (20, 20) )
        faces = np.empty((len(detected), img_size, img_size, 3))

        for (x1,y1,w,h) in detected:
	    x2=x1+w
	    y2=y1+h
            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[:,:,:] = 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)
	#cv2.imwrite('detected.png',img)
        key = cv2.waitKey(30)

        if key == 27:
            break
예제 #20
0
factor = 0.709  # scale factor

# Keras age & gender classification parameters
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)
예제 #21
0
class FaceCV(object):
    """
    Singleton class for face recongnition task
    """
    CASE_PATH = ".\\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)

        # initialize the list of class labels MobileNet SSD was trained to
        # detect, then generate a set of bounding box colors for each class
        CLASSES = [
            "background", "aeroplane", "bicycle", "bird", "boat", "bottle",
            "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse",
            "motorbike", "person", "pottedplant", "sheep", "sofa", "train",
            "tvmonitor"
        ]
        COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

        # load our serialized model from disk
        print("[INFO] loading model...")
        net = cv2.dnn.readNetFromCaffe("MobileNetSSD_deploy.prototxt.txt",
                                       "MobileNetSSD_deploy.caffemodel")

        # initialize the video stream, allow the cammera sensor to warmup,
        # and initialize the FPS counter
        print("[INFO] starting video stream...")
        vs = VideoStream(src=0).start()
        time.sleep(2.0)
        import pyttsx3
        engine = pyttsx3.init()

        oldObjectDict = {}

        # 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)
            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()
            allLabels = []
            # 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)
                allLabels.append(label)

            if len(allLabels) > 0:
                print("age-gender : " + str(allLabels), flush=True)
            #cv2.imshow('Keras Faces', frame)

            frame = imutils.resize(frame, width=400)

            # grab the frame dimensions and convert it to a blob
            (h, w) = frame.shape[:2]
            blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                         0.007843, (300, 300), 127.5)

            # pass the blob through the network and obtain the detections and
            # predictions
            net.setInput(blob)
            detections = net.forward()

            objectDict = {}

            # loop over the detections
            for i in np.arange(0, detections.shape[2]):
                # extract the confidence (i.e., probability) associated with
                # the prediction
                confidence = detections[0, 0, i, 2]

                # filter out weak detections by ensuring the `confidence` is
                # greater than the minimum confidence
                if confidence > 0.8:
                    # extract the index of the class label from the
                    # `detections`, then compute the (x, y)-coordinates of
                    # the bounding box for the object
                    idx = int(detections[0, 0, i, 1])
                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype("int")

                    objectDict[CLASSES[idx]] = confidence * 100
                    # draw the prediction on the frame
                    #label = "object - {}: {:.2f}%".format(CLASSES[idx], confidence * 100)

            for clazz in objectDict:
                if clazz in oldObjectDict:
                    if abs(oldObjectDict[clazz] - objectDict[clazz]) > 10:
                        print("object : " + str(clazz) + ": " +
                              str(oldObjectDict[clazz]),
                              flush=True)
                        oldObjectDict[clazz] = objectDict[clazz]
                else:
                    print("object : " + str(clazz) + ": " +
                          str(objectDict[clazz]),
                          flush=True)
                    oldObjectDict[clazz] = objectDict[clazz]

            for clazz in list(oldObjectDict):
                if not clazz in objectDict:
                    del (oldObjectDict[clazz])
        # When everything is done, release the capture
        video_capture.release()
        cv2.destroyAllWindows()
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()

    image_to_age_map = get_image_to_age_mapping()
    age_diffs = []
    negative_count = 0

    for img, filename 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()
            predicted_age = float(predicted_ages.tolist()[0])

            image_name = filename.split('/')[-1].split('.')[0].replace(
                TARGET_SUFFIX, '')
            actual_age = image_to_age_map[image_name]

            age_diff = predicted_age - actual_age
            if age_diff < 0:
                age_diff = 0  # we should not see negative, treat all negative as model didn't do anything.
                negative_count += 1
            age_diffs.append(age_diff)

    print('Max Age Progression: {:.2f}'.format(max(age_diffs)))
    print('Avg Age Progression: {:.2f}'.format(
        sum(age_diffs) / len(age_diffs)))
    print('10+ Age Progression: {:.2f}%'.format(
        len([x for x in age_diffs if x >= 10]) / len(age_diffs) * 100))
    print('15+ Age Progression: {:.2f}%'.format(
        len([x for x in age_diffs if x >= 15]) / len(age_diffs) * 100))
    print('20+ Age Progression: {:.2f}%'.format(
        len([x for x in age_diffs if x >= 20]) / len(age_diffs) * 100))
    print('Neg Age Progression: {:d} out of {:d}'.format(
        negative_count, len(age_diffs)))
예제 #23
0
from keras.preprocessing import image

# Flask utils
from flask import Flask, redirect, url_for, request, render_template
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer
from wide_resnet import WideResNet

app = Flask(__name__)

MODEL_PATH = 'models/xxx.hdf5'

img_size = 64
model = load_model(MODEL_PATH)
model = WideResNet(img_size, depth=16, k=8)()
model.load_weights(MODEL_PATH)
print('Model loaded. Start serving...')


def model_predict(img_path, model):
    img = cv2.imread(img_path)
    img = cv2.resize(img, (64, 64))
    img = np.expand_dims(img, 0)

    # predict
    results = model.predict(img)
    predicted_genders = results[0]
    ages = np.arange(0, 101).reshape(101, 1)
    predicted_ages = results[1].dot(ages).flatten()
    predicted_ages = str(int(predicted_ages.item(0)))
    if predicted_genders.item(0) > 0.5:
예제 #24
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)
            #print(label)
            #print(get_face_encodings(input_img, detected))
            #faces = get_face_encodings(input_img, detected)
            #if not compare_face_encodings(faces):
            #    print("new face")
            #    known_faces.append(faces)

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

        if key == 27:
            break
예제 #25
0
def main():
    socket = SocketIO('localhost', 3002, LoggingNamespace)
    print("connect")

    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("checkpoints/weights.78-3.51.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()

    # age and gender
    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)
    age_list = []

    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)

        # 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)
                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]
            emotion = get_emotion(imgNum)
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()
            print(int(predicted_ages))
            print(predicted_genders)
            print(emotion)
            print()
            file = './thumbnail0.jpg'
            if os.path.isfile(file):
                os.remove(file)
            age_list.append(int(predicted_ages))
            if len(age_list) == 2:
                predicted_ages_final = (age_list[0] + age_list[1]) / 2
                print(int(predicted_ages_final))
                age_list = []
                crawling = bsObject.body.find_all("em")[2].get_text()
                crawling_num = re.findall("\d+", crawling)
                crawling_dust = bsObject.body.find_all("strong")[3].get_text()
                crawling_text = bsObject.body.find_all("em")[3].get_text()

                for i, d in enumerate(detected):
                    label = "{},{},{},{},{},{}".format(
                        int(predicted_ages_final),
                        "f" if predicted_genders[i][0] < 0.6 else "m",
                        "neutral" if emotion is None else emotion[2][0],
                        crawling_num[0], crawling_dust, crawling_text)
                    listA = label.split(",")
                    print(listA)
                    socket.emit('client1', listA)

                    while True:
                        # sleep(0.5)

                        # Listen
                        socket.on('finish', on_finish)
                        socket.wait(seconds=1)

                        global flag
                        if flag is True:
                            flag = False
                            break

        # 웹캠 실행 시
        key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)

        if key == 27:  # ESC
            break
예제 #26
0
def main():
    args = get_args()
    depth = args.depth
    k = args.width
    # weight_file = args.weight_file
    weight_file = "checkpoints168/weights.48-3.62.hdf5"
    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))

    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()
    # image_generator=yield_images()
    skip_frame = 1
    ind = 0

    for img_ in image_generator:
        print(img_.shape)
        if ind % skip_frame == 0:

            bounding_boxes = detect_face(img_)
            print("face number: ", len(bounding_boxes))
            faces = np.empty((len(bounding_boxes), 64, 64, 3))
            for i, bounding_box in enumerate(bounding_boxes):
                x = int(bounding_box[0])
                y = int(bounding_box[1])
                w = int(bounding_box[2])
                h = int(bounding_box[3])

                img_h, img_w, _ = np.shape(img_)
                _x = max(int(x - margin * w), 0)
                _y = max(int(y - margin * h), 0)
                _x2 = min(int(x + w + margin * w), img_w - 1)
                _y2 = min(int(y + h + margin * h), img_h - 1)

                cv2.rectangle(img_, (x, y), (x + w, y + h), (0, 155, 255), 2)
                cv2.rectangle(img_, (_x, _y), (_x2, _y2), (255, 155, 255), 2)
                faces[i, :, :, :] = cv2.resize(img_[_y:_y2 + 1, _x:_x2 + 1, :],
                                               (64, 64))

                # predict ages and genders of the detected facesƯ
            try:
                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, bounding_box in enumerate(bounding_boxes):
                    label = "{}, {}".format(
                        int(predicted_ages[i]),
                        "Nam" if predicted_genders[i][0] < 0.5 else "Nữ")
                    draw_label(img_,
                               (int(bounding_box[0]), int(bounding_box[1])),
                               label)

                cv2.imshow("result", img_)
                # cv2.waitKey(0)
                # print(ind)
                key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)
                if key == 27:  # ESC
                    break
                continue
            except:
                cv2.imshow("result", img_)
                key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)
                if key == 27:  # ESC
                    break
        ind = ind + 1
예제 #27
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=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)

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

            # 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.imshow("result", img)
        key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)

        if key == 27:  # ESC
            break
예제 #28
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
예제 #29
0
class FaceCV(object):
    """
    Singleton class for face recongnition task
    """
    CASE_PATH = ".\\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()
            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()
예제 #30
0
from wide_resnet import WideResNet
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.inception_v3 import InceptionV3
from keras.layers import Dense, GlobalAveragePooling2D, Dropout
from keras.utils.training_utils import multi_gpu_model
from keras.models import Model
from utilities import *

# Constants
gender_img_size = 64
predict_img_size = 256

# Loading models
gender_model = WideResNet(gender_img_size, depth=16, k=8)()
gender_model.load_weights(
    os.path.join("pretrained weights", "weights.18-4.06.hdf5"))
print("Gender models loaded.")

female_base_model = InceptionV3(weights='imagenet',
                                include_top=False,
                                input_shape=(predict_img_size,
                                             predict_img_size, 3))
male_base_model = InceptionV3(weights='imagenet',
                              include_top=False,
                              input_shape=(predict_img_size, predict_img_size,
                                           3))
print("Base models loaded.")

x_female = female_base_model.output
x_female = GlobalAveragePooling2D()(x_female)
x_female = Dense(1024, activation='relu')(x_female)
예제 #31
0
"""
Created by Alex Wang
On 2018-06-26
"F" if predicted_genders[i][0] > 0.5 else "M"
"""
import os
import traceback
import time

import numpy as np
import cv2
from wide_resnet import WideResNet

face_size = 64
model = WideResNet(face_size, depth=16, k=8)()
model.load_weights('weights.18-4.06.hdf5')


def test_one(image_path):
    try:
        face_cascade = cv2.CascadeClassifier(
            'haarcascades/haarcascade_frontalface_alt.xml')
        img_org = cv2.imread(image_path)
        height, width = img_org.shape[0:2]
        img_gray = cv2.cvtColor(img_org, cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(img_gray, 1.1, 3)
        for (x, y, w, h) in faces:
            up_margin = w / 3
            margin = w / 4
            y_min = max(0, y - up_margin)
예제 #32
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()
    MODEL_NAME = 'ssd_inception_v2_coco_2017_11_17'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

    # Number of classes to detect
    NUM_CLASSES = 90

    # Download Model
    opener = urllib.request.URLopener()
    opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
    tar_file = tarfile.open(MODEL_FILE)
    for file in tar_file.getmembers():
        file_name = os.path.basename(file.name)
        if 'frozen_inference_graph.pb' in file_name:
            tar_file.extract(file, os.getcwd())

    # Load a (frozen) Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    sess = tf.Session(graph=detection_graph)
    # Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Extract detection boxes
    boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Extract detection scores
    scores = detection_graph.get_tensor_by_name('detection_scores:0')
    # Extract detection classes
    classes = detection_graph.get_tensor_by_name('detection_classes:0')
    # Extract number of detectionsd
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    # load model and weights
    img_size = 64
    model = WideResNet(img_size, depth=depth, k=k)()
    model.load_weights(weight_file)

    emotion_model_path = './pretrained_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
    emotion_classifier = load_model(emotion_model_path, compile=False)
    emotion_target_size = emotion_classifier.input_shape[1:3]
    emotion_offsets = (20, 40)
    emotion_labels = get_labels('fer2013')

    image_generator = yield_images_from_dir(
        image_dir) if image_dir else yield_images()
    cv2.namedWindow("result", cv2.WINDOW_NORMAL)
    for img in image_generator:
        input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img_h, img_w, _ = np.shape(input_img)
        image_object = np.expand_dims(img, axis=0)
        # detect faces using dlib detector
        detected = detector(input_img, 1)
        faces = np.empty((len(detected), img_size, img_size, 3))
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        # Extract detection boxes
        boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        # Extract detection scores
        scores = detection_graph.get_tensor_by_name('detection_scores:0')
        # Extract detection classes
        classes = detection_graph.get_tensor_by_name('detection_classes:0')
        # Extract number of detectionsd
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        # Actual detection.
        (boxes, scores, classes,
         num_detections) = sess.run([boxes, scores, classes, num_detections],
                                    feed_dict={image_tensor: image_object})
        if len(detected) > 0:
            vis_util.visualize_boxes_and_labels_on_image_array(
                img,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8)
            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, (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()

            gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            # draw results
            for i, d in enumerate(detected):
                face_coordinates = make_face_coordinates(d)
                x1, x2, y1, y2 = apply_offsets(face_coordinates,
                                               emotion_offsets)
                gray_face = gray_image[y1:y2, x1:x2]
                try:
                    gray_face = cv2.resize(gray_face, (emotion_target_size))
                except:
                    continue
                gray_face = preprocess_input(gray_face, False)
                gray_face = np.expand_dims(gray_face, 0)
                gray_face = np.expand_dims(gray_face, -1)
                emotion_label_arg = np.argmax(
                    emotion_classifier.predict(gray_face))
                emotion_text = emotion_labels[emotion_label_arg]

                label = emotion_text + " {}, {}".format(
                    int(predicted_ages[i] - 4),
                    "F" if predicted_genders[i][0] > 0.5 else "M")

                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
                draw_label(img, (d.left(), d.top()), label)

        cv2.imshow("result", img)

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
예제 #33
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()
예제 #34
0
    def run(self):

        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
        index_image = 0
        model = WideResNet(img_size, depth=depth, k=k)()
        model.load_weights(weight_file)

        # list = os.listdir(pathToDirectory)
        photoN = '\\image' + str(index_image) + '.jpg'

        numOfPerson=0
        while True :
            time.sleep(5)
            list = os.listdir(pathToDirectory)

            print("scan...")
            if len(list) != numOfPerson:
                Index.Nmb_of_people=0
                Index.age_tot = 0
                Index.count_of_men=0
                Index.count_of_women=0
                #get the pictures of the persons from the directory
                for person in list:
                    Index.Nmb_of_people+=1
                    #the adress of one picture
                    name1 = pathToDirectory + person + photoN
                    print(person)
                    if os.path.exists(name1):
                        img = imread(name1)
                       
                        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) ##change la valeur je ne sais pas pourquoi
                                # 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)  ##contien les deux resultats (sexe et age)
                            ######get the gender predicted from the model
                            predicted_genders = results[0]
                            gender = "F" if predicted_genders[i][0] > 0.5 else "M"
                            print(gender)  # imprime le sexe
                            if gender == "M":
                                Index.count_of_men+=1
                            else :
                                Index.count_of_women+=1
                            ####get the age predicted from the model
                            ages = np.arange(0, 101).reshape(101, 1)
                            predicted_ages = results[1].dot(ages).flatten()
                            age = round(predicted_ages[0], 0)
                            print(age)  # imprime l age
                            print("images/"+person+".jpg")
                            print(name1)
                            Index.age_tot+=age
                            
                            ##all this code is used to upload the new datas into the databases
                            pyrUp= storage1.child("images/"+person+".jpg").put(name1, user['idToken'])

                            picUrl = storage1.child("images/"+person+".jpg").get_url(pyrUp['downloadTokens'])
                            time1 = datetime.datetime.now().time()


                            data = {"DisplayLabel": "Age", "identifier": "text", "showHideLabel" : "1","showInListing" : "1" ,"value" : str(age) }
                            db.child("person").child(person).child("Age").set(data)
                            data = {"DisplayLabel": "Gender", "identifier": "text", "showHideLabel": "1",
                                    "showInListing": "1", "value": gender}
                            db.child("person").child(person).child("Gender").set(data)
                            data = {"DisplayLabel": "Picture", "identifier": "file", "showHideLabel": "1",
                                    "showInListing": "1", "value": str(picUrl)}
                            db.child("person").child(person).child("Picture").set(data)
                            data = {"DisplayLabel": "Time", "identifier": "text", "showHideLabel": "1",
                                    "showInListing": "1",
                                    "value": str(time1)}
                            db.child("person").child(person).child("Number Of People").set(data)
                            if not os.path.isfile(pathToDirectory + '\\' + person + '\\predicted.txt'):
                                f = open(pathToDirectory + '\\' + person + '\\predicted.txt', 'w')
                                f.write('gender = ' + gender + ' age = ' + str(age))
                                f.close()
                data = {"DisplayLabel": "People", "identifier": "text", "showHideLabel": "1",
                            "showInListing": "1", "value": "Total : "+str(Index.Nmb_of_people)+" => Women: "+str(Index.count_of_women)+" Men: "+str(Index.count_of_men)}
                db.child("person").child("person0").child("Number Of People").set(data)
                AVG_age = Index.age_tot/Index.Nmb_of_people
                data = {"DisplayLabel": ".......Age AVG", "identifier": "text", "showHideLabel": "1",
                        "showInListing": "1", "value": AVG_age}
                db.child("person").child("person0").child("Age").set(data)
                    # cv2.imshow("result", img)  # affiche le resultat a l'ecran (facultatif)

            numOfPerson = len(list)

        pass