annotate bin/paintserver @ 1575:0d2247ec8f49

bestMatch works in paintserver Ignore-this: c237f9f9a3dafe3ccfa3087423a5259a
author Drew Perttula <drewp@bigasterisk.com>
date Mon, 29 May 2017 07:59:59 +0000
parents 0480fc790527
children 17da56a3c8df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 from __future__ import division
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 from run_local import log
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 import json
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 from twisted.internet import reactor
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7 from light9.greplin_cyclone import StatsForCyclone
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 from light9.rdfdb.syncedgraph import SyncedGraph
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 from light9 import networking, showconfig
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10 from greplin import scales
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 import optparse, sys, logging
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 import cyclone.web
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 from rdflib import URIRef
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 from light9.rdfdb import clientsession
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 import light9.paint.solve
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16 from lib.cycloneerr import PrettyErrorHandler
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
17 from light9.namespaces import RDF, L9, DEV
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1521
diff changeset
19
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
20 class Solve(PrettyErrorHandler, cyclone.web.RequestHandler):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
21 def post(self):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 painting = json.loads(self.request.body)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 with self.settings.stats.solve.time():
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
24 img = self.settings.solver.draw(painting)
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
25 sample, sampleDist = self.settings.solver.bestMatch(img)
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
26 with self.settings.graph.currentState() as g:
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
27 bestPath = g.value(sample, L9['imagePath']).replace(L9[''], '')
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
28 #out = solver.solve(painting)
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
29 #layers = solver.simulationLayers(out)
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
30
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
31 self.write(json.dumps({
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
32 'bestMatch': {'uri': sample, 'path': bestPath, 'dist': sampleDist},
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
33 # 'layers': layers,
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
34 # 'out': out,
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
35 }))
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
36
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
37 def reloadSolver(self):
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
38 reload(light9.paint.solve)
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
39 self.settings.solver = light9.paint.solve.Solver(self.settings.graph)
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
40 self.settings.solver.loadSamples()
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
41
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
42
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
43 class App(object):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44 def __init__(self, show, session):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 self.show = show
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46 self.session = session
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
47
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
49 self.graph.initiallySynced.addCallback(self.launch)
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
50
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
51 self.stats = scales.collection('/', scales.PmfStat('solve'),
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
52 )
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
53
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
54 def launch(self, *args):
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
55
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
56 self.solver = light9.paint.solve.Solver(self.graph)
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
57 self.solver.loadSamples()
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
58
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
59 self.cycloneApp = cyclone.web.Application(handlers=[
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
60 (r'/stats', StatsForCyclone),
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61 (r'/solve', Solve),
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
62 ],
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63 debug=True,
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64 graph=self.graph,
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
65 solver=self.solver,
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
66 stats=self.stats)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67 reactor.listenTCP(networking.paintServer.port, self.cycloneApp)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
68 log.info("listening on %s" % networking.paintServer.port)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
69
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
70
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
71 if __name__ == "__main__":
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
72 parser = optparse.OptionParser()
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
73 parser.add_option('--show',
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
74 help='show URI, like http://light9.bigasterisk.com/show/dance2008',
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
75 default=showconfig.showUri())
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
76 parser.add_option("-v", "--verbose", action="store_true",
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
77 help="logging.DEBUG")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
78 parser.add_option("--twistedlog", action="store_true",
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
79 help="twisted logging")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
80 clientsession.add_option(parser)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
81 (options, args) = parser.parse_args()
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
82 log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
83
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
84 if not options.show:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
85 raise ValueError("missing --show http://...")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
86
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
87 session = clientsession.getUri('paint', options)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
88
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
89 app = App(URIRef(options.show), session)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
90 if options.twistedlog:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
91 from twisted.python import log as twlog
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
92 twlog.startLogging(sys.stderr)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
93 reactor.run()