Skip to content

Instantly share code, notes, and snippets.

@zer0n
Last active February 2, 2016 07:36
Show Gist options
  • Save zer0n/9b7df37402468306cab4 to your computer and use it in GitHub Desktop.
Save zer0n/9b7df37402468306cab4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "%cd ~/src/visual_genome_python_driver/\nfrom src import local as vg\nfrom PIL import Image\nfrom os import path",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "%cd ~/data/VisualGenome\nimages = vg.GetAllImageData('.')\nannotations = vg.GetAllRegionDescriptions('.')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"scrolled": false,
"collapsed": false,
"trusted": true
},
"cell_type": "code",
"source": "with open('subdescriptions.txt', 'w') as f:\n for regions in annotations:\n if len(regions) == 0:\n continue\n \n image_id = regions[0].image.id\n filename = 'images/{0}.jpg'.format(image_id)\n if not path.isfile(filename):\n continue\n \n img = Image.open(filename).convert('RGB')\n for region in regions:\n if region.image.id != image_id:\n continue\n \n box = (region.x, region.y, region.x + region.width, region.y + region.height)\n if region.width < 224 or region.height < 224 or box[0] < 0 or box[1] < 0 or box[2] > img.width or box[3] > img.height:\n continue\n \n subimg = img.crop(box)\n filename = 'subimages/{0}_{1}.jpg'.format(image_id, region.id)\n subimg.save(filename, quality=100)\n f.write('{0} | {1}\\n'.format(filename, str(region.phrase)))",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python2",
"display_name": "Python 2",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"file_extension": ".py",
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"pygments_lexer": "ipython2",
"name": "python",
"version": "2.7.6"
},
"gist_id": "9b7df37402468306cab4"
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment