Skip to content

Commit

Permalink
Better logic for updating cv2.VideoCapture resolution (#52)
Browse files Browse the repository at this point in the history
* adding some better logic for updating resolution

* bumping version and refactoring function

* Automatically reformatting code with black and isort

* one more refactor

---------

Co-authored-by: Auto-format Bot <[email protected]>
  • Loading branch information
timmarkhuff and Auto-format Bot authored Jul 29, 2024
1 parent eb295c2 commit dec3134
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "framegrab"
version = "0.6.3"
version = "0.6.4"
description = "Easily grab frames from cameras or streams"
authors = ["Groundlight <[email protected]>"]
license = "MIT"
Expand Down
27 changes: 19 additions & 8 deletions src/framegrab/grabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,26 @@ def _set_cv2_resolution(self) -> None:
"""Set the resolution of the cv2.VideoCapture object based on the FrameGrabber's config.
If the FrameGrabber lacks both of these properties (height and width), this method
will do nothing.
Similarly, if the specified resolution equals the existing resolution, this function will
do nothing. This is because setting the resolution of a cv2.VideoCapture object is non-trivial and
can take multiple seconds, so we should only do it when something has changed.
"""
resolution = self.config.get("options", {}).get("resolution", {})
height = resolution.get("height")
width = resolution.get("width")

if width:
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
if height:
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
resolution = self.config.get("options", {}).get("resolution")
if resolution is None:
return

new_height = resolution.get("height")
new_width = resolution.get("width")

if new_width:
current_width = int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH))
if new_width != current_width:
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, new_width)
if new_height:
current_height = int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
if new_height != current_height:
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, new_height)

def apply_options(self, options: dict) -> None:
"""Update generic options such as crop and zoom as well as
Expand Down

0 comments on commit dec3134

Please sign in to comment.