Compare commits

..

2 commits

Author SHA1 Message Date
Ruben van de Ven
1945cce0e6 Animation timing depends on walking speed 2024-10-15 10:42:22 +02:00
Ruben van de Ven
950a9447d0 Draw as animated figure 2024-10-14 19:13:16 +02:00
2 changed files with 60 additions and 7 deletions

View file

@ -26,7 +26,7 @@ Interval = float # seconds
logger = logging.getLogger('moonwalk')
colorset = [(0, 0, 0),
colorset = [(255, 0, 0),
(0, 0, 255),
(0, 255, 0),
(0, 255, 255),
@ -36,6 +36,16 @@ colorset = [(0, 0, 0),
]
VELOCITY_FACTOR = 1000*.05 # a pixels/second velocity of 400 gives a frame-duration of .05
walk_image = pyglet.image.load('walk_bw.jpg') # TODO investigaet pyglet.resource: https://pyglet.readthedocs.io/en/latest/programming_guide/image.html#displaying-images
image_grid = pyglet.image.ImageGrid(walk_image, rows=1, columns=4)
# texture_grid = image_grid.get_texture_sequence()
walk_animation = image_grid.get_animation(period=.05)
walk_animation_dim = {
'y': walk_animation.get_max_height(),
'x': walk_animation.get_max_width()
}
@dataclass
class Params:
@ -44,8 +54,9 @@ class Params:
tracker_fps: float = 5.6
# iou = None
emitter_speed: float = 4 # objects per second
object_velocity: float = 80 # pixels/second
object_velocity: float = 400 # pixels/second
velocity_decay: float = 1 # make system a bit springy
iou_threshold: float = 0 # SORT Intersection over union
def add_listener(self, attr: str, callback: callable):
"""
@ -80,18 +91,24 @@ class Params:
for listener in self._listeners[attr]:
listener(attr, old_val, val)
class DetectedObject:
def __init__(self, canvas: Canvas):
self.canvas = canvas
# TODO handle variability
self.v = self.canvas.params.object_velocity
self.l = 0 # left
self.w = 10 # width
self.h = 20 # height
self.w = 80 # width
self.h = 160 # height
self.l = -self.w # left
self.t = (self.canvas.height - self.h)/2 # top
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.batch_figures)
self.shape = pyglet.sprite.Sprite(img=walk_animation,x=self.l, y=self.t, batch=self.canvas.batch_figures)
self.shape.scale_x = self.w/walk_animation_dim['x']
self.shape.scale_y = self.h/walk_animation_dim['y']
# rectangle.opacity = 128
# rectangle.rotation = 33
#TODO renderer
@ -166,6 +183,7 @@ class Canvas:
# Purple background color:
# pyglet.gl.glClearColor(*AnimConfig.clear_color)
self.draw_stats = True
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
@ -194,6 +212,10 @@ 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]
self.params.add_listener('tracker_fps', self.on_tracker_fps_change)
self.params.add_listener('iou_threshold', self.on_iou_threshold_change)
self.params.add_listener('object_velocity', self.on_object_velocity_change)
# trigger first time around
self.on_object_velocity_change('object_velocity', None, self.params.object_velocity)
def getTrackBboxShapes(self, track_id):
color = colorset[int(track_id) % len(colorset)]
@ -203,6 +225,7 @@ class Canvas:
# pyglet.shapes.Lab(0,0,0,0,color=(0,255,0),thickness=2, batch=self.batch_bounding_boxes)
]
def on_tracker_fps_change(self, field, old_value, new_value):
"""
Param dataclass listener for changes to tracker_fps
@ -210,6 +233,26 @@ class Canvas:
for ii in self.interval_items:
ii.interval = 1/new_value
logger.debug(f"Set tracker interval to {ii.interval}")
def on_object_velocity_change(self, field, old_value, new_value):
"""
Param dataclass listener for changes to object_velocity as to change walk animation
"""
duration = max(.001, VELOCITY_FACTOR / new_value)
logger.debug(f"set frame duration to {duration=} (for {new_value} p/s, factor: {VELOCITY_FACTOR})")
for frame in walk_animation.frames:
# a velocity of
frame.duration = duration
# for ii in self.interval_items:
# ii.interval = 1/new_value
# logger.debug(f"Set tracker interval to {ii.interval}")
def on_iou_threshold_change(self, field, old_value, new_value):
"""
Param dataclass listener for changes to iou_threshold
"""
self.tracker.iou_threshold = new_value
def run(self):
@ -224,7 +267,8 @@ class Canvas:
self.batch_figures.draw()
self.batch_bounding_boxes.draw()
self.batch_info.draw()
self.fps_display.draw()
if self.draw_stats:
self.fps_display.draw()
def on_close(self):
logger.info('closing')
@ -240,6 +284,9 @@ class Canvas:
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.S:
self.draw_stats = not self.draw_stats
logger.info(f"rendering stats: {self.draw_stats}")
if symbol == pyglet.window.key.UP:
logger.debug('up')
self.params.object_velocity += (10 if pyglet.window.key.MOD_SHIFT & modifiers else 1)
@ -255,6 +302,12 @@ class Canvas:
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 param_value == 0:
if scroll_y < 1:
param_value -= .1
else:
param_value += .1
if scroll_y < 0:
param_value /= (-scroll_y * 1.25)
else:

BIN
walk_bw.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB