コード例 #1
0
ファイル: pylot_main.py プロジェクト: zengqiang2006/shedskin
 def initProcessor(self):
     self.processor = ThreadedQueueProcessor(CameraHandler(self.camera),
                                             4,
                                             use_processes=True)
     self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
     self.draw = ImageDraw.Draw(self.image)
コード例 #2
0
ファイル: pylot_main.py プロジェクト: GINK03/shedskin
 def initProcessor(self):
   self.processor = ThreadedQueueProcessor(CameraHandler(self.camera), 4,
                                           use_processes=True)
   self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
   self.draw = ImageDraw.Draw(self.image)
コード例 #3
0
ファイル: pylot_main.py プロジェクト: zengqiang2006/shedskin
class Viewport(object):
    def __init__(self, camera):
        self.processor = None
        self.camera = camera
        self.jobs = None
        self.image = None
        self.label = None
        self.app = None
        self.count = 0

    def setLabel(self, label):
        assert not self.label
        self.label = label

    def setApp(self, app):
        assert not self.app
        self.app = app

    def shutdown(self):
        if self.processor:
            self.processor.terminate()
            self.processor = None

    def debugPixel(self, x, y):
        if not self.processor:
            self.initProcessor()
        print "Processing pixel: ", x, ", ", y
        d = get_debug()
        set_debug(True)
        self.camera.runPixelRange(((x, x + 1), (y, y + 1)))
        print "Calling get_debug_rays"
        debug_rays = get_debug_rays()
        self.app.drawDebugRays(debug_rays)
        print "Calling clear_debug_rays"
        set_debug(False)
        clear_debug_rays()

    def handleClick(self, event):
        if self.count and not self.jobs:
            self.stop()
        self.debugPixel(event.x, event.y)

    def getBlock(self, i, j, BLOCKS_WIDE, BLOCKS_TALL):
        cols = self.camera.cols
        rows = self.camera.rows
        return ((int(float(cols) / BLOCKS_WIDE * i),
                 int(float(cols) / BLOCKS_WIDE * (i + 1))),
                (int(float(rows) / BLOCKS_TALL * j),
                 int(float(rows) / BLOCKS_TALL * (j + 1))))

    def initProcessor(self):
        self.processor = ThreadedQueueProcessor(CameraHandler(self.camera),
                                                4,
                                                use_processes=True)
        self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
        self.draw = ImageDraw.Draw(self.image)

    def process(self):
        print("Called process on this viewport.")
        try:
            jobs = []
            if self.jobs:
                jobs = self.jobs
                self.jobs = None
            elif not self.count:
                self.startTime = time.time()
                if not self.processor:
                    self.initProcessor()
                else:
                    self.draw.rectangle(
                        ((0, 0), (self.camera.cols, self.camera.rows)),
                        fill="grey")
                BLOCKS_WIDE = 20
                BLOCKS_TALL = 20
                jobs = []
                for i in range(BLOCKS_WIDE):
                    for j in range(BLOCKS_TALL):
                        jobs.append((self.getBlock(i, j, BLOCKS_WIDE,
                                                   BLOCKS_TALL), False))
                random.shuffle(jobs)
                self.count = BLOCKS_WIDE * BLOCKS_TALL
            for j in jobs:
                self.processor.put(j)
            if jobs:
                self.app.root.after(QUEUE_CHECK_TIME, self.checkQueue)
        except:
            if self.processor:
                self.processor.terminate()
            raise

    def imageFromBlock(self, r, pixels):
        (x, xMax), (y, yMax) = r
        w = xMax - x
        h = yMax - y
        return Image.fromstring('RGB', (w, h), pixels)

    def processSingleThreaded(self):
        startTime = time.time()
        self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
        self.draw = ImageDraw.Draw(self.image)
        self.draw.rectangle(((0, 0), (self.camera.cols, self.camera.rows)),
                            fill="grey")
        r, pixels = self.camera.runImage()
        image_temp = self.imageFromBlock(r, pixels)
        self.image.paste(image_temp, (0, 0))
        self.refreshImage()
        print "That took %.3f seconds." % (time.time() - startTime)

    def stop(self):
        if not self.jobs:
            self.jobs = self.processor.clear()

    def refreshImage(self):
        self.imageTk = ImageTk.PhotoImage(self.image)
        self.label.config(image=self.imageTk)

    def checkQueue(self):
        gotData = False
        if self.processor:
            block = self.processor.get(False)
            while block:
                gotData = True
                self.count -= 1
                assert self.count >= 0
                r, pixels = block
                image_temp = self.imageFromBlock(r, pixels)
                (x, _), (y, _) = r
                self.image.paste(image_temp, (x, y))
                block = self.processor.get(False)
            if gotData:
                self.refreshImage()
                if not self.count:
                    print "That took %.3f seconds." % (time.time() -
                                                       self.startTime)
            self.app.root.after(QUEUE_CHECK_TIME, self.checkQueue)


#    else:
#      self.debugPixel(208, 43)
#      self.app.shutdown()

    def drawDebugRays(self, debug_rays):
        for ray, color in debug_rays:
            origin = self.camera.mapPointToScreen(ray.origin)
            if not origin:
                print("Whoa!  Could not map origin!")
                continue
            offset = self.camera.mapPointToScreen(ray.origin + ray.offset)
            if not offset:
                print("Whoa!  Got a bad offset!")
                continue
            self.draw.line([origin, offset], color)
        self.refreshImage()

    def drawShapeCenters(self, color):
        if not self.image:
            self.image = Image.new("RGBA",
                                   (self.camera.cols, self.camera.rows))
            self.draw = ImageDraw.Draw(self.image)
            self.draw.rectangle(((0, 0), (self.camera.cols, self.camera.rows)),
                                fill="black")
        for shape in world.shapes:
            pixel = self.camera.mapPointToScreen(shape.getLocation())
            if pixel:
                self.draw.point(pixel, color)
        self.refreshImage()

    def save(self, name):
        self.image.save(name)
コード例 #4
0
ファイル: pylot_main.py プロジェクト: GINK03/shedskin
class Viewport(object):
  def __init__(self, camera):
    self.processor = None
    self.camera = camera
    self.jobs = None
    self.image = None
    self.label = None
    self.app = None
    self.count = 0

  def setLabel(self, label):
    assert not self.label
    self.label = label

  def setApp(self, app):
    assert not self.app
    self.app = app

  def shutdown(self):
    if self.processor:
      self.processor.terminate()
      self.processor = None

  def debugPixel(self, x, y):
    if not self.processor:
      self.initProcessor()
    print "Processing pixel: ", x, ", ", y
    d = get_debug()
    set_debug(True)
    self.camera.runPixelRange( ((x, x+1), (y, y+1)) )
    print "Calling get_debug_rays"
    debug_rays = get_debug_rays();
    self.app.drawDebugRays(debug_rays)
    print "Calling clear_debug_rays"
    set_debug(False)
    clear_debug_rays()

  def handleClick(self, event):
    if self.count and not self.jobs:
      self.stop()
    self.debugPixel(event.x, event.y)

  def getBlock(self, i, j, BLOCKS_WIDE, BLOCKS_TALL):
    cols = self.camera.cols
    rows = self.camera.rows
    return ((int(float(cols) / BLOCKS_WIDE * i),
             int(float(cols) / BLOCKS_WIDE * (i + 1))),
            (int(float(rows) / BLOCKS_TALL * j),
             int(float(rows) / BLOCKS_TALL * (j + 1))))

  def initProcessor(self):
    self.processor = ThreadedQueueProcessor(CameraHandler(self.camera), 4,
                                            use_processes=True)
    self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
    self.draw = ImageDraw.Draw(self.image)

  def process(self):
    print("Called process on this viewport.")
    try:
      jobs = []
      if self.jobs:
        jobs = self.jobs
        self.jobs = None
      elif not self.count:
        self.startTime = time.time()
        if not self.processor:
          self.initProcessor()
        else:
          self.draw.rectangle(((0, 0), (self.camera.cols, self.camera.rows)),
                              fill="grey")
        BLOCKS_WIDE = 20
        BLOCKS_TALL = 20
        jobs = []
        for i in range(BLOCKS_WIDE):
          for j in range(BLOCKS_TALL):
            jobs.append((self.getBlock(i, j, BLOCKS_WIDE, BLOCKS_TALL), False))
        random.shuffle(jobs)
        self.count = BLOCKS_WIDE * BLOCKS_TALL
      for j in jobs:
        self.processor.put(j)
      if jobs:
        self.app.root.after(QUEUE_CHECK_TIME, self.checkQueue)
    except:
      if self.processor:
        self.processor.terminate()
      raise

  def imageFromBlock(self, r, pixels):
    (x, xMax), (y, yMax) = r
    w = xMax - x
    h = yMax - y
    return Image.fromstring('RGB', (w, h), pixels)

  def processSingleThreaded(self):
    startTime = time.time()
    self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
    self.draw = ImageDraw.Draw(self.image)
    self.draw.rectangle(((0, 0), (self.camera.cols, self.camera.rows)),
                        fill="grey")
    r, pixels = self.camera.runImage()
    image_temp = self.imageFromBlock(r, pixels)
    self.image.paste(image_temp, (0, 0))
    self.refreshImage()
    print "That took %.3f seconds." % (time.time() - startTime)

  def stop(self):
    if not self.jobs:
      self.jobs = self.processor.clear()

  def refreshImage(self):
    self.imageTk = ImageTk.PhotoImage(self.image)
    self.label.config(image=self.imageTk)

  def checkQueue(self):
    gotData = False
    if self.processor:
      block = self.processor.get(False)
      while block:
        gotData = True
        self.count -= 1
        assert self.count >= 0
        r, pixels = block
        image_temp = self.imageFromBlock(r, pixels)
        (x, _), (y, _) = r
        self.image.paste(image_temp, (x, y))
        block = self.processor.get(False)
      if gotData:
        self.refreshImage()
        if not self.count:
          print "That took %.3f seconds." % (time.time() - self.startTime)
      self.app.root.after(QUEUE_CHECK_TIME, self.checkQueue)
#    else:
#      self.debugPixel(208, 43)
#      self.app.shutdown()

  def drawDebugRays(self, debug_rays):
    for ray, color in debug_rays:
      origin = self.camera.mapPointToScreen(ray.origin)
      if not origin:
        print ("Whoa!  Could not map origin!")
        continue
      offset = self.camera.mapPointToScreen(ray.origin + ray.offset)
      if not offset:
        print ("Whoa!  Got a bad offset!")
        continue
      self.draw.line([origin, offset], color)
    self.refreshImage()

  def drawShapeCenters(self, color):
    if not self.image:
      self.image = Image.new("RGBA", (self.camera.cols, self.camera.rows))
      self.draw = ImageDraw.Draw(self.image)
      self.draw.rectangle(((0, 0), (self.camera.cols, self.camera.rows)),
                          fill="black")
    for shape in world.shapes:
      pixel = self.camera.mapPointToScreen(shape.getLocation())
      if pixel:
        self.draw.point(pixel, color)
    self.refreshImage()

  def save(self, name):
    self.image.save(name)