def _on_expose(self, drawing, event): # just mark lock as callbacks are already inside gtk thread with self._gooey.lock_marker: # get a new cairo context from the drawing area context = self._drawing_area.window.cairo_create() # set a clip region for the expose event context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) context.clip() # zoom and scroll context.scale(self.zoom, self.zoom) context.translate(*self.scroll) # call draw handler to draw contents of canvas to cairo context self.handle_draw(Brush(context, self.size)) return False
def draw(self, p0, p1): Brush.draw(self, p0, p1) #self.probFg(x1-20, x1+20, y1-20, y1+20) #return """color = self.colorTri[self.type] #self.argsScore[5] = np.int32(self.nComponentsFg) #seed = [] hasSeeds = False redoBg = False minX = sys.maxint maxX = -sys.maxint minY = sys.maxint maxY = -sys.maxint for point in self.points[0:nPoints]: #if self.hTriFlat[point] != color: self.hTriFlat[point] = color #seed += point hasSeeds = True minX = min(minX, point%self.width) maxX = max(maxX, point%self.width) minY = min(minY, point/self.width) maxY = max(maxY, point/self.width) #if (point[1]*self.width + point[0]) in self.randIdx: # redoBg = True #if redoBg: # self.probBg(0) #if len(seed) == 0: if not hasSeeds: return minX = max(0, minX-DILATE) maxX = min(self.width-1, maxX + DILATE) minY = max(0, minY-DILATE) maxY = min(self.height-1, maxY + DILATE) """ args = [ np.int32(self.n_points), self.d_points, cl.Sampler(self.context, False, cl.addressing_mode.NONE, cl.filter_mode.NEAREST), self.d_img, self.dSampleFg ] gWorksize = roundUp((self.n_points, ), (256, )) self.kern_get_samples(self.queue, gWorksize, (256,), *args).wait() cl.enqueue_copy(self.queue, self.hSampleFg, self.dSampleFg) # print self.hSampleFg.view(np.uint8).reshape(10240, 4)[0:self.n_points, :] # print self.n_points self.gmmFg.fit(self.dSampleFg, self.n_points) # print w # print m # print c self.gmmFg.score(self.d_img, self.dScoreFg) # self.argsSampleBg = [ # self.d_labels, # np.int32(self.label), # cl.Sampler(self.context, False, cl.addressing_mode.NONE, # cl.filter_mode.NEAREST), # self.d_img, # self.dSampleFg # ] # # gWorksize = roundUp(self.dim, (16, 16)) # # self.kernSampleBg(self.queue, gWorksize, (16, 16), # *(self.argsSampleBg)).wait() # cl.enqueue_copy(self.queue, self.hSampleBg, self.dSampleBg).wait() pass
import time import pyopencl as cl from clutil import Buffer2D import numpy as np platforms = cl.get_platforms() devices = platforms[0].get_devices() devices = [devices[1]] context = cl.Context(devices) cm = cl.mem_flags dim = (800, 608) dStrokes = Buffer2D(context, cm.READ_WRITE, dim, dtype=np.uint8) from Brush import Brush brush = Brush(context, devices, dStrokes) iterations = 10 t0 = time.clock() for i in range(iterations): brush.draw((0, 0), (100, 100)) dt = (time.clock() - t0) / iterations * 1000 print "%.2f milliseconds per iteration (mean)" % dt
def __init__(self, context, devices, d_img, d_labels): Brush.__init__(self, context, devices, d_labels) self.context = context self.queue = cl.CommandQueue(context, properties=cl.command_queue_properties.PROFILING_ENABLE) nComponentsFg = 4 nComponentsBg = 4 self.nDim = 3 self.dim = d_img.dim filename = os.path.join(os.path.dirname(__file__), 'quick.cl') program = createProgram(context, context.devices, [], filename) # self.kernSampleBg = cl.Kernel(program, 'sampleBg') self.kern_get_samples = cl.Kernel(program, 'get_samples') self.lWorksize = (16, 16) self.gWorksize = roundUp(self.dim, self.lWorksize) nSamples = 4 * (self.gWorksize[0] / self.lWorksize[0]) * ( self.gWorksize[1] / self.lWorksize[1]) # self.gmmFg_cpu = mixture.GMM(4) self.gmmFg = GMM(context, 65, nComponentsFg, 10240) self.gmmBg = GMM(context, 65, nComponentsBg, nSamples) self.hScore = np.empty(self.dim, np.float32) self.hSampleFg = np.empty((10240, ), np.uint32) self.hSampleBg = np.empty((12000, ), np.uint32) self.hA = np.empty((max(nComponentsFg, nComponentsBg), 8), np.float32) self.d_img = d_img cm = cl.mem_flags self.dSampleFg = cl.Buffer(context, cm.READ_WRITE, size=4 * 10240) self.dSampleBg = cl.Buffer(context, cm.READ_WRITE, size=4 * 12000) self.dA = cl.Buffer(context, cm.READ_ONLY | cm.COPY_HOST_PTR, hostbuf=self.hA) self.dScoreFg = Buffer2D(context, cm.READ_WRITE, self.dim, np.float32) self.dScoreBg = Buffer2D(context, cm.READ_WRITE, self.dim, np.float32) #self.points = Set() self.capPoints = 200 * 200 * 300 #brush radius 200, stroke length 300 self.points = np.empty((self.capPoints), np.uint32) # self.colorize = Colorize.Colorize(clContext, clContext.devices) # self.hTriFlat = self.hTri.reshape(-1) # self.probBg(1200) self.h_img = np.empty(self.dim, np.uint32) self.h_img = self.h_img.ravel() cl.enqueue_copy(self.queue, self.h_img, self.d_img, origin=(0, 0), region=self.dim).wait() self.samples_bg_idx = np.random.randint(0, self.dim[0] * self.dim[1], 12000) self.hSampleBg = self.h_img[self.samples_bg_idx] cl.enqueue_copy(self.queue, self.dSampleBg, self.hSampleBg).wait() w,m,c = self.gmmBg.fit(self.dSampleBg, 300, retParams=True) print w print m print c self.gmmBg.score(self.d_img, self.dScoreBg) pass
hImg = padArray2D(np.array(img).view(np.uint32).squeeze(), shape, 'edge') dImgGC = cl.Image(context, cl.mem_flags.READ_ONLY, cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.UNSIGNED_INT8), dim ) cl.enqueue_copy(queue, dImgGC, hImg, origin=(0,0), region=dim) dImg = Buffer2D(context, cm.READ_WRITE | cm.COPY_HOST_PTR, hostbuf=hImg) dStrokes = Buffer2D(context, cm.READ_WRITE, dim, dtype=np.uint8) growCut = GrowCut(context, devices, dImgGC, GrowCut.NEIGHBOURHOOD.VON_NEUMANN, GrowCut.WEIGHT_POW2) brush = Brush(context, devices, growCut.dLabelsIn) brush.setLabel(TRI_BG) iteration = 0 refresh = 100 def next(): global iteration growCut.evolve(1) sm.processTrimap(dStrokes, growCut.dLabelsOut, growCut.dStrengthIn, 0.95) if growCut.isComplete: window.updateCanvas() timer.stop()