Skip to content
Permalink
Browse files

add Face Extraction from MTCNN code for test dataset

  • Loading branch information...
dakshitagrawal97 authored and aarushgupta committed Jul 13, 2018
1 parent 1143cef commit 7e5111bfaa6620343ede98988fe43397dc5cbb2a
@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Import Modules"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from src import detect_faces, show_bboxes\n",
"from PIL import Image\n",
"\n",
"import torch\n",
"from torchvision import transforms, datasets\n",
"import numpy as np\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Path Definitions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"dataset_path = '../Dataset/emotiw/'\n",
"\n",
"processed_dataset_path = '../Dataset/FaceCoordinates/'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load Test Dataset"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"test = sorted(os.listdir(dataset_path + 'test_shared/test/'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"test_filelist = [x.split('.')[0] for x in test]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['test_1', 'test_10', 'test_100', 'test_1000', 'test_1001', 'test_1002', 'test_1003', 'test_1004', 'test_1005', 'test_1006']\n"
]
}
],
"source": [
"print(test_filelist[:10])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3011\n"
]
}
],
"source": [
"print(len(test_filelist))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Extract Faces from Image using MTCNN"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(len(test_filelist)):\n",
" print(test_filelist[i])\n",
" img_name = os.path.join(dataset_path, 'test_shared/test/', test_filelist[i]+ '.jpg')\n",
" image = Image.open(img_name)\n",
" try:\n",
" if os.path.isfile(processed_dataset_path + 'test/' + test_filelist[i] + '.npz'):\n",
" print(test_filelist[i] + ' Already present')\n",
" continue\n",
" bounding_boxes, landmarks = detect_faces(image)\n",
" bounding_boxes = np.asarray(bounding_boxes)\n",
" if bounding_boxes.size == 0:\n",
" print('MTCNN model handling empty face condition at ' + test_filelist[i])\n",
" np.savez(processed_dataset_path + 'test/' + test_filelist[i] , a=bounding_boxes, b=landmarks)\n",
" \n",
" except ValueError:\n",
" print('No faces detected for ' + test_filelist[i] + \". Also MTCNN failed.\")\n",
" np.savez(processed_dataset_path + 'test/' + test_filelist[i] , a=np.zeros(1), b=np.zeros(1))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -211,6 +211,57 @@
"# Extract Faces from Image using MTCNN"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(len(training_dataset)):\n",
" image, label = training_dataset[i]\n",
" print(train_filelist[i])\n",
" try:\n",
" if label == 0:\n",
" if os.path.isfile(processed_dataset_path + 'train/Negative/' + train_filelist[i] + '.npz'):\n",
" print(train_filelist[i] + ' Already present')\n",
" continue\n",
" bounding_boxes, landmarks = detect_faces(image)\n",
" bounding_boxes = np.asarray(bounding_boxes)\n",
" if bounding_boxes.size == 0:\n",
" print('MTCNN model handling empty face condition at ' + train_filelist[i])\n",
" np.savez(processed_dataset_path + 'train/Negative/' + train_filelist[i] , a=bounding_boxes, b=landmarks)\n",
"\n",
" elif label == 1:\n",
" if os.path.isfile(processed_dataset_path + 'train/Neutral/' + train_filelist[i] + '.npz'):\n",
" print(train_filelist[i] + ' Already present')\n",
" continue\n",
" bounding_boxes, landmarks = detect_faces(image)\n",
" bounding_boxes = np.asarray(bounding_boxes)\n",
" if bounding_boxes.size == 0:\n",
" print('MTCNN model handling empty face condition at ' + train_filelist[i]) \n",
" np.savez(processed_dataset_path + 'train/Neutral/' + train_filelist[i] , a=bounding_boxes, b=landmarks)\n",
"\n",
" else:\n",
" if os.path.isfile(processed_dataset_path + 'train/Positive/' + train_filelist[i] + '.npz'):\n",
" print(train_filelist[i] + ' Already present')\n",
" continue\n",
" bounding_boxes, landmarks = detect_faces(image)\n",
" bounding_boxes = np.asarray(bounding_boxes)\n",
" if bounding_boxes.size == 0:\n",
" print('MTCNN model handling empty face condition at ' + train_filelist[i])\n",
" np.savez(processed_dataset_path + 'train/Positive/' + train_filelist[i] , a=bounding_boxes, b=landmarks)\n",
" \n",
" except ValueError:\n",
" print('No faces detected for ' + train_filelist[i] + \". Also MTCNN failed.\")\n",
" if label == 0:\n",
" np.savez(processed_dataset_path + 'train/Negative/' + train_filelist[i] , a=np.zeros(1), b=np.zeros(1))\n",
" elif label == 1:\n",
" np.savez(processed_dataset_path + 'train/Neutral/' + train_filelist[i] , a=np.zeros(1), b=np.zeros(1))\n",
" else:\n",
" np.savez(processed_dataset_path + 'train/Positive/' + train_filelist[i] , a=np.zeros(1), b=np.zeros(1))\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Import Modules"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from src import detect_faces, show_bboxes\n",
"from PIL import Image\n",
"\n",
"import torch\n",
"from torchvision import transforms, datasets\n",
"import numpy as np\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Path Definitions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"dataset_path = '../Dataset/emotiw/'\n",
"\n",
"processed_dataset_path = '../Dataset/FaceCoordinates/'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load Test Dataset"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"test = sorted(os.listdir(dataset_path + 'test_shared/test/'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"test_filelist = [x.split('.')[0] for x in test]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['test_1', 'test_10', 'test_100', 'test_1000', 'test_1001', 'test_1002', 'test_1003', 'test_1004', 'test_1005', 'test_1006']\n"
]
}
],
"source": [
"print(test_filelist[:10])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3011\n"
]
}
],
"source": [
"print(len(test_filelist))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Extract Faces from Image using MTCNN"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(len(test_filelist)):\n",
" print(test_filelist[i])\n",
" img_name = os.path.join(dataset_path, 'test_shared/test/', test_filelist[i]+ '.jpg')\n",
" image = Image.open(img_name)\n",
" try:\n",
" if os.path.isfile(processed_dataset_path + 'test/' + test_filelist[i] + '.npz'):\n",
" print(test_filelist[i] + ' Already present')\n",
" continue\n",
" bounding_boxes, landmarks = detect_faces(image)\n",
" bounding_boxes = np.asarray(bounding_boxes)\n",
" if bounding_boxes.size == 0:\n",
" print('MTCNN model handling empty face condition at ' + test_filelist[i])\n",
" np.savez(processed_dataset_path + 'test/' + test_filelist[i] , a=bounding_boxes, b=landmarks)\n",
" \n",
" except ValueError:\n",
" print('No faces detected for ' + test_filelist[i] + \". Also MTCNN failed.\")\n",
" np.savez(processed_dataset_path + 'test/' + test_filelist[i] , a=np.zeros(1), b=np.zeros(1))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 7e5111b

Please sign in to comment.
You can’t perform that action at this time.