Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Image Capture Logic in _still_image_helper (New) #1523

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions providers/base/bin/camera_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,39 +413,30 @@ def image(self):
"""
pixelformat = self._get_default_format()["pixelformat"]
if self.output:
self._still_image_helper(
self._capture_image(
self.output, self._width, self._height, pixelformat
)
else:
with NamedTemporaryFile(
prefix="camera_test_", suffix=".jpg", delete=False
) as f:
self._still_image_helper(
self._capture_image(
f.name, self._width, self._height, pixelformat
)

def _still_image_helper(self, filename, width, height, pixelformat):
def _capture_image(self, filename, width, height, pixelformat):
"""
Captures an image to a given filename. If the image capture fails with
fswebcam, it will try to capture the image with gstreamer.
"""

# Try to take a picture with fswebcam
use_fswebcam = True
use_gstreamer = False

if use_fswebcam:
result = self._capture_image_fswebcam(
filename, width, height, pixelformat
)
if not result:
print("Failed to capture image with fswebcam, using gstreamer")
use_gstreamer = True

# If fswebcam fails, try with gstreamer
if use_gstreamer:
if not self._capture_image_fswebcam(
filename, width, height, pixelformat
):
print("Failed to capture image with fswebcam, using gstreamer")
# If fswebcam fails, try with gstreamer
self._capture_image_gstreamer(filename, width, height, pixelformat)
print("Image saved to %s" % filename)
print("Image saved to %s" % filename)
if not self.headless:
self._display_image(filename, width, height)

Expand All @@ -465,16 +456,15 @@ def _capture_image_fswebcam(self, filename, width, height, pixelformat):
filename,
]
if pixelformat:
if "MJPG" == pixelformat: # special tweak for fswebcam
pixelformat = "MJPEG"
command.extend(["-p", pixelformat])
# special tweak for fswebcam
command.extend(
["-p", pixelformat if pixelformat != "MJPG" else "MJPEG"]
)
try:
check_call(command, stdout=open(os.devnull, "w"), stderr=STDOUT)
if os.path.getsize(filename) == 0:
return False
return os.path.getsize(filename) != 0
except (CalledProcessError, OSError):
return False
return True

def _capture_image_gstreamer(self, filename, width, height, pixelformat):
"""
Expand Down Expand Up @@ -635,7 +625,7 @@ def resolutions(self):
)
print("Taking a picture at %sx%s" % (w, h))

self._still_image_helper(
self._capture_image(
f.name, w, h, pixelformat=format["pixelformat"]
)
if self._validate_image(f.name, w, h):
Expand Down Expand Up @@ -671,7 +661,7 @@ def _save_debug_image(self, format, device, output):
)
print("Saving debug image to %s" % filepath)
with open(filepath, "w") as f:
self._still_image_helper(f.name, w, h, format["pixelformat"])
self._capture_image(f.name, w, h, format["pixelformat"])

def _get_supported_pixel_formats(self, device, maxformats=5):
"""
Expand Down
20 changes: 10 additions & 10 deletions providers/base/tests/test_camera_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_image(self):
}

CameraTest.image(mock_camera)
self.assertEqual(mock_camera._still_image_helper.call_count, 1)
self.assertEqual(mock_camera._capture_image.call_count, 1)

def test_image_without_output(self):
mock_camera = MagicMock()
Expand All @@ -366,32 +366,32 @@ def test_image_without_output(self):

with patch("tempfile.NamedTemporaryFile"):
CameraTest.image(mock_camera)
self.assertEqual(mock_camera._still_image_helper.call_count, 1)
self.assertEqual(mock_camera._capture_image.call_count, 1)

def test_still_image_helper(self):
def test_capture_image_helper(self):
mock_camera = MagicMock()
mock_camera._capture_image_fswebcam.return_value = True
mock_camera._display_image.return_value = True
mock_camera.headless = False
CameraTest._still_image_helper(
CameraTest._capture_image(
mock_camera, "/tmp/test.jpg", 640, 480, "YUYV"
)
self.assertEqual(mock_camera._capture_image_fswebcam.call_count, 1)
self.assertEqual(mock_camera._display_image.call_count, 1)

def test_still_image_headless(self):
def test_capture_image_headless(self):
mock_camera = MagicMock()
mock_camera._capture_image_fswebcam.return_value = True
mock_camera.headless = True
CameraTest._still_image_helper(
CameraTest._capture_image(
mock_camera, "/tmp/test.jpg", 640, 480, "YUYV"
)
self.assertEqual(mock_camera._display_image.call_count, 0)

def test_still_image_helper_fswebcam_fails(self):
def test_capture_image_helper_fswebcam_fails(self):
mock_camera = MagicMock()
mock_camera._capture_image_fswebcam.return_value = False
CameraTest._still_image_helper(
CameraTest._capture_image(
mock_camera, "/tmp/test.jpg", 640, 480, "YUYV"
)
self.assertEqual(mock_camera._capture_image_gstreamer.call_count, 1)
Expand Down Expand Up @@ -514,7 +514,7 @@ def test_resolutions(self):

self.assertEqual(mock_camera._get_default_format.call_count, 1)
self.assertEqual(mock_camera._save_debug_image.call_count, 1)
self.assertEqual(mock_camera._still_image_helper.call_count, 2)
self.assertEqual(mock_camera._capture_image.call_count, 2)
self.assertEqual(mock_camera._validate_image.call_count, 2)

# Test that the function also works with no output
Expand Down Expand Up @@ -545,7 +545,7 @@ def test_save_debug_image(self, mock_exists):
CameraTest._save_debug_image(
mock_camera, format, "/dev/video0", "/tmp"
)
self.assertEqual(mock_camera._still_image_helper.call_count, 1)
self.assertEqual(mock_camera._capture_image.call_count, 1)

@patch("camera_test.os.path.exists")
def test_save_debug_image_fails_if_path_not_exists(self, mock_exists):
Expand Down
Loading