Skip to content

Conversation

@barpavel
Copy link
Contributor

@barpavel barpavel commented Nov 9, 2025

PROBLEM:
Application crashed/froze when camera malfunctioned because camera I/O operations were blocking the main UI thread using QTimer.

SOLUTION:
In order to eliminate application freezes when camera malfunctions, I replaced an existing timer-based camera capture (QTimer approach) with a dedicated background thread (a VideoThread class) that:

  • Inherits from QThread to run camera operations in background thread.
  • Uses pyqtSignal for thread-safe communication with UI thread.
  • Implements proper thread life-cycle management:
    • start()
    • stop()
    • cleanup()
  • Provides robust error handling and user feedback.

CORE CHANGES:

  • Replaced self.timer (a QTimer object) with self.video_thread (a VideoThread object).
  • Moved all camera operations (cv2.VideoCapture(), cap.read(), etc.) to the new background thread.
  • Implemented signal-based communication for thread-safe frame/error handling.
  • Added comprehensive error handling with user-friendly messages.

TECHNICAL DETAILS:

  • Added VideoThread class (QThread subclass) for background camera capture.
  • Removed QTimer-based view_video() method that blocked main thread and unused QTimer module import.
  • Removed unnecessary time.sleep(0.1) camera initialization delay and unused time module import.
  • Removed global timer & cap variables (unsafe across threads) and an ugly globals() usage together with a pending TODO: comment.
  • Signal-based communication: frame_captured and error_occurred signals:
    • Thread-safe frame updates via on_frame_captured() slot.
    • Graceful error handling via on_video_error() slot.
  • Additionally performed some refactoring, i.e.:
    • Modifying a _read_video_frame() static method into a read_frame() inner method of the main thread execution run() method, which results in a more clean and Pythonic code.
    • Updated the error messages to clearly distinguish between initialization failures (the camera/video cannot be opened initially) and runtime failures (the camera/video was working but stopped).

BENEFITS:

  • UI remains responsive during camera errors (no more freezing).
  • Graceful handling of hardware failures (no more crashes).
  • Professional error messages displayed to the user.
  • Follows Qt best practices for hardware I/O.

Resolves: #5

Co-authored-by: Cursor AI
Signed-off-by: Pavel Bar pbar@redhat.com

@barpavel barpavel force-pushed the use-QThread-to-fix-app-crashes branch from 7ac3d45 to c6aa4b9 Compare November 9, 2025 23:06
@barpavel
Copy link
Contributor Author

barpavel commented Nov 9, 2025

@SihabSahariar I implemented the QThread solution in the app.py and temporary added a QThread_design folder with solution documentation.
If you want to read some documentation explaining the solution, you can start reading from DOCUMENTATION_INDEX.md.
When you review the solution (and if possible test the scenarios that previously crashed the application) I will remove the folder. Or maybe I can attach it as zip file to this PR just for history, but not part of the repo.

PROBLEM:
Application crashed/froze when camera malfunctioned because
camera I/O operations were blocking the main UI thread using "QTimer".

SOLUTION:
In order to eliminate application freezes when camera malfunctions,
I replaced an existing timer-based camera capture ("QTimer" approach)
with a dedicated background thread (a "VideoThread" class) that:
- Inherits from "QThread" to run camera operations in background thread.
- Uses "pyqtSignal" for thread-safe communication with UI thread.
- Implements proper thread life-cycle management:
  - start()
  - stop()
  - cleanup()
- Provides robust error handling and user feedback.

CORE CHANGES:
- Replaced "self.timer" (a "QTimer" object)
with "self.video_thread" (a "VideoThread" object).
- Moved all camera operations ("cv2.VideoCapture()", "cap.read()",
etc.) to the new background thread.
- Implemented signal-based communication for thread-safe frame/error handling.
- Added comprehensive error handling with user-friendly messages.

TECHNICAL DETAILS:
- Added "VideoThread" class ("QThread" subclass) for
background camera capture.
- Removed "QTimer"-based "view_video()" method
that blocked main thread and unused "QTimer" module import.
- Removed unnecessary "time.sleep(0.1)" camera initialization
delay and unused "time" module import.
- Removed global "timer" & "cap" variables (unsafe across threads)
and an ugly "globals()" usage together with a pending "TODO:" comment.
- Signal-based communication: "frame_captured" and
"error_occurred" signals:
  - Thread-safe frame updates via "on_frame_captured()" slot.
  - Graceful error handling via "on_video_error()" slot.
- Additionally performed some refactoring, i.e.:
  - Modifying a "_read_video_frame()" static method into a
"read_frame()" inner method of the main thread execution
"run()" method, which results in a more clean and Pythonic code.
  - Updated the error messages to clearly distinguish between
initialization failures (the camera/video cannot be opened initially)
and runtime failures (the camera/video was working but stopped).

BENEFITS:
- UI remains responsive during camera errors (no more freezing).
- Graceful handling of hardware failures (no more crashes).
- Professional error messages displayed to the user.
- Follows Qt best practices for hardware I/O.

Resolves: SihabSahariar#5

Co-authored-by: Cursor AI

Signed-off-by: Pavel Bar <pbar@redhat.com>
@barpavel barpavel force-pushed the use-QThread-to-fix-app-crashes branch from c6aa4b9 to 4c1f338 Compare November 9, 2025 23:30
@SihabSahariar SihabSahariar merged commit a1fa4fe into SihabSahariar:main Nov 10, 2025
@barpavel
Copy link
Contributor Author

@SihabSahariar Actually I planned to remove QThread_design folder with solution documentation after your code review.
I doubt it should be in the repo.

@barpavel barpavel deleted the use-QThread-to-fix-app-crashes branch November 12, 2025 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Camera Lagging & UI Crashes

2 participants