# HG changeset patch # User Drew Perttula # Date 2017-04-19 08:26:08 # Node ID 23cabb70699bdccc372f4bae09f8669b2cde0895 # Parent 69088fe2865e7f2a39e16647e3b22403cf5ea910 solver now scores input images (poorly) Ignore-this: 82d0c9177a5db0a362dc04a39a01e257 diff --git a/light9/paint/solve.py b/light9/paint/solve.py --- a/light9/paint/solve.py +++ b/light9/paint/solve.py @@ -1,5 +1,8 @@ +from __future__ import division from light9.namespaces import RDF, L9 from PIL import Image +import decimal +import numpy import scipy.misc import cairo @@ -16,27 +19,63 @@ class Solver(object): path = 'show/dance2017/cam/test/%s' % g.value(samp, L9['path']) img = Image.open(path) img.thumbnail((100, 100)) - self.samples[samp] = scipy.misc.fromimage(img, mode='RGB') + self.samples[samp] = scipy.misc.fromimage(img, mode='RGB').transpose((1, 0, 2)) def draw(self, painting, w, h): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(surface) - ctx.move_to(0, 0) - ctx.line_to(100,30) + ctx.rectangle(0, 0, w, h) + ctx.fill() - ctx.set_source_rgb(0.3, 0.2, 0.5) + ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.set_line_width(20) - ctx.stroke() + for stroke in painting['strokes']: + for pt in stroke['pts']: + op = ctx.move_to if pt is stroke['pts'][0] else ctx.line_to + op(pt[0] / 4.0, pt[1] / 4.0) # todo scale + + r,g,b = [int(stroke['color'][i:i+2], 16) / 255.0 + for i in 1, 3, 5] + ctx.set_source_rgba(r, g, b, 1) + ctx.stroke() # then blur? - surface.write_to_png('/tmp/surf.png') - + + #surface.write_to_png('/tmp/surf.png') + a = numpy.frombuffer(surface.get_data(), numpy.uint8) + a.shape = (w, h, 4) + return a[:w,:h,:3] + def solve(self, painting): """ given strokes of colors on a photo of the stage, figure out the best light settings to match the image """ - self.draw(painting, 100, 80) - return 0 + pic0 = self.draw(painting, 100, 48) + sampleDist = {} + for sample, picSample in sorted(self.samples.items()): + dist = numpy.sum(numpy.power(pic0.astype(numpy.float) + - picSample, 2), axis=None)**.5 + sampleDist[sample] = dist + results = [(d, uri) for uri, d in sampleDist.items()] + results.sort() + import sys + print >>sys.stderr, results, results[0][0] / results[1][0] + sample = results[0][1] + + out = [] + def getVal(obj): + lit = g.value(obj, L9['value']) or g.value(obj, L9['scaledValue']) + ret = lit.toPython() + if isinstance(ret, decimal.Decimal): + ret = float(ret) + return ret + with self.graph.currentState() as g: + for obj in g.objects(sample, L9['setting']): + out.append((g.value(obj, L9['device']), + g.value(obj, L9['deviceAttr']), + getVal(obj))) + + return out def simulationLayers(self, settings): diff --git a/light9/paint/solve_test.py b/light9/paint/solve_test.py --- a/light9/paint/solve_test.py +++ b/light9/paint/solve_test.py @@ -1,12 +1,27 @@ import unittest import solve +from light9.namespaces import RDF, L9, DEV +from light9.rdfdb.localsyncedgraph import LocalSyncedGraph class TestSolve(unittest.TestCase): def testBlack(self): - s = solve.Solver() + graph = LocalSyncedGraph(files=['show/dance2017/cam/test/bg.n3']) + s = solve.Solver(graph) s.loadSamples() devAttrs = s.solve({'strokes': []}) self.assertEqual([], devAttrs) - + def testSingleLightCloseMatch(self): + graph = LocalSyncedGraph(files=['show/dance2017/cam/test/bg.n3']) + s = solve.Solver(graph) + s.loadSamples() + devAttrs = s.solve({'strokes': [{'pts': [[224, 141], + [223, 159]], + 'color': '#ffffff'}]}) + self.assertEqual(sorted([ + (DEV['aura1'], L9['color'], u"#ffffff"), + (DEV['aura1'], L9['rx'], 0.5 ), + (DEV['aura1'], L9['ry'], 0.573), + ]), sorted(devAttrs)) + diff --git a/light9/rdfdb/localsyncedgraph.py b/light9/rdfdb/localsyncedgraph.py new file mode 100644 --- /dev/null +++ b/light9/rdfdb/localsyncedgraph.py @@ -0,0 +1,13 @@ +from rdflib import ConjunctiveGraph + +from light9.rdfdb.currentstategraphapi import CurrentStateGraphApi +from light9.rdfdb.autodepgraphapi import AutoDepGraphApi +from light9.rdfdb.grapheditapi import GraphEditApi + +class LocalSyncedGraph(CurrentStateGraphApi, AutoDepGraphApi, GraphEditApi): + """for tests""" + def __init__(self, files=None): + self._graph = ConjunctiveGraph() + for f in files or []: + self._graph.parse(f, format='n3') + diff --git a/show/dance2017/cam/test/bg.n3 b/show/dance2017/cam/test/bg.n3 --- a/show/dance2017/cam/test/bg.n3 +++ b/show/dance2017/cam/test/bg.n3 @@ -9,32 +9,32 @@ :sample0 a :LightSample; :path "bg2-a.jpg"; :setting -[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], +[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], [ :device dev:aura1; :deviceAttr :rx; :value 0.2 ], [ :device dev:aura1; :deviceAttr :ry; :value 0.573 ] . :sample1 a :LightSample; :path "bg2-b.jpg"; :setting -[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], +[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], [ :device dev:aura1; :deviceAttr :rx; :value 0.3 ], [ :device dev:aura1; :deviceAttr :ry; :value 0.573 ] . :sample2 a :LightSample; :path "bg2-c.jpg"; :setting -[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], +[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], [ :device dev:aura1; :deviceAttr :rx; :value 0.4 ], [ :device dev:aura1; :deviceAttr :ry; :value 0.573 ] . :sample3 a :LightSample; :path "bg2-d.jpg"; :setting -[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], +[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], [ :device dev:aura1; :deviceAttr :rx; :value 0.5 ], [ :device dev:aura1; :deviceAttr :ry; :value 0.573 ] . :sample4 a :LightSample; :path "bg2-e.jpg"; :setting -[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], +[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], [ :device dev:aura1; :deviceAttr :rx; :value 0.6 ], [ :device dev:aura1; :deviceAttr :ry; :value 0.573 ] . :sample5 a :LightSample; :path "bg2-f.jpg"; :setting -[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], +[ :device dev:aura1; :deviceAttr :color; :scaledValue "#ffffff" ], [ :device dev:aura1; :deviceAttr :rx; :value 0.7 ], [ :device dev:aura1; :deviceAttr :ry; :value 0.573 ] .