Skip to content

Commit c17e6a7

Browse files
committed
refactor: code improvements & warnings fixes
1. Refactored the "_read_video_frame()" method to be static to fix a warning, see [2]. 2. Using the class name to access class-level constants. 3. Using module level "dunders" to state the author and additional module information. This is probably a more elegant solution compared to using comments and/or a dedicated "creator()" method (the latter was removed). More info at the "PEP 8 – Style Guide for Python Code", see [1]. 4. Fixed a few additional warnings, see [3], [4] & [5]. [1] https://peps.python.org/pep-0008/#module-level-dunder-names [2] Method '_read_video_frame' may be 'static' [3] Ui_MainWindow::setupUi() Shadows name 'MainWindow' from outer scope [4] AnalogGaugeWidget::set_MinValue() Shadows built-in name 'min' [5] AnalogGaugeWidget::set_MaxValue() Shadows built-in name 'max' Signed-off-by: Pavel Bar <pbar@redhat.com>
1 parent 673d58f commit c17e6a7

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,28 @@ $ python3 -m venv .venv
4949

5050
Install the mandatory dependencies using the following command:
5151
```bash
52-
$ pip install -r requirements.txt
52+
(.venv) $ pip install -r requirements.txt
5353
```
5454

5555
## Execute
5656
Run the application:
5757
```bash
58-
$ python app.py
58+
(.venv) $ python app.py
5959
```
6060
or
6161
```bash
62-
$ python app.py --play-video /path/to/your/video.mp4
62+
(.venv) $ python app.py --play-video /path/to/your/video.mp4
6363
```
6464
Use `--help` to display the available options
6565
```console
66-
$ python app.py --help
66+
(.venv) $ python app.py --help
6767
usage: app.py [-h] [--play-video path]
6868
6969
Smart Car Dashboard GUI
7070
7171
options:
72-
-h, --help show this help message and exit
73-
--play-video path [Optional] path to video file to play instead of camera
72+
-h, --help show this help message and exit
73+
--play-video path [Optional] path to video file to play instead of camera
7474
```
7575

7676
## Screenshot

app.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Developed By Sihab Sahariar
1+
__author__ = "Sihab Sahariar"
2+
__contact__ = "www.github.com/sihabsahariar"
3+
__credits__ = ["Pavel Bar"]
4+
__version__ = "1.0.1"
5+
26
import io
37
import sys
48
import argparse
@@ -671,7 +675,7 @@ def setupUi(self, MainWindow):
671675

672676
self.webcam = QLabel(self.frame_map)
673677
self.webcam.setObjectName(u"webcam")
674-
self.webcam.setGeometry(QRect(500, 40, self.WEBCAM_WIDTH, self.WEBCAM_HEIGHT))
678+
self.webcam.setGeometry(QRect(500, 40, Ui_MainWindow.WEBCAM_WIDTH, Ui_MainWindow.WEBCAM_HEIGHT))
675679

676680
MainWindow.setCentralWidget(self.centralwidget)
677681
self.show_dashboard()
@@ -689,30 +693,31 @@ def setupUi(self, MainWindow):
689693
)
690694
self.label_km.setAlignment(Qt.AlignCenter)
691695

692-
def _read_video_frame(self):
696+
@staticmethod
697+
def _read_video_frame():
693698
"""Read and validate a video frame from the capture device.
694-
699+
695700
Returns:
696701
numpy.ndarray: Valid image frame, or None if no valid frame available
697702
"""
698703
ret, image = cap.read()
699-
704+
700705
# Validate frame
701706
if not ret or image is None or image.size == 0:
702707
return None
703-
708+
704709
return image
705710

706711
def view_video(self):
707-
image = self._read_video_frame()
712+
image = Ui_MainWindow._read_video_frame()
708713

709714
# Check if frame is valid
710715
if image is None:
711716
# Video ended or no frame available
712717
if self.video_path:
713718
# For video files, restart from beginning (loop)
714719
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
715-
image = self._read_video_frame()
720+
image = Ui_MainWindow._read_video_frame()
716721
if image is None:
717722
# If still no frame, stop the timer
718723
self.quit_video()
@@ -729,8 +734,8 @@ def view_video(self):
729734
height, width, channel = image.shape
730735

731736
# Calculate scaling to fit within target area while maintaining aspect ratio
732-
scale_w = self.WEBCAM_WIDTH / width
733-
scale_h = self.WEBCAM_HEIGHT / height
737+
scale_w = Ui_MainWindow.WEBCAM_WIDTH / width
738+
scale_h = Ui_MainWindow.WEBCAM_HEIGHT / height
734739
scale = min(scale_w, scale_h) # Use smaller scale to fit entirely
735740

736741
# Calculate new dimensions
@@ -887,8 +892,8 @@ def progress(self):
887892
args = parser.parse_args()
888893

889894
app = QApplication(sys.argv)
890-
MainWindow = QMainWindow()
895+
main_app_window = QMainWindow()
891896
ui = Ui_MainWindow(video_path=args.play_video)
892-
ui.setupUi(MainWindow)
893-
MainWindow.show()
897+
ui.setupUi(main_app_window)
898+
main_app_window.show()
894899
sys.exit(app.exec_())

gauge.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Sihab Sahariar (Fixed, 2023)
1+
__author__ = "Sihab Sahariar"
2+
__contact__ = "www.github.com/sihabsahariar"
3+
__credits__ = ["Pavel Bar"]
4+
__version__ = "1.0.1"
25

36
import math
47

@@ -14,11 +17,7 @@
1417

1518

1619
class AnalogGaugeWidget(QWidget):
17-
"""Fetches rows from a Bigtable.
18-
Args:
19-
none
20-
21-
"""
20+
"""Fetches rows from a Bigtable."""
2221
valueChanged = pyqtSignal(int)
2322

2423
def __init__(self, parent=None):
@@ -140,9 +139,6 @@ def rescale_method(self):
140139
self.scale_fontsize = self.initial_scale_fontsize * self.widget_diameter // 400
141140
self.value_fontsize = self.initial_value_fontsize * self.widget_diameter // 400
142141

143-
def creator(self):
144-
print("Sihab Sahariar | www.github.com/sihabsahariar")
145-
146142
def change_value_needle_style(self, design):
147143
# prepared for multiple needle instrument
148144
self.value_needle = []
@@ -277,24 +273,24 @@ def set_scala_main_count(self, count):
277273
if not self.use_timer_event:
278274
self.update()
279275

280-
def set_MinValue(self, min):
281-
if self.value < min:
282-
self.value = min
283-
if min >= self.value_max:
276+
def set_MinValue(self, new_value_min):
277+
if self.value < new_value_min:
278+
self.value = new_value_min
279+
if new_value_min >= self.value_max:
284280
self.value_min = self.value_max - 1
285281
else:
286-
self.value_min = min
282+
self.value_min = new_value_min
287283

288284
if not self.use_timer_event:
289285
self.update()
290286

291-
def set_MaxValue(self, max):
292-
if self.value > max:
293-
self.value = max
294-
if max <= self.value_min:
287+
def set_MaxValue(self, new_value_max):
288+
if self.value > new_value_max:
289+
self.value = new_value_max
290+
if new_value_max <= self.value_min:
295291
self.value_max = self.value_min + 1
296292
else:
297-
self.value_max = max
293+
self.value_max = new_value_max
298294

299295
if not self.use_timer_event:
300296
self.update()

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
folium==0.17.0
22
opencv-python-headless==4.10.0.84
33
opencv-python
4-
python-qt5
54
PyQt5==5.15.10
65
PyQtWebEngine==5.15.7
76
qtwidgets==1.1

0 commit comments

Comments
 (0)