-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Face Extraction from MTCNN code for test dataset
- Loading branch information
1 parent
1143cef
commit 7e5111b
Showing
10 changed files
with
434 additions
and
0 deletions.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
MTCNN/.ipynb_checkpoints/Face_Extractor_BB_Landmarks_Test-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.