|
| 1 | +# Copyright 2023 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""image provides an example for streaming a greyscale image.""" |
| 15 | + |
| 16 | +from absl import app |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +from examples import common |
| 20 | +import multiscope |
| 21 | + |
| 22 | + |
| 23 | +class Board(object): |
| 24 | + """Board class that scans a rectangle top left -> bottom right (wrapping). |
| 25 | +
|
| 26 | + A NumPy array like np.ones((10, 5)) has 10 rows and 5 columns. |
| 27 | + If you print it, it will be 10 rows tall, and 5 columns wide. |
| 28 | + It will print downwards, so [0, 0] will be the top left. |
| 29 | +
|
| 30 | + This is like np.ones((height, width)). |
| 31 | + If trying to index as Cartesian coordinates, it is my_array[y, x]. |
| 32 | +
|
| 33 | + This can be confusing. |
| 34 | +
|
| 35 | + This example is meant to demonstrate top/bottom left/right. |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, width, height): |
| 39 | + super(Board, self).__init__() |
| 40 | + self.width = width |
| 41 | + self.height = height |
| 42 | + self.pos_x = self.pos_y = 0 |
| 43 | + # 2D array will be rendered as grayscale. All values should be in [0, 255]. |
| 44 | + self.img = np.zeros([self.height, self.width], dtype=np.uint8) |
| 45 | + |
| 46 | + def step(self): |
| 47 | + """Step makes the pixel move one step.""" |
| 48 | + self.pos_x += 1 |
| 49 | + if self.pos_x == self.width: |
| 50 | + self.pos_x = 0 |
| 51 | + self.pos_y += 1 |
| 52 | + if self.pos_y == self.height: |
| 53 | + self.pos_y = 0 |
| 54 | + self.img.fill(0) |
| 55 | + self.img[self.pos_y, self.pos_x] = 255 |
| 56 | + |
| 57 | + |
| 58 | +class Box(object): |
| 59 | + """Box class containing a square ball.""" |
| 60 | + |
| 61 | + def __init__(self, size_y, size_x, ball_size): |
| 62 | + super(Box, self).__init__() |
| 63 | + self.size_x = size_x |
| 64 | + self.size_y = size_y |
| 65 | + self.pos_x = size_x / 2 |
| 66 | + self.pos_y = size_y / 2 |
| 67 | + self.vel_x = 3 |
| 68 | + self.vel_y = 5 |
| 69 | + self.ball_size = ball_size |
| 70 | + self.img = np.zeros([size_y, size_x], dtype=np.uint8) |
| 71 | + |
| 72 | + def step(self): |
| 73 | + """Step makes the box move one step.""" |
| 74 | + self.pos_x += self.vel_x |
| 75 | + if self.pos_x < 0: |
| 76 | + self.pos_x = 0 |
| 77 | + self.vel_x *= -1 |
| 78 | + if self.pos_x + self.ball_size > self.size_x - 1: |
| 79 | + self.pos_x = self.size_x - self.ball_size - 1 |
| 80 | + self.vel_x *= -1 |
| 81 | + |
| 82 | + self.pos_y += self.vel_y |
| 83 | + if self.pos_y < 0: |
| 84 | + self.pos_y = 0 |
| 85 | + self.vel_y *= -1 |
| 86 | + if self.pos_y + self.ball_size > self.size_y - 1: |
| 87 | + self.pos_y = self.size_y - self.ball_size - 1 |
| 88 | + self.vel_y *= -1 |
| 89 | + |
| 90 | + self.img.fill(0) |
| 91 | + for ix in range(self.ball_size): |
| 92 | + for iy in range(self.ball_size): |
| 93 | + self.img[int(self.pos_y + iy), int(self.pos_x + ix)] = 255 |
| 94 | + |
| 95 | + |
| 96 | +def main(_): |
| 97 | + multiscope.start_server() |
| 98 | + box = Box(100, 200, 10) |
| 99 | + board = Board(width=5, height=10) |
| 100 | + clock = multiscope.Ticker("main") |
| 101 | + board_writer = multiscope.ImageWriter("Scanner", clock) |
| 102 | + box_writer = multiscope.ImageWriter("Box", clock) |
| 103 | + for _ in common.step(): |
| 104 | + box_writer.write(box.img) |
| 105 | + board_writer.write(board.img) |
| 106 | + box.step() |
| 107 | + board.step() |
| 108 | + clock.tick() |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + app.run(main) |
0 commit comments