-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
checks/_io_checks/_pyglet_checks/check_pyglet_threaded_image_viewer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
|
||
import imgviz | ||
|
||
from check_pyglet_imshow import get_images | ||
|
||
|
||
def main(): | ||
viewer = imgviz.io.PygletThreadedImageViewer(play=True) | ||
for image in get_images(): | ||
viewer.imshow(image) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
# flake8: noqa | ||
|
||
from .base import check_pyglet_available | ||
|
||
from .pyglet_imshow import pyglet_imshow | ||
|
||
from .pyglet_run import pyglet_run | ||
|
||
try: | ||
from .pyglet_threaded_image_viewer import PygletThreadedImageViewer | ||
except ImportError: | ||
|
||
class PygletThreadedImageViewer(object): # type: ignore | ||
|
||
def __init__(self, *args, **kwargs): | ||
check_pyglet_available() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import threading | ||
|
||
import pyglet | ||
|
||
from .pyglet_imshow import _ndarray_to_imagedata | ||
|
||
|
||
class PygletThreadedImageViewer(pyglet.window.Window): | ||
|
||
def __init__(self, play=True, interval=0.5): | ||
self._play = play | ||
self._next = False | ||
|
||
self._updated_at = 0 | ||
self._interval = interval | ||
|
||
self.sprite = None | ||
|
||
self.lock = threading.Lock() | ||
self.thread = threading.Thread(target=self._init_and_start_app) | ||
self.thread.daemon = True # terminate when main thread exit | ||
self.thread.start() | ||
|
||
pyglet.clock.schedule_interval(self.on_update, 1 / 100) | ||
|
||
def _init_and_start_app(self): | ||
super(PygletThreadedImageViewer, self).__init__() | ||
pyglet.app.run() | ||
|
||
def imshow(self, image): | ||
imagedata = _ndarray_to_imagedata(image) | ||
with self.lock: | ||
if self.sprite is None: | ||
self.sprite = pyglet.sprite.Sprite(imagedata) | ||
else: | ||
self.sprite.image = imagedata | ||
inserted_at = self._updated_at | ||
|
||
while True: | ||
with self.lock: | ||
if self._play and \ | ||
self._updated_at > (inserted_at + self._interval): | ||
break | ||
elif self._next: | ||
self._next = False | ||
break | ||
|
||
def on_draw(self): | ||
self.clear() | ||
self.sprite.draw() | ||
|
||
def on_update(self, dt): | ||
if self.sprite is None: | ||
return | ||
with self.lock: | ||
self.on_draw() | ||
self._updated_at += dt | ||
|
||
def on_close(self): | ||
pyglet.clock.unschedule(self.on_update) | ||
with self.lock: | ||
super(PygletThreadedImageViewer, self).on_close() | ||
|
||
def on_key_press(self, symbol, modifiers): | ||
|
||
def usage(): | ||
print('Usage: ') | ||
print(' h: show help') | ||
print(' q: close window') | ||
print(' n: next image') | ||
print(' s: toggle play') | ||
|
||
def short_usage(): | ||
print("Press 'h' to show help") | ||
|
||
if symbol == pyglet.window.key.H: | ||
usage() | ||
elif symbol == pyglet.window.key.Q: | ||
self.on_close() | ||
elif symbol == pyglet.window.key.S: | ||
with self.lock: | ||
self._play = not self._play | ||
elif symbol == pyglet.window.key.N: | ||
with self.lock: | ||
self._next = True | ||
else: | ||
short_usage() |