Skip to content

Commit

Permalink
Add exception when grabbing fails (#46)
Browse files Browse the repository at this point in the history
* adding exception when grabbing fails

* Automatically reformatting code with black and isort

* updating __init__.py

* Automatically reformatting code with black and isort

---------

Co-authored-by: Auto-format Bot <[email protected]>
  • Loading branch information
timmarkhuff and Auto-format Bot authored Jul 17, 2024
1 parent 7b8c0b7 commit 892af4a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/framegrab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .cli.clitools import preview_image
from .exceptions import GrabError
from .grabber import FrameGrabber
from .motion import MotionDetector
from .rtsp_discovery import AutodiscoverModes, ONVIFDeviceInfo, RTSPDiscovery
Expand All @@ -17,7 +18,7 @@
__all__ = [
"FrameGrabber",
"MotionDetector",
"RTSPDiscovery",
"GrabError" "RTSPDiscovery",
"ONVIFDeviceInfo",
"AutodiscoverModes",
"preview_image",
Expand Down
6 changes: 6 additions & 0 deletions src/framegrab/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class GrabError(Exception):
"""Exception raised for errors in the frame grabbing process."""

def __init__(self, message="Failed to grab frame from camera."):
self.message = message
super().__init__(self.message)
6 changes: 6 additions & 0 deletions src/framegrab/grabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import numpy as np
import yaml

from .exceptions import GrabError
from .rtsp_discovery import AutodiscoverModes, RTSPDiscovery
from .unavailable_module import UnavailableModule

Expand Down Expand Up @@ -356,6 +357,11 @@ def grab(self) -> np.ndarray:
"""
frame = self._grab_implementation()

if frame is None:
name = self.config["name"] # all grabbers should have a name, either user-provided or generated
error_msg = f"Failed to grab frame from {name}"
raise GrabError(error_msg)

# apply post processing operations
frame = self._crop(frame)
frame = self._digital_zoom(frame)
Expand Down

0 comments on commit 892af4a

Please sign in to comment.