def __init__(self, world, filename=None, simulator=None, once=False, headless=False): logging.info('Initialising vision') if simulator: self.capture = SimCapture(simulator) else: self.capture = Capture(self.rawSize, filename, once) self.headless = headless self.threshold = threshold.AltRaw() self.pre = Preprocessor(self.rawSize, self.threshold, simulator) self.featureEx = FeatureExtraction(self.pre.cropSize) self.interpreter = Interpreter() self.world = world self.gui = GUI(world, self.pre.cropSize, self.threshold) self.histogram = Histogram(self.pre.cropSize) self.times = [] self.N = 0 #debug.thresholdValues(self.threshold.Tblue, self.gui) logging.debug('Vision initialised')
def runWith(args=[]): global mod, interp, tracer, lastArgs lastArgs = args mod = None interp = Interpreter() tracer = DebugTracer(interp) if len(args) > 1: mod = interp.importModule(args[1], tracer, False) repl(interp, tracer, mod)
def __init__(self, world, filename=None, simulator=None): logging.info('Initialising vision') if simulator: self.capture = SimCapture(simulator) else: self.capture = MPlayerCapture(self.rawSize, filename) #self.capture = Capture(self.rawSize, filename) self.threshold = threshold.PrimaryRaw() self.pre = Preprocessor(self.rawSize, self.threshold, simulator) self.featureEx = FeatureExtraction(self.pre.cropSize) self.interpreter = Interpreter() self.world = world self.gui = GUI(world, self.pre.cropSize, self.threshold) self.histogram = Histogram(self.pre.cropSize) self.times=[] self.N=0 debug.thresholdValues(self.threshold.Tfg, self.gui) logging.debug('Vision initialised')
parser.add_option("-s", "--stacktrace", dest="stacktrace", action="store_true", help="always stacktrace") parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="verbose output") parser.add_option("-i", "--io", dest="io", help="select an IO system: simple (default), vt100") (options, args) = parser.parse_args() if len(args) > 1: parser.print_help() sys.exit(1) io = None if options.io == "vt100": io = vt100 if args: vm = Interpreter.from_file(args[0], history=20, io=io) else: print "Welcome to pitybas. Press Ctrl-D to exit." print vm = Repl(history=20, io=io) if options.verbose: vm.print_tokens() print print "-===[ Running %s ]===-" % args[0] if options.ast: print_ast(vm) sys.exit(0) try:
'--io', dest="io", help="select an IO system: simple (default), vt100") (options, args) = parser.parse_args() if len(args) > 1: parser.print_help() sys.exit(1) io = None if options.io == 'vt100': io = vt100 if args: vm = Interpreter.from_file(args[0], history=20, io=io) else: print 'Welcome to pitybas. Press Ctrl-D to exit.' print vm = Repl(history=20, io=io) if options.verbose: vm.print_tokens() print print '-===[ Running %s ]===-' % args[0] if options.ast: print_ast(vm) sys.exit(0) try:
class Vision(): #rawSize = (768,576) rawSize = (640, 480) # Whether to 'crash' when something non-critical like the GUI fails debug = True def __init__(self, world, filename=None, simulator=None, once=False, headless=False): logging.info('Initialising vision') if simulator: self.capture = SimCapture(simulator) else: self.capture = Capture(self.rawSize, filename, once) self.headless = headless self.threshold = threshold.AltRaw() self.pre = Preprocessor(self.rawSize, self.threshold, simulator) self.featureEx = FeatureExtraction(self.pre.cropSize) self.interpreter = Interpreter() self.world = world self.gui = GUI(world, self.pre.cropSize, self.threshold) self.histogram = Histogram(self.pre.cropSize) self.times = [] self.N = 0 #debug.thresholdValues(self.threshold.Tblue, self.gui) logging.debug('Vision initialised') def formatTime(self, t): return time.strftime('%H:%M:%S', time.localtime(t)) \ + ( '%.3f' % (t - math.floor(t)) )[1:] #discard leading 0 def processFrame(self): startTime = time.time() logging.debug("Frame %d at %s", self.N, self.formatTime(startTime)) self.N += 1 logging.debug("Capturing a frame") frame = self.capture.getFrame() logging.debug("Entering preprocessing") standard = self.pre.get_standard_form(frame) bgsub_vals, bgsub_mask = self.pre.bgsub(standard) logging.debug("Entering feature extraction") hist_props_bgsub = self.histogram.calcHistogram(standard) hist_props_abs = self.histogram.calcHistogram(bgsub_vals) self.threshold.updateBGSubThresholds(hist_props_bgsub) #self.threshold.updateAbsThresholds(hist_props_abs) ents = self.featureEx.features(bgsub_vals, self.threshold) logging.debug("Detected entities:", ents) logging.debug("Entering interpreter") self.interpreter.interpret(ents) logging.debug("Entering World") self.world.update(startTime, ents) logging.debug("Updating GUI") if not self.headless: try: bgsub = self.pre.remove_background(standard) self.gui.updateWindow('raw', frame) self.gui.updateWindow('mask', bgsub_mask) self.gui.updateWindow('foreground', bgsub_vals) self.gui.updateWindow('bgsub', bgsub) self.gui.updateWindow('standard', standard) canny = cv.CreateImage(self.pre.cropSize, 8, 1) # adaptive = cv.CreateImage(self.pre.cropSize, 32,3) # tmp = cv.CreateImage(self.pre.cropSize, 8,3) # cv.Convert(standard, adaptive) cv.CvtColor(bgsub, canny, cv.CV_BGR2GRAY) cv.Threshold(canny, canny, 150, 255, cv.CV_THRESH_OTSU) # cv.Threshold(canny, canny, 100, 255, cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C) # cv.Sobel(adaptive, adaptive, 1,1,1) # cv.Convert(adaptive, tmp) # cv.ConvertScale(tmp, tmp, 10) # cv.CvtColor(tmp, canny, cv.CV_BGR2GRAY) # cv.Threshold(canny,canny, 50, 255, cv.CV_THRESH_BINARY) #cv.Canny(canny,canny, 100, 180,3) cv.CvtColor(canny, bgsub, cv.CV_GRAY2BGR) new = self.featureEx.detectCircles(bgsub) self.gui.updateWindow('adaptive', canny) self.gui.updateWindow('new', new) self.gui.draw(ents, startTime) except Exception, e: logging.error("GUI failed: %s", e) if self.debug: raise endTime = time.time() self.times.append((endTime - startTime))
def setUp(self): self.i = Interpreter()
class TestInterpreter(unittest.TestCase): def setUp(self): self.i = Interpreter() def test_parenthesis(self): self.assertEqual(self.i.interpret(""), []) self.assertEqual(self.i.interpret("()"), []) self.assertEqual(self.i.interpret("(4)"), [4]) self.assertEqual(self.i.interpret("(4 2)"), [4, 2]) self.assertEqual(self.i.interpret("(4) (6) (+)"), [10]) def test_arithmetic(self): self.assertEqual(self.i.interpret("(4 2 +)"), [6]) self.assertEqual(self.i.interpret("(4 2 -)"), [2]) self.assertEqual(self.i.interpret("(5 inc)"), [6]) self.assertEqual(self.i.interpret("(7 dec)"), [6]) self.assertEqual(self.i.interpret("(6 4 mod)"), [2]) def test_infix(self): self.assertEqual(self.i.interpret("(6 (+) 2 swap eval)"), [8]) def test_eval(self): self.assertEqual(self.i.interpret("(5 (inc) eval)"), [6]) self.assertEqual(self.i.interpret("(21 (inc dec) eval)"), [21]) def test_wrap(self): self.assertEqual(self.i.interpret("(2 wrap eval)"), [2]) self.assertEqual(self.i.interpret("((5 inc) wrap eval eval)"), [6]) self.assertEqual(self.i.interpret("(2 4 wrap2 eval)"), [2, 4]) self.assertEqual(self.i.interpret("(1 2 3 4 wrap3)"), [1, [2, 3, 4]]) self.assertEqual(self.i.interpret("(1 1 1 2 (inc) eval wrap2 eval)"), [1,1,1,3]) def test_times(self): self.assertEqual(self.i.interpret("(4 (2 +) 3 times)"), [10]) self.assertEqual(self.i.interpret("(13 (inc) 5 times 2 +)"), [20]) def test_if(self): self.assertEqual(self.i.interpret("(1 5 lt)"), [1]) self.assertEqual(self.i.interpret("(1 5 gt)"), [0]) self.assertEqual(self.i.interpret("(1 5 eq)"), [0]) self.assertEqual(self.i.interpret("(5 5 eq)"), [1]) self.assertEqual(self.i.interpret("(5 5 gt)"), [0]) self.assertEqual(self.i.interpret("(5 5 gte)"), [1]) self.assertEqual(self.i.interpret("(20 1 (10 +) if)"), [30]) self.assertEqual(self.i.interpret("(20 0 (10 +) if)"), [20]) # inc-lt50 = dup 50 lt (inc) if self.assertEqual(self.i.interpret("(45 (dup 50 lt (inc) if) eval)"), [46]) self.assertEqual(self.i.interpret("(50 (dup 50 lt (inc) if) eval)"), [50]) self.assertEqual(self.i.interpret("(45 (dup 50 lt (inc) if) 10 times)"), [50]) def test_swap(self): self.assertEqual(self.i.interpret("(10 12 swap lt)"), [0]) self.assertEqual(self.i.interpret("(50 (inc) swap swap eval)"), [51]) def test_default_stack(self): self.assertEqual(self.i.interpret("(4 +)", [20]), [24]) self.assertEqual(self.i.interpret("(4 swap eval)", [20, '+']), [24]) def test_drop(self): self.assertEqual(self.i.interpret("(10 12 14 drop +)"), [22]) def test_chord(self): def _play_note(s): #print "play note=", s.pop() s.pop() self.i.add_builtin('play-note', _play_note) self.assertEqual(self.i.interpret("(66 play-note)"), []) # major-chord = dup play-note 5 + dup play-note 7 + play-note self.assertEqual(self.i.interpret("(66 (dup play-note 5 + dup play-note 7 + play-note) eval)"), [])
class Vision(): #rawSize = (768,576) rawSize = (640, 480) # Whether to 'crash' when something non-critical like the GUI fails debug = True def __init__(self, world, filename=None, simulator=None, once=False, headless=False): logging.info('Initialising vision') if simulator: self.capture = SimCapture(simulator) else: self.capture = Capture(self.rawSize, filename, once) self.headless = headless self.threshold = threshold.AltRaw() self.pre = Preprocessor(self.rawSize, self.threshold, simulator) self.featureEx = FeatureExtraction(self.pre.cropSize) self.interpreter = Interpreter() self.world = world self.gui = GUI(world, self.pre.cropSize, self.threshold) self.histogram = Histogram(self.pre.cropSize) self.times=[] self.N=0 #debug.thresholdValues(self.threshold.Tblue, self.gui) logging.debug('Vision initialised') def formatTime(self, t): return time.strftime('%H:%M:%S', time.localtime(t)) \ + ( '%.3f' % (t - math.floor(t)) )[1:] #discard leading 0 def processFrame(self): startTime = time.time() logging.debug("Frame %d at %s", self.N, self.formatTime(startTime) ) self.N += 1 logging.debug("Capturing a frame") frame = self.capture.getFrame() logging.debug("Entering preprocessing") standard = self.pre.get_standard_form(frame) bgsub_vals, bgsub_mask = self.pre.bgsub(standard) logging.debug("Entering feature extraction") hist_props_bgsub = self.histogram.calcHistogram(standard) hist_props_abs = self.histogram.calcHistogram(bgsub_vals) self.threshold.updateBGSubThresholds(hist_props_bgsub) #self.threshold.updateAbsThresholds(hist_props_abs) ents = self.featureEx.features(bgsub_vals, self.threshold) logging.debug("Detected entities:", ents) logging.debug("Entering interpreter") self.interpreter.interpret(ents) logging.debug("Entering World") self.world.update(startTime, ents) logging.debug("Updating GUI") if not self.headless: try: bgsub = self.pre.remove_background(standard) self.gui.updateWindow('raw', frame) self.gui.updateWindow('mask', bgsub_mask) self.gui.updateWindow('foreground', bgsub_vals) self.gui.updateWindow('bgsub', bgsub) self.gui.updateWindow('standard', standard) canny = cv.CreateImage(self.pre.cropSize, 8,1) # adaptive = cv.CreateImage(self.pre.cropSize, 32,3) # tmp = cv.CreateImage(self.pre.cropSize, 8,3) # cv.Convert(standard, adaptive) cv.CvtColor(bgsub, canny, cv.CV_BGR2GRAY) cv.Threshold(canny, canny, 150, 255, cv.CV_THRESH_OTSU) # cv.Threshold(canny, canny, 100, 255, cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C) # cv.Sobel(adaptive, adaptive, 1,1,1) # cv.Convert(adaptive, tmp) # cv.ConvertScale(tmp, tmp, 10) # cv.CvtColor(tmp, canny, cv.CV_BGR2GRAY) # cv.Threshold(canny,canny, 50, 255, cv.CV_THRESH_BINARY) #cv.Canny(canny,canny, 100, 180,3) cv.CvtColor(canny, bgsub, cv.CV_GRAY2BGR) new = self.featureEx.detectCircles(bgsub) self.gui.updateWindow('adaptive', canny) self.gui.updateWindow('new', new) self.gui.draw(ents, startTime) except Exception, e: logging.error("GUI failed: %s", e) if self.debug: raise endTime = time.time() self.times.append( (endTime - startTime) )
from interpret import Interpreter import sys argv = sys.argv if len(argv) == 1: print("[!] Please supply a program file to compile") elif len(argv) == 2: interpreter = Interpreter(argv[1]) interpreter.interpret()