Skip to content

Commit 6b411bd

Browse files
committed
core: improved handling of camera & video errors
1. Display error message in the video area in the following cases (runtime error detection during frame reading): a. Camera is unavailable (at initialization or gets disconnected during use). b. Prerecorded video can't be played (file doesn't exist or video stream gets interrupted/corrupted). Created a dedicated "display_error_message()" method to display a visual error message in the exact same location where the video would appear. It uses a black background with a red border to match the UI styling. Shows white text that's bold and centered for clear readability. Maintains the exact same dimensions as the video area (321x331 pixels). The error handling ensures the application won't crash and provides a clear feedback to users about what went wrong, making the dashboard much more user-friendly and robust. 2. Hardware consideration - a more reliable camera detection. Added a small delay (0.1 seconds), to give camera time to initialize properly. Cameras (especially USB cameras) often need a brief moment to become fully operational after a "cv2.VideoCapture()" call. This ensures camera is ready before "view_video()" starts getting called every 20ms, which reduces false negatives where camera exists but isn't ready yet. Documentation was updated accordingly and a screenshot displaying the camera error was added to the "README.md" file. Co-authored-by: Cursor AI Signed-off-by: Pavel Bar <pbar@redhat.com>
1 parent 37f0ec3 commit 6b411bd

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ Welcome to the Car Dashboard (HMI) prototype project! This simple yet intuitive
2424

2525
7. **Camera Streaming**
2626
- Access live camera feeds for improved awareness and safety.
27+
- Handles camera failures (camera is unavailable or gets disconnected during use).
2728

2829
8. **Prerecorded Video Streaming**
2930
- When camera is unavailable (i.e., during development or demos), you can play video instead.
31+
- Handles video file failures (file doesn't exist or video stream gets interrupted/corrupted).
3032

3133
## Development
3234

@@ -79,6 +81,7 @@ options:
7981
<img src = "ss/2.PNG">
8082
<img src = "ss/3.PNG">
8183
<img src = "ss/4.PNG">
84+
<img src = "ss/5.PNG">
8285

8386
## Todo
8487

app.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import io
77
import sys
8+
import time
89
import argparse
910

1011
# import OpenCV module
@@ -15,7 +16,7 @@
1516
# PyQt5 imports - Core
1617
from PyQt5.QtCore import QRect, QSize, QTimer, Qt, QCoreApplication, QMetaObject
1718
# PyQt5 imports - GUI
18-
from PyQt5.QtGui import QPixmap, QImage, QFont
19+
from PyQt5.QtGui import QPixmap, QImage, QFont, QPainter, QPen
1920
# PyQt5 imports - Widgets
2021
from PyQt5.QtWidgets import (
2122
QApplication, QWidget, QHBoxLayout, QLabel, QFrame, QPushButton,
@@ -693,6 +694,31 @@ def setupUi(self, MainWindow):
693694
)
694695
self.label_km.setAlignment(Qt.AlignCenter)
695696

697+
def display_error_message(self, message):
698+
"""Display error message in the video area with proper styling."""
699+
# Create a QPixmap with the same dimensions as the webcam area
700+
error_pixmap = QPixmap(Ui_MainWindow.WEBCAM_WIDTH, Ui_MainWindow.WEBCAM_HEIGHT)
701+
error_pixmap.fill(Qt.black) # Black background to match the UI
702+
703+
# Draw the error message on the pixmap
704+
painter = QPainter(error_pixmap)
705+
painter.setPen(QPen(Qt.red, 2))
706+
painter.setFont(QFont("Arial", 12, QFont.Bold))
707+
708+
# Draw border
709+
painter.drawRect(2, 2, Ui_MainWindow.WEBCAM_WIDTH - 4, Ui_MainWindow.WEBCAM_HEIGHT - 4)
710+
711+
# Draw error message in center
712+
painter.setPen(QPen(Qt.white, 1))
713+
text_rect = error_pixmap.rect()
714+
text_rect.adjust(10, 0, -10, 0) # Add some margin
715+
painter.drawText(text_rect, Qt.AlignCenter | Qt.TextWordWrap, message)
716+
717+
painter.end()
718+
719+
# Set the error pixmap to the webcam label
720+
self.webcam.setPixmap(error_pixmap)
721+
696722
@staticmethod
697723
def _read_video_frame():
698724
"""Read and validate a video frame from the capture device.
@@ -709,6 +735,7 @@ def _read_video_frame():
709735
return image
710736

711737
def view_video(self):
738+
"""Displays camera / video stream and handles errors."""
712739
image = Ui_MainWindow._read_video_frame()
713740

714741
# Check if frame is valid
@@ -719,11 +746,13 @@ def view_video(self):
719746
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
720747
image = Ui_MainWindow._read_video_frame()
721748
if image is None:
722-
# If still no frame, stop the timer
749+
# If still no frame, show error and stop the timer
750+
self.display_error_message("Video file is unavailable or corrupted!\n\nPlease check video file.")
723751
self.quit_video()
724752
return
725753
else:
726-
# For camera, stop the timer
754+
# For camera, show error and stop the timer
755+
self.display_error_message("Camera is unavailable!\n\nPlease check camera connection.")
727756
self.quit_video()
728757
return
729758

@@ -767,6 +796,8 @@ def controlTimer(self):
767796
cap = cv2.VideoCapture(self.video_path)
768797
else:
769798
cap = cv2.VideoCapture(0)
799+
# Give camera time to initialize for better robustness
800+
time.sleep(0.1)
770801
self.timer.start(20)
771802

772803
def retranslateUi(self, MainWindow):

ss/5.PNG

175 KB
Loading

0 commit comments

Comments
 (0)