Last active
November 1, 2018 19:37
-
-
Save hohonuuli/26194a82747e9a3f2e5a3f58df7f00af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import json | |
import sys | |
import requests | |
# This example uses mock data. But it outlines the general steps needed. | |
# If you have a lot of images, they can all be registered in a single | |
# POST request, but you will need to build a JSON array of data first. | |
# The details of this bulk load are NOT covered in this example | |
host = "http://localhost:8080" | |
client_secret = "foo" | |
# Useful endpoints | |
annosaurus = "{}/anno/v1".format(host) | |
auth_url = "{}/auth".format(annosaurus) | |
image_url = "{}/images".format(annosaurus) | |
annotation_url = "{}/annotations".format(annosaurus) | |
def main(url: str, time_string: str): | |
# -- Authenticate so that we can make a post request | |
jwt_auth_header = {"Authorization": "APIKEY " + client_secret} | |
jwt_response = requests.post(auth_url, headers=jwt_auth_header).json() | |
access_token = jwt_response['access_token'] | |
video_reference_uuid = "8d6c2f15-60d4-41c3-b4a3-bab8aea69d77" | |
# -- Register image | |
print(register_image(url, time_string, access_token, video_reference_uuid)) | |
# -- Register annotation at same timestamp as image. This is needed so the | |
# image will be visible in vars-annotation for regular annotation work | |
print(register_annotation(time_string, access_token, video_reference_uuid)) | |
def register_image(url: str, time_string: str, access_token: str, video_reference_uuid): | |
# -- Use POST to register image | |
auth_header = {"Authorization": "Bearer " + access_token} | |
# Required data params: | |
# - video_reference_uuid: The UUID of the video-reference to associate the image to | |
# - url: The image URL (Images really, really should be on a webserver) | |
# - One or more of the following: | |
# - recorded_timestamp: Date image was recorded | |
# - elapsed_time_millis: Usually for framegrabs, index from start of video | |
# - timecode: For tapes, which CSIRO isn't using. | |
# | |
# Optional data params: | |
# - format: mime-type of image | |
# - width_pixels: Width of image in pixels | |
# - height_pixels: Height of image in pixels | |
# - description: Free text info about image. | |
image_data = {"video_reference_uuid": video_reference_uuid, | |
"url": url, | |
"recorded_timestamp": time_string} | |
return requests.post(image_url, image_data, headers=auth_header).json() | |
def register_annotation(time_string: str, access_token: str, video_reference_uuid): | |
# -- Use POST to register annotation | |
auth_header = {"Authorization": "Bearer " + access_token} | |
anno_data = {"video_reference_uuid": video_reference_uuid, | |
"concept": "object", | |
"observer": "brian"} | |
return requests.post(annotation_url, anno_data, headers=auth_header).json() | |
if __name__ == "__main__": | |
""" | |
Args: | |
url -> A URL to an image. Should start with http but file URLS will work to | |
time_string -> Start time of the video. Formatted as yyyy-MM-ddTHH:mm:ssZ | |
Just use UTC times for now. Fractional seconds are allowed: | |
e.g. 2018-11-01T11:22:33.1234Z | |
""" | |
url = sys.argv[1] | |
time_string = sys.argv[2] | |
main(url, time_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment