def test_nested_object_data_attributes(self): vcd = core.VCD() uid_obj1 = vcd.add_object('someName1', '#Some') box1 = types.bbox('head', (0, 0, 10, 10)) box1.add_attribute(types.boolean('visible', True)) self.assertEqual('attributes' in box1.data, True) self.assertEqual('boolean' in box1.data['attributes'], True) self.assertEqual( type(box1.data['attributes']['boolean']) is list, True) self.assertEqual(box1.data['attributes']['boolean'][0]['name'], "visible") self.assertEqual(box1.data['attributes']['boolean'][0]['val'], True) vcd.add_object_data(uid_obj1, box1, 0) if not os.path.isfile('./etc/vcd430_test_nested_object_data.json'): vcd.save('./etc/vcd430_test_nested_object_data.json', True) vcd_read = core.VCD('./etc/vcd430_test_nested_object_data.json', validation=True) vcd_read_stringified = vcd_read.stringify() vcd_stringified = vcd.stringify() # print(vcd_stringified) self.assertEqual(vcd_read_stringified, vcd_stringified)
def test_action_properties(self): # 1.- Create a VCD instance vcd = core.VCD() # 2.- Create the Object uid_action1 = vcd.add_action(name="", semantic_type="#Running", frame_value=(0, 10)) vcd.add_action_data(uid=uid_action1, action_data=types.num(name="confidence", val=0.98), frame_value=0) vcd.add_action_data(uid=uid_action1, action_data=types.vec(name="confidence_vec", val=[0.98, 0.97]), frame_value=0) vcd.add_action_data(uid=uid_action1, action_data=types.text(name="annotation", val="Manual"), frame_value=0) vcd.add_action_data(uid=uid_action1, action_data=types.boolean(name="validated", val=True), frame_value=1) # Same can be done with events and event_data, and contexts and context_data # And can be done as dynamic or static info uid_object1 = vcd.add_object(name="Marcos", semantic_type="#Person") vcd.add_object_data(uid=uid_object1, object_data=types.text(name="Position", val="#Researcher")) uid_context1 = vcd.add_context(name="", semantic_type="#Sunny") vcd.add_context_data(uid=uid_context1, context_data=types.text(name="category", val="#Weather")) vcd.add_context_data(uid=uid_context1, context_data=types.text(name="annotation", val="Manual")) uid_context2 = vcd.add_context(name="", semantic_type="#Highway", frame_value=(0, 5)) vcd.add_context_data(uid=uid_context2, context_data=types.num(name="risk", val=0.7), frame_value=4) vcd.add_context_data(uid=uid_context2, context_data=types.num(name="weight", val=0.5), frame_value=4) if not os.path.isfile('./etc/vcd430_test_actions_with_action_data.json'): vcd.save('./etc/vcd430_test_actions_with_action_data.json', True) vcd_read = core.VCD('./etc/vcd430_test_actions_with_action_data.json', validation=True) vcd_read_stringified = vcd_read.stringify() vcd_stringified = vcd.stringify() # print(vcd_stringified) self.assertEqual(vcd_read_stringified, vcd_stringified)
def test_load(self): # 1.- Create VCD from file vcd = core.VCD('./etc/' + vcd_version_name + '_test_create_search_mid.json', validation=True) vcd.save('./etc/' + vcd_version_name + '_test_create_search_mid_saved.json') vcd_read = core.VCD('./etc/' + vcd_version_name + '_test_create_search_mid_saved.json', validation=True) self.assertEqual(vcd_read.stringify(), vcd.stringify())
def test_polygon2D(self): vcd = core.VCD() uid_obj1 = vcd.add_object('someName1', '#Some') # Add a polygon with SRF6DCC encoding (list of strings) poly1 = types.poly2d('poly1', (5, 5, 10, 5, 11, 6, 11, 8, 9, 10, 5, 10, 3, 8, 3, 6, 4, 5), types.Poly2DType.MODE_POLY2D_SRF6DCC, False) self.assertEqual(poly1.data['name'], "poly1") self.assertEqual(poly1.data['mode'], "MODE_POLY2D_SRF6DCC") self.assertEqual(poly1.data['closed'], False) vcd.add_object_data(uid_obj1, poly1) # Add a polygon with absolute coordinates (list of numbers) poly2 = types.poly2d('poly2', (5, 5, 10, 5, 11, 6, 11, 8, 9, 10, 5, 10, 3, 8, 3, 6, 4, 5), types.Poly2DType.MODE_POLY2D_ABSOLUTE, False) vcd.add_object_data(uid_obj1, poly2) if not os.path.isfile('./etc/vcd430_test_polygon2D.json'): vcd.save('./etc/vcd430_test_polygon2D.json', True) vcd_read = core.VCD('./etc/vcd430_test_polygon2D.json', validation=True) vcd_read_stringified = vcd_read.stringify() vcd_stringified = vcd.stringify() # print(vcd_stringified) self.assertEqual(vcd_read_stringified, vcd_stringified)
def test_VCD330_to_VCD431_pd(self): vcd330_file_name = "./etc/vcd330_sample_pd.json" vcd431 = core.VCD(vcd330_file_name) if not os.path.isfile('./etc/' + vcd_version_name + '_sample_pd.json'): vcd431.save('./etc/' + vcd_version_name + '_sample_pd.json', False) vcd431_read = core.VCD('./etc/' + vcd_version_name + '_sample_pd.json') self.assertEqual(vcd431.stringify(False), vcd431_read.stringify(False))
def test_VCD330_to_VCD430_mesh(self): vcd330_file_name = "./etc/vcd330_sample_mesh_short.json" vcd430 = core.VCD(vcd330_file_name) if not os.path.isfile('./etc/vcd430_sample_mesh_short.json'): vcd430.save('./etc/vcd430_sample_mesh_short.json', False) vcd430_read = core.VCD('./etc/vcd430_sample_mesh_short.json') self.assertEqual(vcd430.stringify(False), vcd430_read.stringify(False))
def test_VCD420_to_VCD430_dmd(self): vcd420_file_name = "./etc/vcd420_1_attm_03-08_ann.json" vcd430 = core.VCD(vcd420_file_name) if not os.path.isfile('./etc/vcd430_1_attm_03-08_ann.json'): vcd430.save('./etc/vcd430_1_attm_03-08_ann.json', False) vcd430_read = core.VCD('./etc/vcd430_1_attm_03-08_ann.json') self.assertEqual(vcd430.stringify(False), vcd430_read.stringify(False))
def test_json_proto(self): vcd_json_file_name = "./etc/vcd430_test_create_search_simple_nopretty.json" vcd_proto_file_name = "./etc/vcd430_test_create_search_simple_nopretty_proto_from_json.txt" vcd_json_file_name_rebuilt = "./etc/vcd430_test_create_search_simple_nopretty_from_proto.json" serializer.json2proto_bin(vcd_json_file_name, vcd_proto_file_name) serializer.proto_bin2json(vcd_proto_file_name, vcd_json_file_name_rebuilt) vcd_src = core.VCD(vcd_json_file_name) vcd_dst = core.VCD(vcd_json_file_name_rebuilt) self.assertEqual(vcd_src.stringify(False), vcd_dst.stringify(False))
def test_objects_without_data(self): vcd = core.VCD() pedestrian_uid = vcd.add_object(name='', semantic_type='#Pedestrian', frame_value=(0, 30)) car_uid = vcd.add_object(name='', semantic_type='#Car', frame_value=(20, 30)) if not os.path.isfile('./etc/' + vcd_version_name + '_test_objects_without_data.json'): vcd.save('./etc/' + vcd_version_name + '_test_objects_without_data.json', True) vcd_read = core.VCD('./etc/' + vcd_version_name + '_test_objects_without_data.json', validation=True) vcd_read_stringified = vcd_read.stringify() vcd_stringified = vcd.stringify() # print(vcd_stringified) self.assertEqual(vcd_read_stringified, vcd_stringified)
def draw_nuscenes(scene_token): # Get annotations vcd_file_name = "../converters/nuScenesConverter/vcd_files/vcd_nuscenes_" + scene_token + ".json" vcd = core.VCD(vcd_file_name) vcd_new = convertnuScenesFromQuaternionToCuboid(vcd) drawerTopView = draw.TopView(vcd_new) # Read frame information fis = vcd.get_frame_intervals() frame_start = fis[0]['frame_start'] frame_end = fis[-1]['frame_end'] # Prepare color map topviewParams = draw.TopView.Params(_imgSize=(2000, 1000), _rangeX=(1400, 1800), _rangeY=(1200, 1400)) # Prepare window screen = screeninfo.get_monitors()[0] cv.namedWindow('nuScenes ' + scene_token, cv.WINDOW_NORMAL) cv.moveWindow('nuScenes ' + scene_token, screen.x + screen.width // 8, screen.y + screen.height // 8) cv.resizeWindow('nuScenes ' + scene_token, (int(3 * screen.width / 4), int(3 * screen.height / 4))) for frame_num in range(frame_start, frame_end+1): topView = drawerTopView.draw(frame_num, _params=topviewParams) cv.imshow('nuScenes ' + scene_token, topView) cv.waitKey(0)
def __init__(self, vcd_file: Path): # vcd variables self._vcd = None self._vcd_file = str(vcd_file) self.__vcd_loaded = False # If vcd_file exists then load data into vcd object if vcd_file.exists(): # -- Load VCD from file -- # Create a VCD instance and load file self._vcd = core.VCD(file_name=self._vcd_file, validation=False) # vcd json is in self._vcd.data #Number of frames in video self.__full_mosaic_frames = int( self._vcd.get_frame_intervals().get_dict()[0]["frame_end"]) + 1 #Number of actions in vcd self.__num_actions = self._vcd.get_num_actions() print("There are %s actions in this VCD" % (self.__num_actions)) self.__vcd_loaded = True else: raise RuntimeError("VCD file not found.")
def update_vcd(self, annotations, validations): """ From an empty VCD, the annotation and validation vectors are parsed to VCD format. """ # Get total number of lines which is equivalent to total number of # frames input video assert (len(annotations) == len(validations)) total_frames = len(annotations) # 1.- Create VCD only with annotations and validations new_vcd = core.VCD() # 2.- VCD Name vcd_name = Path(self._vcd_file).stem new_vcd.add_name(str(vcd_name)) # 3.- Camera # Build Uri to video files general_uri = self._setUpManager._video_file_path general_video_descr = 'Unique general camera' new_vcd.add_stream('general_camera', str(general_uri), general_video_descr, core.StreamType.camera) # 4.- Stream Properties # Real Intrinsics of camera new_vcd.add_stream_properties(stream_name='general_camera', properties={ 'total_frames': total_frames, }) # 5.- Add annotations and validations vcd = self.add_annotations(new_vcd, annotations, validations, 0) # Update current VCD with newly created VCD self._vcd = vcd
def test_online_operation(self): # Simulate an online operation during 1000 frames # And cut-off every 100 frames into a new VCD vcds = [] uid = -1 for frame_num in range(0, 1000): if frame_num % 100 == 0: # Create new VCD vcds.append(core.VCD()) vcd_current = vcds[-1] # Optionally we could here dump into JSON file if frame_num == 0: uid = vcd_current.add_object( 'CARLOTA', 'Car') # leave VCD to assign uid = 0 vcd_current.add_object_data( uid, types.bbox('', (0, frame_num, 0, 0)), frame_num) else: uid = vcd_current.add_object( 'CARLOTA', 'Car', uid=uid) # tell VCD to use last_uid vcd_current.add_object_data( uid, types.bbox('', (0, frame_num, 0, 0)), frame_num) else: # Continue with current VCD vcd_current = vcds[-1] vcd_current.add_object_data( uid, types.bbox('', (0, frame_num, 0, 0)), frame_num) for vcd_this in vcds: self.assertEqual(vcd_this.get_num_objects(), 1) self.assertEqual(vcd_this.get_object(uid)['name'], 'CARLOTA') self.assertEqual(vcd_this.get_object(uid)['type'], 'Car')
def test_create_mesh_with_API_frames(self): # Load from previous test vcd = core.VCD('./etc/test_mesh.json') # Let's assume we know the structure of the mesh # # P0 L0 P1 L4 P4 # *--------*--------* # | | | # |L3 A0 |L1 A1 |L5 # | | | # *--------*--------* # P3 L2 P2 L6 P5 # # V0 [A0,A1] # Let's read mesh object # mesh1 = vcd.get_objects_with_object_data_name("parkslot1") mesh1 = vcd.get_object(0) # Let's read static object_data # od_mesh = mesh1.data['object_data']['mesh'] od_mesh = vcd.get_object_data(0, "parkslot1", frame_num=None) vertices = od_mesh['point3d'] edges = od_mesh['line_reference'] areas = od_mesh['area_reference'] # Let's add some frame-specific attributes (for line-reference and area-references) # Currently, only way to do it is by creating a new mesh object_data # and add it to the existing Object, specifying the frame for frame_num in range(0, 2): mesh1_frame = types.mesh("parkslot1") for line_key, line_val in edges.items(): # If we want to add a line_reference attribute: Reconstruct line_reference # Read existing info # "None" for val, so it is not copied at frame-lev. lX = types.lineReference( line_val['name'], reference_type=types.ObjectDataType.point3d, val=None) lX.add_attribute(types.text("PM_line_colour", "not set")) mesh1_frame.add_edge(lX, line_key) for area_key, area_val in areas.items(): # If we want to add an area_reference attribute: Reconstruct area_reference # Read existing info aX = types.areaReference( area_val['name'], reference_type=types.ObjectDataType.line_reference, val=None) aX.add_attribute(types.text("PM_park_slot_content", "Empty")) mesh1_frame.add_area(aX, area_key) vcd.add_object_data(0, mesh1_frame, frame_num) # Save it if not os.path.isfile('./etc/test_mesh_frame.json'): vcd.save('./etc/test_mesh_frame.json', True)
def test_object_change_from_static_to_dynamic(self): # Static->Dynamic # Case A) (when VCD has no other object with frames) vcd_a = core.VCD() uid_a = vcd_a.add_object("Enara", "Child") # Let's add some object data vcd_a.add_object_data(uid=uid_a, object_data=types.text(name="FavouriteColor", val="Pink")) self.assertEqual(vcd_a.get_object_data(uid=uid_a, data_name='FavouriteColor', frame_num=3), None) self.assertEqual(vcd_a.get_object_data(uid=uid_a, data_name='FavouriteColor')['val'], 'Pink') if not os.path.isfile('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_a_before.json'): vcd_a.save('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_a_before.json') # Let's modify the object so it has a certain frame interval (object_data frame intervals remain void) vcd_a.add_object(name="Enara", semantic_type="Child", uid=uid_a, frame_value=[(5, 10)]) if not os.path.isfile('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_a_after.json'): vcd_a.save('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_a_after.json') # Check that the element data is now also defined for this frame interval (and thus removed from the root) self.assertEqual(vcd_a.get_object_data(uid=uid_a, data_name='FavouriteColor', frame_num=3), None) self.assertEqual(vcd_a.get_object_data(uid=uid_a, data_name='FavouriteColor')['val'], 'Pink') self.assertEqual(vcd_a.get_object_data(uid=uid_a, data_name='FavouriteColor', frame_num=8)['val'], 'Pink') # Case B (when VCD has some other frame intervals already defined): VCD's get_element_data behaves differently vcd_b = core.VCD() vcd_b.add_object(name="room1", semantic_type="Room", frame_value=[(0, 10)]) uid_b = vcd_b.add_object(name="Enara", semantic_type="Child") vcd_b.add_object_data(uid=uid_b, object_data=types.text(name="FavouriteColor", val="Pink")) if not os.path.isfile('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_b_before.json'): vcd_b.save('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_b_before.json') # In this case, as the VCD has frames, the object is assumed to exist in all the scene # when the user asks for element_data at certain frame, VCD looks for element_data at that frame, and if there # is nothing, it then searches at the static part self.assertEqual(vcd_b.get_object_data(uid=uid_b, data_name='FavouriteColor', frame_num=3)['val'], 'Pink') self.assertEqual(vcd_b.get_object_data(uid=uid_b, data_name='FavouriteColor')['val'], 'Pink') vcd_b.add_object(name="Enara", semantic_type="Child", uid=uid_b, frame_value=[(5, 10)]) if not os.path.isfile('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_b_after.json'): vcd_b.save('./etc/' + vcd_version_name + '_test_object_change_from_static_to_dynamic_b_after.json') self.assertEqual(vcd_b.get_object_data(uid=uid_b, data_name='FavouriteColor', frame_num=3), None) self.assertEqual(vcd_b.get_object_data(uid=uid_b, data_name='FavouriteColor')['val'], 'Pink') self.assertEqual(vcd_b.get_object_data(uid=uid_b, data_name='FavouriteColor', frame_num=8)['val'], 'Pink')
def vcd_add_object_debug(): time_0 = time.time() vcd = core.VCD() for frame_num in range(0, 10000): if frame_num % 10 == 0: uid = vcd.add_object('CARLOTA' + str(frame_num), '#Car') vcd.add_object_data(uid, types.bbox("shape", (0, 0, 100, 200)), frame_value=frame_num) time_1 = time.time() elapsed_time_loop = time_1 - time_0 print("Loop: %s seconds. " % elapsed_time_loop)
def convert_town_center_to_VCD4(): if not os.path.isfile('./etc/TownCentreXVID_groundTruth.top'): import urllib.request url = 'https://www.robots.ox.ac.uk/ActiveVision/Research/Projects/2009bbenfold_headpose/Datasets/TownCentre-groundtruth.top' urllib.request.urlretrieve(url, './etc/TownCentreXVID_groundTruth.top') orig_file_name = "./etc/TownCentreXVID_groundTruth.top" vcd = core.VCD() with open(orig_file_name, newline='') as csvfile: my_reader = csv.reader(csvfile, delimiter=',') for row in my_reader: personNumber = int(row[0]) frameNumber = int(row[1]) headValid = int(row[2]) bodyValid = int(row[3]) headLeft = float(row[4]) headTop = float(row[5]) headRight = float(row[6]) headBottom = float(row[7]) headWidth = float((int(1000*headRight) - int(1000*headLeft))/1000) headHeight = float((int(1000*headBottom) - int(1000*headTop))/1000) bodyLeft = float(row[8]) bodyTop = float(row[9]) bodyRight = float(row[10]) bodyBottom = float(row[11]) bodyWidth = float((int(1000*bodyRight) - int(1000*bodyLeft))/1000) bodyHeight = float((int(1000*bodyBottom) - int(1000*bodyTop))/1000) body = types.bbox(name="body", val=(bodyLeft, bodyTop, bodyWidth, bodyHeight)) head = types.bbox("head", (headLeft, headTop, headWidth, headHeight)) if not vcd.has(core.ElementType.object, personNumber): vcd.add_object(name="", semantic_type="Pedestrian", uid=personNumber) if bodyValid: vcd.add_object_data(personNumber, body, frameNumber) if headValid: vcd.add_object_data(personNumber, head, frameNumber) else: if bodyValid: vcd.add_object_data(personNumber, body, frameNumber) if headValid: vcd.add_object_data(personNumber, head, frameNumber) vcd_json_file_name = "./etc/vcd420_towncenter.json" vcd.save(vcd_json_file_name, False) vcd_proto_file_name = "./etc/vcd420_proto_towncenter.txt" serializer.json2proto_bin(vcd_json_file_name, vcd_proto_file_name)
def test_element_data_nested_same_name(self): vcd = core.VCD() uid1 = vcd.add_object('mike', '#Pedestrian') body = types.bbox('body', (0, 0, 100, 150)) body.add_attribute(types.boolean('visible', True)) body.add_attribute(types.boolean('occluded', False)) body.add_attribute(types.boolean('visible', False)) # this is repeated, so it is substituted vcd.add_object_data(uid1, body, (0, 5)) #self.assertEqual(vcd.stringify(False), '{"vcd":{"frames":{"0":{"objects":{"0":{"object_data":{"bbox":[{"name":"body","val":[0,0,100,150],"attributes":{"boolean":[{"name":"visible","val":false},{"name":"occluded","val":false}]}}]}}}},"1":{"objects":{"0":{"object_data":{"bbox":[{"name":"body","val":[0,0,100,150],"attributes":{"boolean":[{"name":"visible","val":false},{"name":"occluded","val":false}]}}]}}}},"2":{"objects":{"0":{"object_data":{"bbox":[{"name":"body","val":[0,0,100,150],"attributes":{"boolean":[{"name":"visible","val":false},{"name":"occluded","val":false}]}}]}}}},"3":{"objects":{"0":{"object_data":{"bbox":[{"name":"body","val":[0,0,100,150],"attributes":{"boolean":[{"name":"visible","val":false},{"name":"occluded","val":false}]}}]}}}},"4":{"objects":{"0":{"object_data":{"bbox":[{"name":"body","val":[0,0,100,150],"attributes":{"boolean":[{"name":"visible","val":false},{"name":"occluded","val":false}]}}]}}}},"5":{"objects":{"0":{"object_data":{"bbox":[{"name":"body","val":[0,0,100,150],"attributes":{"boolean":[{"name":"visible","val":false},{"name":"occluded","val":false}]}}]}}}}},"schema_version":"4.3.0","frame_intervals":[{"frame_start":0,"frame_end":5}],"objects":{"0":{"name":"mike","type":"#Pedestrian","frame_intervals":[{"frame_start":0,"frame_end":5}],"object_data_pointers":{"body":{"type":"bbox","frame_intervals":[{"frame_start":0,"frame_end":5}],"attributes":{"visible":"boolean","occluded":"boolean"}}}}}}}') if not os.path.isfile('./etc/' + vcd_version_name + '_test_element_data_nested_same_name.json'): vcd.save('./etc/' + vcd_version_name + '_test_element_data_nested_same_name.json')
def test_create_search_simple(self): # 1.- Create a VCD instance vcd = core.VCD() # 2.- Create the Object uid_marcos = vcd.add_object(name='marcos', semantic_type="person") self.assertEqual(uid_marcos, "0", "Should be 0") # 3.- Add some data to the object vcd.add_object_data(uid=uid_marcos, object_data=types.bbox(name='head', val=(10, 10, 30, 30))) vcd.add_object_data(uid=uid_marcos, object_data=types.bbox(name='body', val=(0, 0, 60, 120))) vcd.add_object_data(uid=uid_marcos, object_data=types.vec(name='speed', val=(0.0, 0.2))) vcd.add_object_data(uid=uid_marcos, object_data=types.num(name='accel', val=0.1)) uid_peter = vcd.add_object(name='peter', semantic_type="person") vcd.add_object_data(uid=uid_peter, object_data=types.num(name='age', val=38.0)) vcd.add_object_data(uid=uid_peter, object_data=types.vec(name='eyeL', val=(0, 0, 10, 10))) vcd.add_object_data(uid=uid_peter, object_data=types.vec(name='eyeR', val=(0, 0, 10, 10))) # 4.- Write into string vcd_string_pretty = vcd.stringify() vcd_string_nopretty = vcd.stringify(False) # 5.- We can ask VCD marcos_ref = vcd.get_element(element_type=core.ElementType.object, uid=uid_marcos) # print('Found Object: uid = ', uid_marcos, ', name = ', marcosRef['name']) self.assertEqual(uid_marcos, "0", "Should be 0") self.assertEqual(marcos_ref['name'], 'marcos', "Should be marcos") peter_ref = vcd.get_element(element_type=core.ElementType.object, uid=uid_peter) # print('Found Object: uid = ', uid_peter, ', name = ', peterRef['name']) self.assertEqual(uid_peter, "1", "Should be 1") self.assertEqual(peter_ref['name'], 'peter', "Should be peter") # print('VCD string no pretty:\n', vcd_string_nopretty) # print('VCD string pretty:\n', vcd_string_pretty) if not os.path.isfile('./etc/' + vcd_version_name + '_test_create_search_simple_nopretty.json'): vcd.save('./etc/' + vcd_version_name + '_test_create_search_simple_nopretty.json') vcd_file_nopretty = open('./etc/' + vcd_version_name + '_test_create_search_simple_nopretty.json', "r") vcd_string_nopretty_read = vcd_file_nopretty.read() self.assertEqual(vcd_string_nopretty_read, vcd_string_nopretty, "VCD no-pretty not equal to read file") vcd_file_nopretty.close() if not os.path.isfile('./etc/' + vcd_version_name + '_test_create_search_simple_pretty.json'): vcd.save('./etc/' + vcd_version_name + '_test_create_search_simple_pretty.json', True) vcd_file_pretty = open('./etc/' + vcd_version_name + '_test_create_search_simple_pretty.json', "r") vcd_string_pretty_read = vcd_file_pretty.read() self.assertEqual(vcd_string_pretty, vcd_string_pretty_read, "VCD pretty not equal to read file") vcd_file_pretty.close()
def test_uuid_usage_explicit_1(self): vcd = core.VCD() uuid1 = str(uuid.uuid4()) # Adding an object and specifying its uid to be a previously defined UUID, from this call on VCD uses UUID uid1 = vcd.add_object(name='marcos', semantic_type='person', uid=uuid1) object = vcd.get_object(uid1) self.assertEqual(object['name'], 'marcos') uid2 = vcd.add_object(name='orti', semantic_type='person') matches = bool( re.match( r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", uid2)) self.assertEqual(matches, True)
def test_multi_value_attributes(self): # This test shows how to use "Vec" attribute with strings vcd = core.VCD() uid = vcd.add_object(name="car1", semantic_type="car") vcd.add_object_data(uid=uid, object_data=types.vec(name="signals", val=[0.1, 0.2, 0.3])) vcd.add_object_data(uid=uid, object_data=types.vec(name="categories", val=["Tourism", "Large", "Old"])) # Add additionalPropertis at attributes (apart from "name" and "val"), e.g. "quality", "locked", etc # Works for any attribute type (vec, bbox, etc) vcd.add_object_data(uid=uid, object_data=types.vec(name="shape", val=[0, 0, 100, 100], properties={"locked": True, "score": 0.8})) vcd.add_object_data(uid=uid, object_data=types.text(name="color", val="Blue", properties={"locked": False, "status": "validated"})) if not os.path.isfile('./etc/' + vcd_version_name + '_test_multi_value_attributes.json'): vcd.save('./etc/' + vcd_version_name + '_test_multi_value_attributes.json', True) vcd_read = core.VCD('./etc/' + vcd_version_name + '_test_multi_value_attributes.json', validation=True) vcd_read_stringified = vcd_read.stringify() vcd_stringified = vcd.stringify() #print(vcd_stringified) self.assertEqual(vcd_read_stringified, vcd_stringified)
def test_element_data_same_name(self): vcd = core.VCD() uid1 = vcd.add_action('', '#Walking') vcd.add_action_data(uid1, types.boolean('validated', True), (0, 5)) vcd.add_action_data(uid1, types.boolean('occluded', False), (0, 5)) vcd.add_action_data(uid1, types.text('label', 'manual'), (0, 5)) # Now try to add an Action Data with the same name vcd.add_action_data(uid1, types.boolean('validated', False), (0, 5)) # The initial 'validated' Boolean, with value true is substituted by false, instead of added #self.assertEqual(vcd.stringify(False), '{"vcd":{"frames":{"0":{"actions":{"0":{"action_data":{"boolean":[{"name":"validated","val":false},{"name":"occluded","val":false}],"text":[{"name":"label","val":"manual"}]}}}},"1":{"actions":{"0":{"action_data":{"boolean":[{"name":"validated","val":false},{"name":"occluded","val":false}],"text":[{"name":"label","val":"manual"}]}}}},"2":{"actions":{"0":{"action_data":{"boolean":[{"name":"validated","val":false},{"name":"occluded","val":false}],"text":[{"name":"label","val":"manual"}]}}}},"3":{"actions":{"0":{"action_data":{"boolean":[{"name":"validated","val":false},{"name":"occluded","val":false}],"text":[{"name":"label","val":"manual"}]}}}},"4":{"actions":{"0":{"action_data":{"boolean":[{"name":"validated","val":false},{"name":"occluded","val":false}],"text":[{"name":"label","val":"manual"}]}}}},"5":{"actions":{"0":{"action_data":{"boolean":[{"name":"validated","val":false},{"name":"occluded","val":false}],"text":[{"name":"label","val":"manual"}]}}}}},"schema_version":"4.3.0","frame_intervals":[{"frame_start":0,"frame_end":5}],"actions":{"0":{"name":"","type":"#Walking","frame_intervals":[{"frame_start":0,"frame_end":5}],"action_data_pointers":{"validated":{"type":"boolean","frame_intervals":[{"frame_start":0,"frame_end":5}]},"occluded":{"type":"boolean","frame_intervals":[{"frame_start":0,"frame_end":5}]},"label":{"type":"text","frame_intervals":[{"frame_start":0,"frame_end":5}]}}}}}}') if not os.path.isfile('./etc/' + vcd_version_name + '_test_element_data_same_name.json'): vcd.save('./etc/' + vcd_version_name + '_test_element_data_same_name.json')
def test_object_change_from_dynamic_to_static(self): vcd = core.VCD() uid1 = vcd.add_object("Enara", "Child") vcd.add_object_data(uid=uid1, object_data=types.text(name="FavouriteColor", val="Pink")) vcd.add_object_data(uid=uid1, object_data=types.vec(name="Position", val=(1.0, 5.0)), frame_value=8) if not os.path.isfile('./etc/' + vcd_version_name + '_test_object_change_from_dynamic_to_static_before.json'): vcd.save('./etc/' + vcd_version_name + '_test_object_change_from_dynamic_to_static_before.json') vcd.add_object(name="Enara", semantic_type="Child", uid=uid1, set_mode=core.SetMode.replace) if not os.path.isfile('./etc/' + vcd_version_name + '_test_object_change_from_dynamic_to_static_after.json'): vcd.save('./etc/' + vcd_version_name + '_test_object_change_from_dynamic_to_static_after.json') self.assertEqual(vcd.get_object_data(uid=uid1, data_name='FavouriteColor', frame_num=8), None) self.assertEqual(vcd.get_object_data(uid=uid1, data_name='FavouriteColor')['val'], 'Pink') self.assertEqual(vcd.get_object_data(uid=uid1, data_name='Position', frame_num=8), None) self.assertEqual(vcd.get_object_data(uid=uid1, data_name='Position'), None)
def test_metadata(self): vcd = core.VCD() annotator = "Algorithm001" comment = "Annotations produced automatically - SW v0.1" vcd.add_annotator(annotator) vcd.add_comment(comment) # TODO: vcd.add_metadata_properties (a dictionary just like frame_properties) self.assertEqual(vcd.get_metadata()['annotator'], annotator) self.assertEqual(vcd.get_metadata()['comment'], comment) if not os.path.isfile('./etc/test_add_metadata.json'): vcd.save('./etc/test_add_metadata.json')
def get_info_from_VCD(self, vcd_file_copy, staticDict, ctx_id): #load vcd copy_vcd = core.VCD(file_name=vcd_file_copy, validation=False) self._vcd_loaded = True #get shifts body_face_sh = self.get_shift_in_vcd(copy_vcd, "body_camera") hands_face_sh = self.get_shift_in_vcd(copy_vcd, "hands_camera") hands_body_sh = hands_face_sh - body_face_sh #get statics static = self.get_static_in_vcd(copy_vcd, staticDict, ctx_id) #get metadata metadata = self.get_metadata_in_vcd(copy_vcd, ctx_id) #Turn to false to save self._vcd_loaded = False return body_face_sh, hands_body_sh, static, metadata
def __init__(self, setUpManager: ConfigTato): self._setUpManager = setUpManager # Get TaTo annotation mode self._annotation_mode = self._setUpManager._annotation_mode # Internal Variables self._vcd = None self._vcd_file = self._setUpManager._vcd_file_path self._vcd_loaded = False # Get dictionary information self._dict_file = self._setUpManager._config_json self._dicts, self._annotation_levels, \ self._default_levels, self._annotation_types, \ self._level_labels, self._camera_dependencies, \ self._total_levels = self._setUpManager.get_annotation_config() # Dictionary that contains the self._statics_dict = self._setUpManager.get_statics_dict() self._annotation_levels = self._annotation_levels.items() self._default_levels = self._default_levels.items() self._annotation_types = self._annotation_types.items() # If vcd_file exists then load data from file if self._vcd_file.exists(): print("VCD exists") # Create a VCD instance and load file self._vcd = core.VCD(file_name=self._vcd_file, validation=False) self._vcd_loaded = True else: # Create Empty VCD self._vcd = core.VCD() self._vcd_loaded = False
def test_ontology_list(self): vcd = core.VCD() ont_uid_1 = vcd.add_ontology( "http://www.vicomtech.org/viulib/ontology") ont_uid_2 = vcd.add_ontology("http://www.alternativeURL.org/ontology") # Let's create an object with a pointer to the ontology uid_car = vcd.add_object('CARLOTA', '#Car', frame_value=None, uid=None, ont_uid=ont_uid_1) vcd.add_object_data(uid_car, types.text('brand', 'Toyota')) vcd.add_object_data(uid_car, types.text('model', 'Prius')) uid_marcos = vcd.add_object('Marcos', '#Person', frame_value=None, uid=None, ont_uid=ont_uid_2) vcd.add_object_data(uid_marcos, types.bbox('head', (10, 10, 30, 30)), (2, 4)) self.assertEqual(vcd.get_object(uid_car)['ontology_uid'], ont_uid_1) self.assertEqual(vcd.get_object(uid_marcos)['ontology_uid'], ont_uid_2) self.assertEqual(vcd.get_ontology(ont_uid_1), "http://www.vicomtech.org/viulib/ontology") self.assertEqual(vcd.get_ontology(ont_uid_2), "http://www.alternativeURL.org/ontology") if not os.path.isfile('./etc/vcd430_test_ontology.json'): vcd.save('./etc/vcd430_test_ontology.json', True) vcd_read = core.VCD('./etc/vcd430_test_ontology.json', validation=True) self.assertEqual(vcd_read.stringify(), vcd.stringify())
def test_create_image_png(self): # 1.- Create a VCD instance vcd = core.VCD() # 2.- Create image colors = [(125, 32, 64), (98, 12, 65), (12, 200, 190)] classes = ["class1", "class2", "class3"] classes_colors = dict(zip(classes, colors)) img = draw_basic_image(classes_colors) # cv.imshow('src_image', img) # cv.waitKey(1) # 3.- Encode compr_params = [int(cv.IMWRITE_PNG_COMPRESSION), 9] result, payload = cv.imencode('.png', img, compr_params) self.assertEqual(result, True) # 4.- Convert to base64 payload_b64_bytes = base64.b64encode( payload) # starts with b' (NOT SERIALIZABLE!) payload_b64_str = str(base64.b64encode(payload), 'utf-8') # starts with s' # 5.- Insert into VCD vcd_image = types.image('labels', payload_b64_str, 'image/png', 'base64') uid = vcd.add_object('', '') vcd.add_object_data(uid, vcd_image) # 6.- Get and decode od = vcd.get_object_data(uid, 'labels') mime_type = od['mime_type'] encoding = od['encoding'] payload_b64_read = od['val'] payload_read = base64.b64decode(payload_b64_read) self.assertEqual(mime_type, 'image/png') self.assertEqual(encoding, 'base64') img_dec = cv.imdecode(np.frombuffer(payload_read, dtype=np.uint8), 1) # Check equals diff_val = np.sum(cv.absdiff(img, img_dec)) self.assertEqual(diff_val, 0) if not os.path.isfile('./etc/' + vcd_version_name + '_test_image.json'): vcd.save('./etc/' + vcd_version_name + '_test_image.json', True)
def test_remove_simple(self): # 1.- Create VCD vcd = core.VCD() # 2.- Create some objects car1_uid = vcd.add_object(name='BMW', semantic_type='#Car') car2_uid = vcd.add_object(name='Seat', semantic_type='#Car') person1_uid = vcd.add_object(name='John', semantic_type='#Pedestrian') trafficSign1UID = vcd.add_object(name='', semantic_type='#StopSign') # 3.- Add some content # Same FrameInterval (0, 5) vcd.add_object_data(uid=person1_uid, object_data=types.bbox('face', (0, 0, 100, 100)), frame_value=(0, 5)) vcd.add_object_data(uid=person1_uid, object_data=types.bbox('mouth', (0, 0, 10, 10)), frame_value=(0, 5)) vcd.add_object_data(uid=person1_uid, object_data=types.bbox('hand', (0, 0, 30, 30)), frame_value=(0, 5)) vcd.add_object_data(uid=person1_uid, object_data=types.bbox('eyeL', (0, 0, 10, 10)), frame_value=(0, 5)) vcd.add_object_data(uid=person1_uid, object_data=types.bbox('eyeR', (0, 0, 10, 10)), frame_value=(0, 5)) # A different FrameInterval (0, 10) vcd.add_object_data(uid=person1_uid, object_data=types.num('age', 35.0), frame_value=(0, 10)) # Data for the other objects vcd.add_object_data(uid=car1_uid, object_data=types.bbox('position', (100, 100, 200, 400)), frame_value=(0, 10)) vcd.add_object_data(uid=car1_uid, object_data=types.text('color', 'red'), frame_value=(6, 10)) vcd.add_object_data(uid=car2_uid, object_data=types.bbox('position', (300, 1000, 200, 400)), frame_value=(0, 10)) vcd.add_object_data(uid=trafficSign1UID, object_data=types.boolean('visible', True), frame_value=(0, 4)) # print("Frame 5, dynamic only message: ", vcd.stringify_frame(5, dynamic_only=True)) # print("Frame 5, full message: ", vcd.stringify_frame(5, dynamic_only=False)) if not os.path.isfile('./etc/' + vcd_version_name + '_test_remove_simple.json'): vcd.save('./etc/' + vcd_version_name + '_test_remove_simple.json') self.assertEqual(vcd.get_num_objects(), 4, "Should be 4") # 4.- Delete some content vcd.rm_object(uid=car2_uid) self.assertEqual(vcd.get_num_objects(), 3, "Should be 3") vcd.rm_object_by_type(semantic_type='#StopSign') self.assertEqual(vcd.get_num_objects(), 2, "Should be 2") # 5.- Remove all content sequentially vcd.rm_object(uid=person1_uid) self.assertEqual(vcd.get_num_objects(), 1, "Should be 1") vcd.rm_object(uid=car1_uid) self.assertEqual(vcd.get_num_objects(), 0, "Should be 0") self.assertEqual(vcd.get_frame_intervals().empty(), True)
def test_static_dynamic_object_2(self): # 1.- Create a VCD instance vcd = core.VCD() # 2.- Create a dynamic object with static information # The attribute is added ot the objects section uid1 = vcd.add_object(name='line1', semantic_type='#BottsDots', frame_value=(5, 10)) poly = types.poly2d(name='poly', val=(100, 100, 110, 110, 120, 130, 500, 560), mode=types.Poly2DType.MODE_POLY2D_ABSOLUTE, closed=False) poly.add_attribute(object_data=types.text(name='type', val='single_dot')) vcd.add_object_data(uid=uid1, object_data=poly) #print(vcd.stringify(False)) #self.assertEqual(vcd.stringify(False), '{"vcd":{"frames":{"5":{"objects":{"0":{}}},"6":{"objects":{"0":{}}},"7":{"objects":{"0":{}}},"8":{"objects":{"0":{}}},"9":{"objects":{"0":{}}},"10":{"objects":{"0":{}}}},"schema_version":"4.3.0","frame_intervals":[{"frame_start":5,"frame_end":10}],"objects":{"0":{"name":"line1","type":"#BottsDots","frame_intervals":[{"frame_start":5,"frame_end":10}],"object_data":{"poly2d":[{"name":"poly","val":[100,100,110,110,120,130,500,560],"mode":"MODE_POLY2D_ABSOLUTE","closed":false,"attributes":{"text":[{"name":"type","val":"single_dot"}]}}]},"object_data_pointers":{"poly":{"type":"poly2d","frame_intervals":[],"attributes":{"type":"text"}}}}}}}') if not os.path.isfile('./etc/' + vcd_version_name + '_test_static_dynamic_object_2.json'): vcd.save('./etc/' + vcd_version_name + '_test_static_dynamic_object_2.json')