import cv2 from collections import deque import numpy as np import matplotlib.pyplot as plt plt.style.use('ggplot') def variance_of_laplacian(img): return cv2.Laplacian(img, cv2.CV_64F).var() class LivePlotter: def __init__(self): self.x_vec = np.linspace(0, 9, 10) self.y_vec = deque([0 for _ in range(10)], maxlen=10) self.line = [] def live_plot(self, identifier='', pause_time=0.01): global fig if not self.line: fig = plt.figure(figsize=(13, 6)) ax = fig.add_subplot() ax.set_xticks([]) self.line, = ax.plot(self.x_vec, self.y_vec, '-o', alpha=0.8) plt.ylabel('Blur Metric') plt.title('Title: {}'.format(identifier)) plt.ioff() self.line.set_ydata(self.y_vec) if np.min(self.y_vec) <= self.line.axes.get_ylim()[0] or np.max(self.y_vec) >= self.line.axes.get_ylim()[1]: plt.ylim([0, np.max(self.y_vec) + np.std(self.y_vec)]) plt.pause(pause_time) # fig.canvas.draw() data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) return self.line, data video_path = "wagon_top_magnet/source_video/unload_wagon_bbox_only.mp4" cap = cv2.VideoCapture(video_path) plotter = LivePlotter() if not cap.isOpened(): print("Error opening video stream or file") while cap.isOpened(): ret, frame = cap.read() cv2.namedWindow('Frame', cv2.WINDOW_NORMAL) if ret: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) fm = int(variance_of_laplacian(frame)) # Добавить следующее значение plotter.y_vec.append(fm) line, plot = plotter.live_plot() plot = cv2.resize(plot, (3700, 1800)) cv2.imshow('Frame', np.vstack((plot, frame))) if cv2.waitKey(25) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows()