def txt2xml(img_path, txt_path, xml_save_path):
    assert os.path.exists(img_path)
    assert os.path.exists(txt_path)
    if not os.path.exists(xml_save_path):
        os.makedirs(xml_save_path)
    img_files = os.listdir(img_path)
    train_txt = os.listdir(txt_path)
    txt = []
    class_list = []
    for txt_name in train_txt:
        with open(os.path.join(txt_path, txt_name), 'r') as f:
            obj = f.readlines()
            txt.append(obj)
            class_list.append(txt_name.split('-')[0])

    img = cv2.imread(os.path.join(img_path, img_files[0]))
    width = img.shape[1]
    height = img.shape[0]
    dim = img.shape[2]

    for i in range(1, len(img_files)):
        E = objectify.ElementMaker(annotate=False)
        anno_tree = E.annotation(
            E.folder(img_path),
            E.filename("%06d.tiff" % i),
            E.source(E.database('IPIU'), ),
            E.size(E.width(width), E.height(height), E.depth(dim)),
            E.segmented(0),
        )
        # tree.parse(os.path.join(xml_save_path, '%06d.xml' % i))

        for lable, bbox in zip(class_list, txt):
            bbox = list(map(int, bbox[i - 1].strip().split(',')))
            if bbox[0] == -1:
                continue
            E2 = objectify.ElementMaker(annotate=False)
            anno_tree2 = E2.object(
                E.name(lable),
                E.bndbox(E.xmin(bbox[0]), E.ymin(bbox[1]),
                         E.xmax(bbox[0] + bbox[2]), E.ymax(bbox[1] + bbox[3])),
                E.difficult(0))
            anno_tree.append(anno_tree2)

        etree.ElementTree(anno_tree).write(os.path.join(
            xml_save_path, "%06d.xml" % i),
                                           pretty_print=True)
        print('\r%s save %06d.xml' % (img_path, i), end='')
예제 #2
0
파일: pdv.py 프로젝트: mpildek/l10n_hr
    def create_xml_pdvs(self, cr, uid, ids, metadata, zaglavlje, context=None):
        metadata[
            'naslov'] = u"Prijava za stjecanje dobara i primljene usluge iz drugih država članica Europske unije"
        metadata['uskladjenost'] = u'ObrazacPDVS-v1-0'
        md, identifikator = self.etree_meta(metadata)

        cr.execute(
            """
        select row_number() over (order by c.id) rbr,
            c.code drzava, p.vat porbr, 
            coalesce(s.dobra,0) dobra, 
            coalesce(s.usluge,0) usluge
        from l10n_hr_pdvs_line s
            left join res_partner p on p.id=s.partner_id
            left join res_country c on c.id=p.country_id
        where obrazac = %(obrazac)s
        """, {'obrazac': ids[0]})
        pdvs_vals = cr.dictfetchall()

        cr.execute(
            """
        select coalesce(sum(dobra),0) dobra
            ,coalesce(sum(usluge),0) usluge 
        from l10n_hr_pdvs_line where obrazac = %(obrazac)s
        """, {'obrazac': ids[0]})
        pdvs_sum = cr.dictfetchone()
        EM = objectify.ElementMaker(annotate=False)
        isporuke_list = [
            EM.Isporuka(
                EM.RedBr(l['rbr']), EM.KodDrzave(l['drzava']),
                EM.PDVID(l['porbr'].startswith(l['drzava']) and l['porbr'][2:]
                         or l['drzava']), EM.I1(l['dobra']),
                EM.I2(l['usluge'])) for l in pdvs_vals
        ]
        tijelo = EM.Tijelo(
            EM.Isporuke(EM.Isporuka),
            EM.IsporukeUkupno(EM.I1(pdvs_sum['dobra']),
                              EM.I2(pdvs_sum['usluge'])))
        tijelo.Isporuke.Isporuka = isporuke_list

        PDVS = objectify.ElementMaker(
            annotate=False,
            namespace=
            "http://e-porezna.porezna-uprava.hr/sheme/zahtjevi/ObrazacPDVS/v1-0"
        )
        pdvs = PDVS.ObrazacPDVS(md, zaglavlje, tijelo, verzijaSheme="1.0")
        return self.etree_tostring(pdvs), identifikator
예제 #3
0
def add_new_core():
    """
    Add a Solr core for the current OpenOrdini instance.
    """
    require('domain_root', 'app_domain', 'local_repo_root', 'solr_home', 'om_user', 
            provided_by=('staging', 'production'))
    execute(update_core_conf)
    ## symlink configuration dir for the new core
    with hide('commands', 'warnings'):
        ln_dest = '%(solr_home)s/cores/%(app_domain)s' % env
        ln_src = os.path.join(env.domain_root, 'private', 'solr')
        if files.exists(ln_dest, use_sudo=True):
            fastprint("Removing file %s..." % ln_dest, show_prefix=True)
            sudo('rm -f %s' % ln_dest)
            fastprint(" done." % env, end='\n')
        fastprint("Symlinking core configuration...", show_prefix=True)    
        sudo('ln -s %s %s' % (ln_src, ln_dest))
        fastprint(" done." % env, end='\n')
        ## create a data dir for this core, if not existing
        with cd(os.path.join(env.solr_home, 'data')):
            if not files.exists(env.app_domain):
                fastprint("Creating a data dir for this core...", show_prefix=True)
                sudo('mkdir -p %(app_domain)s' % env)
                # Tomcat needs write permissions to cores' data dirs
                sudo('chmod 2770 %(app_domain)s' % env) 
                fastprint(" done." % env, end='\n')
        ## add to ``solr.xml`` a definition for the new core (as an additional ``<core>`` element)  
        with cd(env.solr_home):
            # copy remote version of ``solr.xml`` to the local machine
            fastprint("Adding new core definition to `solr.xml'... ", show_prefix=True)
            tmp_fname = os.path.join(env.local_repo_root, 'system', 'solr', 'solr.xml.remote')
            get(remote_path=os.path.join('cores', 'solr.xml'), local_path=tmp_fname)
            # parse ``solr.xml`` into a tree of Python objects
            tree = objectify.parse(tmp_fname)
            # retrieve the ``<cores>`` XML element
            cores_el = tree.getroot().cores
            # build a factory function for ``<core>`` elements
            E = objectify.ElementMaker(annotate=False)
            CORE = E.core
            # if a core definition for this OpenOrdini instance already exists, 
            # drop it.
            existing_cores = [el.attrib['name'] for el in cores_el.iterchildren()]
            if env.om_user in existing_cores:
                [cores_el.remove(el) for el in cores_el.iterchildren() if el.attrib['name'] == env.om_user]
            # append a new ``<core>`` element to ``<cores>``
            cores_el.append(CORE(name=env.om_user,  
                                 instanceDir=env.app_domain,
                                 dataDir='%(solr_home)s/data/%(app_domain)s' % env
                                 ))
            # write back to ``solr.xml.remote``
            tree.write(tmp_fname, pretty_print=True, xml_declaration=True, encoding='UTF-8')
            # update ``solr.xml`` on the server machine
            src = tmp_fname
            dest = os.path.join('cores', 'solr.xml')  
            put(src, dest, mode=0644)
            # cleanup
            local('rm %s' % tmp_fname)
            fastprint(" done." % env, end='\n')
            restart_tomcat()
예제 #4
0
 def write_box(self, tree, xmin, ymin, xmax, ymax):
     E = objectify.ElementMaker(annotate=False)
     subtree = E.object(
         E.name('crack'), E.pose('Unspecified'), E.truncated(0),
         E.difficult(0),
         E.bndbox(E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax)))
     tree.append(subtree)
     return tree
예제 #5
0
def get_example_xml_tree_elementmaker(filebase):
    rootdir = pathlib.Path(__file__).resolve().parent.parent
    hpxmlfilename = str(rootdir / 'examples' / f'{filebase}.xml')
    tree = objectify.parse(hpxmlfilename)
    root = tree.getroot()
    ns = re.match(r'\{(.+)\}', root.tag).group(1)
    E = objectify.ElementMaker(annotate=False, namespace=ns)
    return tree, E
예제 #6
0
def xml_root(filename, height, width):
    E = objectify.ElementMaker(annotate=False)
    return E.annotation(E.filename(filename),
                        E.size(
                            E.width(width),
                            E.height(height),
                            E.depth(3),
                        ))
예제 #7
0
 def add_object(self, object_name, xmin, ymin, xmax, ymax):
     print("------------------")
     E = objectify.ElementMaker(annotate=False)
     temp = E.object(
         E.name(object_name), E.pose("Unspecified"), E.truncated(0),
         E.Difficult(0),
         E.bndbox(E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax)))
     self.anno_tree.append(temp)
예제 #8
0
def create_new_obj(obj_name, x1, y1, x2, y2):
    rec_name = str(obj_name)
    E2 = objectify.ElementMaker(annotate=False)
    anno_tree2 = E2.object(
        E2.name(rec_name),
        E2.bndbox(E2.xmin(x1), E2.ymin(y1), E2.xmax(x2), E2.ymax(y2)),
        E2.difficult(0))
    return anno_tree2
예제 #9
0
def addObject(xmin, ymin, xmax, ymax, E, anno_tree, carType, carNumber):
    E2 = objectify.ElementMaker(annotate=False)
    anno_tree2 = E2.object(
        E.name(carType[carNumber]),
        E.bndbox(E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax)),
        E.difficult(0))
    #print carType[carNumber]
    anno_tree.append(anno_tree2)
def process_specs(infile, modules=None):
    """Process the module specifications in the input PDF into an XML tree"""
    # initialise the pdfminer interface --
    # we use a custom "render device" to receive the text objects in the PDF for further processing
    resman = PDFResourceManager()
    doc = PDFDocument(PDFParser(open(infile, 'rb')))
    laparams = LAParams(
        line_margin=0.1, char_margin=0.7
    )  # parameters optimised to prevent incorrectly joining together words

    device = PDFTableParser(resman, laparams, stop_at='VHDL')
    interpreter = PDFPageInterpreter(resman, device)

    # parse the PDF table of contents to figure out what modules exist and which pages to process
    port_pages = parse_module_pages(doc, 'Port Desc')
    if modules is None or len(modules) == 0:
        modules = port_pages.keys()  # default to processing ALL modules
    attrib_list = parse_module_pages(
        doc, 'Available Attrib'
    )  # NB: not all modules have attributes

    # parse the specifications and generate an XML tree
    E = objectify.ElementMaker(annotate=False)
    root = E.xml(source=infile, processed=datetime.now().isoformat())

    # run through the modules
    for module in modules:
        if module == 'PLL_BASE': continue   # UG615/PLL_BASE is a problem
        sys.stderr.write('Processing %s...\n' % module)
        node = E.module(name=module)
        # process the ports of this module
        for pg, top, bottom in port_pages[module]:
            device.reset(top, bottom)
            interpreter.process_page(pg)
            for P in process_ports(device):
                node.append(
                    E.port(
                        name=P['Port'], type=P['Type'], width=str(P['Width'])
                    )
                )
            if device.done: break
        # process the attributes of this module
        for pg, top, bottom in attrib_list.get(module, []):
            device.reset(top, bottom)
            interpreter.process_page(pg)
            for A in process_attributes(device):
                node.append(
                    E.attribute(
                        name=A['Attribute'],
                        type=A['Type'],
                        default=A['Default'].replace('"', ''),
                        values=A['Allowed'].replace('"', '')
                    )
                )
            if device.done: break
        # add it to the root object
        root.append(node)
    return root
예제 #11
0
    def getToken(self, user, password):
        self.user = user
        self.password = password

        #----------------------
        # génération flux xml
        #----------------------
        E = objectify.ElementMaker(annotate=False,
                                   namespace=xmlns,
                                   nsmap={None: xmlns})
        root = E.LogonRequest(E.Metadata(E.Atom),
                              E.UserID(self.user, kb="CUR", kxe="false"),
                              E.Password(self.password, kb="CUR", kxe="false"),
                              schemaVersion="V1_1_0")
        body = etree.tostring(root,
                              pretty_print=True,
                              xml_declaration=True,
                              encoding=u"UTF-8",
                              standalone=u"yes")

        #---------------------
        # envoi requête PUT
        #---------------------
        headers = {
            "Content-type": "application/vnd.ibm.powervm.web+xml",
            "Accept": "application/vnd.ibm.powervm.web+xml"
        }
        self.log.debug('Requête HTTP : PUT /rest/api/web/Logon')
        self.log.debug('Requête HTTP body : \n' + body)
        self.cnx.request("PUT", "/rest/api/web/Logon", body, headers)

        #--------------------------
        # traitement réponse HTTP
        #--------------------------
        reponse = self.cnx.getresponse()
        self.log.debug('HTTP Status Code : ' + str(reponse.status) + ' ' +
                       str(reponse.reason))
        self.log.debug('HTTP headers : \n' + str(reponse.msg))
        result = reponse.read()
        root = etree.fromstring(result)
        #log.debug(result)
        if (reponse.status == 200):
            res = root.xpath('//n:X-API-Session/text()',
                             namespaces={'n': xmlns})
            if res is not None:
                self.token = res[0]
                return res[0]
            if res is None:
                self.token = None
                return None
        if (reponse.status != 200):
            res1 = root.xpath('//n:RequestURI/text()', namespaces={'n': xmlns})
            res2 = root.xpath('//n:ReasonCode/text()', namespaces={'n': xmlns})
            res3 = root.xpath('//n:Message/text()', namespaces={'n': xmlns})
            self.log.debug('RequestURI : ' + str(res1[0]))
            self.log.debug('ReasonCode : ' + str(res2[0]))
            self.log.debug('Message : ' + str(res3[0]))
            return None
def save_annotations(dataType, filename, objs):
    annopath = CKanno_dir + "/" + filename[:-3] + "xml"
    img_path = dataDir + "/" + dataType +"/" + filename
    dst_path = CKimg_dir + "/" + filename
    img = cv2.imread(img_path, 0)
    im = Image.open(img_path)
    if im.mode != "RGB":
        print(filename + " not a RGB image")
        im.close()
        return
    im.close()
    # shutil.copy(img_path, dst_path)
    cv2.imwrite(dst_path, img)
    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.annotation(
        E.folder('1'),
        E.filename(filename),
        E.source(
            E.database('CKdemo'),
            E.annotation('VOC'),
            E.image('CK')
        ),
        E.size(
            E.width(img.shape[1]),
            E.height(img.shape[0]),
            E.depth(1) # 这里需要强制指定为1
        ),
        E.segmented(0)
    )
    for obj in objs:
        E2 = objectify.ElementMaker(annotate=False)
        anno_tree2 = E2.object(
            E.name(obj[0]),
            E.pose(),
            E.truncated("0"),
            E.difficult(0),
            E.bndbox(
                E.xmin(obj[2]),
                E.ymin(obj[3]),
                E.xmax(obj[4]),
                E.ymax(obj[5])
            )
        )
        anno_tree.append(anno_tree2)
    etree.ElementTree(anno_tree).write(annopath, pretty_print=True)
def root(folder, filename, width, height):
    E = objectify.ElementMaker(annotate=False)
    return E.annotation(E.folder(folder), E.filename(filename),
                        E.source(E.database('VisualTB'), ),
                        E.size(
                            E.width(width),
                            E.height(height),
                            E.depth(3),
                        ))
예제 #14
0
 def __init__(self, swf=None, margin=0):
     self._e = objectify.ElementMaker(annotate=False,
                                      namespace=SVG_NS,
                                      nsmap={
                                          None: SVG_NS,
                                          "xlink": XLINK_NS
                                      })
     self._margin = margin
     super(SVGExporter, self).__init__(swf)
예제 #15
0
def create_element_maker():
    nsmap = {
        None: "http://www.materialsdb.org",
        "xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "SchemaLocation": "http://www.materialsdb.org/schemas/materialsdb103.xsd",
    }
    return objectify.ElementMaker(
        namespace="http://www.materialsdb.org", nsmap=nsmap, annotate=False
    )
예제 #16
0
def instance2xml_bbox(bbox, category_id):
    """bbox_type: xyxy (xmin, ymin, xmax, ymax); xywh (xmin, ymin, width, height)"""
    xmin, ymin, xmax, ymax = bbox
    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.object(
        E.name(category_id),
        E.bndbox(E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax)),
        E.difficult(0))
    return anno_tree
def instance_to_xml(obj):
    E = objectify.ElementMaker(annotate=False)
    return E.object(
        E.name(obj['name']), E.trackid(obj['trackid']),
        E.bndbox(
            E.xmin(obj['xmin']),
            E.ymin(obj['ymin']),
            E.xmax(obj['xmax']),
            E.ymax(obj['ymax']),
        ))
예제 #18
0
 def addObject(self, className, xmin, ymin, xmax, ymax, E, anno_tree):
     E2 = objectify.ElementMaker(annotate=False)
     anno_tree2 = E2.object(
         E.name(className),
         E.pose('Unspecified'),
         E.truncated(0),
         E.difficult(0),
         E.bndbox(E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax)),
     )
     anno_tree.append(anno_tree2)
예제 #19
0
def editXml(xml_path, name, left, top, right, down):
    anno_tree = objectify.parse(xml_path).getroot()
    E = objectify.ElementMaker(annotate=False)
    anno_tree_son = E.object(
        E.name(str(name)), E.pose('Unspecified'), E.truncated('0'),
        E.difficult('0'),
        E.bndbox(E.xmin(str(left)), E.ymin(str(top)), E.xmax(str(right)),
                 E.ymax(str(down))))
    anno_tree.append(anno_tree_son)
    return anno_tree
    def test_validation_fail(self):
        myE = objectify.ElementMaker(annotate=False,
                                     namespace=None,
                                     nsmap=None)
        self.mm.xml_object.animals = myE.root()
        self.mm.xml_object.animals.tiger = myE.frog('white')
        self.mm.xml_object.animals.frog = myE.frog('green')

        with self.assertRaises(XMLError):
            self.mm.validate()
예제 #21
0
def instance2xml_base(anno, img_size, bbox_type='xyxy'):
    """bbox_type: xyxy (xmin, ymin, xmax, ymax); xywh (xmin, ymin, width, height)"""
    assert bbox_type in ['xyxy', 'xywh']
    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.annotation(
        E.folder('VOC2014_instance/person'),
        E.filename(anno['id']),
        E.source(
            E.database('Caltech pedestrian'),
            E.annotation('Caltech pedestrian'),
            E.image('Caltech pedestrian'),
            E.url('None')
        ),
        E.size(
            E.width(img_size[0]),
            E.height(img_size[1]),
            E.depth(3)
        ),
        E.segmented(0),
    )
    for index, bbox in enumerate(anno['bbox']):
        bbox = [float(x) for x in bbox]
        if bbox_type == 'xyxy':
            xmin, ymin, w, h = bbox
            xmax = xmin+w
            ymax = ymin+h
        else:
            xmin, ymin, xmax, ymax = bbox
        E = objectify.ElementMaker(annotate=False)
        anno_tree.append(
            E.object(
            E.name(anno['label']),
            E.bndbox(
                E.xmin(xmin),
                E.ymin(ymin),
                E.xmax(xmax),
                E.ymax(ymax)
            ),
            E.difficult(0),
            E.occlusion(anno["occlusion"][index])
            )
        )
    return anno_tree
예제 #22
0
 def __instance_to_xml(x_min, y_min, x_max, y_max, cat_id):
     element = objectify.ElementMaker(annotate=False)
     return element.object(
         element.name(cat_id),
         element.bndbox(
             element.xmin(x_min),
             element.ymin(y_min),
             element.xmax(x_max),
             element.ymax(y_max),
         ),
     )
예제 #23
0
def instance2xml_base(anno):
    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.annotation(
        E.folder('VOC2014_instance/{}'.format(anno['category_id'])),
        E.filename(anno['file_name']),
        E.source(E.database('MS COCO 2014'), E.annotation('MS COCO 2014'),
                 E.image('Flickr'), E.url(anno['coco_url'])),
        E.size(E.width(anno['width']), E.height(anno['height']), E.depth(3)),
        E.segmented(0),
    )
    return anno_tree
예제 #24
0
def obj_to_xml(bbx, label):
    E = objectify.ElementMaker(annotate=False)
    xmin, ymin, xmax, ymax = bbx
    return E.object(
        E.name(label),
        E.bndbox(
            E.xmin(xmin),
            E.ymin(ymin),
            E.xmax(xmax),
            E.ymax(ymax),
        ))
예제 #25
0
 def __init__(self, defs=None):
     self.defs = defs
     self.current_draw_command = ""
     self.path_data = ""
     self._e = objectify.ElementMaker(annotate=False,
                                      namespace=SVG_NS,
                                      nsmap={
                                          None: SVG_NS,
                                          "xlink": XLINK_NS
                                      })
     super(DefaultSVGShapeExporter, self).__init__()
예제 #26
0
def instance_to_xml(xmin, ymin, xmax, ymax, activity):
    E = objectify.ElementMaker(annotate=False)
    return E.object(
        E.name(label),
        E.bndbox(
            E.xmin(xmin),
            E.ymin(ymin),
            E.xmax(xmax),
            E.ymax(ymax),
        ),
    )
예제 #27
0
def instance_to_xml(anno):
    E = objectify.ElementMaker(annotate=False)
    xmin, ymin, width, height = anno['bbox']
    return E.object(
        E.name(anno['category_id']),
        E.bndbox(
            E.xmin(xmin),
            E.ymin(ymin),
            E.xmax(xmin + width),
            E.ymax(ymin + height),
        ),
    )
예제 #28
0
def instance_to_xml(annotation):
    E = objectify.ElementMaker(annotate=False)
    x_min, y_min, x_max, y_max = annotation["bbox"]
    return E.object(
        E.name(annotation["category_id"]),
        E.bndbox(
            E.xmin(x_min),
            E.ymin(y_min),
            E.xmax(x_max),
            E.ymax(y_max),
        ),
    )
예제 #29
0
def writeXmlRoot(anno):
    # 写基础字段
    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.annotation(  # 根目录
        E.folder(anno['folder']),  # 根目录内容
        E.filename(anno['filename']),
        E.size(
            E.width(anno['width']),  # 子目录内容
            E.height(anno['height']),
            E.depth(anno['channel'])),
    )
    return anno_tree  # anno_tree.append(writeXmlSubRoot(anno))
예제 #30
0
def create_new_anno(file_name, width, height):
    w = width
    h = height
    name = str(file_name)

    E = objectify.ElementMaker(annotate=False)
    anno_tree = E.annotation(E.folder('a'), E.filename(name),
                             E.source(E.database('traffic'), ),
                             E.size(E.width(w), E.height(h), E.depth(3)),
                             E.segmented(0))

    return anno_tree