From 3a38b32b722c3378f812310d152b397301415fac Mon Sep 17 00:00:00 2001 From: WillB97 Date: Mon, 20 May 2024 21:32:10 +0100 Subject: [PATCH 1/3] Add support for defining how to convert a frame source's frame to grey --- april_vision/frame_sources.py | 3 +++ april_vision/utils.py | 11 ++++++++--- april_vision/vision.py | 8 ++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/april_vision/frame_sources.py b/april_vision/frame_sources.py index 0321513..53f449b 100644 --- a/april_vision/frame_sources.py +++ b/april_vision/frame_sources.py @@ -16,6 +16,9 @@ class FrameSource: Allows april_vision.Processor to be created prior to frames being available. """ + # The conversion to apply to the frame to get it to grayscale + COLOURSPACE = cv2.COLOR_BGR2GRAY + def read(self, fresh: bool = True) -> NDArray: """ The method for getting a new frame. diff --git a/april_vision/utils.py b/april_vision/utils.py index 95684d9..23bc072 100644 --- a/april_vision/utils.py +++ b/april_vision/utils.py @@ -2,7 +2,7 @@ from collections import deque from math import hypot from pathlib import Path -from typing import Deque, NamedTuple, Tuple, Union +from typing import Deque, Optional, NamedTuple, Tuple, Union import cv2 import numpy as np @@ -21,9 +21,14 @@ class Frame(NamedTuple): colour_frame: NDArray @classmethod - def from_colour_frame(cls, colour_frame: NDArray) -> 'Frame': + def from_colour_frame( + cls, + colour_frame: NDArray, + colourspace: Optional[int] = cv2.COLOR_BGR2GRAY, + ) -> 'Frame': """Load frame from a colour image in a numpy array.""" - grey_frame = cv2.cvtColor(colour_frame, cv2.COLOR_BGR2GRAY) + if colourspace is not None: + grey_frame = cv2.cvtColor(colour_frame, colourspace) return cls( grey_frame=grey_frame, diff --git a/april_vision/vision.py b/april_vision/vision.py index f45a421..85fbff9 100644 --- a/april_vision/vision.py +++ b/april_vision/vision.py @@ -67,7 +67,7 @@ def _capture(self, fresh: bool = True) -> Frame: # hook to allow modification of the captured frame colour_frame = self.capture_filter(colour_frame) - return Frame.from_colour_frame(colour_frame) + return Frame.from_colour_frame(colour_frame, colourspace=self._frame_source.COLOURSPACE) def _detect(self, frame: Frame) -> List[Marker]: """Locate fiducial markers in frame using pyapriltags.""" @@ -193,7 +193,7 @@ def see(self, *, frame: Optional[NDArray] = None) -> List[Marker]: if frame is None: frames = self._capture() else: - frames = Frame.from_colour_frame(frame) + frames = Frame.from_colour_frame(frame, colourspace=self._frame_source.COLOURSPACE) return self._detect(frames) def see_ids(self, *, frame: Optional[NDArray] = None) -> List[int]: @@ -201,7 +201,7 @@ def see_ids(self, *, frame: Optional[NDArray] = None) -> List[int]: if frame is None: frames = self._capture() else: - frames = Frame.from_colour_frame(frame) + frames = Frame.from_colour_frame(frame, colourspace=self._frame_source.COLOURSPACE) markers = self._detect(frames) return [marker.id for marker in markers] @@ -215,7 +215,7 @@ def save( if frame is None: frames = self._capture() else: - frames = Frame.from_colour_frame(frame) + frames = Frame.from_colour_frame(frame, colourspace=self._frame_source.COLOURSPACE) markers = self._detect(frames) frames = self._annotate( frames, From d6c8113a79dadcdab535c731b7471f043dbb8f05 Mon Sep 17 00:00:00 2001 From: WillB97 Date: Mon, 20 May 2024 21:34:00 +0100 Subject: [PATCH 2/3] Linting --- april_vision/utils.py | 2 +- april_vision/vision.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/april_vision/utils.py b/april_vision/utils.py index 23bc072..43e1f44 100644 --- a/april_vision/utils.py +++ b/april_vision/utils.py @@ -2,7 +2,7 @@ from collections import deque from math import hypot from pathlib import Path -from typing import Deque, Optional, NamedTuple, Tuple, Union +from typing import Deque, NamedTuple, Optional, Tuple, Union import cv2 import numpy as np diff --git a/april_vision/vision.py b/april_vision/vision.py index 85fbff9..abafb20 100644 --- a/april_vision/vision.py +++ b/april_vision/vision.py @@ -67,7 +67,10 @@ def _capture(self, fresh: bool = True) -> Frame: # hook to allow modification of the captured frame colour_frame = self.capture_filter(colour_frame) - return Frame.from_colour_frame(colour_frame, colourspace=self._frame_source.COLOURSPACE) + return Frame.from_colour_frame( + colour_frame, + colourspace=self._frame_source.COLOURSPACE, + ) def _detect(self, frame: Frame) -> List[Marker]: """Locate fiducial markers in frame using pyapriltags.""" From 6464c09c4a81db26a0a0371b06af13951334eb62 Mon Sep 17 00:00:00 2001 From: WillB97 Date: Tue, 21 May 2024 18:40:08 +0100 Subject: [PATCH 3/3] Fix handling grey input frames --- april_vision/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/april_vision/utils.py b/april_vision/utils.py index 43e1f44..bec9fa1 100644 --- a/april_vision/utils.py +++ b/april_vision/utils.py @@ -29,6 +29,8 @@ def from_colour_frame( """Load frame from a colour image in a numpy array.""" if colourspace is not None: grey_frame = cv2.cvtColor(colour_frame, colourspace) + else: + grey_frame = colour_frame.copy() return cls( grey_frame=grey_frame,