diff --git a/providers/base/bin/camera_test.py b/providers/base/bin/camera_test.py index e4b104d64..812851308 100755 --- a/providers/base/bin/camera_test.py +++ b/providers/base/bin/camera_test.py @@ -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) @@ -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): """ @@ -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): @@ -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): """ diff --git a/providers/base/tests/test_camera_test.py b/providers/base/tests/test_camera_test.py index c303bc5e2..87c645131 100644 --- a/providers/base/tests/test_camera_test.py +++ b/providers/base/tests/test_camera_test.py @@ -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() @@ -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) @@ -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 @@ -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):