Fix camera malfunction crashes by implementing QThread-based solution
#6
+1,596
−72
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 (
QTimerapproach) with a dedicated background thread (aVideoThreadclass) that:QThreadto run camera operations in background thread.pyqtSignalfor thread-safe communication with UI thread.start()stop()cleanup()CORE CHANGES:
self.timer(aQTimerobject) withself.video_thread(aVideoThreadobject).cv2.VideoCapture(),cap.read(), etc.) to the new background thread.TECHNICAL DETAILS:
VideoThreadclass (QThreadsubclass) for background camera capture.QTimer-basedview_video()method that blocked main thread and unusedQTimermodule import.time.sleep(0.1)camera initialization delay and unusedtimemodule import.timer&capvariables (unsafe across threads) and an uglyglobals()usage together with a pendingTODO:comment.frame_capturedanderror_occurredsignals:on_frame_captured()slot.on_video_error()slot._read_video_frame()static method into aread_frame()inner method of the main thread executionrun()method, which results in a more clean and Pythonic code.BENEFITS:
Qtbest practices for hardware I/O.Resolves: #5
Co-authored-by: Cursor AI
Signed-off-by: Pavel Bar pbar@redhat.com