Uploading Bisque XML via Python

Assuming we have the necessary files on our Bisque server (perhaps uploaded with our script), and we have a set of bisque compliant XML annotations (perhaps generated with our script), we would like to upload them to the Bisque server so that they can be evaluated or modified. That is what this post is about  :)

Again, we use the python library requests to facilitate the posting of data to the bisque server. Uploading the annotations can be broken down into 2 steps

  1. We need to find the associated URI for the filename that we’re interested in “annotating”
  2. Using the URI, we need to POST our beautifully formatted xml annotations.
Step 1: Finding URI

We need to create a query to search for the URI based on the filename. Ultimately, this is very similar to the previous post on uploading the file.

We query the /ds/image resource using GET with the name of the file we’re interested in getting. “ds” in this case is short for data_service which handles the request. Additional documentation here.

  1. uri_pattern = re.compile(ur'uri="(?P<uri>\S+)"')
  2. uri_url="%s/ds/image?name=%s.tif" % (BASE_URL,fname[0:-4])
  3. response=requests.get(uri_url,auth=(USER,PASSWD))
  4. file_uri=re.findall(uri_pattern, response.text)[1]

We take the returned text, and grab the URI using our regular expression. In this case, the URI that we want is the second one found by the regexp as the first one is the URI of the resource for the query. This of course assumes that there is only one file with that filename and/or the first file is the one we want to add the annotation to.

Step 2: Uploading XML

Now that we know where we want to upload the xml, we simply need to use a post and upload it.

  1. upload_url='%s/gobject' % file_uri
  2. headers = {'content-type': 'text/xml'}
  3.  
  4. response=requests.post(upload_url,data=open(fname).read(), auth=(USER,PASSWD),headers=headers)

We can see that the data of our request is the contents of the xml file being read. We also need to specify in the headers that the data is in xml format so that the server knows how to process it properly.

And that’s it!


bisque_annotation

We can see here that the annotations are indeed present. From here, we can modify them as we see fit and the internal representation will be updated.

Full source for my script, which is a bit more fully featured as it lets you specify on the command line all of the necessary parameters, is available here.

Leave a Reply

Your email address will not be published. Required fields are marked *