Skip to content

Commit c7d02d5

Browse files
committed
Extract method for fps profiling
1 parent 7f886e1 commit c7d02d5

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

kvm_serial/backend/video.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ def _configure_dshow_camera(cam: cv2.VideoCapture, width=1920, height=1080):
4949
return mjpg_ok
5050

5151

52+
def _measure_framerate(cam: cv2.VideoCapture):
53+
"""
54+
Helper method to retrive a semi-accurate framerate as a fallback
55+
if the backend does not report CAP_PROP_FPS.
56+
57+
:param cam: OpenCV VideoCapture device to profile
58+
:type cam: cv2.VideoCapture
59+
:param index: index of the device (system offset)
60+
"""
61+
FPS_SAMPLE_FRAMES = 5
62+
t0 = time.perf_counter()
63+
for _ in range(FPS_SAMPLE_FRAMES):
64+
cam.read()
65+
elapsed = time.perf_counter() - t0
66+
fps = int(FPS_SAMPLE_FRAMES // elapsed) if elapsed > 0 else 0
67+
return fps
68+
69+
5270
class CameraProperties:
5371
"""
5472
Describe a reference to a camera attached to the system
@@ -157,12 +175,7 @@ def getCameras() -> List[CameraProperties]:
157175
# Use reported FPS if available, otherwise measure it
158176
fps = int(cam.get(cv2.CAP_PROP_FPS))
159177
if fps <= 0:
160-
FPS_SAMPLE_FRAMES = 5
161-
t0 = time.perf_counter()
162-
for _ in range(FPS_SAMPLE_FRAMES):
163-
cam.read()
164-
elapsed = time.perf_counter() - t0
165-
fps = int(FPS_SAMPLE_FRAMES / elapsed) if elapsed > 0 else 0
178+
fps = _measure_framerate(cam)
166179
logger.debug(f"Camera {index} measured FPS: {fps}")
167180

168181
cameras.append(

0 commit comments

Comments
 (0)