Images Example

Continuing the [Simple Example](Simple-Example), we will add some images to the Platform using the [AddImage](AddDescriptorSet) command, and we will link those images to the “Hike” event and to the people that appear in the photo.

We will again use the open source vdms Python Client module to connect to the server and send queries.

import vdms

db = vdms.vdms()
db.connect("localhost") # Will connect to localhost on port 55555

First, lets read the images from some pre-defined location.

blob_arr = []

fd = open("hike/hiking_1.jpg")
blob_arr.append(fd.read())

Let’s assume that our friend “Sophia” is present in the image. Let’s insert the image and represent the fact that Sophia is in that image. For this, we need a reference to the entity representing “Sophia”. So, let’s use [FindEntity](FindEntity) to find “Sophia”, and use a reference to link “Sophia” to the new image.

all_queries = []

query = {
          "FindEntity" : {
             "class": "Person",
             "_ref": 3,                     # We set the reference as 3
             "constraints": {
                 "name": ["==", "Sophia"]
             }
          }
       }

all_queries.append(query)

And now, let’s include the image (using the [AddImage](AddDescriptorSet)) and connect it to Sophia (using the [link](Block-link) block). Now, assume we want all the images to be resized before storage, which may not be the same size as the original image. We can use the [operations](Block-operations) block for this. The operation will be applied at insertion time.

props = {}
props["name_file"] = "hiking_1"
props["type"] = "hike"
props["date"] = "March 24th, 2017"

resize = {}
resize["type"] = "resize"
resize["height"] = 500
resize["width"]  = 400

operations = []
operations.append(resize)

link = {}
link["ref"] = 3                 # We use the set reference
link["direction"] = "in"
link["class"] = "is_in"

addImage = {}
addImage["properties"] = props
addImage["operations"] = operations
addImage["link"] = link
addImage["format"] = "jpg"      # Format use to store it on ApertureDB

query = {}
query["AddImage"] = addImage

all_queries.append(query)

print "Query Sent:"
vdms.aux_print_json(all_queries)

response, res_arr = db.query(all_queries, [blob_arr])

We should see:

Query Sent:
[
    {
      "FindEntity" : {
         "class": "Person",
         "_ref": 3,
         "constraints": {
             "name": ["==", "Sophia"]
         }
      }
    },
    {
        "AddImage": {
            "link": {
                "ref": 3
            },
            "operations": [
                {
                    "type": "resize",
                    "height": 400,
                    "width": 500
                }
            ],
            "properties": {
                "name_file": "hiking_1",
                "type": "hike",
                "date": "March 24th, 2017"
            }
        }
    }
]

Note that we are sending the image using the blob field in:

db.query(all_queries, **[blob_arr]**)

Now let’s print the response we get from the server.

response = json.loads(response)

print "Response:"
vdms.aux_print_json(response)
Response:
[
    {
        "FindEntity": {
            "status": 0,
            "returned": 1
        }
    },
    {
        "AddImage": {
            "status": 0
        }
    }
]

Awesome! we have our first image and its metadata pushed to ApertureDB.

For finding image or repeating a similar example with videos, you can refer to the API command details in the rest of this documentation.