Skip to content

Instantly share code, notes, and snippets.

@pgolding
Created May 17, 2019 22:25
Show Gist options
  • Save pgolding/631c605cf4310cd286aa2ca16e66693b to your computer and use it in GitHub Desktop.
Save pgolding/631c605cf4310cd286aa2ca16e66693b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Prepare Images for Deep Learning\n\nGet your images with faces in them that should be roughly square already and with the face in the middle approx.\n\nThen run the script below."
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "import os\nimport subprocess\n\ndirectory_in_str = '/Users/paulg/Documents/developer/fastai/test/nad'\ndirectory = os.fsencode(directory_in_str)\n\nfor file in os.listdir(directory):\n filename = os.fsdecode(file)\n if filename.lower().endswith(\".jpg\"):\n input_file = os.path.join(directory_in_str, filename)\n print(\"processing:{}\".format(input_file))\n output_file = \"{}/{}\".format(directory_in_str,filename.split('.')[0])\n resize_command = \"ffmpeg -i {} -vf scale=224:-1 {}_r.jpg\".format(input_file,output_file)\n crop_command = \"ffmpeg -i {}_r.jpg -vf crop=224:224:ih/2-112:iw-2-112 {}_c.jpg\".format(output_file,output_file)\n cleanup_orig = \"rm {}.jpg\".format(output_file)\n cleanup_resized = \"rm {}_r.jpg\".format(output_file)\n #print(resize_command)\n #print(crop_command)\n subprocess.run(resize_command.split(' '))\n subprocess.run(crop_command.split(' '))\n subprocess.run(cleanup_orig.split(' '))\n subprocess.run(cleanup_resized.split(' '))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-nlp-py",
"display_name": "Python [conda env:nlp]",
"language": "python"
},
"language_info": {
"name": "python",
"file_extension": ".py",
"mimetype": "text/x-python",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"nbconvert_exporter": "python",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment