Modifications
Create a connection to ApertureDB
from aperturedb import Connector
from aperturedb import NotebookHelpers as nh
# ApertureDB Server Info for establishing connection
db_host = "aperturedb.local" # assuming local installation as provided
user = "admin" # requires authentication
password = input("Please insert your password: ") # use the password provided for the instance or "admin" by default
db = Connector.Connector(db_host, user=user, password=password)
print("Connected to ApertureDB server...")
# Pick an example video guid for use in subsequent queries
my_guid = "cc1c685b1feb6dc27931ce62d78838a6" #fc8bd7bac259eb598b7b81410c0fbf3 #cc1c685b1feb6dc27931ce62d78838a6
Find a specific video
query = [{
"FindVideo": {
"constraints": {
"guid": ["==", my_guid]
},
"blobs": True,
"results": {
"all_properties": True
}
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
nh.display_video_mp4(blobs[0])
Add new properties, modify the video itself
query = [{
"FindVideo": {
"_ref": 1,
"constraints": {
"guid": ["==", my_guid]
},
"results": {
"all_properties": True
},
"blobs": False
}
},
{
"UpdateVideo": { # the speed of this operation depends on the size of the video itself
"ref": 1,
"properties": {
"activity": "walking", # this could actually be inferred with an activity recognition model
"event": "street",
},
"operations": [{
"type": "resize",
"scale": 0.5
}]
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
# Verify it was updated
query = [{
"FindVideo": {
"constraints": {
"guid": ["==", my_guid]
},
"blobs": True,
"results": {
"all_properties": True
}
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
nh.display_video_mp4(blobs[0])
Restore the updates
query = [{
"FindVideo": {
"_ref": 1,
"constraints": {
"guid": ["==", my_guid]
},
"results": {
"all_properties": True
},
"blobs": False
}
},
{
"UpdateVideo": {
"ref": 1,
"remove_props": ["activity", "event"],
"operations": [{
"type": "resize",
"scale": 2
}]
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
# Verify it is back as it was
query = [{
"FindVideo": {
"constraints": {
"guid": ["==", my_guid]
},
"blobs": True,
"results": {
"all_properties": True
}
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
nh.display_video_mp4(blobs[0])
Upload a new video with associated metadata and connections
query = [{
"FindEntity": {
"_ref": 1,
"with_class": "Camera",
"constraints": {
"id": ["==", 1234] # id of Nikon camera
},
"results": {
"all_properties": True
}
}
},{
"AddVideo": {
"properties": { # video metadata
"guid": "e869b2342bd7ba30a49cba61d3de9dbnz",
"frame_rate": 30,
"start_timestamp": {"_date": "2021-08-30T00:00:00+00:00"}, # this application always captures videos at fixed time every day
"end_timestamp": {"_date": "2021-08-30T23:59:59+00:00"},
"activity": "launching",
"event": "nasa-launch"
},
"connect": { # video object is connected to Camera object
"ref": 1,
"class": "captured_by"
},
"if_not_found": { # conditional add based on guid check
"guid": ["==", "e869b2342bd7ba30a49cba61d3de9dbnz"]
}
}
}]
# Read the video itself to supply with AddVideo command
fd = open("sample_data/videos/SpaceShip3.mp4", 'rb')
video_arr = [ fd.read() ]
fd.close()
response, blobs = db.query(query, video_arr)
print(db.get_last_response_str())
Find the video that was captured by given camera during a given interval
query = [{
"FindEntity": { # Find the given camera
"_ref": 1,
"with_class": "Camera",
"constraints": {
"id": ["==", 1234]
},
"results": {
"list": ["brand", "model", "resolution"]
}
}
}, {
"FindVideo": { # Find the video connected to the cameras in the given timeframe
"_ref": 2,
"is_connected_to": {
"ref": 1
},
"blobs": True,
"constraints": {
"start_timestamp": [">=", {"_date": "2021-08-30T00:00:00+00:00"}],
"end_timestamp": ["<=", {"_date": "2021-08-30T23:59:59+00:00"}]
},
"results": {
"list": ["guid", "frame_rate", "start_timestamp"],
"limit": 1
}
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
nh.display_video_mp4(blobs[0])
Extract a specific clip from the video above
video_guid = response[1]["FindVideo"]["entities"][0]["guid"]
print(video_guid)
frame_rate = response[1]["FindVideo"]["entities"][0]["frame_rate"]
print(frame_rate)
start_ts = response[1]["FindVideo"]["entities"][0]["start_timestamp"]
print(start_ts)
# The API currently supports intervals using frame numbers which can be derived from certain parameters
# We are looking for a clip of 1 min, 2 mins after start of video
# As of now, interval operation on videos supports frame numbers. So we translate timing into frame numbers
query = [{
"FindVideo": {
"constraints": {
"guid": ["==", "e869b2342bd7ba30a49cba61d3de9dbnz"]
},
"blobs": True,
"results": {
"all_properties": True,
"limit": 1
},
"operations": [{ # Original videos are longer. We just want to sample and prepare them
"type": "resize", # performance depends on the video complexity
"height": 300,
"width": 300
},{
"type": "interval", # Interval Operation, performance depends on the video complexity
"start": 1800, # Start from 1 min (30fps * 60)
"stop": 3600, # Continue for a 1 min clip
"step": 5 # Read every 5th frame
}]
}
}]
response, blobs = db.query(query)
print(db.get_last_response_str())
nh.display_video_mp4(blobs[0])