Skip to content

Commit e0a51d9

Browse files
committed
add error checks to virtual camera initialization
1 parent 492c252 commit e0a51d9

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

modules/hitl/camera_emulator.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,33 @@ def create(cls, images_path: str) -> "tuple[True, CameraEmulator] | tuple[False,
3636
if not isinstance(images_path, str):
3737
return False, None
3838

39-
return True, CameraEmulator(cls.__create_key, images_path)
39+
try:
40+
virtual_camera_instance = pyvirtualcam.Camera(IMAGE_SIZE[0], IMAGE_SIZE[1], CAMERA_FPS)
41+
42+
# Required for catching library exceptions
43+
# pylint: disable-next=broad-exception-caught
44+
except Exception as e:
45+
print(
46+
"Error creating virtual camera (Check if OBS or v4l2loopback is installed): "
47+
+ str(e)
48+
)
49+
return False, None
50+
51+
if virtual_camera_instance is None:
52+
return False, None
53+
54+
return True, CameraEmulator(cls.__create_key, images_path, virtual_camera_instance)
4055

41-
def __init__(self, class_private_create_key: object, images_path: str) -> None:
56+
def __init__(
57+
self, class_private_create_key: object, images_path: str, virtual_camera: pyvirtualcam
58+
) -> None:
4259
"""
4360
Private constructor, use create() method.
4461
"""
4562
assert class_private_create_key is CameraEmulator.__create_key, "Use create() method"
4663

4764
self.__image_folder_path = images_path
48-
self.__virtual_camera = pyvirtualcam.Camera(IMAGE_SIZE[0], IMAGE_SIZE[1], CAMERA_FPS)
65+
self.__virtual_camera = virtual_camera
4966
self.__image_paths: "list[str]" = []
5067
self.__current_frame = None
5168
self.__image_index = 0
@@ -64,7 +81,7 @@ def send_frame(self) -> None:
6481
# Required for catching library exceptions
6582
# pylint: disable-next=broad-exception-caught
6683
except Exception as e:
67-
print("Cannot send frame" + e)
84+
print("Cannot send frame" + str(e))
6885

6986
def sleep_until_next_frame(self) -> None:
7087
"""
@@ -92,7 +109,7 @@ def update_current_image(self) -> None:
92109
# Required for catching library exceptions
93110
# pylint: disable-next=broad-exception-caught
94111
except Exception as e:
95-
print("Could not read image: " + image_path + " Error: " + e)
112+
print("Could not read image: " + image_path + " Error: " + str(e))
96113
self.next_image()
97114
loop_count += 1
98115

@@ -116,4 +133,4 @@ def __get_images(self) -> None:
116133
# Required for catching library exceptions
117134
# pylint: disable-next=broad-exception-caught
118135
except Exception as e:
119-
print("Error reading images: " + e)
136+
print("Error reading images: " + str(e))

0 commit comments

Comments
 (0)