In [ ]:
import os
if not os.path.isfile('api/visual_genome_python_driver/README.md'):
! mkdir -p api; cd api; git clone https://github.com/ranjaykrishna/visual_genome_python_driver.git
!ls -l api/visual_genome_python_driver/
#!ls -l api/visual_genome_python_driver/visual_genome
In [ ]:
import sys
sys.path.append('./api/visual_genome_python_driver')
In [ ]:
#from src import api as vg
from visual_genome import api as vg
#import api as vg
In [ ]:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image as PIL_Image
import requests
# https://stackoverflow.com/questions/11914472/stringio-in-python3
try:
from StringIO import StringIO as ReadBytes
except ImportError:
print("Using BytesIO, since we're in Python3")
#from io import StringIO #..
from io import BytesIO as ReadBytes
In [ ]:
image_id = 2415075 # This is the first image of training set
image_id = 498263 # This has phrase 'toiletin a bathroom'
In [ ]:
#image = vg.GetImageData(id=image_id)
image = vg.get_image_data(id=image_id)
print("The url of the image is: %s" % image.url)
In [ ]:
#regions = vg.GetRegionDescriptionsOfImage(id=image_id)
regions = vg.get_region_descriptions_of_image(id=image_id)
print("The first region descriptions is: %s" % regions[0].phrase)
print("It is located in a bounding box specified by x:%d, y:%d, width:%d, height:%d"
% (regions[0].x, regions[0].y, regions[0].width, regions[0].height))
In [ ]:
for i, region in enumerate(regions):
print("regions[%2d].id=%7d .phrase='%s'" % (i, region.id, region.phrase,))
In [ ]:
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
def visualize_regions(image, regions):
response = requests.get(image.url)
img = PIL_Image.open(ReadBytes(response.content))
plt.imshow(img)
ax = plt.gca()
for region in regions:
ax.add_patch(Rectangle((region.x, region.y),
region.width, region.height,
fill=False,
edgecolor='red', linewidth=3))
ax.text(region.x, region.y, region.phrase, style='italic',
bbox={'facecolor':'white', 'alpha':0.7, 'pad':10})
fig = plt.gcf()
#plt.tick_params(labelbottom='off', labelleft='off')
plt.tick_params(labelbottom=False, labelleft=False)
plt.show()
visualize_regions(image, regions[:8])
In [ ]:
region_id=38502 # Guitar leaning on wall
#region_id=0 # Guitar leaning on wall ? Returns error...
region_id=5969774 # 'toiletin a bathroom'
In [ ]:
region_graph=vg.get_region_graph_of_region(image_id=image_id, region_id=region_id)
[ e for e in dir(region_graph) if not e.startswith('_') ]
In [ ]:
region_graph.relationships
In [ ]:
region_graph.objects
In [ ]:
region_graph.attributes
In [ ]:
region_graph.image
In [ ]:
scene_graph = vg.get_scene_graph_of_image(id=image_id)
In [ ]:
[ e for e in dir(scene_graph) if not e.startswith('_') ]
In [ ]:
#scene_graph.bounding_boxes
scene_graph.objects
In [ ]:
[ e for e in dir(scene_graph.objects[0]) if not e.startswith('_') ]
In [ ]:
scene_graph.relationships # No guitar/wall relationship... sigh
In [ ]:
scene_graph.attributes
In [ ]: