Skip to content

Commit af8146c

Browse files
committed
feat: add "--camera-device" parameter with mutual exclusivity for video source selection
Added an optional command-line parameter "--camera-device" (integer with default value of 0) to allow users to specify which camera device index to use (default: 0). This parameter is mutually exclusive with the existing "--play-video" parameter (only one video source can be specified), enforced using mutually exclusive group feature of "argparse" module. Backward compatibility: - No parameters provided → uses camera device 0 (existing behavior preserved). - Single "--camera-device <idx>" → uses specified camera device index. - Single "--play-video <path>" → uses video file (existing behavior preserved). - Both parameters → error with clear message from "argparse" module. Usage examples (valid): ------------------------------------------------------------------- $ python app.py # camera device 0 (default) $ python app.py --camera-device 1 # camera device 1 $ python app.py --play-video video.mp4 # video file playback ------------------------------------------------------------------- Usage examples (invalid): ------------------------------------------------------------------------------- $ python app.py --camera-device 0 --play-video file.mp4 usage: app.py [-h] [--camera-device idx | --play-video path] app.py: error: argument --play-video: not allowed with argument --camera-device ------------------------------------------------------------------------------- Co-authored-by: Cursor AI Signed-off-by: Pavel Bar <pbar@redhat.com>
1 parent 9c66dbf commit af8146c

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,22 @@ Run the application:
6767
```
6868
or
6969
```bash
70+
(.venv) $ python app.py --camera-device 4
71+
```
72+
or
73+
```bash
7074
(.venv) $ python app.py --play-video /path/to/your/video.mp4
7175
```
72-
Use `--help` to display the available options
76+
Use `--help` to display the available options:
7377
```console
7478
(.venv) $ python app.py --help
75-
usage: app.py [-h] [--play-video path]
79+
usage: app.py [-h] [--camera-device idx | --play-video path]
7680
7781
Smart Car Dashboard GUI
7882
7983
options:
8084
-h, --help show this help message and exit
85+
--camera-device idx [Optional] camera device index to use (default: 0)
8186
--play-video path [Optional] path to video file to play instead of camera
8287
```
8388

app.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ class VideoThread(QThread):
109109
# Signal emitted when an error occurs
110110
error_occurred = pyqtSignal(str) # Emits error message
111111

112-
def __init__(self, video_path=None, start_frame=0):
112+
def __init__(self, camera_device=0, video_path=None, start_frame=0):
113113
super().__init__()
114+
self.camera_device = camera_device
114115
self.video_path = video_path
115116
self.start_frame = start_frame
116117
self.cap = None
@@ -130,11 +131,11 @@ def read_frame():
130131
self._should_stop = False
131132

132133
try:
133-
# Initialize video capture (use video file if provided, otherwise use camera device 0)
134+
# Initialize video capture (use video file if provided, otherwise use camera device)
134135
if self.video_path:
135136
self.cap = cv2.VideoCapture(self.video_path)
136137
else:
137-
self.cap = cv2.VideoCapture(0)
138+
self.cap = cv2.VideoCapture(self.camera_device)
138139

139140
# Check if capture device opened successfully
140141
if not self.cap.isOpened():
@@ -216,7 +217,8 @@ class Ui_MainWindow(object):
216217
WEBCAM_WIDTH = 321
217218
WEBCAM_HEIGHT = 331
218219

219-
def __init__(self, video_path=None):
220+
def __init__(self, camera_device=0, video_path=None):
221+
self.camera_device = camera_device
220222
self.video_path = video_path
221223
self.video_thread = None
222224
self.last_frame_position = 0 # Track video position for resume
@@ -984,6 +986,7 @@ def start_video(self):
984986
if not self.is_video_running():
985987
# Create and start the video thread (with resume position for videos)
986988
self.video_thread = VideoThread(
989+
camera_device=self.camera_device,
987990
video_path=self.video_path,
988991
start_frame=self.last_frame_position
989992
)
@@ -1161,7 +1164,23 @@ def progress(self):
11611164
if __name__ == "__main__":
11621165
# Parse command-line arguments
11631166
parser = argparse.ArgumentParser(description='Smart Car Dashboard GUI')
1164-
parser.add_argument('--play-video', metavar='path', type=str, help='[Optional] path to video file to play instead of camera')
1167+
1168+
# Create mutually exclusive group for video source selection
1169+
source_group = parser.add_mutually_exclusive_group()
1170+
source_group.add_argument(
1171+
'--camera-device',
1172+
metavar='idx',
1173+
type=int,
1174+
default=0,
1175+
help='[Optional] camera device index to use (default: 0)'
1176+
)
1177+
source_group.add_argument(
1178+
'--play-video',
1179+
metavar='path',
1180+
type=str,
1181+
help='[Optional] path to video file to play instead of camera'
1182+
)
1183+
11651184
args = parser.parse_args()
11661185

11671186
# Enable automatic high DPI scaling
@@ -1173,7 +1192,7 @@ def progress(self):
11731192

11741193
app = QApplication(sys.argv)
11751194
main_app_window = QMainWindow()
1176-
ui = Ui_MainWindow(video_path=args.play_video)
1195+
ui = Ui_MainWindow(camera_device=args.camera_device, video_path=args.play_video)
11771196
ui.setupUi(main_app_window)
11781197

11791198
# Center window on screen

0 commit comments

Comments
 (0)