Ejemplo n.º 1
0
def step_impl(context):  # noqa: F811
    m = GetAsset(context.macrosrc)
    m.root.command = objectify.StringElement(git_comment_str + '\n' +
                                             m.root.command.text)
    context.macroname = random_string()
    m.assemble(context.macroname)
    assert os.path.exists(
        context.macrofile), f"{context.macrofile} not found in '{os.getcwd()}'"
Ejemplo n.º 2
0
def cropAnnotations(_ann, roi, filename, minsize=4):
    '''
    For a given roi and annotation, returns a cropped annotation that contains all bounding
    boxes that can fit within the specified roi. If a bounding box is too small after
    cropping, it is discarded.
    Parameters:
    ann: objectified annotation xml
    roi: Input region of interest as [ymin,xmin,ymax,xmax]
    filename: New file name to use instead of the old one
    minsize: Resultant bounding boxes under this size (width or height) are discarded
    '''
    ymin, xmin, ymax, xmax = [int(f) for f in roi]

    ann = copy.deepcopy(_ann)
    ## Change the size based on RoI
    ann.size.width = objectify.StringElement(str(xmax - xmin))
    ann.size.height = objectify.StringElement(str(ymax - ymin))
    folder = ann.folder
    ann.filename = objectify.StringElement(filename)
    filepath = os.path.join(str(folder), 'JPEGImages', str(filename))
    ann.path = objectify.StringElement(filepath)

    for obj in ann.iter('object'):
        b_xmin = obj.bndbox.xmin
        b_ymin = obj.bndbox.ymin
        b_xmax = obj.bndbox.xmax
        b_ymax = obj.bndbox.ymax
        # Clamp the bbox to RoI to make things simpler (this crops the bbox to remain within RoI)
        if b_xmin < xmin: b_xmin = xmin
        if b_ymin < ymin: b_xmin = ymin
        if b_xmax > xmax: b_xmax = xmax
        if b_ymax > ymax: b_xmax = ymax

        if (  # T-L within RoI?
            (xmin <= b_xmin and b_xmin <= xmax and ymin <= b_ymin
             and b_ymin <= ymax) and
                # B-R within RoI?
            (xmin <= b_xmax and b_xmax <= xmax and ymin <= b_ymax
             and b_ymax <= ymax) and
                # minsize criteria met?
            ((b_xmax - b_xmin) >= minsize) and ((b_ymax - b_ymin) >= minsize)):
            ## The [cropped] bbox is within the RoI. Now we only have to translate to (xmin,ymin) as origin
            obj.bndbox.xmin = objectify.StringElement(str(b_xmin - xmin))
            obj.bndbox.ymin = objectify.StringElement(str(b_ymin - ymin))
            obj.bndbox.xmax = objectify.StringElement(str(b_xmax - xmin))
            obj.bndbox.ymax = objectify.StringElement(str(b_ymax - ymin))
        else:
            ##--## Discard the obj --> Check this manually
            ##--obj_xml = etree.tostring(obj, pretty_print=True, xml_declaration=False)
            ##--print("Deleting object {}".format(obj_xml))
            obj.getparent().remove(obj)

    return ann
Ejemplo n.º 3
0
 def test_token_assemble_sha_gmname(self, tmpdir):
     tokenname = 'Lib:' + random_string()
     tokennameq = tokenname.replace(':', '-')
     m = GetAsset('MVToken')
     assert m is not None
     m.root.name = objectify.StringElement(tokenname)
     m.assemble(tokennameq)
     m = GetAsset(MacroNameQuote(tokennameq + '.' + tagset.token.ext))
     assert m is not None
     assert 'gmName' in [x.tag for x in m.root.iterchildren()]
     assert m.root.gmName == git_tag_str
Ejemplo n.º 4
0
def step_impl(context):  # noqa: F811
    context.tokensrc = context.source['token']['src'][1]
    context.tokenfile = context.source['token']['rptok'][1]
    content_xml = os.path.join(context.tokensrc, 'content.xml')
    cxml = objectify.parse(content_xml)
    for m in cxml.findall('macroPropertiesMap/entry/' + tagset.macro.tag):
        m.command = objectify.StringElement(git_comment_str + '\n' +
                                            m.command.text)
    cxml.write(content_xml, pretty_print=True)
    run_assemble(context, context.tokensrc)
    assert b'Error' not in context.stderr, f"{context.stderr} in\tmpdir = '{os.getcwd()}'"
 def test_macro_extract_git_sha_remove(self, tmpdir):
     macroname = random_string()
     m = GetAsset('macro/MVMacro1')
     assert m is not None
     m.root.label = objectify.StringElement(macroname)
     m.assemble(save_name=macroname)
     m = GetAsset(macroname + '.' + tagset.macro.ext)
     assert m is not None
     assert m.is_macro
     m.extract()
     assert os.path.exists(f'macro/{macroname}.xml'), f'expected macro/{macroname}.xml in {glob("**/*")=}'
     assert os.path.exists(f'macro/{macroname}.command'), f'{glob("**/*")=}'
Ejemplo n.º 6
0
def scaleAnnotations(ann, SCALE):
    ## Change the size based on scale
    ann.size.width = objectify.StringElement(str(int(ann.size.width * SCALE)))
    ann.size.height = objectify.StringElement(str(int(ann.size.height *
                                                      SCALE)))
    folder = ann.folder
    filename = ann.filename
    filepath = os.path.join(str(folder), 'JPEGImages', str(filename))
    ann.path = objectify.StringElement(filepath)
    for obj in ann.iter('object'):
        obj.bndbox.xmin = objectify.StringElement(str(obj.bndbox.xmin * SCALE))
        obj.bndbox.ymin = objectify.StringElement(str(obj.bndbox.ymin * SCALE))
        obj.bndbox.xmax = objectify.StringElement(str(obj.bndbox.xmax * SCALE))
        obj.bndbox.ymax = objectify.StringElement(str(obj.bndbox.ymax * SCALE))

    return ann
Ejemplo n.º 7
0
def EventIvorn(ivorn, cite_type):
    """
    Used to cite earlier VOEvents.

    Use in conjunction with :func:`.add_citations`

    Args:
        ivorn(str): It is assumed this will be copied verbatim from elsewhere,
            and so these should have any prefix (e.g. 'ivo://','http://')
            already in place - the function will not alter the value.
        cite_type (:class:`.definitions.cite_types`): String conforming to one
            of the standard citation types.

    """
    # This is an ugly hack around the limitations of the  lxml.objectify API:
    c = objectify.StringElement(cite=cite_type)
    c._setText(ivorn)
    c.tag = "EventIVORN"
    return c
Ejemplo n.º 8
0
def readAndScaleAnnotations(i_anndir, imgbase, SCALE):
    ## Load annotations file
    i_annfile = os.path.join(i_anndir, imgbase + ".xml")
    #print("Input annotation file: {}".format(i_annfile))
    with open(i_annfile) as f:
        xml = f.read()
    ann = objectify.fromstring(xml)

    ## Change the size based on scale
    ann.size.width = objectify.StringElement(str(int(ann.size.width * SCALE)))
    ann.size.height = objectify.StringElement(str(int(ann.size.height *
                                                      SCALE)))
    folder = ann.folder
    filename = ann.filename
    filepath = os.path.join(str(folder), 'JPEGImages', str(filename))
    ann.path = objectify.StringElement(filepath)
    for obj in ann.iter('object'):
        obj.bndbox.ymin = objectify.StringElement(str(obj.bndbox.ymin * SCALE))
        obj.bndbox.xmin = objectify.StringElement(str(obj.bndbox.xmin * SCALE))
        obj.bndbox.ymax = objectify.StringElement(str(obj.bndbox.ymax * SCALE))
        obj.bndbox.xmax = objectify.StringElement(str(obj.bndbox.xmax * SCALE))

    return ann