Project

General

Profile

How to read XmpBag / XmpSeq / hierarchical structured XmpData the easy way

Added by Benjamin H. almost 7 years ago

... without manually walking through the (numbered) elements of a complex metadata object by trial and error?

How can I iterate (complex) elements of an XmpBag or XmpSeq via exiv2?I want to read following hierarchical structured data with C++. These are my first steps with C++, so excuse if I just did not understand API. However, I am wondering that all elements I get via my current iterator below are of type XmpText. I expected to either find the bag as type, but "Bag" is just contained in the value of elements. Maybe the type is not correctly set when reading metadata? Or why is done this way and how can I optimally iterate through the data like: for element in Bag, do ...

XMP data

   <MP:RegionInfo rdf:parseType="Resource">
    <MPRI:Regions>
     <rdf:Bag>
      <rdf:li
       MPReg:Rectangle="0.661608, 0.244310, 0.130501, 0.261002" 
       MPReg:PersonDisplayName="Baby Tux"/>
      <rdf:li
       MPReg:Rectangle="0.205615, 0.226100, 0.130501, 0.261002" 
       MPReg:PersonDisplayName="Baby Gnu"/>
     </rdf:Bag>
    </MPRI:Regions>
   </MP:RegionInfo>

simple iteration
 for (Exiv2::XmpData::iterator iterator = xmpData.begin(); iterator != xmpData.end(); iterator++) {
  std::cout << iterator->key() << " -- " << iterator->tagLabel() << " -- " << iterator->value() << " (" << iterator->typeName() << " -- " << iterator->count() << ")" << std::endl;
 } 

outut
Xmp.MP.RegionInfo -- RegionInfo -- type="Struct" (XmpText -- 0)
Xmp.MP.RegionInfo/MPRI:Regions -- Regions -- type="Bag" (XmpText -- 0)
Xmp.MP.RegionInfo/MPRI:Regions[1] -- RegionInfo/MPRI:Regions[1] -- type="Struct" (XmpText -- 0)
Xmp.MP.RegionInfo/MPRI:Regions[1]/MPReg:Rectangle -- Rectangle -- 0.661608, 0.244310, 0.130501, 0.261002 (XmpText -- 38)
Xmp.MP.RegionInfo/MPRI:Regions[1]/MPReg:PersonDisplayName -- PersonDisplayName -- Baby Tux (XmpText -- 8)
Xmp.MP.RegionInfo/MPRI:Regions[2] -- RegionInfo/MPRI:Regions[2] -- type="Struct" (XmpText -- 0)
Xmp.MP.RegionInfo/MPRI:Regions[2]/MPReg:Rectangle -- Rectangle -- 0.205615, 0.226100, 0.130501, 0.261002 (XmpText -- 38)
Xmp.MP.RegionInfo/MPRI:Regions[2]/MPReg:PersonDisplayName -- PersonDisplayName -- Baby Gnu (XmpText -- 8)


Replies (5)

Example image - Added by Benjamin H. almost 7 years ago

The example image above is BabyGnuTux-BigWLPG.jpg

RE: How to read XmpBag / XmpSeq / hierarchical structured XmpData the easy way - Added by Robin Mills almost 7 years ago

Benjamin

I'm the build/test engineer for Exiv2. Although I have been contributing to Exiv2 for almost 7 years, I didn't write Exiv2 and I am not an expert on either metadata nor the Exiv2 APIs.

I understand however that Exiv2 iterator API is effectively a "linear-list" model and very effective for the dictionaries of key/values used in the Exif 2.x standards. From the output you have shown, I think you can build a recursive STL data structure using our API and the result will be similar to a DOM.

For highly structured data, it may be better to extract the XMP payload and process it with a DOM/XML parser. Here is a forum discussion about locating the metadata in an image: http://dev.exiv2.org/boards/3/topics/1608?r=1624#message-1624

Another team member may be able to provide more insight into this - however this reply, regrettably, reaches the limits of my knowledge on this topic.

Robin

RE: How to read XmpBag / XmpSeq / hierarchical structured XmpData the easy way - Added by Robin Mills over 6 years ago

Benjamin

Mikeyel has asked a question which I think it the same as yours. And I couldn't answer his question either. Perhaps you and Mikayel can cooperate to find a solution. http://dev.exiv2.org/boards/3/topics/1951

I'm willing to help you both. However, I honestly don't know what's being discussed here.

Alan Pater asked recently about converting ACDC Category Tags into Adobe Lightroom Hierarchy Tags. I was able to give him a little Perl script to perform the transformation outside of Exiv2. http://dev.exiv2.org/boards/3/topics/1912

I think that topic is a little away from your question - however perhaps it's a helpful suggestion.

RE: How to read XmpBag / XmpSeq / hierarchical structured XmpData the easy way - Added by Robin Mills over 6 years ago

I have added a new option to exiv2(.exe) -pX to extract the XMP block from an image. With this option, you can easily extract the xml and process it with xmllint, perl, python, xslt or any external tool. r3650

515 rmills@rmillsmbp:~/clanmills $ exiv2 -pX ~/Downloads/BabyGnuTux-BigWLPG.jpg  | xmllint --pretty 1 --noblanks -
<?xml version="1.0"?>
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.0-c060 61.134777, 2010/02/12-17:32:00        ">
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stEvt="http://ns.adobe.com/xap/1.0/sType/ResourceEvent#" rdf:about="" xmp:CreatorTool="Adobe Photoshop CS5 Windows" xmp:CreateDate="2012-01-04T20:32:52+01:00" xmp:ModifyDate="2012-01-04T20:34:45+01:00" xmp:MetadataDate="2012-01-04T20:34:45+01:00" dc:format="image/jpeg" photoshop:ColorMode="3" photoshop:ICCProfile="sRGB IEC61966-2.1" xmpMM:InstanceID="xmp.iid:4E9C1E280B37E111A568FF94F417581A" xmpMM:DocumentID="xmp.did:4D9C1E280B37E111A568FF94F417581A" xmpMM:OriginalDocumentID="xmp.did:4D9C1E280B37E111A568FF94F417581A">
      <xmpMM:History>
        <rdf:Seq>
          <rdf:li stEvt:action="created" stEvt:instanceID="xmp.iid:4D9C1E280B37E111A568FF94F417581A" stEvt:when="2012-01-04T20:32:52+01:00" stEvt:softwareAgent="Adobe Photoshop CS5 Windows"/>
          <rdf:li stEvt:action="converted" stEvt:parameters="from image/png to image/jpeg"/>
          <rdf:li stEvt:action="saved" stEvt:instanceID="xmp.iid:4E9C1E280B37E111A568FF94F417581A" stEvt:when="2012-01-04T20:34:45+01:00" stEvt:softwareAgent="Adobe Photoshop CS5 Windows" stEvt:changed="/"/>
        </rdf:Seq>
      </xmpMM:History>
    </rdf:Description>
    <rdf:Description xmlns:MP="http://ns.microsoft.com/photo/1.2/">
      <MP:RegionInfo>
        <rdf:Description xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
          <MPRI:Regions xmlns:MPRI="http://ns.microsoft.com/photo/1.2/t/RegionInfo#">
            <rdf:Bag xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
              <rdf:li>
                <rdf:Description xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
                  <MPReg:Rectangle xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">0.661608, 0.244310, 0.130501, 0.261002</MPReg:Rectangle>
                  <MPReg:PersonDisplayName xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">Baby Tux</MPReg:PersonDisplayName>
                </rdf:Description>
              </rdf:li>
              <rdf:li>
                <rdf:Description xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
                  <MPReg:Rectangle xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">0.205615, 0.226100, 0.130501, 0.261002</MPReg:Rectangle>
                  <MPReg:PersonDisplayName xmlns:MPReg="http://ns.microsoft.com/photo/1.2/t/Region#">Baby Gnu</MPReg:PersonDisplayName>
                </rdf:Description>
              </rdf:li>
            </rdf:Bag>
          </MPRI:Regions>
        </rdf:Description>
      </MP:RegionInfo>
    </rdf:Description>
  </rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>
516 rmills@rmillsmbp:~/clanmills $ 

RE: How to read XmpBag / XmpSeq / hierarchical structured XmpData the easy way - Added by Robin Mills over 6 years ago

Benjamin

I have overhauled exiv2json.cpp to build a deeply recursive data structure using Jzon. You can walk the elements in C++ using the Jzon code built into libexiv2, or you can serialize and read it with your favourite JSON parser. r1054.

Robin

    (1-5/5)