Exemplo n.º 1
0
    def draw(self):
        surface = gizeh.Surface(width=self.width,
                                height=self.height,
                                bg_color=(1, 1, 1))

        if self.bg:
            fill = gizeh.ImagePattern(self.bg.data, self.bg.pos, filter='best')
            bg_circle = gizeh.circle(r=self.width * 5, fill=fill)
            bg_circle.draw(surface)

        if self.dragon:
            xy_pos = self.dragon.pos
            dragon_circle = gizeh.circle(r=10, xy=xy_pos, fill=(1, 0, 0))
            dragon_circle.draw(surface)

        for target in self.target_list:
            circle = gizeh.circle(r=target.size,
                                  xy=target.pos,
                                  fill=target.color)
            circle.draw(surface)

        surface.get_npimage()

        img_name = "temp/scr" + str(self.time) + ".png"
        surface.write_to_png(img_name)

        self.time += 1
Exemplo n.º 2
0
def draw_seconds(seconds):
    center = numpy.array([40 / 2, 16 / 2])

    surface = gizeh.Surface(width=40, height=16,
                            bg_color=(0, 0, 0))  # in pixels

    image = alpha_to_color(misc.imread('alpaka.png'))
    alpaka = gizeh.ImagePattern(image, pixel_zero=center)

    r = gizeh.rectangle(lx=40, ly=16, xy=(24, 8), fill=alpaka)
    r.draw(surface)

    text = gizeh.text("{:02d}".format(seconds),
                      fontfamily="DIN Condensed",
                      fontsize=20,
                      fill=(0.99, 0.82, 0.25),
                      xy=(32, 8),
                      angle=0)  # Pi/12)
    text.draw(surface)

    #surface.write_to_png("circle.png")
    out = surface.get_npimage()
    fb = out.tobytes()

    prepped = prepare_message(fb, unpack=False)
    send_array(prepped, '100.100.218.106')
Exemplo n.º 3
0
    def fl(im):
        """ transforms the image by adding a zoom """

        surface = gz.Surface.from_image(im)
        fill = gz.ImagePattern(im, pixel_zero=target_center,
                               filter='best')
        line = gz.polyline([target_center, zoom_center],
                           stroke_width=3)
        circle_target= gz.circle(zoom_radius, xy=target_center,
                                 fill=fill, stroke_width=2)
        circle_zoom = gz.circle(zoom_radius, xy=zoom_center, fill=fill,
                       stroke_width=2).scale(zoomx, center=zoom_center)
        for e in line, circle_zoom, circle_target:
            e.draw(surface)
        return surface.get_npimage()
Exemplo n.º 4
0
def my_filter(get_frame, t):
    """ Transforms a frame (given by get_frame(t)) into a different
    frame, using vector graphics."""

    surface = gz.Surface(w, h)
    fill = (gz.ImagePattern(get_frame(t),
                            pixel_zero=center).scale(1.5, center=center))
    for (nfaces, angle, f) in ([3, 0, 1.0 / 6], [5, np.pi / 3, 3.0 / 6],
                               [7, 2 * np.pi / 3, 5.0 / 6]):
        xy = (f * w, h * (.5 + .05 * np.sin(2 * np.pi * (t / d + f))))
        shape = gz.regular_polygon(w / 6,
                                   nfaces,
                                   xy=xy,
                                   fill=fill.rotate(angle, center))
        shape.draw(surface)
    return surface.get_npimage()
Exemplo n.º 5
0
def draw_gt_bboxes(data, image_size=512, black=False, extra=1):
    colors = create_pascal_label_colormap(data.num_classes - 1)

    for images, images_info, (images_bboxes, images_classes) in data:
        image = Image.open(
            os.path.join(data.root_dir[0], 'JPEGImages',
                         images_info['id'] + '.jpg'))
        if black:
            d = np.load(
                os.path.join(
                    os.path.join(data.root_dir[0], 'Raw',
                                 images_info['id'] + '.npz')))
            signal = d['signal']
            samp_rate = d['samp_rate']
            N_fft = d['N_fft']
            N_overlap = d['N_overlap']
            signal = signal[0] + 1.j * signal[1]
            st, _, _ = SSDemoDataset.stft(signal,
                                          N_fft=N_fft,
                                          N_overlap=N_overlap,
                                          samp_rate=samp_rate)
            image = Image.fromarray(np.uint8(cm.binary_r(np.abs(st)) * 255))
        image = image.resize((image_size, image_size))
        image = gizeh.ImagePattern(np.array(image))
        image = gizeh.rectangle(2 * image_size,
                                2 * image_size,
                                xy=(0, 0),
                                fill=image)
        pdf = gizeh.PDFSurface(
            'detections/gt_{}.pdf'.format(images_info['id']), image_size,
            image_size)
        image.draw(pdf)
        _bboxes = images_bboxes * image_size
        _classes = images_classes
        argsort_x = torch.argsort(_bboxes[:, 0])
        argsort_y = torch.argsort(_bboxes[argsort_x][:, 1])
        _bboxes = _bboxes[argsort_x][argsort_y]
        _classes = _classes[argsort_x][argsort_y]
        torch.manual_seed(5)
        for bb, cl in zip(_bboxes, _classes):
            cl = torch.argmax(cl)
            for _ in range(extra):
                b = bb + torch.rand(4) * 10 if extra > 1 else bb
                rect = [[int(b[0]), int(b[1])], [int(b[2]),
                                                 int(b[1])],
                        [int(b[2]), int(b[3])], [int(b[0]),
                                                 int(b[3])]]
                rect = gizeh.polyline(rect,
                                      close_path=True,
                                      stroke_width=4,
                                      stroke=colors[cl - 1])
                rect.draw(pdf)
        torch.manual_seed(5)
        for bb, cl in zip(_bboxes, _classes):
            cl = torch.argmax(cl)
            for _ in range(extra):
                b = bb + torch.rand(4) * 10 if extra > 1 else bb
                w, h = len(data.classes[cl]) * 8.5 + 8, 15
                rect = gizeh.rectangle(w,
                                       h,
                                       xy=(int(b[0] + w / 2 - 2),
                                           max((int(b[1] - h / 2 + 7)), 7)),
                                       fill=(1, 1, 1, 0.5))

                rect.draw(pdf)
                txt = gizeh.text(
                    '{}'.format(data.classes[cl]),
                    'monospace',
                    fontsize=16,
                    xy=(int(b[0]), max((int(b[1])), 7)),  # - 12),
                    fill=(0., 0., 0.),
                    v_align='center',
                    h_align='left')
                txt.draw(pdf)
        pdf.flush()
        pdf.finish()
Exemplo n.º 6
0
def draw_predicted_bboxes(model,
                          data,
                          image_size=512,
                          threshold=.8,
                          black=False,
                          clean=False):
    colors = create_pascal_label_colormap(data.num_classes - 1)

    bboxes, classes, confidence, image_idx = model.predict(
        dataset=data,
        confidence_threshold=threshold,
        overlap_threshold=.4,
        show=False,
        export=False)

    for idx in np.unique(image_idx):
        image = Image.open(
            os.path.join(data.root_dir[0], 'JPEGImages', idx + '.jpg'))
        if black:
            d = np.load(
                os.path.join(
                    os.path.join(data.root_dir[0], 'Raw', idx + '.npz')))
            signal = d['signal']
            samp_rate = d['samp_rate']
            N_fft = d['N_fft']
            N_overlap = d['N_overlap']
            signal = signal[0] + 1.j * signal[1]
            st, _, _ = SSDemoDataset.stft(signal,
                                          N_fft=N_fft,
                                          N_overlap=N_overlap,
                                          samp_rate=samp_rate)
            image = Image.fromarray(np.uint8(cm.binary_r(np.abs(st)) * 255))
        image = image.resize((image_size, image_size))
        image = gizeh.ImagePattern(np.array(image))
        image = gizeh.rectangle(2 * image_size,
                                2 * image_size,
                                xy=(0, 0),
                                fill=image)
        pdf = gizeh.PDFSurface('detections/frcn_{}.pdf'.format(idx),
                               image_size, image_size)
        image.draw(pdf)
        mask = np.array(image_idx) == idx
        _bboxes = bboxes[mask]
        _classes = classes[mask]
        _confidence = confidence[mask]
        argsort_x = torch.argsort(_bboxes[:, 0])
        argsort_y = torch.argsort(_bboxes[argsort_x][:, 1])
        _bboxes = _bboxes[argsort_x][argsort_y]
        _classes = _classes[argsort_x][argsort_y]
        _confidence = _confidence[argsort_x][argsort_y]
        if not clean:
            for bb, cl, co in zip(_bboxes, _classes, _confidence):
                rect = [[int(bb[0]), int(bb[1])], [int(bb[2]),
                                                   int(bb[1])],
                        [int(bb[2]), int(bb[3])], [int(bb[0]),
                                                   int(bb[3])]]
                rect = gizeh.polyline(rect,
                                      close_path=True,
                                      stroke_width=4,
                                      stroke=colors[cl - 1])
                rect.draw(pdf)
            for bb, cl, co in zip(_bboxes, _classes, _confidence):
                w, h = len(data.classes[cl]) * 8.5 + 65, 15
                rect = gizeh.rectangle(w,
                                       h,
                                       xy=(int(bb[0] + w / 2 - 2),
                                           int(bb[1] - h / 2 + 7)),
                                       fill=(1, 1, 1, 0.5))

                rect.draw(pdf)
                txt = gizeh.text(
                    '{}: {:.2f}'.format(data.classes[cl], co),
                    'monospace',
                    fontsize=16,
                    xy=(int(bb[0]), int(bb[1])),  # - 12),
                    fill=(0., 0., 0.),
                    v_align='center',
                    h_align='left')
                txt.draw(pdf)
        pdf.flush()
        pdf.finish()
Exemplo n.º 7
0
def show_ground_truth(model,
                      dataset,
                      idx,
                      device='cpu',
                      classes='../../../Data/SS/ss.names',
                      overlap_threshold=1.,
                      black=False,
                      clean=False):

    lst_classes = read_classes(classes)
    colors = create_pascal_label_colormap(len(lst_classes))

    data = dataset[idx]
    images, image_info, targets = data
    model.set_image_size(images.shape[-2:])
    images = images[None]
    image_info = {k: [v] for k, v in image_info.items()}
    image_info['padding'] = list(map(lambda x: [x], image_info['padding'][0]))
    image_info['scale'] = list(map(lambda x: [x], image_info['scale'][0]))
    targets = [t[None].to(device) for t in targets]
    bboxes, classes, confidences, image_idx = model.process_bboxes(
        targets, image_info, 1e-5, overlap_threshold, nms=False)

    for i, data in enumerate(zip(image_info['id'], images)):
        idx, image = data
        image = Image.open(
            os.path.join(dataset.root_dir[0], 'JPEGImages',
                         image_info['id'][0] + '.jpg'))
        if black:
            for images, images_info, targets in dataset:
                if images_info['id'] == image_info['id'][0]:
                    image = Image.fromarray(
                        np.uint8(cm.binary_r(images[0].numpy()) * 255))
                    break
        image = image.resize(model.default_image_size)
        image = gizeh.ImagePattern(np.array(image))
        image = gizeh.rectangle(2. * model.default_image_size[0],
                                2. * model.default_image_size[1],
                                xy=(0, 0),
                                fill=image)
        pdf = gizeh.PDFSurface('detections/yolo_gt_{}.pdf'.format(idx),
                               model.default_image_size[0],
                               model.default_image_size[1])
        image.draw(pdf)
        mask = np.array(image_idx) == idx
        if not clean:
            for bb, cl, co in zip(bboxes[mask], classes[mask],
                                  confidences[mask]):
                rect = [[int(bb[0]), int(bb[1])], [int(bb[2]),
                                                   int(bb[1])],
                        [int(bb[2]), int(bb[3])], [int(bb[0]),
                                                   int(bb[3])]]
                rect = gizeh.polyline(rect,
                                      close_path=True,
                                      stroke_width=4,
                                      stroke=colors[cl])
                rect.draw(pdf)
            for bb, cl, co in zip(bboxes[mask], classes[mask],
                                  confidences[mask]):
                w, h = len(dataset.classes[cl]) * 8.5 + 8, 15
                rect = gizeh.rectangle(
                    w,
                    h,
                    xy=(int(bb[0] + w / 2 - 2), max((int(bb[1] - h / 2 + 7)),
                                                    7)),  # - 2)),
                    # fill=colors[cl])
                    fill=(1, 1, 1, 0.5))

                rect.draw(pdf)
                txt = gizeh.text(
                    lst_classes[cl],
                    # 'Helvetica',
                    'monospace',
                    fontsize=16,
                    xy=(int(bb[0]), max((int(bb[1]), 10))),  # - 12),
                    fill=(0., 0., 0.),
                    v_align='center',  # 'bottom',
                    h_align='left')
                txt.draw(pdf)
        pdf.flush()
        pdf.finish()
Exemplo n.º 8
0
def make_frame(t):

    surface = gz.Surface(size, size, bg_color=blue)
    progress = t / dur

    # background lines
    x = 500 * progress
    for i in range(10):
        gz.polyline(points=[(0, 500 - x - i * 50), (x + i * 50, 500)],
                    stroke=pink,
                    stroke_width=5).draw(surface)
        gz.polyline(points=[(x + i * 50, 0), (500, 500 - x - i * 50)],
                    stroke=pink,
                    stroke_width=5).draw(surface)

    scale = bounce(progress)
    randPoints = np.random.randint(500, size=2)
    randR = np.random.randint(100)
    flickeringYellow = (1, .972, .0823, np.random.rand(1))

    # pink 'triangle'
    tr = gz.polyline(points=[(0, 40), (460, 500), (0, 500)],
                     stroke=flickeringYellow,
                     stroke_width=3,
                     fill=pink)
    tr.draw(surface)
    gz.polyline(points=[(0, 500), (230, 270)],
                stroke=flickeringYellow,
                stroke_width=3).draw(surface)
    gz.polyline(points=[(230, 270), (230, 500)],
                stroke=flickeringYellow,
                stroke_width=3).draw(surface)
    gz.polyline(points=[(230, 270), (0, 270)],
                stroke=flickeringYellow,
                stroke_width=3).draw(surface)

    # random squares
    e = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    for i in e:
        if i == progress:
            # if progress%2:
            gz.regular_polygon(r=randR,
                               n=4,
                               xy=randPoints,
                               fill=flickeringYellow).scale(
                                   1 + scale, center=center).draw(surface)
            gz.regular_polygon(r=randR,
                               n=4,
                               xy=randPoints,
                               fill=flickeringYellow).scale(
                                   1 + scale, center=center).draw(surface)

    # really cool rabbit
    img = gz.ImagePattern(image=data,
                          pixel_zero=[110, 150],
                          filter="best",
                          extend="none")

    r = np.random.choice([1, -1, 0], 1, p=[0.1, 0.1,
                                           0.8])  # disturbing rotation
    circ = gz.circle(r=130, stroke_width=5, stroke=yellow, fill=img,
                     xy=center).rotate((r[0] * qpi) * progress, center=center)
    circ.draw(surface)

    return surface.get_npimage()