Changeset - 23cabb70699b
[Not reviewed]
default
0 3 1
Drew Perttula - 8 years ago 2017-04-19 08:26:08
drewp@bigasterisk.com
solver now scores input images (poorly)
Ignore-this: 82d0c9177a5db0a362dc04a39a01e257
3 files changed with 75 insertions and 8 deletions:
0 comments (0 inline, 0 general)
light9/paint/solve.py
Show inline comments
 
from __future__ import division
 
from light9.namespaces import RDF, L9
 
from PIL import Image
 
import decimal
 
import numpy
 
import scipy.misc
 
import cairo
 

	
 
class Solver(object):
 
    def __init__(self, graph):
 
        self.graph = graph
 
@@ -13,33 +16,69 @@ class Solver(object):
 

	
 
        with self.graph.currentState() as g:
 
            for samp in g.subjects(RDF.type, L9['LightSample']):
 
                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)
 
        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):
 
        """
 
        how should a simulation preview approximate the light settings
 
        (device attribute values) by combining photos we have?
light9/paint/solve_test.py
Show inline comments
 
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))
 

	
 
        
light9/rdfdb/localsyncedgraph.py
Show inline comments
 
new file 100644
 
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')
 
            
0 comments (0 inline, 0 general)