IOLink Python 1.11.0
Loading...
Searching...
No Matches
metadataTree.py

This code demonstrates how to store, modify and parse metadata in ImageViews.

This code demonstrates how to store, modify and parse metadata in ImageViews. An ImageView is created and metadata are stored into it. Then the metadata are modified and parsed.

1import iolink
2
3
7
8# method to create a metadata tree and assign it to given image
9def add_metadata(image: iolink.ImageView):
10 # if given image does not have write capability, metadata cannot be added
11 if not image.support(iolink.ImageCapability.WRITE):
12 raise AttributeError("Image does not have WRITE capability")
13
14 # create a metadata tree as following
15 # root
16 # |- "name" : "generated image for example"
17 # |- "author" : "Arthur Conley"
18 # |- "creation_date" : "2024-08-23"
19 # |- "parameters"
20 # |- "param1" : 42
21 # |- "param2" : 3.14
22 # |- "param3" : "hello world!"
23 # |- "version" : 1
24
25 # Nodes are created one by one
26 root = iolink.MetadataNodeFactory.create("root", None)
27 nameNode = iolink.MetadataNodeFactory.create("name", iolink.VariantDataValueFactory.create("generated image for example"))
28 authorNode = iolink.MetadataNodeFactory.create("author", iolink.VariantDataValueFactory.create("Arthur Conley"))
29 createDateNode = iolink.MetadataNodeFactory.create("creation_date", iolink.VariantDataValueFactory.create("2024-08-23"))
30
31 root.add_child(nameNode)
32 root.add_child(authorNode)
33 root.add_child(createDateNode)
34
35 # you can also use the helper class to create the tree
36 # the helper class is more convenient to create trees with complex hierarchies
37 iolink.MetadataNodeHelper.create_path(root, "parameters/param1", iolink.VariantDataValueFactory.create(42))
38 iolink.MetadataNodeHelper.create_path(root, "parameters/param2", iolink.VariantDataValueFactory.create(3.14))
39 iolink.MetadataNodeHelper.create_path(root, "parameters/param3", iolink.VariantDataValueFactory.create("Hello world!"));
40
41 iolink.MetadataNodeHelper.create_path(root, "parameters/version", iolink.VariantDataValueFactory.create(1))
42
43 # assign the metadata tree to the image
44 image.metadata = root
45
46# increment 'version' value in metadata of given image
47def increment_version_metadata(image: iolink.ImageView):
48 # if given image does not have WRITE capability, metadata cannot be updated
49 if not image.support(iolink.ImageCapability.WRITE):
50 raise AttributeError("Image does not have WRITE capability")
51
52 # the node to be updated cannot be modified directly
53 # you need to create a new node with the same name and replace the old one
54 root = image.metadata
55
56 # retrieve version number
57 versionValue = iolink.MetadataNodeHelper.get_node(root, "parameters/version")
58
59 # if version node does not exist, raise an error
60 if versionValue is None:
61 raise AttributeError("version node does not exist")
62
63 version = versionValue.value[0]
64
65 # clone the tree for modification
66 rootCloned = root.clone()
67 iolink.MetadataNodeHelper.create_path(rootCloned, "parameters/version", iolink.VariantDataValueFactory.create(version + 1))
68
69 # assign the new tree including modification to the image
70 image.metadata = rootCloned
71
72# recursive method to display metadata tree
73def recursive_display_method(root: iolink.MetadataNode, level: int):
74 indent = " " * level
75 print(indent, root.key, ":", end='')
76
77 # if the node has a value, display it
78 if root.value is not None:
79 print(root.value[0], end='')
80 print("")
81
82 for child in root:
83 recursive_display_method(child, level + 1)
84
85# display metadata tree of given image
86def display_metadata(image:iolink.ImageView):
87 if image.metadata is None:
88 print("No metadata to dislay")
89 return
90
91 root = image.metadata
92 print("Metadata tree:")
93 recursive_display_method(root, 0)
94
95
96# create an image view in CPU memory
97image = iolink.ImageViewFactory.allocate((100, 200), iolink.DataTypeId.UINT8)
98
99# add metadata to the image
100add_metadata(image)
101
102# display metadata tree
103display_metadata(image)
104
105# do some modifications in metadata
106increment_version_metadata(image)
107
108# display metadata tree after modification
109display_metadata(image)
110
111print("SUCCESS")