Skip to content

Commit

Permalink
Merge pull request #18 from UCL/17-null-video
Browse files Browse the repository at this point in the history
17 null video
  • Loading branch information
thompson318 committed Aug 4, 2020
2 parents 3bc61c6 + 725ec80 commit f8da07b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
9 changes: 6 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
scikit-surgeryarucotracker
===============================

.. image:: https://github.com/UCL/scikit-surgeryarucotracker/raw/master/project-icon.png
.. image:: https://github.com/UCL/scikit-surgeryarucotracker/raw/master/project-icon.gif
:height: 128px
:width: 128px
:target: https://github.com/UCL/scikit-surgeryarucotracker
Expand All @@ -19,13 +19,16 @@ scikit-surgeryarucotracker
:target: http://scikit-surgeryarucotracker.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status

.. image:: https://img.shields.io/badge/Cite-SciKit--Surgery-informational
:target: https://doi.org/10.1007/s11548-020-02180-5
:alt: The SciKit-Surgery paper


Author: Stephen Thompson

scikit-surgeryarucotracker provides a simple Python interface between OpenCV's ARuCo marker tracking libraries and other Python packages designed around scikit-surgerytrackers. It allows you to treat an object tracked using ARuCo markers in the same way as an object tracked using other tracking hardware (e.g. aruco - scikit-surgerynditracker).

scikit-surgeryarucotracker is part of the `SNAPPY`_ software project, developed at the `Wellcome EPSRC Centre for Interventional and Surgical Sciences`_, part of `University College London (UCL)`_.
scikit-surgeryarucotracker is part of the `SciKit-Surgery`_ software project, developed at the `Wellcome EPSRC Centre for Interventional and Surgical Sciences`_, part of `University College London (UCL)`_.

scikit-surgeryarucotracker is tested with Python 3.6 and may support other Python versions.

Expand Down Expand Up @@ -106,7 +109,7 @@ Supported by `Wellcome`_ and `EPSRC`_.
.. _`Wellcome EPSRC Centre for Interventional and Surgical Sciences`: http://www.ucl.ac.uk/weiss
.. _`source code repository`: https://github.com/UCL/scikit-surgeryarucotracker
.. _`Documentation`: https://scikit-surgeryarucotracker.readthedocs.io
.. _`SNAPPY`: https://github.com/UCL/scikit-surgery/wikis/home
.. _`SciKit-Surgery`: https://github.com/UCL/scikit-surgery/wikis/home
.. _`University College London (UCL)`: http://www.ucl.ac.uk/
.. _`Wellcome`: https://wellcome.ac.uk/
.. _`EPSRC`: https://www.epsrc.ac.uk/
Expand Down
Binary file added project-icon.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed project-icon.png
Binary file not shown.
64 changes: 36 additions & 28 deletions sksurgeryarucotracker/arucotracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ def __init__(self, configuration):
"""

self._ar_dict = None
self._marker_size = 50
self._camera_projection_matrix = None
self._camera_distortion = array([0.0, 0.0, 0.0, 0.0, 0.0],
dtype=float32)
self._use_camera_projection = False
self._state = None
self._capture = VideoCapture()

self._frame_number = 0
self._debug = False

if "debug" in configuration:
self._debug = configuration.get("debug")
self._debug = configuration.get("debug", False)

video_source = configuration.get("video source", 0)

video_source = 0
if "video source" in configuration:
video_source = configuration.get("video source")
if video_source != 'none':
self._capture = VideoCapture()
else:
self._capture = None

ar_dictionary_name = getattr(aruco, 'DICT_4X4_50')
if "aruco dictionary" in configuration:
Expand All @@ -101,29 +101,30 @@ def __init__(self, configuration):

self._ar_dict = aruco.getPredefinedDictionary(ar_dictionary_name)

if "marker size" in configuration:
self._marker_size = configuration.get("marker size")
self._marker_size = configuration.get("marker size", 50)

if "calibration" in configuration:
self._camera_projection_matrix, self._camera_distortion = \
_load_calibration(configuration.get("calibration"))
self._check_pose_estimation_ok()

if self._capture.open(video_source):
#try setting some properties
if "capture properties" in configuration:
props = configuration.get("capture properties")
for prop in props:
cvprop = getattr(cv2, prop)
value = props[prop]
self._capture.set(cvprop, value)

# self._capture.set(3,1280)
# self._capture.set(4,1024)
self._state = "ready"
if video_source != 'none':
if self._capture.open(video_source):
#try setting some properties
if "capture properties" in configuration:
props = configuration.get("capture properties")
for prop in props:
cvprop = getattr(cv2, prop)
value = props[prop]
self._capture.set(cvprop, value)

self._state = "ready"
else:
raise OSError('Failed to open video source {}'
.format(video_source))
else:
raise OSError('Failed to open video source {}'
.format(video_source))
self._state = "ready"


def _check_pose_estimation_ok(self):
"""Checks that the camera projection matrix and camera distortion
Expand All @@ -143,13 +144,16 @@ def close(self):
:raise Exception: ValueError
"""
self._capture.release()
del self._capture
if self._capture is not None:
self._capture.release()
del self._capture
self._state = None

def get_frame(self):
def get_frame(self, frame=None):
"""Gets a frame of tracking data from the Tracker device.
:params frame: an image to process, if None, we use the OpenCV
video source.
:return:
port_numbers : list of port handles, one per tool
Expand All @@ -168,7 +172,11 @@ def get_frame(self):
if self._state != "tracking":
raise ValueError('Attempted to get frame, when not tracking')

_, frame = self._capture.read()
if self._capture is not None:
_, frame = self._capture.read()

if frame is None:
raise ValueError('Frame not set, and capture.read failed')

marker_corners, marker_ids, _ = \
aruco.detectMarkers(frame, self._ar_dict)
Expand Down
23 changes: 22 additions & 1 deletion tests/test_sksurgeryarucotracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""scikit-surgeryarucotracker tests"""

import pytest
from cv2 import VideoCapture
from sksurgeryarucotracker.arucotracker import ArUcoTracker


def test_on_video_with_single_tag():
"""
connect track and close with single tag,
Expand All @@ -23,6 +23,27 @@ def test_on_video_with_single_tag():
tracker.close()


def test_no_video_single_tag():
"""
connect track and close with single tag,
reqs: 03, 04 ,05
"""
config = {'video source' : 'none'}

tracker = ArUcoTracker(config)
tracker.start_tracking()
with pytest.raises(ValueError):
tracker.get_frame()
capture = VideoCapture('data/output.avi')
for _ in range(10):
_, frame = capture.read()
(_port_handles, _timestamps, _framenumbers,
_tracking, _quality) = tracker.get_frame(frame)

tracker.stop_tracking()
tracker.close()


def test_on_video_with_debug():
"""
connect track and close with single tag,
Expand Down

0 comments on commit f8da07b

Please sign in to comment.