Commit 2d24dc8f authored by Thomas Dowrick's avatar Thomas Dowrick

Merge branch '11-import-common_overlay_apps-from-scikit-surgeryvtk' into 'master'

Resolve "Import common_overlay_apps from scikit-surgeryvtk"

Closes #11

See merge request WEISS/SoftwareRepositories/SNAPPY/scikit-surgeryutils!2
parents 7d985740 5ea8cdc5
Pipeline #1411 passed with stages
in 15 minutes and 54 seconds
"""Common use cases for vtk_overlay_window"""
#pylint: disable=no-member, no-name-in-module, protected-access
# coding=utf-8
import cv2
from PySide2.QtCore import QTimer
from sksurgeryvtk.widgets.vtk_overlay_window import VTKOverlayWindow
from sksurgeryvtk.models.vtk_surface_model_directory_loader \
import VTKSurfaceModelDirectoryLoader
class OverlayBaseApp():
"""
Base class for applications that use vtk_overlay_window.
The update() method should be implemented in the child
class.
:param video_source: OpenCV compatible video source (int or filename)
"""
def __init__(self, video_source):
self.vtk_overlay_window = VTKOverlayWindow()
self.video_source = cv2.VideoCapture(video_source)
self.update_rate = 30
self.img = None
self.timer = None
def start(self):
"""Show the overlay widget and
set a timer running"""
self.vtk_overlay_window.show()
self.timer = QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(1000.0 / self.update_rate)
def add_vtk_models_from_dir(self, directory):
"""
Add VTK models to the foreground.
:param: directory, location of models
"""
model_loader = VTKSurfaceModelDirectoryLoader(directory)
self.vtk_overlay_window.add_vtk_models(model_loader.models)
def update(self):
""" Update the scene background and/or foreground.
Should be implemented by sub class """
raise NotImplementedError('Should have implemented this method.')
def stop(self):
"""
Make sure that the VTK Interactor terminates nicely, otherwise
it can throw some error messages, depending on the usage.
"""
self.vtk_overlay_window._RenderWindow.Finalize()
self.vtk_overlay_window.TerminateApp()
class OverlayOnVideoFeed(OverlayBaseApp):
"""
Uses the acquired video feed as the background image,
with no additional processing.
"""
def update(self):
_, self.img = self.video_source.read()
self.vtk_overlay_window.set_video_image(self.img)
self.vtk_overlay_window._RenderWindow.Render()
......@@ -55,7 +55,7 @@ def main(args=None):
"-v", "--version",
action='version',
version='sksurgeryrendermodelslikecamera version '
+ friendly_version_string)
+ friendly_version_string)
args = parser.parse_args(args)
......
# -*- coding: utf-8 -*-
import pytest
from PySide2.QtWidgets import QApplication
@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def setup_qt():
""" Create the QT application. """
app = QApplication([])
return app
return app
\ No newline at end of file
# -*- coding: utf-8 -*-
import cv2
import pytest
import sksurgeryutils.common_overlay_apps as coa
def test_OverlayOnVideoFeed(setup_qt):
"""
Test will only run if there is a camera avilable.
"""
# Try to open a camera. If one isn't available, the rest of test
# will be skipped.
camera_source = 5
x = cv2.VideoCapture(camera_source)
if x.isOpened():
x.release()
overlay_app = coa.OverlayOnVideoFeed(camera_source)
overlay_app.add_vtk_models_from_dir('tests/data/models/Liver')
overlay_app.start()
overlay_app.update()
overlay_app.stop()
def test_OverlayBaseAppRaisesNotImplementedError(setup_qt):
"""
Test will only run if there is a camera avilable
"""
class ErrorApp(coa.OverlayBaseApp):
def something(self):
pass
with pytest.raises(NotImplementedError):
overlay_app = ErrorApp(0)
overlay_app.update()
......@@ -21,7 +21,6 @@ def test_image_to_pixel_invalid_because_input_is_greyscale():
def test_image_to_pixel_valid_rgb_example(setup_qt):
app = setup_qt
blank_image = np.zeros((50, 100, 3), dtype=np.uint8)
pixmap = iu.image_to_pixmap(blank_image)
assert pixmap.width() == blank_image.shape[1]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment