Example #1
0
    def persist(self):
        # Create an xml 
        outs = cStringIO.StringIO()
        w = XMLWriter(outs)
        root = w.start("parsedlog", submitter=self.__submitter)
        #self.parser.asXml(w)
        self.loot.asXml(w)
        self.attendance.asXml(w)
        self.kills.asXml(w)
        w.close(root)

        # Stuff the xml into an http post
        headers = {"Content-type": "application/xml",
                   "Accept": "text/plain"}

        #conn = httplib.HTTPConnection('beauty.quonic.net')
        #conn.request("POST", "/~erik/eqlogdb/submit.psp", outs.getvalue())
        conn = httplib.HTTPConnection('beauty.quonic.net')
        conn.request("POST", "/~erik/eqlogdb/submit.psp", outs.getvalue())
        response = conn.getresponse()
        if response.status == 200:
                self.loot.clear()
                self.attendance.clear()
                self.kills.clear()
        print response.read()
        conn.close()
def createMetadata(item_ID):
	# get number of files
	images = os.listdir("/processing/"+item_ID+"/images")
	leaf_count = len(images) - 1 #accounts for /thumbs directory in there		

	# get dimensions of cover and create cover image
	cover_path = Template('/processing/$item_ID/images/$item_ID').substitute(item_ID=item_ID) + "00001.jpg"
	im = Image.open(cover_path)
	width, height = im.size #returns tuple

	# generate cover thumbnail
	max_cover_width = 200
	max_cover_height = 200
	im.thumbnail((max_cover_width, max_cover_height), Image.ANTIALIAS)				
	im.save(Template('/processing/$item_ID/$item_ID').substitute(item_ID=item_ID) + "_cover.jpg")
	print "Cover created for",item_ID	

	# write to xml
	fhand = Template("/processing/$item_ID/$item_ID$suffix.xml").substitute(item_ID=item_ID, suffix="_metadata")
	w = XMLWriter(fhand, "utf-8")

	metadata = w.start("add")
	w.start("doc")
	w.element("field", Template("meta:$item_ID").substitute(item_ID=item_ID), name="id") 
	w.element("field", Template("$item_ID").substitute(item_ID=item_ID), name="ItemID") #no underscore for solr index in "ItemID"
	#creats overall ratio - height / width
	w.element("field", str(height), name="pheight")
	w.element("field", str(width), name="pwidth")
	w.element("field", str(leaf_count), name="leafs")
	w.element("field", Template("$item_ID").substitute(item_ID=item_ID), name="item_title") #how will we generate this? ItemID for now... 
	w.end() #closes <doc>

	w.close(metadata)
Example #3
0
def create_gpx(track, positions):
    f = StringIO()
    w = XMLWriter(f, 'utf-8')

    gpx = w.start('gpx', creator='Gypsum', version='1.1')

    w.start('trk')
    w.start('name')
    w.data(track.__unicode__())
    w.end('name')

    w.start('trkseg')

    for p in positions:
        w.start('trkpt', lat=str(p.latitude), lon=str(p.longitude))

        if p.altitude != None:
            w.start('ele')
            w.data(str(p.altitude))
            w.end('ele')
        w.start('time')
        w.data(datetime.datetime.strftime(p.time, '%Y-%m-%dT%H:%M:%S.%fZ'))
        w.end('time')

        w.end('trkpt')

    w.end('trkseg')

    w.end('trk')

    w.close(gpx)

    return f.getvalue()
Example #4
0
def project_save1(tree):
    f = open('resar.xml', 'w')
    w = XMLWriter(f)
    html = w.start("data")
    w.element("empresa", '"' + tree.GetItemText(tree.GetRootItem(), 0) + '"')
    w.start('items')

    def printChildren(tree, treeItem):
        subItem = tree.GetFirstChild(treeItem)[0]
        name = tree.GetItemText(treeItem, 1)
        index = tree.GetItemText(treeItem, 0)
        try:
            parent = tree.GetItemText(tree.GetItemParent(treeItem), 1)
        except Exception:
            parent = 'root'

        if not subItem.IsOk():
            w.element("item", nombre=name, indice=index, parent=parent)

        else:
            w.element("item", nombre=name, indice=index, parent='root')

        while subItem.IsOk():
            printChildren(tree, subItem)
            subItem = tree.GetNextSibling(subItem)

    printChildren(tree, tree.GetRootItem())
   
    w.end('items')
    w.close(html)
    f.close()
def installerLog(package_details):
    '''
    This module writes the required fields of a package to a temporary 
    XML file which is then appended to the list of installedLog.xml.
    Right now - only name and version written to the installedLog.xml.
    '''
    
    #package_details contain all the info about the package
    w = XMLWriter('tempholder.xml')
    xml = w.start("xml")
    w.start("Package")
    w.element("name", package_details[0])
    w.element("version", package_details[1])
    #w.element("architecture", package_details[2])
    #w.element("short-description",package_details[3])
    #w.element("installed",package_details[4])
    #w.element("long-description",package_details[4])
    #-----^basic logging data above^------
    #w.element("section",package_details[5])
    #w.element("installed-size",package_details[6])
    #w.element("maintainer",package_details[7])
    #w.element("original-maintainer",package_details[8])
    #w.element("replaces",package_details[9])
    #w.element("provides",package_details[10])
    #w.element("pre-depends",package_details[11])
    #w.element("depends",package_details[5])
    #w.element("recomends",package_details[13])
    #w.element("suggests",package_details[14])
    #w.element("conflicts",package_details[15])
    #w.element("filename",package_details[16])
    #w.element("size",package_details[17])
    #w.element("md5sum",package_details[18])
    #w.element("homepage",package_details[19])
    w.end()
    w.close(xml)
    def process(self, args):
        # Perform any base class processing.
        if not super(NfoProcess, self).process(args):
            return

        # If we don't have an output parameter, we figure it out from
        # the input filename.
        output = args.output

        if not output:
            output = os.path.splitext(args.tmdb)[0] + ".nfo"

        # Check to see if the file exists.
        if os.path.isfile(output) and not args.force:
            print "Cannot overwrite file: " + output
            return False

        # Open up the output file.
        xml = open(output, 'w')
        xml.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")

        w = XMLWriter(xml, 'utf-8')
        tag = w.start("movie", ThumbGen="1")
        w.element("hasrighttoleftdirection", "false")
        w.element("title", self.movie['title'])
        w.element("originaltitle", self.movie['original_title'])
        w.element("filename", os.path.splitext(args.tmdb)[0] + ".mp4")
        w.element("tagline", self.movie['tagline'])
        w.element("releasedate", self.movie['release_date'])
        w.element("id", self.movie['imdb_id'])
        w.element("runtime", format(self.movie['runtime']))
        w.element("plot", self.movie['overview'])

        # Write out the genres.
        w.start("genre")

        for genre in self.movie['genres']:
            w.element("name", genre['name'])

        w.end()

        # Media information
        w.start("mediainfo")
        w.start("Resolution")
        w.element("Flag", "Resolution_480p")
        w.end()
        w.element("resolution", "480P")
        w.end()

        # Finish up the document.
        w.end()
        w.close(tag)
        xml.close()

        # Report that we created the file.
        self.log.info("Created " + output)
Example #7
0
 def asXml(self):
     outs = cStringIO.StringIO()
     w = XMLWriter(outs)
     root = w.start("parsedlog", submitter=self.myname)
     self.fights.asXml(w)
     self.loot.asXml(w)
     self.attendance.asXml(w)
     self.kills.asXml(w)
     w.close(root)
     return str(outs.getvalue())
Example #8
0
def post():
   try:
      d = request.form['thedata']
      ds = json.loads(d)['activity']
      stime = ds['startTimeUtc']
      stime = dateutil.parser.parse(stime)
      points = ds['geo']['waypoints']
      name = ds['name']

      f = StringIO.StringIO()
      w = XMLWriter(f)
      gpx_attribs = {"version":"1.1",
                        "creator":"unrealduck.com",
                        "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
                        "xmlns:gpxtpx":"http://www.garmin.com/xmlschemas/TrackPointExtension/v1",
                        "xmlns":"http://www.topografix.com/GPX/1/1",
                        "xsi:schemaLocation":"http://www.topografix.com/GPX/1/1 http://www.topografix.com/gpx/1/1/gpx.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"}
      gpx = w.start("gpx", gpx_attribs)
      w.start("metadata")
      w.element("name", name)
      w.element("time", str(stime).replace(" ", "T"))
      w.element("desc", "Import from nikeplus.com")
      w.end()

      w.start("trk")
      w.element("name", name)
      w.start("trkseg")

      for i,pt in zip(range(len(points)), points):            
         time = stime + datetime.timedelta(seconds=i)
         w.start("trkpt", lon=str(pt['lon']), lat=str(pt['lat']))
         w.element("ele", str(pt['ele']))
         w.element("time", str(time).replace(" ", "T"))
         w.end()
                     
      w.end()
      w.end()
         
      w.close(gpx)

      filename = '_'.join([ds['activityType'], ds['deviceType'], str(stime.date()), str(stime.time())])
      response = make_response("<?xml version=\"1.0\" ?>"+f.getvalue())
      response.headers['Content-Type'] = 'application/gpx+xml'         
      response.headers['Content-Disposition'] = 'attachment; filename='+filename+'.gpx'                 
      return response
      
   except KeyError:
      print "KeyError"
      return make_response("KeyError")
   except:
      print "UnexpectedError"
      print sys.exc_info()[0]
      return make_response("Unexpected error")
Example #9
0
def create_project(name, path, template=None):
    os.mkdir(path)
    os.mkdir(os.path.join(path, 'bin'))
    os.mkdir(os.path.join(path, 'contabilidad'))
    os.mkdir(os.path.join(path, 'documentos'))
    f = open(os.path.join(path, 'bin','empresa.xml'), 'w')
    w = XMLWriter(f)
    html = w.start("data")
    w.element("empresa", '"' + name + '"')
    w.start('items')
    w.element('item', nombre=name, indice='', parent='root')
    w.end('items')
    w.close(html)
    f.close()
Example #10
0
def run1():
	x = XMLWriter("test.xml")

	musicLibrary = x.start("musciLibrary")

	x.start("song")
	x.element("id", "2")
	x.element("title", "Jigga What/Faint")
	x.element("artist", "Linkin Park/Jay Z")
	x.element("year", "2004")
	x.element("trackNumber", "3")
	x.element("totalTracks", "6")
	x.element("genre", "Rap/Nu Metal")
	x.end("song")
	x.close(musicLibrary)
Example #11
0
def tagcloudxml(request):
    xml = cStringIO.StringIO()
    w = XMLWriter(xml)
    tags = w.start("tags")
    for tag in tagcloud():
        w.element("a",
            text=tag['tag'],
            attrs={'href': "http://www.bhamtechevents.org/browse_events/%s".format(tag['tag']),
                   'title': "%s topics".format(tag["count"]), 'rel': "tag",
                   'style': "font-size: %s".format(tag["size"])}
        )
    w.end()
    w.close(tags)
    w.flush()
    return HttpResponse(xml.read())
def write_keymap(keymap, filename):
    contexts = list(set([c for c, a, k in keymap]))
    actions = list(set([a for c, a, k in keymap]))

    w = XMLWriter(filename, "utf-8")
    doc = w.start("keymap")

    for context in contexts:
        w.start(context)
        w.start("keyboard")
        for c, a, k in keymap:
            if c == context:
                w.element("key", a, id=k)
        w.end()
        w.end()
    w.end()
    w.close(doc)
def write_keymap(keymap, filename):
  contexts = list(set([ c for c,a,k in keymap ]))
  actions  = list(set([ a for c,a,k in keymap ]))
  
  w = XMLWriter(filename, "utf-8")
  doc = w.start("keymap")
  
  for context in contexts:
    w.start(context)
    w.start("keyboard")
    for c,a,k in keymap:
      if c==context:
        w.element("key", a, id=k)
    w.end()
    w.end()
  w.end()
  w.close(doc)
Example #14
0
def intWriter(path, camMat, distCoe, calibError={}):
    try:
        print 'Generating Intrinsic Parameters to:', path, '...'
        with open(path, 'w') as int_xml:
            w = XMLWriter(int_xml)
            w.declaration()
            
            # Camera Intrinsic (Root)
            root = w.start('StdIntrinsic')
            
            # Camera Matrix
            w.element('CamMat',
                     fx=str(camMat[0][0]), fy=str(camMat[1][1]),
                     cx=str(camMat[0][2]), cy=str(camMat[1][2]))
            
            # Distortion Coefficients
            if (len(distCoe[0]) == 8): # 8 coefficients Rational Model, k4 k5 k6 enabled
                w.element('DistCoe', k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                          p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                          k3=str(distCoe[0][4]), k4=str(distCoe[0][5]),
                          k5=str(distCoe[0][6]), k6=str(distCoe[0][7]))
            elif (len(distCoe[0]) == 12): # 12 coefficients Prism Model, c1, c2, c3, c4 enabled, new in OpenCV 3.0.0
                w.element('DistCoe', k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                               p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                               k3=str(distCoe[0][4]), k4=str(distCoe[0][5]),
                               k5=str(distCoe[0][6]), k6=str(distCoe[0][7]),
                               c1=str(distCoe[0][8]), c2=str(distCoe[0][9]),
                                c3=str(distCoe[0][10]),c4=str(distCoe[0][11]))
            else:
                w.element('DistCoe', k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                          p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                          k3=str(distCoe[0][4]))
            
            # Error values
            if len(calibError) > 0:
                w.element('Error', rms=str(calibError['rms']), total=str(calibError['tot_err']), arth=str(calibError['arth_err']))
            
            w.close(root)
        print 'Intrinsic calibration has been generated successfully.'
        
    except Exception as e:
        print 'ERROR: occurred in writing intrinsic XML file.'
        raise e # keep it bubbling up, to catch in main()
def _record_key():
    	dialog = KeyListener()
    	dialog.doModal()
    	key = dialog.key
    	del dialog
	w = XMLWriter(twitter_file, "utf-8")
	doc = w.start("keymap")
	w.start("global")
	w.start("keyboard")
        w.element("key", "addon.opensettings(script.service.twitter)", id=str(key))
	w.end()
	w.end()
	w.start("fullscreenvideo")
	w.start("keyboard")
	w.element("key", "addon.opensettings(script.service.twitter)", id=str(key))
	w.end()
	w.end()
	w.end()
	w.close(doc)
Example #16
0
def writeXML(frames, path, args):
    out_xml = XMLWriter(path)
    out_xml.declaration()
    doc = out_xml.start("AnnotationEvaluation")
    
    # source information
    out_xml.element("video", mark_in=str(args[4]), mark_out=str(args[5]))
    out_xml.element("mapper", os.path.basename(args[1]))
    out_xml.element("annotation", os.path.basename(args[2]))
    out_xml.element("comparison", mean_err=str(calMean(frames)))
    # compared points
    out_xml.start("frames", total=str((args[5]-1) - (args[4]+1)+1), compared=str(len(frames)))
    for f in frames.keys():
        out_xml.start("frame", num=str(f))
        for key in frames[f]:
            out_xml.start("object", lens=Theta.name(frames[f][key]["lens"]), name=key, err=str(frames[f][key]["err"]))
            out_xml.element("annotatedCentroid", x=str(frames[f][key]["ann_x"]), y=str(frames[f][key]["ann_y"]))
            out_xml.element("mappedCentroid", x=str(frames[f][key]["map_x"]), y=str(frames[f][key]["map_y"]))
            out_xml.end() # object
        out_xml.end() # frames
    
    # clean up
    out_xml.close(doc)
Example #17
0
def write_graph_xml(graph, output_file='street_graph.xml'):
    w = XMLWriter(output_file)

    graph_e = w.start("graph")

    # write nodes
    w.start("nodes")

    for n in graph:
        w.element("node", id=str(n), lat=str(graph[n].lat), lon=str(graph[n].lng))

    w.end()

    # write arcs
    w.start("arcs")
    for n in graph:
        w.start("node", id=str(n))
        for arc in graph[n]:
            w.element("arc", ref=str(arc), weight=str(graph[n][arc]))
            #print arc, graph[n][arc]
        w.end()
    w.end()

    w.close(graph_e)
Example #18
0
    (entrysize, guid, type, unknown) = readBlockHeader()
    print "ENTRY SIZE %d" % entrysize
    print "POSS TYPE %s" % hex(type)
    print "GUID %s" % guid

    if type == 0x01:
        print "Waypoint"
        writeWaypoint(readWaypoint())
        waypoints+=1
    elif type == 0x21 or type == 0x22:
        print "Route"
        writeRoute(readRoute(type))
        routes+=1
    elif entrysize == 0xffff:
        print "End of output reached"
        done = True
    else:
        print "Skipping unknown type %s" % hex(type)

    # Pad byte, usually 0xcd
    if pos % 2 != 0:
        getAsByte()

    print "==="
    print "pos is %d - %s" % (pos, hex(pos))
    block+=1

print "Read %d waypoints and %d routes" % (waypoints, routes)

w.close(gpx)
Example #19
0
def main(sysargs):
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="mapper")

    if "help" in args:
        usage()
        return 0

    if ["calib", "trainer", "output"] not in args:
        print "Must specify: -calib, -trainer, -output files"
        usage()
        return 1

    if len(args) == 1:
        print "Not enough input CSV files"
        usage()
        return 1

    if len(args) > 2 and args.map_trainer_mode:
        print "Too many CSV for trainer-mapping mode"
        usage()
        return 1

    if "force_side" in args:
        side = Theta.resolve(args.force_side)
        if side == Theta.NonDual:
            print "Invalid force_side argument:", args.force_side
            usage()
            return 1

        # set side overrides
        force_button = (side == Theta.Buttonside)
        force_back = not force_button
    else:
        force_button = force_back = False

    # working vars
    csvs = {}
    frame_num = 0

    # open source CSV datasets
    for i in range(1, len(args)):
        print args[i]
        csvs[i] = Memset(args[i])

    # reel all the files up to their first flash
    for i in csvs:
        csvs[i].restrict()
        if len(csvs[i].row()) < 10:
            print "CSV file:", args[i], "contains no marker data!\nAborting."
            return 1

    # override csv name
    if args.map_trainer_mode:
        csvs[1]._name = cfg.trainer_target

    # open calib files
    try:
        buttonside = Mapper(args.calib, args.trainer, cfg, Theta.Buttonside)
        backside = Mapper(args.calib, args.trainer, cfg, Theta.Backside)
    except Exception as e:
        print e.message
        return 1

    count = {'bts': 0, 'bks': 0, 'rej': 0}

    # open destination XML
    with open(args.output, "w") as xmlfile:
        w = XMLWriter(xmlfile)
        w.declaration()
        xmlfile.write(
            "<!DOCTYPE dataset SYSTEM \"http://storage.gwillz.com.au/eagleeye_v2.dtd\">"
        )
        doc = w.start("dataset")

        # iterates until all vicon csvs reach eof
        while True:
            w.start("frameInformation")
            w.element("frame", number=str(frame_num))

            #iterates through each vicon csv at the current row
            for i in csvs:
                c = csvs[i]
                # determine marker quality
                try:
                    max_reflectors = int(c.row()[8])
                    visible_reflectors = int(c.row()[9])
                except:
                    print "Error in reading quality at row {}".format(i)
                    return 1

                try:
                    # read VICON data
                    x = float(c.row()[2])
                    y = float(c.row()[3])
                    z = float(c.row()[4])

                    # TODO: is this necessary? We never use the object's rotation
                    rx = float(c.row()[5])
                    ry = float(c.row()[6])
                    rz = float(c.row()[7])
                except:
                    print "Error occurred when converting VICON data at row {}".format(
                        i)
                    return 1

                # run projection/mapping on VICON data
                if backside.isVisible((x, y, z)):
                    points = backside.reprojpts((x, y, z))
                    side = 'backside'
                    count['bks'] += 1

                elif buttonside.isVisible((x, y, z)):
                    points = buttonside.reprojpts((x, y, z))
                    points[
                        0] += 960  # add 960 to x for rightside points (Ricoh video is both frames side by side)
                    side = 'buttonside'
                    count['bts'] += 1

                # TODO don't write non visible dots?
                else:
                    points = [0., 0.]
                    count['rej'] += 1

                # TODO: Change DTD and double check with Manjung
                w.start("object",
                        id=str(i),
                        name=c.name(),
                        lens=Theta.name(side))
                w.element("boxinfo",
                          height="99",
                          width="99",
                          x=str(points[0] - 50),
                          y=str(points[1] - 50))
                w.element("centroid",
                          x=str(points[0]),
                          y=str(points[1]),
                          rx=str(rx),
                          ry=str(ry),
                          rz=str(rz))
                w.element("visibility",
                          visible=str(visible_reflectors),
                          visibleMax=str(max_reflectors))
                w.end()

            w.end()

            # test end of files
            eofs = 0
            for i in csvs:
                if csvs[i].eof(): eofs += 1

            if len(csvs) == eofs:
                print "end of all datasets"
                break

            # load next vicon frame
            frame_num += 1
            for i in csvs:
                csvs[i].next()

        w.close(doc)

        print "\nbuttonside", count['bts']
        print "backside", count['bks']
        print "rejected", count['rej']

    return 0
Example #20
0
def intWriter(path, buttonside=None, backside=None):
    try:
        status = ""
        print 'Generating Intrinsic Parameters to:', path, '...'
        with open(path, 'w') as int_xml:
            w = XMLWriter(int_xml)
            w.declaration()
            
            # Camera Intrinsic (Root)
            root = w.start('dual_intrinsic')
            num_sides = range(0, 2)
            #num_sides = range(0, 1) if buttonside is None or backside is None else range(0, 2)
            for i in num_sides:
                w.start("Buttonside" if i == 0 else "Backside")

                if i == 0 and buttonside[0].size > 0 and buttonside[1].size > 0:
                    status += 'Buttonside'
                    camMat = buttonside[0]
                    distCoe = buttonside[1]
                    calibError = buttonside[2]
                elif i == 1 and backside[0].size > 0 and backside[1].size > 0:
                    if status == "":    status += 'Backside'
                    else:   status += ' & Backside'
                    camMat = backside[0]
                    distCoe = backside[1]
                    calibError = backside[2]
                else:
                    w.end()
                    continue
                
                
                # Camera Matrix
                w.element('CamMat',
                         fx=str(camMat[0][0]), fy=str(camMat[1][1]),
                         cx=str(camMat[0][2]), cy=str(camMat[1][2]))
                
                # Distortion Coefficients
                if (len(distCoe[0]) == 8): # 8 coefficients Rational Model, k4 k5 k6 enabled
                    w.element('DistCoe', 
                                k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                                p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                                k3=str(distCoe[0][4]), k4=str(distCoe[0][5]),
                                k5=str(distCoe[0][6]), k6=str(distCoe[0][7]))
                elif (len(distCoe[0]) == 12): # 12 coefficients Prism Model, c1, c2, c3, c4 enabled, new in OpenCV 3.0.0
                    w.element('DistCoe', 
                                k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                                p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                                k3=str(distCoe[0][4]), k4=str(distCoe[0][5]),
                                k5=str(distCoe[0][6]), k6=str(distCoe[0][7]),
                                c1=str(distCoe[0][8]), c2=str(distCoe[0][9]),
                                c3=str(distCoe[0][10]),c4=str(distCoe[0][11]))
                else:
                    w.element('DistCoe', 
                                k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                                p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                                k3=str(distCoe[0][4]))
                
                # Error values
                if len(calibError) > 0:
                    w.element('Error', 
                                rms=str(calibError['rms']), 
                                total=str(calibError['tot_err']), 
                                arth=str(calibError['arth_err']))
                
                w.end() #buttonside/backside
                
            w.close(root)
        print status, 'Intrinsic calibration has been generated successfully.'
        
    except Exception as e:
        # keep it bubbling up, to catch in main()
        raise Exception("{}: {}\n'ERROR: occurred in writing intrinsic XML file.'".format(type(e), e.message))
Example #21
0
    def generate(self):
        '''
        XML-File fuer Dale erzeugen
        '''
        context = self.context
        # Layout
        dc = IZopeDublinCore(context, None)
        if dc:
            creator = dc.creators[0]
            principal = Principal(creator, creator, creator)
        else:
            principal = self.request.principal
        principal = self.request.principal
        #stammdaten = IStammdaten(self.request.principal)
        adresse = principal.getAdresse()
        traegeroid = u''
        oid = str(adresse['oid']).strip()
        if len(oid) == 9:
            oid = '000000' + oid
        #session = Session()
        #s = select([traegeruaz], and_(traegeruaz.c.trgrcd == str(oid)))
        #res = session.execute(s).fetchone()
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        print oid
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        #if res:
        #    traegeroid = str(res['trgmnr']).strip()
        xml_file = self.base_file
        io = StringIO()
        xml = XMLWriter(io, encoding='utf-8')
        bv = ''
        iknr = ''  #Ik-Nummer des Mitgliedsunternehmens muss noch ermittelt werden
        ikdav = '99999999'
        iknum = '120692198'
        date = strftime("%d.%m.%Y", localtime())
        time = strftime("%H:%M", localtime())
        unb_6 = '0'
        docid = context.__name__
        if len(docid.split('-')) == 2:
            unb_6 = docid.split('-')[1]

        # Daten schreiben
        suaz = xml.start('suaz_file')

        xml.start("unb")
        xml.element("unb_2", iknr)
        xml.element("unb_3", ikdav)
        xml.element("unb_4", date)
        xml.element("unb_5", '00:00')
        xml.element("unb_6", unb_6)
        xml.element("unb_9", '01')
        xml.end("unb")

        xml.start("unh")
        xml.element("unh_2", 'SUAZ:09:1:01:UV')
        xml.element("unh_3", iknum)
        xml.end("unh")

        xml.start("uvt")
        xml.element("uvt_1", bv)
        xml.element("uvt_2", iknum)
        xml.element("uvt_3", date)
        uvt_4 = "%s.%s.20%s" % (context.unfdatum[0:2], context.unfdatum[3:5],
                                context.unfdatum[8:11])
        xml.element("uvt_4", uvt_4)
        xml.element("uvt_5", '')
        xml.end("uvt")

        xml.start("vin")
        xml.element("vin_1", context.prsname[:30])
        xml.element("vin_2", context.prsvor[:30])
        xml.element("vin_3", context.prssta)
        xml.element("vin_4", context.prssex[0])
        xml.element("vin_5", context.ikzplz[:6])
        xml.element("vin_6", context.ikzort[:30])
        vin_7 = "%s %s" % (context.ikstr, context.iknr)
        xml.element("vin_7", vin_7[:46])
        xml.element("vin_8", '')
        if int(context.prsgeb[6:11]) > int(strftime("%Y", localtime())):
            vin_9 = "%s.%s.19%s" % (context.prsgeb[0:2], context.prsgeb[3:5],
                                    context.prsgeb[8:10])
        else:
            vin_9 = "%s.%s.%s" % (context.prsgeb[0:2], context.prsgeb[3:5],
                                  context.prsgeb[6:10])
        xml.element("vin_9", vin_9)
        xml.element("vin_10", '')
        xml.element("vin_11", '')
        xml.end("vin")

        xml.start("ufb")
        # Mitglied
        if False:
            pass
        #if IMitglied.providedBy(self.request.principal):
        #    enummer = context.enummer
        #    if enummer == None:
        #        enummer = ''
        #    name1 = context.ename
        #    name2 = context.estrasse + ' ' + enummer
        #    name3 = context.eplz + ' ' + context.eort
        #    stra = context.estrasse + ' ' + enummer
        #    plz = context.eplz
        #    ort = context.eort
        #    adressetraeger = adresse['iknam1'] # WICHTIG Hier noch die weitere Adresse einfuegen und testen
        # Einrichtung
        else:
            if 'iknam1' in adresse:
                name1 = adresse['iknam1']
                name2 = adresse['iknam2']
                name3 = adresse['iknam3']
                stra = adresse['ikstr'] + adresse['ikhnr']
                plz = str(adresse['ikhplz'])
                ort = adresse['ikhort']
            else:
                name1 = "fehlt:adresse['iknam1']"
                name2 = "fehlt:adresse['iknam2']"
                name3 = "fehlt:adresse['iknam3']"
                stra = "fehlt:adresse['ikstr'] + adresse['ikhnr']"
                plz = "fehlt:str(adresse['ikhplz'])"
                ort = "fehlt:adresse['ikhort']"

            adressetraeger = context.traeger
        # Daten einfuegen...
        ufb_1 = '%s %s %s %s' % (name1, name2, name3, adressetraeger)
        xml.element("ufb_1", ufb_1[:200])
        xml.element("ufb_2", '')
        xml.element("ufb_3", plz)
        xml.element("ufb_4", ort[:30])
        xml.element("ufb_5", stra[:46])
        xml.element("ufb_6", '')
        xml.element("ufb_7", '')
        xml.end("ufb")

        xml.start("eti")
        xml.element("eti_1", date)
        xml.element("eti_2", time)
        xml.end("eti")

        xml.start("ksd")
        xml.element("ksd_1", 'unbekannt')
        xml.element("ksd_5", '1')
        xml.element("ksd_2", '')
        xml.element("ksd_3", '')
        xml.element("ksd_4", '')
        xml.end("ksd")

        xml.start("ufd")
        xml.element("ufd_1", context.unfzeit)
        xml.element("ufd_2", context.uadbavon)
        xml.element("ufd_3", context.uadbabis)
        xml.end("ufd")

        xml.start("ebh")
        # ebh_1 Pseudodatum eigefuegt 30.11.2011
        xml.element("ebh_1", '99.99.9999')
        if context.unfeba1:
            xml.element("ebh_2", context.unfeba1[:30])
        else:
            xml.element("ebh_2", '')
        xml.end("ebh")

        xml.start("dis")
        dis_1 = "%s %s" % (context.diavkt, context.diaadv)
        if context.prstkz == 'ja':
            dis_1 = u'tödlicher Unfall: %s' % dis_1
        xml.element("dis_1", dis_1[:100])
        xml.element("dis_4", '')
        xml.element("dis_3", '')
        xml.end("dis")

        xml.start("afb")
        if context.unfae1:
            eingestellt = context.unfae1
        else:
            eingestellt = ' '
        toedlich = context.prstkz
        if eingestellt == 'nein':
            afb_1 = '0'
            afb_4 = ''
        elif toedlich == 'ja':
            afb_1 = '1'
            afb_4 = ''
        else:
            afb_1 = '1'
        xml.element("afb_1", afb_1)
        if 'sofort' in eingestellt:
            afb_4 = uvt_4
        elif 'spaeter' in eingestellt:
            afb_4 = '%s.%s.20%s' % (context.unfaedatum[0:2],
                                    context.unfaedatum[3:5],
                                    context.unfaedatum[8:11])
            #In der Unfallanzeige kann das Datum in afb_4 nicht verarbeitet werden
            #afb_4 = '%s.%s.20%s %s' % (context.unfaedatum[0:2],context.unfaedatum[3:5],
            #                           context.unfaedatum[8:11], context.unfaezeit)
        xml.element("afb_4", afb_4)
        arbeitsfaehig = context.unfwa1
        if arbeitsfaehig == 'nein' or arbeitsfaehig == None:
            afb_7 = ''
        else:
            if context.unfwax == None:
                afb_7 = ''
            else:
                afb_7 = '%s.%s.%s' % (context.unfwax[0:2], context.unfwax[3:5],
                                      context.unfwax[6:11])
        xml.element("afb_7", afb_7)
        xml.element("afb_8", '')
        xml.end("afb")

        xml.start("abs")
        xml.element("abs_1", context.unfus2[:81])
        xml.element("abs_2", '')
        xml.element("abs_3", '')
        xml.element("abs_4", 'Extranet')
        xml.element("abs_5", '')
        xml.element("abs_6", context.anspfon)
        xml.element("abs_7", context.anspname)
        xml.end("abs")

        xml.start("uaz")
        xml.element("uaz_1", context.unfhg1[:3000])
        uaz_2 = context.unfhg2
        if uaz_2 == 'des Versicherten':
            uaz_2 = '1'
        else:
            uaz_2 = '2'
        xml.element("uaz_2", uaz_2)
        xml.element("uaz_3", context.unfort[:200])

        if context.prstkz == 'ja':
            uaz_4 = '1'
        else:
            uaz_4 = '2'
        xml.element("uaz_4", uaz_4)
        xml.element("uaz_5", context.unfkn1)
        if context.unfkn2 == 'ja':
            uaz_6 = '1'
        else:
            uaz_6 = '2'
        xml.element("uaz_6", uaz_6)
        xml.element("uaz_7", 'nein')
        xml.element("uaz_8", '1')
        xml.element("uaz_9", '0')
        xml.element("uaz_10", context.prsvtr)
        xml.element("uaz_11", oid)
        xml.end("uaz")

        xml.close(suaz)
        io.seek(0)
        # utf-8 wurde auskommentiert, da cusa nur Dateien mit header iso-8859-1 verarbeiten kann (das verstehe wer will)
        # xml_file.write(etree.tostring(etree.parse(io), pretty_print=True, encoding="utf-8", xml_declaration=True))
        xml_file.write(
            etree.tostring(etree.parse(io),
                           pretty_print=True,
                           encoding="ISO-8859-1",
                           xml_declaration=True))
        xml_file.close()
        return io
	#HTML
	if each.endswith('.htm'):
		w.start('foxml:datastream',CONTROL_GROUP="M", ID="HTML_FULL", STATE="A")
		w.start('foxml:datastreamVersion',MIMETYPE="text/html", ID="HTML_FULL.0", LABEL=Template("Fulltext HTML for item").substitute(count=str(count)))
		w.start('foxml:contentLocation',TYPE="URL",REF=Template("$base_URL/$item_ID/fullbook/$each").substitute(base_URL=base_URL,item_ID=item_ID,each=each))	
		w.end('foxml:contentLocation')
		w.end('foxml:datastreamVersion')
		w.end('foxml:datastream')

	#PDF
	if each.endswith('.pdf'):
		w.start('foxml:datastream',CONTROL_GROUP="M", ID="PDF_FULL", STATE="A")
		w.start('foxml:datastreamVersion',MIMETYPE="text/pdf", ID="PDF_FULL.0", LABEL=Template("Fulltext PDF document for item").substitute(count=str(count)))
		w.start('foxml:contentLocation',TYPE="URL",REF=Template("$base_URL/$item_ID/fullbook/$each").substitute(base_URL=base_URL,item_ID=item_ID,each=each))	
		w.end('foxml:contentLocation')
		w.end('foxml:datastreamVersion')
		w.end('foxml:datastream')
	
w.end('fullbook_docs')	
##############################################################
w.end('objects')
w.close(manifest)

print "finis!"
print "***********************************************"




Example #23
0
def main(sysargs):
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="mapper")
    
    if "help" in args:
        usage()
        return 0
    
    if ["calib", "trainer", "output"] not in args:
        print "Must specify: -calib, -trainer, -output files"
        usage()
        return 1
        
    if len(args) == 1:
        print "Not enough input CSV files"
        usage()
        return 1
    
    if len(args) > 2 and args.map_trainer_mode:
        print "Too many CSV for trainer-mapping mode"
        usage()
        return 1
    
    if "force_side" in args:
        side = Theta.resolve(args.force_side)
        if side == Theta.NonDual:
            print "Invalid force_side argument:", args.force_side
            usage()
            return 1
        
        # set side overrides
        force_button = (side == Theta.Buttonside)
        force_back   = not force_button
    else:
        force_button = force_back = False
    
    # working vars
    csvs = {}
    frame_num = 0
    
    # open source CSV datasets
    for i in range(1, len(args)):
        print args[i]
        csvs[i] = Memset(args[i])
    
    
    # reel all the files up to their first flash
    for i in csvs:
        csvs[i].restrict()
        if len(csvs[i].row()) < 10:
            print "CSV file:", args[i], "contains no marker data!\nAborting."
            return 1
    
    # override csv name
    if args.map_trainer_mode:
        csvs[1]._name = cfg.trainer_target
    
    # open calib files
    try:
        buttonside = Mapper(args.calib, args.trainer, cfg, Theta.Buttonside)
        backside = Mapper(args.calib, args.trainer, cfg, Theta.Backside)
    except Exception as e:
        print e.message
        return 1
    
    count = {'bts':0, 'bks':0, 'rej':0}
    
    # open destination XML
    with open(args.output, "w") as xmlfile:
        w = XMLWriter(xmlfile)
        w.declaration()
        xmlfile.write("<!DOCTYPE dataset SYSTEM \"http://storage.gwillz.com.au/eagleeye_v2.dtd\">")
        doc = w.start("dataset")
        
        # main loop
        while True:
            w.start("frameInformation")
            w.element("frame", number=str(frame_num))
            
            for i in csvs:
                c = csvs[i]
                # determine marker quality
                try:
                    max_reflectors = int(c.row()[8])
                    visible_reflectors = int(c.row()[9])
                except:
                    print "Error in reading quality at row {}".format(i)
                    return 1

                try:
                    # read VICON data
                    x = float(c.row()[2])
                    y = float(c.row()[3])
                    z = float(c.row()[4])
                    
                    # TODO: is this necessary? We never use the object's rotation
                    rx = float(c.row()[5])
                    ry = float(c.row()[6])
                    rz = float(c.row()[7])
                except:
                    print "Error occurred when converting VICON data at row {}".format(i)
                    return 1
                
                # run projection/mapping on VICON data
                if backside.isVisible((x,y,z)):
                    points = backside.reprojpts((x, y, z))
                    side = 'backside'
                    count['bks'] += 1
                
                elif buttonside.isVisible((x,y,z)):
                    points = buttonside.reprojpts((x, y, z))
                    points[0] += 960 # add 960 to x for rightside points
                    side = 'buttonside'
                    count['bts'] += 1
                
                # TODO don't write non visible dots? 
                else:
                    points = [0.,0.]
                    count['rej'] += 1
                
                # TODO: Change DTD and double check with Manjung
                w.start("object", id=str(i), name=c.name(), lens=Theta.name(side))
                w.element("boxinfo", height="99", width="99", x=str(points[0]-50), y=str(points[1]-50))
                w.element("centroid", x=str(points[0]), y=str(points[1]), rx=str(rx), ry=str(ry), rz=str(rz))
                w.element("visibility", visible=str(visible_reflectors), visibleMax=str(max_reflectors))
                w.end()
                
            w.end()
            
            # test end of files
            eofs = 0
            for i in csvs:
                if csvs[i].eof(): eofs += 1
            
            if len(csvs) == eofs:
                print "end of all datasets"
                break
            
            
            # load next frame
            frame_num += 1
            for i in csvs:
                csvs[i].next()
        
        w.close(doc)
        
        print "\nbuttonside", count['bts']
        print "backside", count['bks']
        print "rejected", count['rej']
        
    return 0
Example #24
0
class TaraxuDjangoWriter:
    def __init__(self, filename, encoding="utf-8", x=XmlFormat):
        self.xmlfile = open(filename, "w")
        self.writer = XMLWriter(self.xmlfile, encoding)
        self.x = x
        self.doc = self.writer.start(self.x.HEAD)

    def close(self):
        self.writer.close(self.doc)
        self.xmlfile.close()

    def start_task(self, task):
        # detect what type of task this is and remember it for encapsulated elements
        self.ttype = type(task).__name__
        # start task and remember it so that we can close
        self.task = self.writer.start(
            self.x.TASK[self.ttype], id=task.corpus.custom_id, sourcelanguage=task.corpus.language.name
        )

    def end_task(self, task):
        self.writer.close(self.task)

    def add_result(self, result):
        label = self.x.ITEM[self.ttype]

        # pythonic way to get to the particular type of the result
        try:
            result = result.rankingresult
        except:
            pass
        else:
            self.writer.start(
                label,
                id=result.item.source_sentence.custom_id.strip(),
                duration=str(result.duration),
                user=result.user.username,
            )
            ranks = _RankingRank.objects.filter(result=result)
            self.writer.element(self.x.SOURCE, result.item.source_sentence.text)
            for rank in ranks:
                self.writer.element(
                    self.x.TRANSLATION,
                    rank.translation.text,
                    system=rank.translation.document.translateddocument.translation_system.name,
                    rank=str(rank.rank),
                )
            self.writer.end()

        try:
            result = result.selectandposteditresult
        except:
            pass
        else:
            self.writer.start(
                label,
                id=result.item.source_sentence.custom_id.strip(),
                duration=str(result.duration),
                user=result.user.username,
            )
            self.writer.element(self.x.SOURCE, result.item.source_sentence.text)

            try:
                translation = Translation.objects.get(
                    source_sentence=result.item.source_sentence,
                    document__translateddocument__translation_system=result.system,
                )
                self.writer.element(self.x.TRANSLATION, translation.text)
            except:
                pass
            try:
                self.writer.element(
                    self.x.POSTEDIT, result.sentence, system=result.system.name, fromscratch=str(result.fromScratch)
                )
            except:
                self.writer.element(self.x.POSTEDIT, result.sentence, system="?", fromscratch=str(result.fromScratch))
            self.writer.end()
Created on Sun Dec 28 21:43:03 2014

@author: jmalinchak
"""

from elementtree.SimpleXMLWriter import XMLWriter
import sys

w = XMLWriter(sys.stdout)

html = w.start("html")

w.start("head")
w.element("title", "my document")
w.element("meta", name="generator", value="my application 1.0")
w.end()

w.start("body")
w.element("h1", "this is a heading")
w.element("p", "this is a paragraph")

w.start("p")
w.data("this is ")
w.element("b", "bold")
w.data(" and ")
w.element("i", "italic")
w.data(".")
w.end("p")

w.close(html)
Example #26
0
def write_graph_xml(g, output_file='transit_graph.xml'):
	from elementtree.SimpleXMLWriter import XMLWriter
	#w = XMLWriter(sys.stdout)#output_file)

	w = XMLWriter(output_file)

	data = w.start("data")

	gx = w.start("gtfs")

	sx = w.start("stops")
	for s in g.stops:
		w.element("stop", 
			id=str(s),
			name=str(g.stops[s].name),
			desc=str(g.stops[s].desc),
			lat=str(g.stops[s].lat),
			lng=str(g.stops[s].lng),
			url=str(g.stops[s].url),
			location_type=str(g.stops[s].location_type),
			parent_station=str(g.stops[s].parent_station)
		)
	w.end() # stops


	w.start("stop_times")
	for st in g.stop_times:
		w.element("stop_time",
			id=str(st),
			stop_id=str(g.stop_times[st].stop_id),
			trip_id=str(g.stop_times[st].trip_id),
			stop_sequence=str(g.stop_times[st].stop_sequence),
			pickup_type=str(g.stop_times[st].pickup_type),
			dropoff_type=str(g.stop_times[st].dropoff_type),
			arrival_time_s=str(g.stop_times[st].arrival_time_s),
			departure_time_s=str(g.stop_times[st].departure_time_s),
			arrival_time=str(g.stop_times[st].arrival_time),
			departure_time=str(g.stop_times[st].departure_time)
		)
	w.end() # stoptimes

	w.start("trips")
	for t in g.trips:
		w.element("trip",
			id=str(g.trips[t].id),
			route_id=str(g.trips[t].route_id),
			service_id=str(g.trips[t].service_id),
			trip_headsign=str(g.trips[t].trip_headsign),
			block_id=str(g.trips[t].block_id)
		)
	w.end() # trips

	w.start("routes")
	for r in g.routes:
		w.element("route",
			id=str(g.routes[r].id),
			short_name=str(g.routes[r].short_name),
			long_name=str(g.routes[r].long_name),
			desc=str(g.routes[r].desc),
			type=str(g.routes[r].type)
		)
	w.end() # routes


	w.end() # gtfs


	w.start("graph")

	# write nodes
	w.start("nodes")
	for n in g:
		w.element("node",
			id=str(n),
			stop_id=str(g[n].stop_id),
			stop_time_id=str(g[n].stop_time_id),
			arrival_time=str(g[n].arrival_time),
			departure_time=str(g[n].departure_time)
		)

	w.end() # nodes


	# write arcs
	w.start("arcs")
	for n in g:
		w.start("node", id=str(n))

		for arc in g[n]:
			w.element("arc", 
				id=str(arc), 
				weight=str(g[n][arc]), 
				type=str(g[n][arc].type)
			)

		w.end() # arc-node

	w.end() # arcs


	w.end() # graph

	w.close(data)
		rootnode = writer.start("root")
		for val1 in val1s:
			for val2 in val2s:
				if val1 is not val2:
					start_time = time.time()
					print "Modelling",val1,"(independent) against",val2,"(dependent)."
					ed = None
					if (val1,val2) not in found.keys():
						writer.start("model",ind=val1,dep=val2)
						ods = []
						weights = [0.4,0.3,0.2]
						for w in weights:
							ods.append(ObservedDistribution(parser, val1, contours, val2, w, retrain=True)) 
						#[0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100]
						#[0.01, 0.033, 0.1, 0.33, 1, 3, 10, 33, 100] 
						#[0.01, 0.1, 1, 10, 100]
						#[0.1,1,10]
						ed = GridSearchED(ods,{'C':Cs[val1],'gamma':gammas[val1]},grid={'gamma':[0.1,1],'C':[3]},parallel=True, log=writer)
						fn = os.path.join(prefix,"".join(['-'.join([val1,val2,str(ed.OD.weight_std_ratio),str(ed.params[0.5]['C']),str(ed.params[0.5]['gamma']),'.pdf'])]))
						plotAndSave(ed.OD,ed,fn)
						writer.end("model")
					elif printFound==False:
						print '--skipping as already found.'
					else:
						od = ObservedDistribution(parser, val1, contours, val2, None, prefix=odpath, retrain=True)
						ed = ExpectedDistribution(od,found[(val1,val2)])
						fn = os.path.join(prefix,"".join(['-'.join([val1,val2,str(od.weight_std_ratio),str(ed.params[0.5]['C']),str(ed.params[0.5]['gamma'])])+'.pdf']))
						plotAndSave(od,ed,fn)
					print time.time() - start_time, "seconds"
		writer.close(rootnode)
	Log(logfn).plotGridErrors()
    iso = date.split(' ')[0]

    filename = 'nl_' + iso + '_' +  title.replace(' ','_').lower() + '.xml'
    w = XMLWriter(filename, encoding='utf-8')
    html = w.start("broadcast")

    content = ' '.join(article_content)

    w.element("title", title)
    w.element("meta", name="author", value=author)
    w.element("meta", name="published", value=date)
    w.element("meta", name="corpus", value=filename)
    w.element("meta", name="source", value=u)
    w.element("meta", name="generator", value="https://github.com/caio1982/Nuntii-Latini")
    
    w.start("headline")
    w.element("title", title)
    w.element("content", content)
    w.end("headline")

    data = zip(subnews, subnews_content)
    for d in data:
        subnews = d[0]
        subnews_content = d[1]
        w.start("news")
        w.element("title", subnews)
        w.element("content", subnews_content)
        w.end("news")
    
    w.close(html)
Example #29
0
                print 'EOF: ' + str(x)
                break

            empty = u'\xa0\xa0'
            if len(entry) > 0:
                for e in entry:
                    if e.startswith(empty):
                        # apparently PHI texts have double blank spaces indicating new paragraphs
                        lines.append(''.join(e.replace(empty, '')))
                    else:
                        lines.append(''.join(e))

                paragraph = ' '.join(lines)
                y = codecs.open(path, "w", "utf8")
                y.write(paragraph)
                y.write
        else:
            # if text has been fetched ok, process it
            paragraph = codecs.open(path, "r", "utf8")
            strings = paragraph.read()

            # finally writes the new content to the corpus file
            w.start("page", id=str(x))
            w.element("paragraph", strings)
            w.end("page")

        # give the PHI server some time until the next fetch
        # time.sleep(5)
    # generates the output file
    w.close(xml)
Example #30
0
def main(sysargs):
    # settings
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="trainer")
    max_clicks = args.clicks or cfg.default_clicks
    window_name = "EagleEye Trainer"
    
    if "help" in args:
        usage()
        return 0
    
    # grab marks from args
    if len(args) > 5:
        mark_in = args[4]
        mark_out = args[5]
        
        # test integer-ness
        try: int(mark_in) and int(mark_out)
        except: 
            usage()
            return 1
        
    elif len(args) > 3:
        ret, mark_in, mark_out = marker_tool(args[1], cfg.buffer_size, window_name)
        if not ret:
            print "Not processing - exiting."
            return 1
    else:
        usage()
        return 1
    
    ## clicking time!
    cropped_total = mark_out - mark_in
    print "video cropped at:", mark_in, "to", mark_out, "- ({} frames)".format(cropped_total)
    
    # clicking function
    def on_mouse(event, x, y, flags, params):
        # left click to mark
        if event == cv2.EVENT_LBUTTONDOWN:
            params['pos'] = (x, y)
            params['status'] = Status.record
            
        # right click to skip
        elif event == cv2.EVENT_RBUTTONDOWN:
            params['status'] = Status.skip
    
    # working variables
    params = {'status': Status.wait, 'pos': None}
    write_xml = False
    textstatus = ""
    dataQuality = 0   # 0 = good, >0 = bad/potentially bad
    
    # default right side (buttonside)
    if cfg.dual_mode:
        lens = Theta.Right
        trainer_points = {Theta.Right:{}, Theta.Left:{}}
    else: # both sides otherwise
        lens = Theta.Both
        trainer_points = {Theta.Both:{}}
    
    print "Minimum reflectors: {} | Ignore Negative xyz: {}".format(cfg.min_reflectors, cfg.check_negatives)
        
    # load video (again)
    in_vid = BuffSplitCap(args[1], crop=(0,0,0,0), rotate=BuffSplitCap.r0, buff_max=cfg.buffer_size)
    in_vid.restrict(mark_in, mark_out)
    
    # load csv (with appropriate ratio)
    in_csv = Memset(args[2])
    in_csv.restrict()
    in_csv.setRatio(cropped_total)
    
    # test for marker data
    if len(in_csv.row()) < 10:
        print "This CSV contains no marker data!\nAborting."
        return 1
    
    # status
    print ""
    print "Writing to:", args[3]
    print "Number of clicks at:", max_clicks
    print ""
    
    cv2.namedWindow(window_name)
    cv2.setMouseCallback(window_name, on_mouse, params)
    
    # grab clicks (Process 2)
    while in_vid.isOpened():
        frame = in_vid.frame(side=lens)
        
        sys.stdout.write(in_vid.status() + " | Clicks {} / {}\r".format(
                                                len(trainer_points[lens]), max_clicks))
        sys.stdout.flush()
        
        # prepare CSV data, click data
        tx = float(in_csv.row()[2])
        ty = float(in_csv.row()[3])
        tz = float(in_csv.row()[4])
        rx = float(in_csv.row()[5])
        ry = float(in_csv.row()[6])
        rz = float(in_csv.row()[7])
        
        # data quality status
        visible = int(in_csv.row()[9])
        max_visible = int(in_csv.row()[8])
        
        # status text to write
        textrow = "VICON - x: {:.4f} y: {:.4f} z: {:.4f} | rx: {:.4f} ry: {:.4f} rz: {:.4f}".format(tx, ty, tz, rx, ry, rz)
        textquality = "Visible: {} , Max Visible: {}".format(visible, max_visible)
        textstatus = "{} | {}/{} clicks".format(in_vid.status(), len(trainer_points[lens]), max_clicks)
        
        if lens == Theta.Left:
            textstatus += " - back side"
        elif lens == Theta.Right:
            textstatus += " - button side"
        #else none, no lens split
        
        # if data is qualified bad, reduce timeout by one
        if dataQuality > 0:
            dataQuality -= 1
        
        if dataQuality == 0:
            dataStatus = " - Good data!!"
            dataStatus_colour = (0, 255, 0) # green
        else:
            dataStatus = " - Potentially bad data (wait {})".format(dataQuality)
            dataStatus_colour = (0, 255, 255) # yellow
        
        # Data tests
        # values must be above 0 and minimum reflectors
        if (cfg.check_negatives and (tx <= 0 or ty <= 0 or tz <= 0)) \
                or visible < cfg.min_reflectors:
            dataStatus = " - Bad data!!"
            dataStatus_colour = (0, 0, 255) # red
            
            if cfg.ignore_baddata:
                dataStatus += " Ignored."
                dataQuality = 1 + cfg.quality_delay
        
        # draw the trainer dot (if applicable)
        if in_vid.at() in trainer_points[lens]:
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 1, cfg.dot_colour, 2)
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 15, cfg.dot_colour, 1)
        
        # draw text and show
        displayText(frame, textrow, top=True)
        displayText(frame, textquality)
        displayText(frame, textstatus)
        displayText(frame, dataStatus, endl=True, colour=dataStatus_colour)
        
        cv2.imshow(window_name, frame)
        
        # pause for input
        while params['status'] == Status.wait:
            key = cv2.waitKey(10)
            if key == Key.esc:
                params['status'] = Status.stop
            elif key == Key.enter:
                write_xml = True
                params['status'] = Status.stop
            elif key == Key.right:
                params['status'] = Status.skip
            elif key == Key.left:
                params['status'] = Status.back
            elif key == Key.backspace:
                params['status'] = Status.remove
            elif Key.char(key, '1') and cfg.dual_mode:
                params['status'] = Status.still
                lens = Theta.Left
            elif Key.char(key, '2') and cfg.dual_mode:
                params['status'] = Status.still
                lens = Theta.Right
            
        # catch exit status
        if params['status'] == Status.stop:
            print "\nprocess aborted!"
            break
        
        # write data
        if params['status'] == Status.record \
                and len(trainer_points[lens]) != max_clicks: # TODO: does this disable recording clicks on the last frame
                
            if dataQuality == 0:
                trainer_points[lens][in_vid.at()] = (params['pos'], in_csv.row()[2:5], in_csv.row()[8:10])
            params['status'] = Status.skip
        
        # or remove it
        elif params['status'] == Status.remove \
                and in_vid.at() in trainer_points[lens]:
            del trainer_points[lens][in_vid.at()]
            print "\nremoved dot"
        
        # load next csv frame
        if params['status'] == Status.skip:
            if in_vid.next():
                in_csv.next()
            else:
                write_xml = True
                print "\nend of video: {}/{}".format(in_vid.at() -1, mark_out -1)
                break
        
        # or load previous csv frame
        elif params['status'] == Status.back:
            if in_vid.back():
                in_csv.back()
        
        # reset status
        params['status'] = Status.wait
    
    # clean up
    cv2.destroyAllWindows()
    
    
    ## write xml
    if write_xml:
        out_xml = XMLWriter(args[3])
        out_xml.declaration()
        doc = out_xml.start("TrainingSet")
        
        # source information
        out_xml.start("video", mark_in=str(mark_in), mark_out=str(mark_out))
        out_xml.data(os.path.basename(args[1]))
        out_xml.end()
        out_xml.element("csv", os.path.basename(args[2]))
        
        # training point data
        for lens in trainer_points:
            if lens == Theta.Right:
                out_xml.start("buttonside", points=str(len(trainer_points[lens])))
            elif lens == Theta.Left:
                out_xml.start("backside", points=str(len(trainer_points[lens])))
            else: # non dualmode
                out_xml.start("frames", points=str(len(trainer_points[lens])))
            
            for i in trainer_points[lens]:
                pos, row, markers = trainer_points[lens][i]
                x, y = pos
                
                out_xml.start("frame", num=str(i))
                out_xml.element("plane", 
                                x=str(x), 
                                y=str(y))
                out_xml.element("vicon", 
                                x=str(row[0]), y=str(row[1]), z=str(row[2]))
                out_xml.element("visibility", 
                                visibleMax=str(markers[0]), 
                                visible=str(markers[1]))
                out_xml.end()
                
            out_xml.end() # frames
        
        # clean up
        out_xml.close(doc)
        
        print "Data was written."
    else:
        print "No data was written"
    
    print "\nDone."
    return 0
Example #31
0
def intWriter(path, camMat, distCoe, calibError={}):
    try:
        print 'Generating Intrinsic Parameters to:', path, '...'
        with open(path, 'w') as int_xml:
            w = XMLWriter(int_xml)
            w.declaration()

            # Camera Intrinsic (Root)
            root = w.start('StdIntrinsic')

            # Camera Matrix
            w.element('CamMat',
                      fx=str(camMat[0][0]),
                      fy=str(camMat[1][1]),
                      cx=str(camMat[0][2]),
                      cy=str(camMat[1][2]))

            # Distortion Coefficients
            if (len(distCoe[0]) == 8
                ):  # 8 coefficients Rational Model, k4 k5 k6 enabled
                w.element('DistCoe',
                          k1=str(distCoe[0][0]),
                          k2=str(distCoe[0][1]),
                          p1=str(distCoe[0][2]),
                          p2=str(distCoe[0][3]),
                          k3=str(distCoe[0][4]),
                          k4=str(distCoe[0][5]),
                          k5=str(distCoe[0][6]),
                          k6=str(distCoe[0][7]))
            elif (
                    len(distCoe[0]) == 12
            ):  # 12 coefficients Prism Model, c1, c2, c3, c4 enabled, new in OpenCV 3.0.0
                w.element('DistCoe',
                          k1=str(distCoe[0][0]),
                          k2=str(distCoe[0][1]),
                          p1=str(distCoe[0][2]),
                          p2=str(distCoe[0][3]),
                          k3=str(distCoe[0][4]),
                          k4=str(distCoe[0][5]),
                          k5=str(distCoe[0][6]),
                          k6=str(distCoe[0][7]),
                          c1=str(distCoe[0][8]),
                          c2=str(distCoe[0][9]),
                          c3=str(distCoe[0][10]),
                          c4=str(distCoe[0][11]))
            else:
                w.element('DistCoe',
                          k1=str(distCoe[0][0]),
                          k2=str(distCoe[0][1]),
                          p1=str(distCoe[0][2]),
                          p2=str(distCoe[0][3]),
                          k3=str(distCoe[0][4]))

            # Error values
            if len(calibError) > 0:
                w.element('Error',
                          rms=str(calibError['rms']),
                          total=str(calibError['tot_err']),
                          arth=str(calibError['arth_err']))

            w.close(root)
        print 'Intrinsic calibration has been generated successfully.'

    except Exception as e:
        print 'ERROR: occurred in writing intrinsic XML file.'
        raise e  # keep it bubbling up, to catch in main()
Example #32
0
def main(sysargs):
    # settings
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="trainer")
    max_clicks = args.clicks or cfg.default_clicks
    window_name = "EagleEye Trainer"

    if "help" in args:
        usage()
        return 0

    # grab marks from args
    if len(args) > 5:
        mark_in = args[4]
        mark_out = args[5]

        # test integer-ness
        try:
            int(mark_in) and int(mark_out)
        except:
            usage()
            return 1

    elif len(args) > 3:
        ret, mark_in, mark_out = marker_tool(args[1], cfg.buffer_size,
                                             window_name)
        if not ret:
            print "Not processing - exiting."
            return 1
    else:
        usage()
        return 1

    ## clicking time!
    cropped_total = mark_out - mark_in
    print "video cropped at:", mark_in, "to", mark_out, "- ({} frames)".format(
        cropped_total)

    # clicking function
    def on_mouse(event, x, y, flags, params):
        # left click to mark
        if event == cv2.EVENT_LBUTTONDOWN:
            params['pos'] = (x, y)
            params['status'] = Status.record

        # right click to skip
        elif event == cv2.EVENT_RBUTTONDOWN:
            params['status'] = Status.skip

    # working variables
    params = {'status': Status.wait, 'pos': None}
    write_xml = False
    textstatus = ""
    dataQuality = 0  # 0 = good, >0 = bad/potentially bad

    # default right side (buttonside)
    if cfg.dual_mode:
        lens = Theta.Right
        trainer_points = {Theta.Right: {}, Theta.Left: {}}
    else:  # both sides otherwise
        lens = Theta.Both
        trainer_points = {Theta.Both: {}}

    print "Minimum reflectors: {} | Ignore Negative xyz: {}".format(
        cfg.min_reflectors, cfg.check_negatives)

    # load video (again)
    in_vid = BuffSplitCap(args[1],
                          crop=(0, 0, 0, 0),
                          rotate=BuffSplitCap.r0,
                          buff_max=cfg.buffer_size)
    in_vid.restrict(mark_in, mark_out)

    # load csv (with appropriate ratio)
    in_csv = Memset(args[2])
    in_csv.restrict()
    in_csv.setRatio(cropped_total)

    # test for marker data
    if len(in_csv.row()) < 10:
        print "This CSV contains no marker data!\nAborting."
        return 1

    # status
    print ""
    print "Writing to:", args[3]
    print "Number of clicks at:", max_clicks
    print ""

    cv2.namedWindow(window_name)
    cv2.setMouseCallback(window_name, on_mouse, params)

    # grab clicks (Process 2)
    while in_vid.isOpened():
        frame = in_vid.frame(side=lens)

        sys.stdout.write(in_vid.status() + " | Clicks {} / {}\r".format(
            len(trainer_points[lens]), max_clicks))
        sys.stdout.flush()

        # prepare CSV data, click data
        tx = float(in_csv.row()[2])
        ty = float(in_csv.row()[3])
        tz = float(in_csv.row()[4])
        rx = float(in_csv.row()[5])
        ry = float(in_csv.row()[6])
        rz = float(in_csv.row()[7])

        # data quality status
        visible = int(in_csv.row()[9])
        max_visible = int(in_csv.row()[8])

        # status text to write
        textrow = "VICON - x: {:.4f} y: {:.4f} z: {:.4f} | rx: {:.4f} ry: {:.4f} rz: {:.4f}".format(
            tx, ty, tz, rx, ry, rz)
        textquality = "Visible: {} , Max Visible: {}".format(
            visible, max_visible)
        textstatus = "{} | {}/{} clicks".format(in_vid.status(),
                                                len(trainer_points[lens]),
                                                max_clicks)

        if lens == Theta.Left:
            textstatus += " - back side"
        elif lens == Theta.Right:
            textstatus += " - button side"
        #else none, no lens split

        # if data is qualified bad, reduce timeout by one
        if dataQuality > 0:
            dataQuality -= 1

        if dataQuality == 0:
            dataStatus = " - Good data!!"
            dataStatus_colour = (0, 255, 0)  # green
        else:
            dataStatus = " - Potentially bad data (wait {})".format(
                dataQuality)
            dataStatus_colour = (0, 255, 255)  # yellow

        # Data tests
        # values must be above 0 and minimum reflectors
        if (cfg.check_negatives and (tx <= 0 or ty <= 0 or tz <= 0)) \
                or visible < cfg.min_reflectors:
            dataStatus = " - Bad data!!"
            dataStatus_colour = (0, 0, 255)  # red

            if cfg.ignore_baddata:
                dataStatus += " Ignored."
                dataQuality = 1 + cfg.quality_delay

        # draw the trainer dot (if applicable)
        if in_vid.at() in trainer_points[lens]:
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 1,
                       cfg.dot_colour, 2)
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 15,
                       cfg.dot_colour, 1)

        # draw text and show
        displayText(frame, textrow, top=True)
        displayText(frame, textquality)
        displayText(frame, textstatus)
        displayText(frame, dataStatus, endl=True, colour=dataStatus_colour)

        cv2.imshow(window_name, frame)

        # pause for input
        while params['status'] == Status.wait:
            key = cv2.waitKey(10)
            if key == Key.esc:
                params['status'] = Status.stop
            elif key == Key.enter:
                write_xml = True
                params['status'] = Status.stop
            elif key == Key.right:
                params['status'] = Status.skip
            elif key == Key.left:
                params['status'] = Status.back
            elif key == Key.backspace:
                params['status'] = Status.remove
            elif Key.char(key, '1') and cfg.dual_mode:
                params['status'] = Status.still
                lens = Theta.Left
            elif Key.char(key, '2') and cfg.dual_mode:
                params['status'] = Status.still
                lens = Theta.Right

        # catch exit status
        if params['status'] == Status.stop:
            print "\nprocess aborted!"
            break

        # write data
        if params['status'] == Status.record \
                and len(trainer_points[lens]) != max_clicks: # TODO: does this disable recording clicks on the last frame

            if dataQuality == 0:
                trainer_points[lens][in_vid.at()] = (params['pos'],
                                                     in_csv.row()[2:5],
                                                     in_csv.row()[8:10])
            params['status'] = Status.skip

        # or remove it
        elif params['status'] == Status.remove \
                and in_vid.at() in trainer_points[lens]:
            del trainer_points[lens][in_vid.at()]
            print "\nremoved dot"

        # load next csv frame
        if params['status'] == Status.skip:
            if in_vid.next():
                in_csv.next()
            else:
                write_xml = True
                print "\nend of video: {}/{}".format(in_vid.at() - 1,
                                                     mark_out - 1)
                break

        # or load previous csv frame
        elif params['status'] == Status.back:
            if in_vid.back():
                in_csv.back()

        # reset status
        params['status'] = Status.wait

    # clean up
    cv2.destroyAllWindows()

    ## write xml
    if write_xml:
        out_xml = XMLWriter(args[3])
        out_xml.declaration()
        doc = out_xml.start("TrainingSet")

        # source information
        out_xml.start("video", mark_in=str(mark_in), mark_out=str(mark_out))
        out_xml.data(os.path.basename(args[1]))
        out_xml.end()
        out_xml.element("csv", os.path.basename(args[2]))

        # training point data
        for lens in trainer_points:
            if lens == Theta.Right:
                out_xml.start("buttonside",
                              points=str(len(trainer_points[lens])))
            elif lens == Theta.Left:
                out_xml.start("backside",
                              points=str(len(trainer_points[lens])))
            else:  # non dualmode
                out_xml.start("frames", points=str(len(trainer_points[lens])))

            for i in trainer_points[lens]:
                pos, row, markers = trainer_points[lens][i]
                x, y = pos

                out_xml.start("frame", num=str(i))
                out_xml.element("plane", x=str(x), y=str(y))
                out_xml.element("vicon",
                                x=str(row[0]),
                                y=str(row[1]),
                                z=str(row[2]))
                out_xml.element("visibility",
                                visibleMax=str(markers[0]),
                                visible=str(markers[1]))
                out_xml.end()

            out_xml.end()  # frames

        # clean up
        out_xml.close(doc)

        print "Data was written."
    else:
        print "No data was written"

    print "\nDone."
    return 0