Skip to content

Commit

Permalink
Issue #23: Raise error if can't set resolution to desired values
Browse files Browse the repository at this point in the history
  • Loading branch information
tdowrick committed Nov 30, 2020
1 parent 39bf167 commit acbc821
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
24 changes: 22 additions & 2 deletions sksurgeryimage/acquire/video_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def __init__(self, source_num_or_file, dims=None):
if height < 1:
raise ValueError("Height must be >= 1")

self.source.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.source.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.set_resolution(width, height)



else:
width = int(self.source.get(cv2.CAP_PROP_FRAME_WIDTH))
Expand All @@ -64,6 +65,25 @@ def __init__(self, source_num_or_file, dims=None):
self.frame = np.empty((height, width, 3), dtype=np.uint8)
self.ret = None

def set_resolution(self, width: int, height: int):
"""Set the resolution of the input source.
:param width: Width
:type width: int
:param height: Height
:type height: int
:raises ValueError: If resolution is not supported.
"""
self.source.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.source.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

set_w = self.source.get(cv2.CAP_PROP_FRAME_WIDTH)
set_h = self.source.get(cv2.CAP_PROP_FRAME_HEIGHT)

if set_w != width or set_h != height:
raise ValueError(f"Tried to set width/height to {width} x {height} \
but failed. Width and height set to {set_w} x {set_h}")

def grab(self):
"""
Call the cv2.VideoCapture grab function and get a timestamp.
Expand Down
15 changes: 14 additions & 1 deletion tests/acquire/video_source/test_video_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,21 @@ def test_add_source_from_camera_custom_dimensions(video_source_wrapper):
except IndexError:
return

def test_add_source_from_camera_invalid_dimensions(video_source_wrapper):
"""
Add a camera and pass in custom dimensions to cv2.VideoCapture.
"""
try:
camera_input = 0
custom_dims = [100, 24] # default is 640 x 480

with pytest.raises(ValueError):
video_source_wrapper.add_camera(camera_input, custom_dims)

except IndexError:
return

def test_add_source_from_camera_invalid_dims(video_source_wrapper):
def test_add_source_from_file_invalid_dims(video_source_wrapper):
camera_input = 'tests/data/acquire/100x50_100_frames.avi'

custom_dims = ["happy", "birthday"]
Expand Down

0 comments on commit acbc821

Please sign in to comment.