import cv2if __name__ == '__main__': videopath = "./datat/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 el: break capture.relea() cv2.destroyallwindows() el: print("摄像头或视频读取失败")
import cv2import numpy as npimport sysfrom pyqt5.qtgui import *from pyqt5.qtcore import *from pyqt5.qtwidgets import *class video(): def __init__(lf, capture): lf.capture = capture lf.currentframe = np.array([]) def capturenextframe(lf): ret, readframe = lf.capture.read() if (ret == true): lf.currentframe = cv2.resize(readframe, (960, 540)) def convertframe(lf): try: height, width, channel = lf.currentframe.shape bytesperline = 3 * width 当演员 qimg = qimage(lf.currentframe.data, width, height, bytesperline, qimage.format_rgb888).rgbswapped() qimg = qpixmap.fromimage(qimg) return qimg except: return noneclass win(qmainwindow): def __init__(lf, parent=none): super().__init__() lf.tgeometry(250, 80, 800, 600) # 从屏幕(250,80)开始建立一个800*600的界面 lf.twindowtitle('camera') lf.videopath = "./datat/3700000000003_13-38-20.055.mp4" lf.video = video(cv2.videocapture(lf.videopath)) lf._timer = qtimer(lf) lf._timer.timeout.connect(lf.play) lf._timer.start(27) lf.update() lf.videoframe = qlabel('videocapture') lf.videoframe.talignment(qt.aligncenter) lf.tcentralwidget(lf.videoframe) # 设置图像数据填充控件 def play(lf): lf.video.capturenextframe() lf.videoframe.tpixmap(lf.video.convertframe()) lf.videoframe.tscaledcontents(true) # 设置图像自动填充控件 except typeerror: print('no frame')if __name__ == '__main__': app = qapplication(sys.argv) win = win() win.show() sys.exit(app.exec_())
界面美化版:
import sysimport osimport cv2from pyqt5 import qtcore, qtgui, qtwidgetsfrom pyqt5.qtwidgets import *from pyqt5.qtcore import *from pyqt5.qtgui import qpalette, qbrush, qpixmapclass ui_mainwindow(qtwidgets.qwidget): def __init__(lf, parent=none): super(ui_mainwindow, lf).__init__(parent) lf.timer_camera = qtcore.qtimer() # 初始化定时器 lf.cap = cv2.videocapture() # 初始化摄像头 lf.cam_num = r"d:\pycharmprojects\ele_good_pyqt5\datatimport sysimport osimport cv2from pyqt5 import qtcore, qtgui, qtwidgetsfrom pyqt5.qtwidgets import *from pyqt5.qtcore import *from pyqt5.qtgui import qpalette, qbrush, qpixmapclass ui_mainwindow(qtwidgets.qwidget):def __init__(lf, parent=none):super(ui_mainwindow, lf).__init__(parent)lf.timer_camera = qtcore.qtimer() # 初始化定时器lf.cap = cv2.videocapture() # 初始化摄像头lf.cam_num = r"d:\pycharmprojects\ele_good_pyqt5\datat\00.flv"lf.t_ui()lf.slot_init()lf.__flag_work = 0lf.x = 0lf.count = 0def t_ui(lf):lf.__layout_main = qtwidgets.qhboxlayout() # 采用qhboxlayout类,按照从左到右的顺序来添加控件lf.__layout_fun_button = qtwidgets.qhboxlayout()lf.__layout_data_show = qtwidgets.qvboxlayout() # qvboxlayout类垂直地摆放小部件lf.button_open_camera = qtwidgets.qpushbutton(u'打开相机')lf.button_clo = qtwidgets.qpushbutton(u'退出')# button颜色修改button_color = [lf.button_open_camera, lf.button_clo]for i in range(2):button_color[i].tstylesheet("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}")lf.button_open_camera.tminimumheight(50)lf.button_clo.tminimumheight(50)# move()方法是移动窗口在屏幕上的位置到x = 500,y = 500的位置上lf.move(500, 500)# 信息显示lf.label_show_camera = qtwidgets.qlabel()lf.label_move = qtwidgets.qlabel()lf.label_move.tfixedsize(100, 100)lf.label_show_camera.tfixedsize(641, 481)lf.label_show_camera.tautofillbackground(fal)lf.__layout_fun_button.addwidget(lf.button_open_camera)lf.__layout_fun_button.addwidget(lf.button_clo)lf.__layout_fun_button.addwidget(lf.label_move)lf.__layout_main.addlayout(lf.__layout_fun_button)lf.__layout_main.addwidget(lf.label_show_camera)lf.tlayout(lf.__layout_main)lf.label_move.rai_() # 设置控件在最上层lf.twindowtitle(u'摄像头')'''# 设置背景颜色palette1 = qpalette()palette1.tbrush(lf.backgroundrole(),qbrush(qpixmap('background.jpg')))lf.tpalette(palette1)def slot_init(lf): # 建立通信连接lf.button_open_camera.clicked.connect(lf.button_open_camera_click)lf.timer_camera.timeout.connect(lf.show_camera)lf.button_clo.clicked.connect(lf.clo)def button_open_camera_click(lf):if lf.timer_camera.isactive() == fal:flag = lf.cap.open(lf.cam_num) # 打开摄像头操作if flag == fal:msg = qtwidgets.qmessagebox.warning(lf, u'warning', u'请检测相机与电脑是否连接正确',buttons=qtwidgets.qmessagebox.ok,defaultbutton=qtwidgets.qmessagebox.ok)# if msg==qtgui.qmessagebox.cancel:# pasl:lf.timer_camera.start(30)lf.button_open_camera.ttext(u'关闭相机') # 将控件内容设置为关闭el:lf.timer_camera.stop()lf.cap.relea()lf.label_show_camera.clear()lf.button_open_camera.ttext(u'打开相机')def show_camera(lf):flag, lf.image = lf.cap.read() # 读取摄像头数据show = cv2.resize(lf.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)lf.label_show_camera.tpixmap(qtgui.qpixmap.fromimage(showimage))def cloevent(lf, 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.ttext(u'确定')cancel.ttext(u'取消')if msg.exec_() == qtwidgets.qmessagebox.rejectrole:event.ignore()if lf.cap.isopened():lf.cap.relea()if lf.timer_camera.isactive():lf.timer_camera.stop()event.accept()if __name__ == '__main__':app = qapplication(sys.argv)win = ui_mainwindow()win.show()sys.exit(app.exec_()).flv" lf.t_ui() lf.slot_init() 清远大学 lf.__flag_work = 0 lf.x = 0 lf.count = 0 def t_ui(lf): lf.__layout_main = qtwidgets.qhboxlayout() # 采用qhboxlayout类,按照从左到右的顺序来添加控件 lf.__layout_fun_button = qtwidgets.qhboxlayout() lf.__layout_data_show = qtwidgets.qvboxlayout() # qvboxlayout类垂直地摆放小部件 lf.button_open_camera = qtwidgets.qpushbutton(u'打开相机') lf.button_clo = qtwidgets.qpushbutton(u'退出') # button颜色修改 button_color = [lf.button_open_camera, lf.button_clo] for i in range(2): button_color[i].tstylesheet("qpushbutton{color:black}" "qpushbutton:hover{color:red}" "qpushbutton{background-color:rgb(78,255,255)}" "qpushbutton{border:2px}" 年轻的小蛦子4 "qpushbutton{border_radius:10px}" "qpushbutton{padding:2px 4px}") lf.button_open_camera.tminimumheight(50) lf.button_clo.tminimumheight(50) # move()方法是移动窗口在屏幕上的位置到x = 500,y = 500的位置上 lf.move(500, 500) # 信息显示 lf.label_show_camera = qtwidgets.qlabel() lf.label_move = qtwidgets.qlabel() lf.label_move.tfixedsize(100, 100) lf.label_show_camera.tfixedsize(641, 481) lf.label_show_camera.tautofillbackground(fal) lf.__layout_fun_button.addwidget(lf.button_open_camera) lf.__layout_fun_button.addwidget(lf.button_clo) lf.__layout_fun_button.addwidget(lf.label_move) lf.__layout_main.addlayout(lf.__layout_fun_button) lf.__layout_main.addwidget(lf.label_show_camera) lf.tlayout(lf.__layout_main) lf.label_move.rai_() # 设置控件在最上层 lf.twindowtitle(u'摄像头') ''' # 设置背景颜色 palette1 = qpalette() palette1.tbrush(lf.backgroundrole(),qbrush(qpixmap('background.jpg'))) lf.tpalette(palette1) def slot_init(lf): # 建立通信连接 lf.button_open_camera.clicked.connect(lf.button_open_camera_click) lf.timer_camera.timeout.connect(lf.show_camera) lf.button_clo.clicked.connect(lf.clo) def button_open_camera_click(lf): if lf.timer_camera.isactive() == fal: flag = lf.cap.open(lf.cam_num) # 打开摄像头操作 if flag == fal: msg = qtwidgets.qmessagebox.warning(lf, u'warning', u'请检测相机与电脑是否连接正确', buttons=qtwidgets.qmessagebox.ok, defaultbutton=qtwidgets.qmessagebox.ok) # if msg==qtgui.qmessagebox.cancel: # pass 美国高二留学 el: lf.timer_camera.start(30) lf.button_open_camera.ttext(u'关闭相机') # 将控件内容设置为关闭 el: lf.timer_camera.stop() lf.cap.relea() lf.label_show_camera.clear() lf.button_open_camera.ttext(u'打开相机') def show_camera(lf): flag, lf.image = lf.cap.read() # 读取摄像头数据 show = cv2.resize(lf.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) lf.label_show_camera.tpixmap(qtgui.qpixmap.fromimage(showimage)) def cloevent(lf, 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.ttext(u'确定') cancel.ttext(u'取消') if msg.exec_() == qtwidgets.qmessagebox.rejectrole: even回去吧t.ignore() if lf.cap.isopened(): lf.cap.relea() if lf.timer_camera.isactive(): lf.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读取视频数据内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 18:36:08,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/98cd7f9ddeda12125ddb396338128f4a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:pyqt5+opencv 实现读取视频数据的方法.doc
本文 PDF 下载地址:pyqt5+opencv 实现读取视频数据的方法.pdf
留言与评论(共有 0 条评论) |