如何在PyQtGraph(结合OpenGL)中显示实时帧率?
Hey there, let's get this FPS monitoring set up for your real-time heatmap—no multi-threaded mess required, just clean integration into your existing update method. This will help you quickly diagnose if the lag comes from hardware sampling limits or your device's rendering performance.
Step 1: Initialize FPS Tracking Variables
First, add some variables to your widget's constructor to track time and frame counts. We'll also set up a text item to display the FPS on screen, so you can see it in real time alongside your heatmap.
import time from pyqtgraph import OpenGLViewWidget, GLImageItem, TextItem class HeatmapViewer(OpenGLViewWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize your heatmap item as you already do self.heatmap = GLImageItem() self.addItem(self.heatmap) # FPS tracking setup self._last_fps_update = time.time() self._frame_counter = 0 self._current_fps = 0.0 # Create a text item for FPS display (positioned at bottom-right to avoid blocking the heatmap) self.fps_display = TextItem(color=(255, 255, 255), anchor=(1, 1)) self.fps_display.setPos(self.width() - 20, self.height() - 20) self.addItem(self.fps_display)
Step 2: Integrate FPS Calculation into Your Update Method
Modify your update method to calculate and refresh the FPS value. We'll update the FPS every 0.5 seconds to keep readings stable without adding unnecessary overhead to your loop.
def update(self): # 1. Fetch hardware data and update heatmap (your existing logic here) # Replace this line with your actual hardware data retrieval code hardware_touch_data = self.get_latest_hardware_data() self.heatmap.setImage(hardware_touch_data) # 2. Calculate and update FPS current_time = time.time() self._frame_counter += 1 elapsed_time = current_time - self._last_fps_update # Refresh FPS every 0.5 seconds for smoother, consistent readings if elapsed_time >= 0.5: self._current_fps = self._frame_counter / elapsed_time # Update the display text (formatted to 1 decimal place for clarity) self.fps_display.setText(f"*FPS: {self._current_fps:.1f}*") # Reset counters for the next interval self._frame_counter = 0 self._last_fps_update = current_time # 3. Trigger the next update (keep your existing refresh logic here) self.update() # Or use QTimer if you're relying on Qt's event loop instead
Step 3: Diagnose the Root Cause
Once the FPS is displaying, you can quickly narrow down the source of your卡顿:
- If the FPS matches (or is very close to) your hardware's advertised sampling rate: The lag is likely due to the hardware not delivering data fast enough. Check your hardware connection or adjust its sampling settings.
- If the FPS is significantly lower than the hardware's sampling rate: The bottleneck is on your device side. Try these fixes:
- Reduce the resolution of your heatmap to lighten the OpenGL rendering load
- Optimize your data processing code (e.g., use NumPy vectorization instead of slow loops)
- Ensure you're passing data to
GLImageItemin an efficient format (like pre-converted numpy arrays)
Quick Tips
- The 0.5-second update interval strikes a balance between frequent feedback and low overhead—you can tweak this to 1 second if you prefer more stable readings.
- You can adjust the
setPos()values for thefps_displayto move the text anywhere on the screen that doesn't block your heatmap. - All logic runs in the same thread as your update loop, so no thread safety issues to worry about.
内容的提问来源于stack exchange,提问作者busssard

