Mercurial > code > home > repos > light9
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 |
rev | line source |
---|---|
1521 | 1 #!bin/python |
2 | |
3 from __future__ import division | |
4 from run_local import log | |
5 import json | |
6 from twisted.internet import reactor | |
7 from light9.greplin_cyclone import StatsForCyclone | |
8 from light9.rdfdb.syncedgraph import SyncedGraph | |
9 from light9 import networking, showconfig | |
10 from greplin import scales | |
11 import optparse, sys, logging | |
12 import cyclone.web | |
13 from rdflib import URIRef | |
14 from light9.rdfdb import clientsession | |
15 import light9.paint.solve | |
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 | 18 |
1522
69088fe2865e
more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
19 |
1521 | 20 class Solve(PrettyErrorHandler, cyclone.web.RequestHandler): |
21 def post(self): | |
22 painting = json.loads(self.request.body) | |
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 | 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 | 43 class App(object): |
44 def __init__(self, show, session): | |
45 self.show = show | |
46 self.session = session | |
47 | |
48 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer") | |
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 | 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 | 59 self.cycloneApp = cyclone.web.Application(handlers=[ |
60 (r'/stats', StatsForCyclone), | |
61 (r'/solve', Solve), | |
62 ], | |
63 debug=True, | |
64 graph=self.graph, | |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
65 solver=self.solver, |
1521 | 66 stats=self.stats) |
67 reactor.listenTCP(networking.paintServer.port, self.cycloneApp) | |
68 log.info("listening on %s" % networking.paintServer.port) | |
69 | |
70 | |
71 if __name__ == "__main__": | |
72 parser = optparse.OptionParser() | |
73 parser.add_option('--show', | |
74 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
75 default=showconfig.showUri()) | |
76 parser.add_option("-v", "--verbose", action="store_true", | |
77 help="logging.DEBUG") | |
78 parser.add_option("--twistedlog", action="store_true", | |
79 help="twisted logging") | |
80 clientsession.add_option(parser) | |
81 (options, args) = parser.parse_args() | |
82 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
83 | |
84 if not options.show: | |
85 raise ValueError("missing --show http://...") | |
86 | |
87 session = clientsession.getUri('paint', options) | |
88 | |
89 app = App(URIRef(options.show), session) | |
90 if options.twistedlog: | |
91 from twisted.python import log as twlog | |
92 twlog.startLogging(sys.stderr) | |
93 reactor.run() |