Get rid of renderer and handle scroll over labels

This commit is contained in:
Ruben van de Ven 2024-10-04 19:56:16 +02:00
parent a877e1cc12
commit 89b1434651

View file

@ -47,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.renderer.batch_figures)
self.shape = pyglet.shapes.Rectangle(self.l, self.t, self.w, self.h, color=(255, 22, 20), batch=self.canvas.batch_figures)
# rectangle.opacity = 128
# rectangle.rotation = 33
#TODO renderer
@ -83,26 +83,53 @@ class Canvas:
"""
A canvas with moving objects
"""
def __init__(self, params: Params, renderer: Renderer):
def __init__(self, params: Params):
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.renderer = renderer
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.tracks = []
self.labels = {
'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),
'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),
}
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.renderer.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.batch_info)
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.track_shapes = defaultdict(lambda: pyglet.shapes.Box(0,0,0,0,color=(0,255,0),thickness=2, batch=self.batch_bounding_boxes))
self.tracker = Sort(max_age=5, min_hits=2, iou_threshold=0) #DeepSort(max_age=5)
@ -111,6 +138,56 @@ class Canvas:
self.interval_items: list[pyglet.clock._ScheduledIntervalItem] = [i for i in pyglet.clock._default._schedule_interval_items if i.func == self.on_track]
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)
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):
setattr(self.params, param_name, param_value+scroll_y)
def on_track(self, dt):
# bbs = object_detector.detect(frame)
objects = self.snapshot()
@ -194,88 +271,13 @@ 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()
renderer = Renderer(params)
canvas = Canvas(params, renderer)
renderer.run()
canvas = Canvas(params)
canvas.run()