{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import dlib\n", "import os\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/pareshar/Personal/Github/temp/Facial-feature-detector'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "current_dir = os.getcwd()\n", "parent_dir = os.path.dirname(current_dir)\n", "os.chdir(parent_dir)\n", "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# static variables\n", "path_to_images = \"data/images_age_gender/\"\n", "image_files = os.listdir(path_to_images)\n", "face_detector_weights = \"models/face_detection/res10_300x300_ssd_iter_140000.caffemodel\"\n", "face_detector_config = \"models/face_detection/deploy.prototxt.txt\"\n", "age_weights = \"models/face_age/age_net.caffemodel\"\n", "age_config = \"models/face_age/age_deploy.prototxt\"\n", "gender_weights = \"models/face_gender/gender_net.caffemodel\"\n", "gender_config = \"models/face_gender/gender_deploy.prototxt\"\n", "age_list = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']\n", "gender_list = ['Male', 'Female']\n", "model_mean = (78.4263377603, 87.7689143744, 114.895847746) # taken from paper" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(columns=[\"file_name\", \"model\", \"confidence_face_detected\", \"age_range\", \"age_confidence\", \"gender\", \"gender_confidence\"])\n", "df_list = []\n", "\n", "\n", "for image_file in image_files:\n", " image = cv2.imread(path_to_images + image_file)\n", " h, w = image.shape[:2]\n", " blob = cv2.dnn.blobFromImage(image=image, scalefactor=1.0, size=(300, 300))\n", " \n", " face_detector_net = cv2.dnn.readNetFromCaffe(face_detector_config, face_detector_weights)\n", " face_detector_net.setInput(blob)\n", " face_detections = face_detector_net.forward() \n", " age_net = cv2.dnn.readNet(age_weights, age_config)\n", " gender_net = cv2.dnn.readNet(gender_weights, gender_config)\n", " \n", " d = None\n", " \n", " for i in range(0, face_detections.shape[2]):\n", " confidence = face_detections[0, 0, i, 2]\n", " if confidence > 0.97:\n", " box = face_detections[0, 0, i, 3:7] * np.array([w, h, w, h])\n", " (startX, startY, endX, endY) = box.astype(\"int\")\n", " face = image[startY:endY, startX:endX]\n", " \n", " blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), model_mean, swapRB=False)\n", " \n", " age_net.setInput(blob)\n", " age_preds = age_net.forward()\n", " i = age_preds[0].argmax()\n", " age = age_list[i]\n", " age_confidence_score = age_preds[0][i]\n", " \n", " gender_net.setInput(blob)\n", " gender_preds = gender_net.forward()\n", " i = gender_preds[0].argmax()\n", " gender = gender_list[i]\n", " gender_confidence_score = gender_preds[0][i]\n", " \n", " # plt.imshow(face)\n", " # plt.show() \n", " \n", " d = {\n", " \"file_name\": image_file,\n", " \"model\": \"ageNet\",\n", " \"confidence_face_detected\": confidence,\n", " \"age_range\": age,\n", " \"age_confidence\": age_confidence_score,\n", " \"gender\": gender,\n", " \"gender_confidence\": gender_confidence_score \n", " }\n", " df_list.append(d)\n", " break\n", " \n", " if d is None or image_file != d[\"file_name\"]:\n", " \n", " d = {\n", " \"file_name\": image_file,\n", " \"model\": \"ageNet\",\n", " \"confidence_face_detected\": confidence,\n", " \"age_range\": \"NA\",\n", " \"age_confidence\": \"NA\",\n", " \"gender\": \"NA\",\n", " \"gender_confidence\": \"NA\" \n", " }\n", " \n", " df_list.append(d)\n", " \n", "df = pd.concat([df, pd.DataFrame(df_list)], ignore_index=True).round(2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | file_name | \n", "model | \n", "confidence_face_detected | \n", "age_range | \n", "age_confidence | \n", "gender | \n", "gender_confidence | \n", "
---|---|---|---|---|---|---|---|
1 | \n", "22_me.jpg | \n", "ageNet | \n", "0.98 | \n", "(25-32) | \n", "0.67165 | \n", "Male | \n", "1.0 | \n", "
3 | \n", "25_32_woman.jpg | \n", "ageNet | \n", "1.00 | \n", "(25-32) | \n", "0.859894 | \n", "Female | \n", "0.952863 | \n", "
2 | \n", "38_43_man.jpg | \n", "ageNet | \n", "1.00 | \n", "(25-32) | \n", "0.681306 | \n", "Male | \n", "0.999431 | \n", "
8 | \n", "38_43_woman.jpg | \n", "ageNet | \n", "0.99 | \n", "(48-53) | \n", "0.886763 | \n", "Female | \n", "0.998737 | \n", "
7 | \n", "4_6_boy.jpg | \n", "ageNet | \n", "0.99 | \n", "(4-6) | \n", "0.639939 | \n", "Male | \n", "0.999049 | \n", "
4 | \n", "4_6_girl.jpg | \n", "ageNet | \n", "0.99 | \n", "(4-6) | \n", "0.319971 | \n", "Female | \n", "0.998801 | \n", "
6 | \n", "60_100_man.jpg | \n", "ageNet | \n", "0.99 | \n", "(4-6) | \n", "0.548595 | \n", "Male | \n", "0.999973 | \n", "
5 | \n", "60_100_woman.jpg | \n", "ageNet | \n", "1.00 | \n", "(60-100) | \n", "0.332936 | \n", "Female | \n", "0.984078 | \n", "
9 | \n", "60_100_woman_2.jpg | \n", "ageNet | \n", "1.00 | \n", "(38-43) | \n", "0.414388 | \n", "Male | \n", "0.518144 | \n", "
0 | \n", "minion.jpg | \n", "ageNet | \n", "0.00 | \n", "NA | \n", "NA | \n", "NA | \n", "NA | \n", "