def run(self): """Initialize the basic board model first, then continually update the image and add new ink to the board""" global debugSurface print "Board Watcher Started: pid %s" % (self.pid,) try: import pydevd pydevd.settrace(stdoutToServer=True, stderrToServer=True, suspend=False) except: pass rawImage = None while self.keepGoing.is_set() and rawImage is None: try: rawImage = deserializeImage(self.imageQueue.get(True, 1)) except EmptyException: pass if not self.keepGoing.is_set(): print "Board watcher stopping" return saveimg(rawImage, name="Initial_Board_Image") self.boardWatcher.setBoardImage(rawImage) self.boardWatcher.setBoardCorners(self.warpCorners, self.targetCorners) boardWidth = self.board.getDimensions()[0] warpImage = warpFrame(rawImage, self.warpCorners, self.targetCorners) warpImage = resizeImage(warpImage, dims=GTKGUISIZE) strokeList = ISC.cvimgToStrokes(flipMat(warpImage), targetWidth=boardWidth)['strokes'] for stk in strokeList: self.board.addStroke(stk) while self.keepGoing.is_set(): rawImage = deserializeImage(self.imageQueue.get(block=True)) self.boardWatcher.updateBoardImage(rawImage) if self.boardWatcher.isCaptureReady: saveimg(rawImage, name="Raw Image") saveimg(self.boardWatcher._fgFilter.getBackgroundImage(), name="BG_Image") (newInk, newErase) = self.boardWatcher.captureBoardDifferences() newInk = warpFrame(newInk, self.warpCorners, self.targetCorners) newInk = resizeImage(newInk, dims=GTKGUISIZE) newErase = warpFrame(newErase, self.warpCorners, self.targetCorners) newErase = resizeImage(newErase, dims=GTKGUISIZE) saveimg(newInk, name="NewInk") saveimg(newErase, name="NewErase") cv.AddWeighted(newInk, -1, newInk, 0, 255, newInk) acceptedImage = self.boardWatcher.acceptCurrentImage() saveimg(acceptedImage, name="AcceptedImage") strokeList = ISC.cvimgToStrokes(flipMat(newInk), targetWidth=boardWidth)['strokes'] for stk in strokeList: self.board.addStroke(stk) print "Board watcher stopping"
def processImage(image, strokeQueue, scaleDims): """This function will extract strokes from an image file. It will add the extracted strokes from the image to strokeQueue, scaled according to scaleDims""" pruneLen = 10 width, height = scaleDims log.debug("Process spawned") try: strokeDict = ImageStrokeConverter.cvimgToStrokes(image) except Exception as e: raise strokes = strokeDict['strokes'] w,h = strokeDict['dims'] scale_x = width / float(w) scale_y = height / float(h) log.debug( "Got %s Strokes" % (len(strokes)) ) for s in strokes: if len(s.Points) > pruneLen: pointList = [] for x,y in s.Points: newPoint = Point(scale_x * x, height - (scale_y *y)) pointList.append(newPoint) newStroke = Stroke(pointList) strokeQueue.put(newStroke)
def loadStrokesFromImage(self, filename=None, image=None): width, height = self.window.get_size() if image is None: if filename is None: return else: image = ImageStrokeConverter.loadFile(filename) p = Process(target = processImage, args = (image, self.strokeQueue, (width,height) ) ) p.start()
def LoadStrokesFromImage(self, image = None): global WIDTH, HEIGHT pruneLen = 10 if image != None: try: strokeDict = ImageStrokeConverter.cvimgToStrokes(image) except: logger.error("Error importing strokes from frame") raise else: fname = askopenfilename(initialdir='/home/jbrowne/src/photos/') if fname == "": return try: logger.debug( "Loading strokes...") strokeDict = ImageStrokeConverter.imageToStrokes(fname) logger.debug( "Loaded %s strokes from '%s'" % (len(strokeDict['strokes']), fname)) except Exception as e: logger.debug( "Error importing strokes from image '%s':\n %s" % (fname, e)) raise strokes = strokeDict['strokes'] w,h = strokeDict['dims'] scale_x = WIDTH / float(w) scale_y = HEIGHT / float(h) for s in strokes: if len(s.points) > pruneLen: pointList = [] for x,y in s.points: newPoint = Point(scale_x * x, HEIGHT - (scale_y *y)) pointList.append(newPoint) newStroke = Stroke(pointList) self.AddStroke(newStroke)
def LoadStrokesFromImage(self): fname = askopenfilename(initialdir='./') if fname == "": return try: logger.debug( "Loading strokes...") strokes = ImageStrokeConverter.imageToStrokes(fname) except Exception as e: logger.debug( "Error importing strokes from image '%s':\n %s" % (fname, e)) return logger.debug( "Loaded %s strokes from '%s'" % (len(strokes), fname)) for s in strokes: newStroke = Stroke() for x,y in s.points: scale = WIDTH / float(1280) newPoint = Point(scale * x,HEIGHT - scale * y) newStroke.addPoint(newPoint) #self.Board.AddStroke(newStroke) self.StrokeList.append(newStroke)
def drawStroke(self, *args, **kargs): op = partial(TkSketchFrame.do_drawStroke, self, *args, **kargs) self.OpQueue.put(op) if __name__ == "__main__": if len(sys.argv) > 1: #Do something with the CLI arguments fname = sys.argv[1] board = Board() _initializeBoard(board) stkLoader = StrokeStorage(fname+".dat") stkDict = ImageStrokeConverter.imageToStrokes(fname) stks = stkDict['strokes'] WIDTH, HEIGHT = stkDict['dims'] strokeList = [] for s in stks: pointList = [] for x,y in s.points: newPoint = Point(x, HEIGHT - y) pointList.append(newPoint) strokeList.append(Stroke(pointList)) board.AddStroke(Stroke(pointList)) stkLoader.saveStrokes(strokeList) #fout = open("standalone.xml", "w") #print >> fout, ET.tostring(board.xml(WIDTH, HEIGHT)) #fout.close()
def traceStroke(stroke): """Take in a true stroke with timing data, bitmap it and then trace the data for it""" #logger.debug("Stripping Timing Information from Stroke") #logger.debug("Stroke in, %s points" % len(stroke.Points)) strokeLen = GeomUtils.strokeLength(stroke) sNorm = GeomUtils.strokeNormalizeSpacing(stroke, int(len(stroke.Points) * 1.5)) #Normalize to ten pixel spacing graph = {} #Graph structure looks like # { <point (x, y)> : {'kids' : <set of Points>, 'thickness' : <number>} } #Find self intersections intersections = {} for i in range(len(sNorm.Points) - 1): seg1 = (sNorm.Points[i], sNorm.Points[i+1]) for j in range(i+1, len(sNorm.Points) - 1 ): seg2 = (sNorm.Points[j], sNorm.Points[j+1]) cross = GeomUtils.getLinesIntersection( seg1, seg2) #Create a new node at the intersection if cross != None \ and cross != seg1[0] \ and cross != seg2[0]: crossPt = (cross.X, cross.Y) intDict = intersections.setdefault(crossPt, {'kids' : set(), 'thickness' : 1}) for pt in seg1 + seg2: #Add the segment endpoints as kids coords = (int(pt.X), int(pt.Y)) if coords != crossPt: intDict['kids'].add(coords) prevPt = None #for i in range(1, len(sNorm.Points)): for pt in sNorm.Points: curPt = (int(pt.X), int(pt.Y)) if prevPt != None: #prevPt = (pt.X, pt.Y) graph[curPt] = {'kids' : set([prevPt]), 'thickness':1} graph[prevPt]['kids'].add(curPt) else: graph[curPt] = {'kids' : set(), 'thickness' :1 } prevPt = curPt for pt, ptDict in intersections.items(): for k in graph.get(pt, {'kids' : []})['kids']: ptDict['kids'].add(k) graph[k]['kids'].add(pt) for k in ptDict['kids']: graph[k]['kids'].add(pt) graph[pt] = ptDict strokeList = ImageStrokeConverter.graphToStrokes(graph) if len(strokeList) > 1: #logger.debug("Stroke tracing split into multiple strokes") strokeList.sort(key=(lambda s: -len(s.points))) retPts = [] if len(strokeList) > 0: for pt in strokeList[0].points: #logger.debug("Adding point %s" % (str(pt))) retPts.append(Point(pt[0], pt[1])) #logger.debug("Stroke out, %s points" % len(retPts)) retStroke = Stroke(retPts) #saver = StrokeStorage.StrokeStorage() #saver.saveStrokes([stroke, retStroke]) return retStroke