Compare commits
No commits in common. "788c03983405df1686f17e2894ba3fa7c5bcea4a" and "a877e1cc12f27b590aaaf5fd65ee3cacfd9b55db" have entirely different histories.
788c039834
...
a877e1cc12
1 changed files with 86 additions and 133 deletions
219
moonwalk.py
219
moonwalk.py
|
@ -35,38 +35,6 @@ class Params:
|
|||
object_velocity: float = 40 # pixels/second
|
||||
velocity_decay: float = 1 # make system a bit springy
|
||||
|
||||
def add_listener(self, attr: str, callback: callable):
|
||||
"""
|
||||
Add a listener for a change, callback gets
|
||||
called with (attribute_name, old_value, new_value)
|
||||
"""
|
||||
if not attr in self.get_listeners():
|
||||
self._listeners[attr] = []
|
||||
self._listeners[attr].append(callback)
|
||||
|
||||
def get_listeners(self) -> dict[str, callable]:
|
||||
if not hasattr(self, "_listeners"):
|
||||
self._listeners = {}
|
||||
return self._listeners
|
||||
|
||||
def __setattr__(self, attr, val):
|
||||
if attr == '_listeners':
|
||||
super().__setattr__(attr, val)
|
||||
return
|
||||
|
||||
if attr == 'tracker_fps' and val < .1:
|
||||
# limit tracker fps
|
||||
val = .1
|
||||
|
||||
if attr == 'emitter_speed' and val < .1:
|
||||
# limit tracker fps
|
||||
val = .1
|
||||
|
||||
old_val = getattr(self, attr)
|
||||
super().__setattr__(attr, val)
|
||||
if attr in self.get_listeners():
|
||||
for listener in self._listeners[attr]:
|
||||
listener(attr, old_val, val)
|
||||
|
||||
|
||||
class DetectedObject:
|
||||
|
@ -79,7 +47,7 @@ class DetectedObject:
|
|||
self.w = 10 # width
|
||||
self.h = 20 # height
|
||||
|
||||
self.shape = pyglet.shapes.Rectangle(self.l, self.t, self.w, self.h, color=(255, 22, 20), batch=self.canvas.batch_figures)
|
||||
self.shape = pyglet.shapes.Rectangle(self.l, self.t, self.w, self.h, color=(255, 22, 20), batch=self.canvas.renderer.batch_figures)
|
||||
# rectangle.opacity = 128
|
||||
# rectangle.rotation = 33
|
||||
#TODO renderer
|
||||
|
@ -115,123 +83,33 @@ class Canvas:
|
|||
"""
|
||||
A canvas with moving objects
|
||||
"""
|
||||
def __init__(self, params: Params):
|
||||
def __init__(self, params: Params, renderer: Renderer):
|
||||
self.width = 1280
|
||||
self.height = 720
|
||||
self.objects: list[DetectedObject] = []
|
||||
self.lastSnapshot: Optional[float] = None
|
||||
self.params = params
|
||||
self.emitter = ObjectEmitter(self.params, self)
|
||||
|
||||
|
||||
self.hide_stats = False
|
||||
|
||||
config = pyglet.gl.Config(sample_buffers=1, samples=4, double_buffer=True)
|
||||
# , fullscreen=self.config.render_window
|
||||
self.window = pyglet.window.Window(width=self.width, height=self.height, config=config, fullscreen=False)
|
||||
self.window.set_handler('on_draw', self.on_draw)
|
||||
self.window.set_handler('on_key_press', self.on_key_press)
|
||||
self.window.set_handler('on_mouse_scroll', self.on_mouse_scroll)
|
||||
|
||||
self.window.set_handler('on_refresh', self.on_refresh)
|
||||
|
||||
# self.window.set_handler('on_refresh', self.on_refresh)
|
||||
# self.window.set_handler('on_close', self.on_close)
|
||||
|
||||
# Purple background color:
|
||||
# pyglet.gl.glClearColor(*AnimConfig.clear_color)
|
||||
self.fps_display = pyglet.window.FPSDisplay(window=self.window, color=(255,255,255,255))
|
||||
self.fps_display.label.x = self.window.width - 150
|
||||
self.fps_display.label.y = self.window.height - 17
|
||||
self.fps_display.label.bold = False
|
||||
self.fps_display.label.font_size = 10
|
||||
|
||||
self.label_time = pyglet.text.Label("t", x=20, y=self.height - 17, color=(255,255,255,255))
|
||||
|
||||
self.batch_figures = pyglet.graphics.Batch()
|
||||
self.batch_bounding_boxes = pyglet.graphics.Batch()
|
||||
self.batch_info = pyglet.graphics.Batch()
|
||||
|
||||
self.renderer = renderer
|
||||
self.tracks = []
|
||||
|
||||
self.labels = {
|
||||
'objects': pyglet.text.Label("", x=20, y=30, color=(255,255,255,255), batch=self.batch_info),
|
||||
'tracks': pyglet.text.Label("", x=120, y=30, color=(255,255,255,255), batch=self.batch_info),
|
||||
'objects': pyglet.text.Label("", x=20, y=30, color=(255,255,255,255), batch=self.renderer.batch_info),
|
||||
'tracks': pyglet.text.Label("", x=120, y=30, color=(255,255,255,255), batch=self.renderer.batch_info),
|
||||
}
|
||||
for i, field in enumerate(dataclasses.fields(self.params)):
|
||||
self.labels[field.name] = pyglet.text.Label(f"{field.name}: {field.default}", x=20, y=30 + 15*(i+1), color=(255,255,255,255), batch=self.batch_info)
|
||||
self.labels[field.name] = pyglet.text.Label(f"{field.name}: {field.default}", x=20, y=30 + 15*(i+1), color=(255,255,255,255), batch=self.renderer.batch_info)
|
||||
|
||||
# TODO: Add a number next to the box using pyglet.graphics.Group()
|
||||
self.track_shapes = defaultdict(lambda: pyglet.shapes.Box(0,0,0,0,color=(0,255,0),thickness=2, batch=self.batch_bounding_boxes))
|
||||
self.track_shapes = defaultdict(lambda: pyglet.shapes.Box(0,0,0,0,color=(0,255,0),thickness=2, batch=self.renderer.batch_bounding_boxes))
|
||||
|
||||
self.renderer.window.set_handler('on_refresh', self.on_refresh)
|
||||
|
||||
self.tracker = Sort(max_age=5, min_hits=2, iou_threshold=0) #DeepSort(max_age=5)
|
||||
|
||||
pyglet.clock.schedule_interval(self.on_track, 1/self.params.tracker_fps)
|
||||
|
||||
self.interval_items: list[pyglet.clock._ScheduledIntervalItem] = [i for i in pyglet.clock._default._schedule_interval_items if i.func == self.on_track]
|
||||
self.params.add_listener('tracker_fps', self.on_tracker_fps_change)
|
||||
|
||||
def on_tracker_fps_change(self, field, old_value, new_value):
|
||||
"""
|
||||
Param dataclass listener for changes to tracker_fps
|
||||
"""
|
||||
for ii in self.interval_items:
|
||||
ii.interval = 1/new_value
|
||||
logger.debug(f"Set tracker interval to {ii.interval}")
|
||||
|
||||
def run(self):
|
||||
|
||||
self.event_loop = pyglet.app.EventLoop()
|
||||
# pyglet.clock.schedule_interval(self.check_running, 0.1)
|
||||
# pyglet.clock.schedule(self.check_frames)
|
||||
# pyglet.clock.schedule(self.track)
|
||||
self.event_loop.run()
|
||||
|
||||
def on_draw(self):
|
||||
# print(time.monotonic())
|
||||
self.label_time.text = f"{time.monotonic()}"
|
||||
self.window.clear()
|
||||
self.batch_figures.draw()
|
||||
self.batch_bounding_boxes.draw()
|
||||
self.batch_info.draw()
|
||||
self.fps_display.draw()
|
||||
self.label_time.draw()
|
||||
|
||||
def on_close(self):
|
||||
logger.info('closing')
|
||||
pass
|
||||
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == pyglet.window.key.Q or symbol == pyglet.window.key.ESCAPE:
|
||||
self.window.close()
|
||||
exit()
|
||||
|
||||
if symbol == pyglet.window.key.V:
|
||||
level = logging.INFO if logger.getEffectiveLevel() == logging.DEBUG else logging.DEBUG
|
||||
logger.setLevel(level)
|
||||
logger.info(f"set log level: {level}")
|
||||
if symbol == pyglet.window.key.UP:
|
||||
logger.debug('up')
|
||||
self.params.object_velocity += (10 if pyglet.window.key.MOD_SHIFT & modifiers else 1)
|
||||
if symbol == pyglet.window.key.DOWN:
|
||||
logger.debug('down')
|
||||
self.params.object_velocity -= (10 if pyglet.window.key.MOD_SHIFT & modifiers else 1)
|
||||
|
||||
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
|
||||
# determine xy position to select var to change,
|
||||
# then change according to scroll_y
|
||||
for param_name, param_value in dataclasses.asdict(self.params).items():
|
||||
if x >= self.labels[param_name].x and \
|
||||
x <= (self.labels[param_name].x + self.labels[param_name].content_width) and \
|
||||
y >= self.labels[param_name].y and \
|
||||
y <= (self.labels[param_name].y + self.labels[param_name].content_height):
|
||||
if scroll_y < 0:
|
||||
param_value /= (-scroll_y * 1.25)
|
||||
else:
|
||||
param_value *= (scroll_y * 1.25)
|
||||
setattr(self.params, param_name, param_value)
|
||||
|
||||
|
||||
def on_track(self, dt):
|
||||
# bbs = object_detector.detect(frame)
|
||||
|
@ -316,13 +194,88 @@ class Canvas:
|
|||
# id(objects)
|
||||
|
||||
|
||||
class Renderer:
|
||||
def __init__(self, params: Params):
|
||||
# self.canvas = canvas
|
||||
self.params = params
|
||||
self.frame_size = (1280, 720)
|
||||
|
||||
self.hide_stats = False
|
||||
|
||||
config = pyglet.gl.Config(sample_buffers=1, samples=4, double_buffer=True)
|
||||
# , fullscreen=self.config.render_window
|
||||
self.window = pyglet.window.Window(width=self.frame_size[0], height=self.frame_size[1], config=config, fullscreen=False)
|
||||
self.window.set_handler('on_draw', self.on_draw)
|
||||
self.window.set_handler('on_key_press', self.on_key_press)
|
||||
# self.window.set_handler('on_refresh', self.on_refresh)
|
||||
# self.window.set_handler('on_close', self.on_close)
|
||||
|
||||
# Purple background color:
|
||||
# pyglet.gl.glClearColor(*AnimConfig.clear_color)
|
||||
self.fps_display = pyglet.window.FPSDisplay(window=self.window, color=(255,255,255,255))
|
||||
self.fps_display.label.x = self.window.width - 150
|
||||
self.fps_display.label.y = self.window.height - 17
|
||||
self.fps_display.label.bold = False
|
||||
self.fps_display.label.font_size = 10
|
||||
|
||||
self.label_time = pyglet.text.Label("t", x=20, y=self.frame_size[1] - 17, color=(255,255,255,255))
|
||||
|
||||
|
||||
self.batch_figures = pyglet.graphics.Batch()
|
||||
self.batch_bounding_boxes = pyglet.graphics.Batch()
|
||||
self.batch_info = pyglet.graphics.Batch()
|
||||
|
||||
|
||||
|
||||
def run(self):
|
||||
|
||||
self.event_loop = pyglet.app.EventLoop()
|
||||
# pyglet.clock.schedule_interval(self.check_running, 0.1)
|
||||
# pyglet.clock.schedule(self.check_frames)
|
||||
# pyglet.clock.schedule(self.track)
|
||||
|
||||
self.event_loop.run()
|
||||
|
||||
|
||||
|
||||
|
||||
def on_draw(self):
|
||||
# print(time.monotonic())
|
||||
self.label_time.text = f"{time.monotonic()}"
|
||||
self.window.clear()
|
||||
self.batch_figures.draw()
|
||||
self.batch_bounding_boxes.draw()
|
||||
self.batch_info.draw()
|
||||
self.fps_display.draw()
|
||||
self.label_time.draw()
|
||||
|
||||
def on_close(self):
|
||||
logger.info('closing')
|
||||
pass
|
||||
|
||||
|
||||
def on_key_press(self, symbol, modifiers):
|
||||
if symbol == pyglet.window.key.Q:
|
||||
self.window.close()
|
||||
exit()
|
||||
|
||||
if symbol == pyglet.window.key.V:
|
||||
level = logging.INFO if logger.getEffectiveLevel() == logging.DEBUG else logging.DEBUG
|
||||
logger.setLevel(level)
|
||||
logger.info(f"set log level: {level}")
|
||||
if symbol == pyglet.window.key.UP:
|
||||
logger.debug('up')
|
||||
self.params.object_velocity += (10 if pyglet.window.key.MOD_SHIFT & modifiers else 1)
|
||||
if symbol == pyglet.window.key.DOWN:
|
||||
logger.debug('down')
|
||||
self.params.object_velocity -= (10 if pyglet.window.key.MOD_SHIFT & modifiers else 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
params = Params()
|
||||
canvas = Canvas(params)
|
||||
canvas.run()
|
||||
renderer = Renderer(params)
|
||||
canvas = Canvas(params, renderer)
|
||||
renderer.run()
|
||||
|
||||
|
Loading…
Reference in a new issue