pyqt5+opencv 实现读取视频数据的方法
程序员文章站
2022-06-15 15:03:56
1、opencv读取视频数据import cv2if __name__ == '__main__': videopath = "./dataset/3700000000003_13-38-20....
1、opencv读取视频数据
import cv2 if __name__ == '__main__': videopath = "./dataset/3700000000003_13-38-20.055.mp4" namewindow = "detection window" # 窗体名称 cv2.namedwindow(namewindow) # 设置窗体 capture = cv2.videocapture(videopath) if capture.isopened(): size = (capture.get(cv2.cap_prop_frame_width), capture.get(cv2.cap_prop_frame_height)) # 读取帧的宽、高 speed = capture.get(cv2.cap_prop_fps) # 获得帧速 while true: ret, frame = capture.read() if ret: frame = cv2.resize(frame, (960, 540)) cv2.imshow(namewindow, frame) if cv2.waitkey(1) & 0xff == 27: break else: break capture.release() cv2.destroyallwindows() else: print("摄像头或视频读取失败")
2、opencv集成pyqt5读取视频数据
import cv2 import numpy as np import sys from pyqt5.qtgui import * from pyqt5.qtcore import * from pyqt5.qtwidgets import * class video(): def __init__(self, capture): self.capture = capture self.currentframe = np.array([]) def capturenextframe(self): ret, readframe = self.capture.read() if (ret == true): self.currentframe = cv2.resize(readframe, (960, 540)) def convertframe(self): try: height, width, channel = self.currentframe.shape bytesperline = 3 * width qimg = qimage(self.currentframe.data, width, height, bytesperline, qimage.format_rgb888).rgbswapped() qimg = qpixmap.fromimage(qimg) return qimg except: return none class win(qmainwindow): def __init__(self, parent=none): super().__init__() self.setgeometry(250, 80, 800, 600) # 从屏幕(250,80)开始建立一个800*600的界面 self.setwindowtitle('camera') self.videopath = "./dataset/3700000000003_13-38-20.055.mp4" self.video = video(cv2.videocapture(self.videopath)) self._timer = qtimer(self) self._timer.timeout.connect(self.play) self._timer.start(27) self.update() self.videoframe = qlabel('videocapture') self.videoframe.setalignment(qt.aligncenter) self.setcentralwidget(self.videoframe) # 设置图像数据填充控件 def play(self): self.video.capturenextframe() self.videoframe.setpixmap(self.video.convertframe()) self.videoframe.setscaledcontents(true) # 设置图像自动填充控件 except typeerror: print('no frame') if __name__ == '__main__': app = qapplication(sys.argv) win = win() win.show() sys.exit(app.exec_())
界面美化版:
import sys import os import cv2 from pyqt5 import qtcore, qtgui, qtwidgets from pyqt5.qtwidgets import * from pyqt5.qtcore import * from pyqt5.qtgui import qpalette, qbrush, qpixmap class ui_mainwindow(qtwidgets.qwidget): def __init__(self, parent=none): super(ui_mainwindow, self).__init__(parent) self.timer_camera = qtcore.qtimer() # 初始化定时器 self.cap = cv2.videocapture() # 初始化摄像头 self.cam_num = r"d:\pycharmprojects\ele_good_pyqt5\dataset\00.flv" self.set_ui() self.slot_init() self.__flag_work = 0 self.x = 0 self.count = 0 def set_ui(self): self.__layout_main = qtwidgets.qhboxlayout() # 采用qhboxlayout类,按照从左到右的顺序来添加控件 self.__layout_fun_button = qtwidgets.qhboxlayout() self.__layout_data_show = qtwidgets.qvboxlayout() # qvboxlayout类垂直地摆放小部件 self.button_open_camera = qtwidgets.qpushbutton(u'打开相机') self.button_close = qtwidgets.qpushbutton(u'退出') # button颜色修改 button_color = [self.button_open_camera, self.button_close] for i in range(2): button_color[i].setstylesheet("qpushbutton{color:black}" "qpushbutton:hover{color:red}" "qpushbutton{background-color:rgb(78,255,255)}" "qpushbutton{border:2px}" "qpushbutton{border_radius:10px}" "qpushbutton{padding:2px 4px}") self.button_open_camera.setminimumheight(50) self.button_close.setminimumheight(50) # move()方法是移动窗口在屏幕上的位置到x = 500,y = 500的位置上 self.move(500, 500) # 信息显示 self.label_show_camera = qtwidgets.qlabel() self.label_move = qtwidgets.qlabel() self.label_move.setfixedsize(100, 100) self.label_show_camera.setfixedsize(641, 481) self.label_show_camera.setautofillbackground(false) self.__layout_fun_button.addwidget(self.button_open_camera) self.__layout_fun_button.addwidget(self.button_close) self.__layout_fun_button.addwidget(self.label_move) self.__layout_main.addlayout(self.__layout_fun_button) self.__layout_main.addwidget(self.label_show_camera) self.setlayout(self.__layout_main) self.label_move.raise_() # 设置控件在最上层 self.setwindowtitle(u'摄像头') ''' # 设置背景颜色 palette1 = qpalette() palette1.setbrush(self.backgroundrole(),qbrush(qpixmap('background.jpg'))) self.setpalette(palette1) def slot_init(self): # 建立通信连接 self.button_open_camera.clicked.connect(self.button_open_camera_click) self.timer_camera.timeout.connect(self.show_camera) self.button_close.clicked.connect(self.close) def button_open_camera_click(self): if self.timer_camera.isactive() == false: flag = self.cap.open(self.cam_num) # 打开摄像头操作 if flag == false: msg = qtwidgets.qmessagebox.warning(self, u'warning', u'请检测相机与电脑是否连接正确', buttons=qtwidgets.qmessagebox.ok, defaultbutton=qtwidgets.qmessagebox.ok) # if msg==qtgui.qmessagebox.cancel: # pass else: self.timer_camera.start(30) self.button_open_camera.settext(u'关闭相机') # 将控件内容设置为关闭 else: self.timer_camera.stop() self.cap.release() self.label_show_camera.clear() self.button_open_camera.settext(u'打开相机') def show_camera(self): flag, self.image = self.cap.read() # 读取摄像头数据 show = cv2.resize(self.image, (640, 480)) show = cv2.cvtcolor(show, cv2.color_bgr2rgb) showimage = qtgui.qimage(show.data, show.shape[1], show.shape[0], qtgui.qimage.format_rgb888) self.label_show_camera.setpixmap(qtgui.qpixmap.fromimage(showimage)) def closeevent(self, event): print("关闭") ok = qtwidgets.qpushbutton() cancel = qtwidgets.qpushbutton() msg = qtwidgets.qmessagebox(qtwidgets.qmessagebox.warning, u'关闭', u'是否关闭!') msg.addbutton(ok, qtwidgets.qmessagebox.actionrole) msg.addbutton(cancel, qtwidgets.qmessagebox.rejectrole) ok.settext(u'确定') cancel.settext(u'取消') if msg.exec_() == qtwidgets.qmessagebox.rejectrole: event.ignore() if self.cap.isopened(): self.cap.release() if self.timer_camera.isactive(): self.timer_camera.stop() event.accept() if __name__ == '__main__': app = qapplication(sys.argv) win = ui_mainwindow() win.show() sys.exit(app.exec_())
显示效果如下所示:
到此这篇关于pyqt5+opencv 实现读取视频数据的文章就介绍到这了,更多相关pyqt5 opencv读取视频数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!