18 from typing
import Optional, Dict, Tuple
20 import xml.etree.ElementTree
as ET
23 def getTag(root: ET.Element, tag: str) -> Optional[str]:
24 element = root.find(tag)
31 def toFloat(rootEl: ET.Element, firstEl: str, secondEl: str) -> Tuple[Optional[float], Optional[float]]:
32 firstVal = getTag(rootEl, firstEl)
33 secondVal = getTag(rootEl, secondEl)
35 if firstVal
is None or secondVal
is None:
38 return (float(firstVal), float(secondVal))
41 def toInt(rootEl: ET.Element, firstEl: str, secondEl: str) -> Tuple[Optional[int], Optional[int]]:
42 firstVal = getTag(rootEl, firstEl)
43 secondVal = getTag(rootEl, secondEl)
45 if firstVal
is None or secondVal
is None:
48 return (int(firstVal), int(secondVal))
51 def getBoxes(bndbox: ET.Element) -> Optional[Dict[str, float]]:
52 xmin, ymin = toFloat(bndbox,
"xmin",
"ymin")
53 xmax, ymax = toFloat(bndbox,
"xmax",
"ymax")
55 if xmax
is None:
return None
56 if xmin
is None:
return None
57 if ymax
is None:
return None
58 if ymin
is None:
return None
64 "height": ymax - ymin,