示例#1
0
def visualize_consumer(to):
    """A consumer that visualizes the data on an image

    :param TrackerOut to: A TrackerOut objects that receives data
    """
    img = Image((600, 600))
    while True:
        to.flush()
        img.clearLayers()
        for pos, timestamp in to:
            for finger in pos:
                if finger is not None:
                    img.dl().rectangle2pts((finger[0]-4, finger[1]-4),
                                           (finger[0]+4, finger[1]+4),
                                           color=Color.RED,
                                           filled=True)
            print "{}: {}".format(int(timestamp), pos)
            img.show()
        to.clear()
    del img
示例#2
0
def get_bounding_box(keyword, url, filename):
    # get the image
    img = Image(url)

    # resize the image so things aren't so slow, if necessary
    w, h = img.size()
    if w > 1200 or h > 1200:
        maxdim = max(w, h)
        ratio = math.ceil(maxdim/800.0)
        print "   resizing..."
        img = img.resize(w=int(w/ratio), h=int(h/ratio))
    else:
        ratio = 1

    # get the canvas
    disp = Display((800, 800))
    # text overlay
    textlayer = DrawingLayer(img.size())
    textlayer.setFontSize(30)
    cx, cy = 10, 10
    for xoff in range(-2, 3):
        for yoff in range(-2, 3):
            textlayer.text(keyword, (cx + xoff, cy + yoff), color=Color.BLACK)
    textlayer.text(keyword, (cx, cy), color=Color.WHITE)

    # two points to declare a bounding box
    point1 = None
    point2 = None
    while disp.isNotDone():
        cursor = (disp.mouseX, disp.mouseY)
        if disp.leftButtonUp:
            if point1 and point2:
                point1 = None
                point2 = None
            if point1:
                point2 = disp.leftButtonUpPosition()
            else:
                point1 = disp.leftButtonUpPosition()
        bb = None
        if point1 and point2:
            bb = disp.pointsToBoundingBox(point1, point2)
        elif point1 and not point2:
            bb = disp.pointsToBoundingBox(point1, cursor)

        img.clearLayers()
        drawlayer = DrawingLayer(img.size())
        if bb:
            drawlayer.rectangle((bb[0], bb[1]), (bb[2], bb[3]), color=Color.RED)

        # keyboard commands
        if pygame.key.get_pressed()[pygame.K_s]:
            # skip for now
            raise Skip()
        elif pygame.key.get_pressed()[pygame.K_b]:
            # mark it as an invalid picture
            raise BadImage()
        elif pygame.key.get_pressed()[pygame.K_RETURN]:
            if point1 and point2:
                bb = disp.pointsToBoundingBox(scale(ratio, point1), scale(ratio, point2))
                return bb
            elif not point1 and not point2:
                bb = disp.pointsToBoundingBox((0, 0), (w, h))
                return bb


        drawlayer.line((cursor[0], 0), (cursor[0], img.height), color=Color.BLUE)
        drawlayer.line((0, cursor[1]), (img.width, cursor[1]), color=Color.BLUE)
        #drawlayer.circle(cursor, 2, color=Color.BLUE, filled=True)
        img.addDrawingLayer(textlayer)
        img.addDrawingLayer(drawlayer)
        img.save(disp)
示例#3
0
class Window2:
    generation = 0

    def __init__(self, filename, scale=1):
        filename = os.path.expanduser(filename)
        self.img = Image(filename)
        self.max_x, self.max_y = self.img.width, self.img.height
        self.scale = scale

        self.array_map = np.array([[0 for y in range(self.max_y)] for x in range(self.max_x)])
        for x in range(self.max_x):
            for y in range(self.max_y):
                pixel = self.img.getPixel(x, y)
                self.array_map[x][y] = (pixel == (255, 255, 255))

        # scale image
        self.img = self.img.resize(self.img.width*scale, self.img.height*scale)
        self.img_size = self.img.width, self.img.height

        self.display = Display(self.img_size)
        self.img.save(self.display)

    def dot(self, p, color=Color.WHITE, size=0):
        x, y = p[0], p[1]
        #print "Drawing robot particle at {}, {}".format(x, y)
        if x < 0 or x >= self.max_x:
            print "Oh my god! x=", x
            raise RuntimeError
        if y < 0 or y >= self.max_y:
            print "Oh shit! y=", y
            raise RuntimeError
        else:
            self.img.dl().circle(center=(x*self.scale, y*self.scale), radius=size, color=color, width=1, filled=True)

    def dot_red(self, p, color=Color.RED):
        self.dot(p, color, 2)

    def dots(self, coords, color=Color.WHITE, size=0):
        for (x, y) in coords:
            self.dot((x, y), color, size)

    def clear(self):
        self.img = Image(self.img_size)
        #self.display.clear()
        self.img.save(self.display)

    def clear_dl(self):
        self.img.clearLayers()
        self.img.save(self.display)

    def show(self):
        self.img.save(self.display)
        self.generation += 1
        print "Generation = {}".format(self.generation)
        self.wait_for_mouse()
        print "Mouse pressed!"

    def draw_robot(self, position, orientation):
        color = Color.RED
        #self.img.drawRectangle(p[0], p[1], 20, 40, color, 1)
        self.dot(position, color, 2)

        length = 20
        bx = int(round(position[0] + cos(orientation) * length))
        by = int(round(position[1] + sin(orientation) * length))

        self.vector(position, orientation, length, detect_collision=False, color=color)
        self.vector((bx, by), orientation - 3*pi/4, length=8, detect_collision=False, color=color)
        self.vector((bx, by), orientation + 3*pi/4, length=8, detect_collision=False, color=color)

    def vector(self, x, orientation, length, detect_collision=True, color=Color.FORESTGREEN):
        bx = int(round(x[0] + cos(orientation) * length))
        by = int(round(x[1] + sin(orientation) * length))
        #self.dot_red((bx, by))
        return self.line(x, (bx, by), detect_collision=detect_collision, color=color)
        #return bx, by

    # a = startpunkt, b = endpunkt
    #@profile
    def line(self, a, b, detect_collision=True, color=Color.BLUE):
        """http://en.wikipedia.org/wiki/Bresenham's_line_algorithm"""

        # performance => use local vars
        max_x = self.max_x
        max_y = self.max_y
        array_map = self.array_map

        x0, y0 = a
        x1, y1 = b
        dx = abs(x1-x0)
        dy = -abs(y1-y0)
        if x0 < x1:
            sx = 1
        else:
            sx = -1

        if y0 < y1:
            sy = 1
        else:
            sy = -1
        err = dx+dy

        while True:
            if x0 <= 0 or x0 >= max_x or y0 <= 0 or y0 >= max_y:
                break
            if color:
                self.dot((x0, y0), color, 0)
            #if detect_collision and self.img.getPixel(x0, y0) == (255, 255, 255):
            if detect_collision and array_map[x0][y0]:
                break
            if x0 == x1 and y0 == y1:
                break
            e2 = 2*err

            if e2 > dy:
                err += dy
                x0 += sx

            if x0 == x1 and y0 == y1:
                #if color:
                #    self.dot((x0, y0), color, 0)
                break

            if e2 < dx:
                err = err + dx
                y0 += sy
        return x0, y0

    def wait_for_mouse(self):
        while True:
            for event in pg.event.get():
                if event.type == pg.MOUSEBUTTONDOWN:
                    print event
                    #self.clear()
                    return